Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D0A8F90E4 for ; Sun, 3 Jun 2012 09:40:47 +0000 (UTC) Received: (qmail 95995 invoked by uid 500); 3 Jun 2012 09:40:46 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 95946 invoked by uid 500); 3 Jun 2012 09:40:45 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 95923 invoked by uid 99); 3 Jun 2012 09:40:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 03 Jun 2012 09:40:45 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 03 Jun 2012 09:40:40 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 92A1E238896F; Sun, 3 Jun 2012 09:40:20 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1345637 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/properties/ components/camel-spring/src/main/java/org/apache/camel/spring/ components/camel-spring/src/main/java/org/apache/camel/spring/spi/ components/camel-s... Date: Sun, 03 Jun 2012 09:40:20 -0000 To: commits@camel.apache.org From: davsclaus@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120603094020.92A1E238896F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: davsclaus Date: Sun Jun 3 09:40:19 2012 New Revision: 1345637 URL: http://svn.apache.org/viewvc?rev=1345637&view=rev Log: CAMEL-3547: Added bridge property placeholder between Spring and Camel, to make it easier to use Spring placeholders in Camel apps, without double configuration. Added: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.java camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/HelloBean.java camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.xml camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/cheese.properties Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java?rev=1345637&r1=1345636&r2=1345637&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java Sun Jun 3 09:40:19 2012 @@ -32,10 +32,10 @@ public interface PropertiesResolver { /** * Resolve properties from the given uri * - * * @param context the camel context * @param ignoreMissingLocation ignore silently if the property file is missing - * @param uri uri(s) defining the source(s) @return the properties + * @param uri uri(s) defining the source(s) + * @return the properties * @throws Exception is thrown if resolving the properties failed */ Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, String... uri) throws Exception; Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java?rev=1345637&r1=1345636&r2=1345637&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java (original) +++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java Sun Jun 3 09:40:19 2012 @@ -18,6 +18,7 @@ package org.apache.camel.spring; import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; @@ -31,6 +32,7 @@ import org.apache.camel.RoutesBuilder; import org.apache.camel.ShutdownRoute; import org.apache.camel.ShutdownRunningTask; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.core.xml.AbstractCamelContextFactoryBean; import org.apache.camel.core.xml.CamelJMXAgentDefinition; import org.apache.camel.core.xml.CamelPropertyPlaceholderDefinition; @@ -51,6 +53,7 @@ import org.apache.camel.model.config.Pro import org.apache.camel.model.dataformat.DataFormatsDefinition; import org.apache.camel.spi.PackageScanFilter; import org.apache.camel.spi.Registry; +import org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; @@ -232,6 +235,40 @@ public class CamelContextFactoryBean ext } } + @Override + protected void initPropertyPlaceholder() throws Exception { + super.initPropertyPlaceholder(); + + Map beans = applicationContext.getBeansOfType(BridgePropertyPlaceholderConfigurer.class); + if (beans.size() == 1) { + // setup properties component that uses this beans + BridgePropertyPlaceholderConfigurer configurer = beans.values().iterator().next(); + String id = beans.keySet().iterator().next(); + LOG.info("Bridging Camel and Spring property placeholder configurer with id: " + id); + + // get properties component + PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class); + // replace existing resolver with us + configurer.setResolver(pc.getPropertiesResolver()); + String ref = "ref:" + id; + pc.setPropertiesResolver(configurer); + // and update locations to have our as ref first + String[] locations = pc.getLocations(); + String[] updatedLocations; + if (locations != null && locations.length > 0) { + updatedLocations = new String[locations.length + 1]; + updatedLocations[0] = ref; + System.arraycopy(locations, 0, updatedLocations, 1, locations.length); + } else { + updatedLocations = new String[]{ref}; + } + pc.setLocations(updatedLocations); + } else if (beans.size() > 1) { + LOG.warn("Cannot bridge Camel and Spring property placeholders, as only exact 1 bean of type CamelSpringPropertyPlaceholderConfigurer" + + " must be defined, was {} beans", beans.size()); + } + } + public void onApplicationEvent(ApplicationEvent event) { // From Spring 3.0.1, The BeanFactory applicationEventListener // and Bean's applicationEventListener will be called, @@ -248,7 +285,7 @@ public class CamelContextFactoryBean ext // now lets start the CamelContext so that all its possible // dependencies are initialized try { - LOG.debug("Starting the context now!"); + LOG.trace("Starting the context now"); getContext().start(); } catch (Exception e) { throw wrapRuntimeCamelException(e); Added: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java?rev=1345637&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java (added) +++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java Sun Jun 3 09:40:19 2012 @@ -0,0 +1,78 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.spring.spi; + +import java.util.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.properties.PropertiesResolver; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; + +/** + * A {@link PropertyPlaceholderConfigurer} that bridges Camel's + * property placeholder with the Spring property placeholder mechanism. + */ +public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements PropertiesResolver { + + // NOTE: this class must be in the spi package as if its in the root package, then Spring fails to parse the XML + // files due some weird spring issue. But that is okay as having this class in the spi package is fine anyway. + + private final Properties properties = new Properties(); + private PropertiesResolver resolver; + private String id; + + @Override + protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { + // store all the spring properties so we can refer to them later + properties.putAll(props); + super.processProperties(beanFactoryToProcess, props); + } + + @Override + public void setBeanName(String beanName) { + this.id = beanName; + super.setBeanName(beanName); + } + + @Override + public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, String... uri) throws Exception { + // return the spring properties, if it + Properties answer = new Properties(); + for (String u : uri) { + String ref = "ref:" + id; + if (ref.equals(u)) { + answer.putAll(properties); + } else if (resolver != null) { + Properties p = resolver.resolveProperties(context, ignoreMissingLocation, u); + if (p != null) { + answer.putAll(p); + } + } + } + if (answer.isEmpty()) { + return null; + } else { + return answer; + } + } + + public void setResolver(PropertiesResolver resolver) { + this.resolver = resolver; + } +} Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.java?rev=1345637&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.java Sun Jun 3 09:40:19 2012 @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.properties; + +import org.apache.camel.spring.SpringTestSupport; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * + */ +public class CamelSpringPropertyPlaceholderConfigurer2Test extends SpringTestSupport { + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.xml"); + } + + public void testCamelSpringPropertyPlaceholderConfigurerTest() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bonjour Camel"); + + template.sendBody("direct:bar", "Camel"); + + assertMockEndpointsSatisfied(); + } + +} Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.java?rev=1345637&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.java Sun Jun 3 09:40:19 2012 @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.properties; + +import org.apache.camel.spring.SpringTestSupport; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * + */ +public class CamelSpringPropertyPlaceholderConfigurerTest extends SpringTestSupport { + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml"); + } + + public void testCamelSpringPropertyPlaceholderConfigurerTest() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Bonjour Camel"); + + template.sendBody("direct:bar", "Camel"); + + assertMockEndpointsSatisfied(); + } + +} Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/HelloBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/HelloBean.java?rev=1345637&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/HelloBean.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/properties/HelloBean.java Sun Jun 3 09:40:19 2012 @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.properties; + +/** + * + */ +public class HelloBean { + + private String greeting; + + public String getGreeting() { + return greeting; + } + + public void setGreeting(String greeting) { + this.greeting = greeting; + } + + public String say(String hi) { + return greeting + " " + hi; + } +} Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.xml?rev=1345637&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.xml (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer2Test.xml Sun Jun 3 09:40:19 2012 @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml?rev=1345637&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml Sun Jun 3 09:40:19 2012 @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/cheese.properties URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/cheese.properties?rev=1345637&r1=1345636&r2=1345637&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/cheese.properties (original) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/cheese.properties Sun Jun 3 09:40:19 2012 @@ -22,4 +22,6 @@ myCoolCharset=iso-8859-1 slipDelimiter=## delay=25 -max=3 \ No newline at end of file +max=3 + +hi=Bonjour \ No newline at end of file