Author: gnodet
Date: Wed Feb 2 21:50:16 2011
New Revision: 1066642
URL: http://svn.apache.org/viewvc?rev=1066642&view=rev
Log:
[CAMEL-3614] When the OsgiTypeConverter looks for TypeConverterLoader services
in the OSGi registry, it should use its own BundleContext instead of the one
from the client bundle so that class space consistency can be fully enforced
Added:
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/utils/BundleContextUtils.java
Modified:
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDefaultCamelContext.java
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/osgi/OsgiSpringCamelContext.java
Modified:
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java?rev=1066642&r1=1066641&r2=1066642&view=diff
==============================================================================
---
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
(original)
+++
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintCamelContext.java
Wed Feb 2 21:50:16 2011
@@ -23,6 +23,7 @@ import org.apache.camel.core.osgi.OsgiCl
import org.apache.camel.core.osgi.OsgiFactoryFinderResolver;
import org.apache.camel.core.osgi.OsgiPackageScanClassResolver;
import org.apache.camel.core.osgi.OsgiTypeConverter;
+import org.apache.camel.core.osgi.utils.BundleContextUtils;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Registry;
import org.osgi.framework.BundleContext;
@@ -87,7 +88,12 @@ public class BlueprintCamelContext exten
@Override
protected TypeConverter createTypeConverter() {
- return new OsgiTypeConverter(bundleContext, getInjector());
+ // CAMEL-3614: make sure we use a bundle context which imports
org.apache.camel.impl.converter package
+ BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
+ if (ctx == null) {
+ ctx = bundleContext;
+ }
+ return new OsgiTypeConverter(ctx, getInjector());
}
@Override
Modified:
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDefaultCamelContext.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDefaultCamelContext.java?rev=1066642&r1=1066641&r2=1066642&view=diff
==============================================================================
---
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDefaultCamelContext.java
(original)
+++
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDefaultCamelContext.java
Wed Feb 2 21:50:16 2011
@@ -17,6 +17,7 @@
package org.apache.camel.core.osgi;
import org.apache.camel.TypeConverter;
+import org.apache.camel.core.osgi.utils.BundleContextUtils;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Registry;
import org.osgi.framework.BundleContext;
@@ -48,7 +49,12 @@ public class OsgiDefaultCamelContext ext
@Override
protected TypeConverter createTypeConverter() {
- return new OsgiTypeConverter(bundleContext, getInjector());
+ // CAMEL-3614: make sure we use a bundle context which imports
org.apache.camel.impl.converter package
+ BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
+ if (ctx == null) {
+ ctx = bundleContext;
+ }
+ return new OsgiTypeConverter(ctx, getInjector());
}
@Override
Added:
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/utils/BundleContextUtils.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/utils/BundleContextUtils.java?rev=1066642&view=auto
==============================================================================
---
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/utils/BundleContextUtils.java
(added)
+++
camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/utils/BundleContextUtils.java
Wed Feb 2 21:50:16 2011
@@ -0,0 +1,65 @@
+/**
+ * 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.core.osgi.utils;
+
+import java.lang.reflect.Method;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+/**
+ * Helper class
+ */
+public class BundleContextUtils {
+
+ /**
+ * Retrieve the BundleContext that the given class has been loaded from.
+ *
+ * @param clazz the class to find the bundle context from
+ * @return the bundle context or <code>null</code> if it can't be found
+ */
+ public static final BundleContext getBundleContext(Class clazz) {
+ //
+ // Ideally we should use
FrameworkUtil.getBundle(clazz).getBundleContext()
+ // but that does not exist in OSGi 4.1, so until we upgrade, we keep
that one
+ //
+ try
+ {
+ ClassLoader cl = clazz.getClassLoader();
+ Class clClazz = cl.getClass();
+ Method mth = null;
+ while (clClazz != null) {
+ try {
+ mth = clClazz.getDeclaredMethod("getBundle");
+ break;
+ } catch (NoSuchMethodException e) {
+ // Ignore
+ }
+ clClazz = clClazz.getSuperclass();
+ }
+ if (mth != null) {
+ mth.setAccessible(true);
+ return ((Bundle) mth.invoke(cl)).getBundleContext();
+ }
+ }
+ catch (Throwable t)
+ {
+ // Ignore
+ }
+ return null;
+ }
+}
Modified:
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/osgi/OsgiSpringCamelContext.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/osgi/OsgiSpringCamelContext.java?rev=1066642&r1=1066641&r2=1066642&view=diff
==============================================================================
---
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/osgi/OsgiSpringCamelContext.java
(original)
+++
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/osgi/OsgiSpringCamelContext.java
Wed Feb 2 21:50:16 2011
@@ -36,7 +36,12 @@ public class OsgiSpringCamelContext exte
@Override
protected TypeConverter createTypeConverter() {
- return new OsgiTypeConverter(bundleContext, getInjector());
+ // CAMEL-3614: make sure we use a bundle context which imports
org.apache.camel.impl.converter package
+ BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
+ if (ctx == null) {
+ ctx = bundleContext;
+ }
+ return new OsgiTypeConverter(ctx, getInjector());
}
@Override