Author: ningjiang Date: Fri Jul 2 07:00:02 2010 New Revision: 959879 URL: http://svn.apache.org/viewvc?rev=959879&view=rev Log: CAMEL-2895 fixed the issue of OsgiPackageScanClassResolver
Added: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/ camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/MyRouteBuilder.java (with props) camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/OSGiPackageScanTest.java (with props) camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/ camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml (with props) Modified: camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/OSGiIntegrationTestSupport.java Modified: camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java?rev=959879&r1=959878&r2=959879&view=diff ============================================================================== --- camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java (original) +++ camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiPackageScanClassResolver.java Fri Jul 2 07:00:02 2010 @@ -16,11 +16,18 @@ */ package org.apache.camel.core.osgi; +import java.lang.reflect.Method; +import java.net.URL; import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; import java.util.Set; -import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader; import org.apache.camel.impl.DefaultPackageScanClassResolver; +import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader; +import org.apache.camel.spi.PackageScanFilter; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -36,8 +43,55 @@ public class OsgiPackageScanClassResolve } public Set<ClassLoader> getClassLoaders() { + // now we just use bundle classloader to load the class return Collections.<ClassLoader>singleton(new BundleDelegatingClassLoader(bundle)); } + public void find(PackageScanFilter test, String packageName, Set<Class<?>> classes) { + packageName = packageName.replace('.', '/'); + loadImplementationsInBundle(test, packageName, classes); + } + + private void loadImplementationsInBundle(PackageScanFilter test, String packageName, Set<Class<?>> classes) { + Set<String> urls = getImplementationsInBundle(test, packageName); + if (urls != null) { + for (String url : urls) { + // substring to avoid leading slashes + addIfMatching(test, url, classes); + } + } + } + + @SuppressWarnings("unchecked") + private Set<String> getImplementationsInBundle(PackageScanFilter test, String packageName) { + try { + Bundle[] bundles; + if (bundle.getBundleContext()!= null) { + bundles = bundle.getBundleContext().getBundles(); + } else { + bundles = new Bundle[]{bundle}; + } + Set<String> urls = new HashSet<String>(); + for (Bundle bd : bundles) { + if (log.isTraceEnabled()) { + log.trace("Searching in bundle:" + bd); + } + Enumeration<URL> paths = bd.findEntries("/" + packageName, "*.class", true); + while (paths != null && paths.hasMoreElements()) { + URL path = paths.nextElement(); + String pathString = path.getPath(); + String urlString = pathString.substring(pathString.indexOf(packageName)); + urls.add(urlString); + if (log.isTraceEnabled()) { + log.trace("Added url: " + urlString); + } + } + } + return urls; + } catch (Throwable t) { + log.error("Could not search osgi bundles for classes matching criteria: " + test + "due to an Exception: " + t.getMessage()); + return null; + } + } } Modified: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/OSGiIntegrationTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/OSGiIntegrationTestSupport.java?rev=959879&r1=959878&r2=959879&view=diff ============================================================================== --- camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/OSGiIntegrationTestSupport.java (original) +++ camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/OSGiIntegrationTestSupport.java Fri Jul 2 07:00:02 2010 @@ -86,8 +86,9 @@ public class OSGiIntegrationTestSupport "camel-core", "camel-spring", "camel-test"), workingDirectory("target/paxrunner/"), - - felix(), equinox()); + + equinox(), + felix()); return options; } Added: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/MyRouteBuilder.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/MyRouteBuilder.java?rev=959879&view=auto ============================================================================== --- camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/MyRouteBuilder.java (added) +++ camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/MyRouteBuilder.java Fri Jul 2 07:00:02 2010 @@ -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.itest.osgi.packages; + +import org.springframework.stereotype.Component; +import org.apache.camel.builder.RouteBuilder; + +...@component +public class MyRouteBuilder extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("direct:start").to("mock:result"); + } + +} Propchange: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/MyRouteBuilder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/MyRouteBuilder.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/OSGiPackageScanTest.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/OSGiPackageScanTest.java?rev=959879&view=auto ============================================================================== --- camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/OSGiPackageScanTest.java (added) +++ camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/OSGiPackageScanTest.java Fri Jul 2 07:00:02 2010 @@ -0,0 +1,43 @@ +/** + * 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.itest.osgi.packages; + +import org.ops4j.pax.exam.junit.JUnit4TestRunner; +import org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.itest.osgi.OSGiIntegrationSpringTestSupport; +import org.junit.Test; +import org.junit.runner.RunWith; + +...@runwith(JUnit4TestRunner.class) +public class OSGiPackageScanTest extends OSGiIntegrationSpringTestSupport { + @Test + public void testSendMessage() throws Exception { + MockEndpoint mock = getMandatoryEndpoint("mock:result", MockEndpoint.class); + assertNotNull("The mock endpoint should not be null", mock); + + mock.expectedBodiesReceived("Hello World"); + template.sendBody("direct:start", "Hello World"); + assertMockEndpointsSatisfied(); + } + + @Override + protected OsgiBundleXmlApplicationContext createApplicationContext() { + return new OsgiBundleXmlApplicationContext(new String[]{"org/apache/camel/itest/osgi/packages/CamelContext.xml"}); + } + +} Propchange: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/OSGiPackageScanTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/packages/OSGiPackageScanTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml?rev=959879&view=auto ============================================================================== --- camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml (added) +++ camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml Fri Jul 2 07:00:02 2010 @@ -0,0 +1,36 @@ +<?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" + xmlns:osgi="http://camel.apache.org/schema/osgi" + xmlns:camel="http://camel.apache.org/schema/spring" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> + + <!-- context:component-scan base-package="org.apache.camel.itest.osgi.packages" /--> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <package>org.apache.camel.itest.osgi.packages</package> + </camelContext> + +</beans> Propchange: camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/packages/CamelContext.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml