You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/04/16 16:39:03 UTC
[5/5] git commit: CAMEL-7361: Spring main should be able to detect
additional spring xml locations on the classpath without any additional
configuration.
CAMEL-7361: Spring main should be able to detect additional spring xml locations on the classpath without any additional configuration.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5e130035
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5e130035
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5e130035
Branch: refs/heads/camel-2.13.x
Commit: 5e1300351ce5cdf40aa77f4ca6bf00ba8cd45103
Parents: 933cf04
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Apr 16 07:41:35 2014 -0700
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Apr 16 07:42:42 2014 -0700
----------------------------------------------------------------------
.../main/java/org/apache/camel/spring/Main.java | 72 ++++++++++++++++++--
.../java/org/apache/camel/spring/DummyBean.java | 30 ++++++++
.../org/apache/camel/spring/MainDummyTest.java | 39 +++++++++++
.../test/resources/META-INF/spring/dummy.xml | 28 ++++++++
4 files changed, 162 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/5e130035/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
index c0e9a90..23fc233 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/Main.java
@@ -16,7 +16,14 @@
*/
package org.apache.camel.spring;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.Enumeration;
import java.util.HashMap;
+import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
@@ -36,12 +43,13 @@ import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* A command line tool for booting up a CamelContext using an optional Spring
* ApplicationContext
- *
- * @version
*/
@SuppressWarnings("deprecation")
public class Main extends MainSupport {
+
+ public static final String LOCATION_PROPERTIES = "META-INF/spring/location.properties";
protected static Main instance;
+ private static final Charset UTF8 = Charset.forName("UTF-8");
private String applicationContextUri = "META-INF/spring/*.xml";
private String fileApplicationContextUri;
@@ -82,7 +90,7 @@ public class Main extends MainSupport {
public static Main getInstance() {
return instance;
}
-
+
// Properties
// -------------------------------------------------------------------------
public AbstractApplicationContext getApplicationContext() {
@@ -165,12 +173,15 @@ public class Main extends MainSupport {
return getCamelContexts().get(0).createProducerTemplate();
}
- protected AbstractApplicationContext createDefaultApplicationContext() {
+ protected AbstractApplicationContext createDefaultApplicationContext() throws IOException {
+ // daisy chain the parent and additional contexts
+ ApplicationContext parentContext = getParentApplicationContext();
+ parentContext = addAdditionalLocationsFromClasspath(parentContext);
+
// file based
if (getFileApplicationContextUri() != null) {
String[] args = getFileApplicationContextUri().split(";");
- ApplicationContext parentContext = getParentApplicationContext();
if (parentContext != null) {
return new FileSystemXmlApplicationContext(args, parentContext);
} else {
@@ -180,14 +191,13 @@ public class Main extends MainSupport {
// default to classpath based
String[] args = getApplicationContextUri().split(";");
- ApplicationContext parentContext = getParentApplicationContext();
if (parentContext != null) {
return new ClassPathXmlApplicationContext(args, parentContext);
} else {
return new ClassPathXmlApplicationContext(args);
}
}
-
+
protected Map<String, CamelContext> getCamelContextMap() {
Map<String, SpringCamelContext> map = applicationContext.getBeansOfType(SpringCamelContext.class);
Set<Map.Entry<String, SpringCamelContext>> entries = map.entrySet();
@@ -203,4 +213,52 @@ public class Main extends MainSupport {
protected ModelFileGenerator createModelFileGenerator() throws JAXBException {
return new ModelFileGenerator(new CamelNamespaceHandler().getJaxbContext());
}
+
+ protected ApplicationContext addAdditionalLocationsFromClasspath(ApplicationContext parentContext) throws IOException {
+ StringBuilder sb = new StringBuilder();
+
+ Set<String> locations = new LinkedHashSet<String>();
+ findLocations(locations, Main.class.getClassLoader());
+
+ if (!locations.isEmpty()) {
+ LOG.info("Found locations for additional Spring XML files: {}", locations);
+
+ String[] locs = locations.toArray(new String[locations.size()]);
+ ClassPathXmlApplicationContext additionalContext;
+ if (parentContext != null) {
+ additionalContext = new ClassPathXmlApplicationContext(locs, parentContext);
+ } else {
+ additionalContext = new ClassPathXmlApplicationContext(locs);
+ }
+ // and we must start the app context as well
+ additionalContext.start();
+ return additionalContext;
+ } else {
+ return null;
+ }
+ }
+
+ protected void findLocations(Set<String> locations, ClassLoader classLoader) throws IOException {
+ Enumeration<URL> resources = classLoader.getResources(LOCATION_PROPERTIES);
+ while (resources.hasMoreElements()) {
+ URL url = resources.nextElement();
+ BufferedReader reader = IOHelper.buffered(new InputStreamReader(url.openStream(), UTF8));
+ try {
+ while (true) {
+ String line = reader.readLine();
+ if (line == null) {
+ break;
+ }
+ line = line.trim();
+ if (line.startsWith("#") || line.length() == 0) {
+ continue;
+ }
+ locations.add(line);
+ }
+ } finally {
+ IOHelper.close(reader, null, LOG);
+ }
+ }
+ }
+
}
http://git-wip-us.apache.org/repos/asf/camel/blob/5e130035/components/camel-spring/src/test/java/org/apache/camel/spring/DummyBean.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/DummyBean.java b/components/camel-spring/src/test/java/org/apache/camel/spring/DummyBean.java
new file mode 100644
index 0000000..7be54af
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/DummyBean.java
@@ -0,0 +1,30 @@
+/**
+ * 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;
+
+public class DummyBean {
+
+ private String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/5e130035/components/camel-spring/src/test/java/org/apache/camel/spring/MainDummyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/MainDummyTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/MainDummyTest.java
new file mode 100644
index 0000000..8336172
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/MainDummyTest.java
@@ -0,0 +1,39 @@
+/**
+ * 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;
+
+import junit.framework.TestCase;
+import org.apache.camel.CamelContext;
+
+public class MainDummyTest extends TestCase {
+
+ public void testMain() throws Exception {
+ Main main = new Main();
+ main.start();
+
+ // should also be a Camel
+ CamelContext camel = main.getApplicationContext().getBean(CamelContext.class);
+ assertNotNull("Camel should be in Spring", camel);
+
+ DummyBean dummy = (DummyBean) main.getApplicationContext().getBean("dummy");
+ assertNotNull(dummy);
+ assertEquals("John Doe", dummy.getName());
+
+ main.stop();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/5e130035/components/camel-spring/src/test/resources/META-INF/spring/dummy.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/META-INF/spring/dummy.xml b/components/camel-spring/src/test/resources/META-INF/spring/dummy.xml
new file mode 100644
index 0000000..e83e5d8
--- /dev/null
+++ b/components/camel-spring/src/test/resources/META-INF/spring/dummy.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+ <bean id="dummy" class="org.apache.camel.spring.DummyBean">
+ <property name="name" value="John Doe"/>
+ </bean>
+
+</beans>