You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2011/06/02 22:10:07 UTC
svn commit: r1130775 - in /cxf/trunk: distribution/bundle/all/
distribution/bundle/jaxrs/ distribution/bundle/minimal/
rt/core/src/main/java/org/apache/cxf/bus/extension/
rt/core/src/main/java/org/apache/cxf/bus/osgi/
Author: dkulp
Date: Thu Jun 2 20:10:06 2011
New Revision: 1130775
URL: http://svn.apache.org/viewvc?rev=1130775&view=rev
Log:
Add a BundleActivator to find the bus-extension.txt files in other
bundles so the Bus can pick them up automatically and get them
registered and usable.
Added:
cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionRegistry.java (with props)
cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/
cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java (with props)
Modified:
cxf/trunk/distribution/bundle/all/pom.xml
cxf/trunk/distribution/bundle/jaxrs/pom.xml
cxf/trunk/distribution/bundle/minimal/pom.xml
cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java
cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionFragmentParser.java
cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java
Modified: cxf/trunk/distribution/bundle/all/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/distribution/bundle/all/pom.xml?rev=1130775&r1=1130774&r2=1130775&view=diff
==============================================================================
--- cxf/trunk/distribution/bundle/all/pom.xml (original)
+++ cxf/trunk/distribution/bundle/all/pom.xml Thu Jun 2 20:10:06 2011
@@ -367,6 +367,7 @@
javax.xml.ws*;version="[0.0,3)",
*
</Import-Package>
+ <Bundle-Activator>org.apache.cxf.bus.osgi.OSGiExtensionLocator</Bundle-Activator>
<Export-Package>
org.apache.cxf.*;version=${cxf.osgi.version.clean};-split-package:=merge-first,
'=META-INF.cxf.osgi',
Modified: cxf/trunk/distribution/bundle/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/distribution/bundle/jaxrs/pom.xml?rev=1130775&r1=1130774&r2=1130775&view=diff
==============================================================================
--- cxf/trunk/distribution/bundle/jaxrs/pom.xml (original)
+++ cxf/trunk/distribution/bundle/jaxrs/pom.xml Thu Jun 2 20:10:06 2011
@@ -170,6 +170,7 @@
javax.wsdl*;resolution:=optional,
*
</Import-Package>
+ <Bundle-Activator>org.apache.cxf.bus.osgi.OSGiExtensionLocator</Bundle-Activator>
<Export-Package>
org.apache.cxf.*;version=${cxf.osgi.version.clean};-split-package:=merge-first,
'=META-INF.cxf.osgi',
Modified: cxf/trunk/distribution/bundle/minimal/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/distribution/bundle/minimal/pom.xml?rev=1130775&r1=1130774&r2=1130775&view=diff
==============================================================================
--- cxf/trunk/distribution/bundle/minimal/pom.xml (original)
+++ cxf/trunk/distribution/bundle/minimal/pom.xml Thu Jun 2 20:10:06 2011
@@ -217,6 +217,7 @@
javax.xml.ws*;version="[0.0,3)",
*
</Import-Package>
+ <Bundle-Activator>org.apache.cxf.bus.osgi.OSGiExtensionLocator</Bundle-Activator>
<Export-Package>
org.apache.cxf.*;version=${cxf.osgi.version.clean};-split-package:=merge-first,
'=META-INF.cxf',
Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java?rev=1130775&r1=1130774&r2=1130775&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/Extension.java Thu Jun 2 20:10:06 2011
@@ -28,12 +28,25 @@ import org.apache.cxf.common.util.String
public class Extension {
- private String className;
- private Class<?> clazz;
- private String interfaceName;
- private boolean deferred;
- private Collection<String> namespaces = new ArrayList<String>();
- private Object obj;
+ protected String className;
+ protected ClassLoader classloader;
+ protected Class<?> clazz;
+ protected String interfaceName;
+ protected boolean deferred;
+ protected Collection<String> namespaces = new ArrayList<String>();
+ protected Object obj;
+
+ public Extension() {
+ }
+ public Extension(Extension ext) {
+ className = ext.className;
+ interfaceName = ext.interfaceName;
+ deferred = ext.deferred;
+ namespaces = ext.namespaces;
+ obj = ext.obj;
+ clazz = ext.clazz;
+ classloader = ext.classloader;
+ }
public String getName() {
return StringUtils.isEmpty(interfaceName) ? className : interfaceName;
@@ -42,6 +55,13 @@ public class Extension {
return obj;
}
+ public Extension cloneNoObject() {
+ Extension ext = new Extension(this);
+ ext.obj = null;
+ ext.clazz = null;
+ return ext;
+ }
+
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("class: ");
@@ -63,26 +83,11 @@ public class Extension {
return buf.toString();
}
- Class<?> getClassObject(ClassLoader cl) {
- if (clazz == null) {
- try {
- clazz = cl.loadClass(className);
- } catch (ClassNotFoundException ex) {
- try {
- // using the extension classloader as a fallback
- clazz = this.getClass().getClassLoader().loadClass(className);
- } catch (ClassNotFoundException nex) {
- throw new ExtensionException(nex);
- }
- }
- }
- return clazz;
- }
- String getClassname() {
+ public String getClassname() {
return className;
}
- void setClassname(String i) {
+ public void setClassname(String i) {
clazz = null;
className = i;
}
@@ -95,19 +100,45 @@ public class Extension {
interfaceName = i;
}
- boolean isDeferred() {
+ public boolean isDeferred() {
return deferred;
}
- void setDeferred(boolean d) {
+ public void setDeferred(boolean d) {
deferred = d;
}
- Collection<String> getNamespaces() {
+ public Collection<String> getNamespaces() {
return namespaces;
}
- Object load(ClassLoader cl, Bus b) {
+
+
+
+ public Class<?> getClassObject(ClassLoader cl) {
+ if (clazz == null) {
+ if (classloader != null) {
+ try {
+ clazz = classloader.loadClass(className);
+ return clazz;
+ } catch (ClassNotFoundException nex) {
+ //ignore, fall into the stuff below
+ }
+ }
+ try {
+ clazz = cl.loadClass(className);
+ } catch (ClassNotFoundException ex) {
+ try {
+ // using the extension classloader as a fallback
+ clazz = this.getClass().getClassLoader().loadClass(className);
+ } catch (ClassNotFoundException nex) {
+ throw new ExtensionException(nex);
+ }
+ }
+ }
+ return clazz;
+ }
+ public Object load(ClassLoader cl, Bus b) {
try {
Class<?> cls = getClassObject(cl);
try {
@@ -130,8 +161,16 @@ public class Extension {
return obj;
}
- Class loadInterface(ClassLoader cl) {
+ public Class loadInterface(ClassLoader cl) {
Class<?> cls = null;
+ if (classloader != null) {
+ try {
+ return classloader.loadClass(interfaceName);
+ } catch (ClassNotFoundException nex) {
+ //ignore, fall into the stuff below
+ }
+ }
+
try {
cls = cl.loadClass(interfaceName);
} catch (ClassNotFoundException ex) {
Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionFragmentParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionFragmentParser.java?rev=1130775&r1=1130774&r2=1130775&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionFragmentParser.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionFragmentParser.java Thu Jun 2 20:10:06 2011
@@ -42,7 +42,7 @@ public class ExtensionFragmentParser {
private static final String INTERFACE_ATTR_NAME = "interface";
private static final String DEFERRED_ATTR_NAME = "deferred";
- List<Extension> getExtensionsFromXML(InputStream is) {
+ public List<Extension> getExtensionsFromXML(InputStream is) {
Document document = null;
try {
document = StaxUtils.read(is);
@@ -62,7 +62,7 @@ public class ExtensionFragmentParser {
* @return list of Extensions
* @throws IOException
*/
- List<Extension> getExtensionsFromText(InputStream is) throws IOException {
+ public List<Extension> getExtensionsFromText(InputStream is) throws IOException {
List<Extension> extensions = new ArrayList<Extension>();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = reader.readLine();
Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java?rev=1130775&r1=1130774&r2=1130775&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionManagerImpl.java Thu Jun 2 20:10:06 2011
@@ -101,6 +101,12 @@ public class ExtensionManagerImpl implem
} catch (IOException ex) {
throw new ExtensionException(ex);
}
+
+ for (Map.Entry<String, Extension> ext : ExtensionRegistry.getRegisteredExtensions().entrySet()) {
+ if (all.containsKey(ext.getKey())) {
+ all.put(ext.getKey(), ext.getValue());
+ }
+ }
}
public void add(Extension ex) {
all.put(ex.getName(), ex);
@@ -140,16 +146,24 @@ public class ExtensionManagerImpl implem
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
InputStream is = url.openStream();
- List<Extension> exts;
- if (resource.endsWith("xml")) {
- LOG.log(Level.WARNING, "DEPRECATED_EXTENSIONS",
- new Object[] {resource, url, BUS_EXTENSION_RESOURCE});
- exts = new ExtensionFragmentParser().getExtensionsFromXML(is);
- } else {
- exts = new ExtensionFragmentParser().getExtensionsFromText(is);
- }
- for (Extension e : exts) {
- all.put(e.getName(), e);
+ try {
+ List<Extension> exts;
+ if (resource.endsWith("xml")) {
+ LOG.log(Level.WARNING, "DEPRECATED_EXTENSIONS",
+ new Object[] {resource, url, BUS_EXTENSION_RESOURCE});
+ exts = new ExtensionFragmentParser().getExtensionsFromXML(is);
+ } else {
+ exts = new ExtensionFragmentParser().getExtensionsFromText(is);
+ }
+ for (Extension e : exts) {
+ all.put(e.getName(), e);
+ }
+ } finally {
+ try {
+ is.close();
+ } catch (IOException ex) {
+ //ignore
+ }
}
}
}
Added: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionRegistry.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionRegistry.java?rev=1130775&view=auto
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionRegistry.java (added)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionRegistry.java Thu Jun 2 20:10:06 2011
@@ -0,0 +1,61 @@
+/**
+ * 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.cxf.bus.extension;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * Static registry of extensions that are loaded in addition to the
+ * extensions the Bus will automatically detect. Mostly used by
+ * the OSGi bundle activator to detect extensions in bundles outside
+ * the CXF bundle.
+ */
+public final class ExtensionRegistry {
+ private static ConcurrentMap<String, Extension> extensions
+ = new ConcurrentHashMap<String, Extension>();
+
+ private ExtensionRegistry() {
+ //singleton
+ }
+
+ public static Map<String, Extension> getRegisteredExtensions() {
+ Map<String, Extension> exts = new HashMap<String, Extension>(extensions.size());
+ for (Map.Entry<String, Extension> ext : extensions.entrySet()) {
+ exts.put(ext.getKey(), ext.getValue().cloneNoObject());
+ }
+ return exts;
+ }
+
+ public static void removeExtensions(List<Extension> list) {
+ for (Extension e : list) {
+ extensions.remove(e.getName(), e);
+ }
+ }
+
+ public static void addExtensions(List<Extension> list) {
+ for (Extension e : list) {
+ extensions.putIfAbsent(e.getName(), e);
+ }
+ }
+}
Propchange: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionRegistry.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/extension/ExtensionRegistry.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java?rev=1130775&view=auto
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java (added)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java Thu Jun 2 20:10:06 2011
@@ -0,0 +1,141 @@
+/**
+ * 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.cxf.bus.osgi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.cxf.bus.extension.Extension;
+import org.apache.cxf.bus.extension.ExtensionFragmentParser;
+import org.apache.cxf.bus.extension.ExtensionRegistry;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.SynchronousBundleListener;
+
+/**
+ *
+ */
+public class OSGiExtensionLocator implements BundleActivator, SynchronousBundleListener {
+ private ConcurrentMap<Long, List<Extension>> extensions
+ = new ConcurrentHashMap<Long, List<Extension>>();
+ private long id;
+
+ /** {@inheritDoc}*/
+ public void bundleChanged(BundleEvent event) {
+ if (event.getType() == BundleEvent.RESOLVED && id != event.getBundle().getBundleId()) {
+ try {
+ register(event.getBundle());
+ } catch (Exception ex) {
+ //ignore
+ }
+ } else if (event.getType() == BundleEvent.UNRESOLVED || event.getType() == BundleEvent.UNINSTALLED) {
+ unregister(event.getBundle().getBundleId());
+ }
+ }
+
+ /** {@inheritDoc}*/
+ public void start(BundleContext context) throws Exception {
+ context.addBundleListener(this);
+ id = context.getBundle().getBundleId();
+ for (Bundle bundle : context.getBundles()) {
+ if ((bundle.getState() == Bundle.RESOLVED
+ || bundle.getState() == Bundle.STARTING
+ || bundle.getState() == Bundle.ACTIVE
+ || bundle.getState() == Bundle.STOPPING)
+ && bundle.getBundleId() != context.getBundle().getBundleId()) {
+ register(bundle);
+ }
+ }
+ }
+
+ /** {@inheritDoc}*/
+ public void stop(BundleContext context) throws Exception {
+ context.removeBundleListener(this);
+ while (!extensions.isEmpty()) {
+ unregister(extensions.keySet().iterator().next());
+ }
+ }
+
+
+ protected void register(final Bundle bundle) throws IOException {
+ List<Extension> list = extensions.get(bundle.getBundleId());
+ Enumeration e = bundle.findEntries("META-INF/cxf/", "bus-extensions.txt", false);
+ if (e != null) {
+ while (e.hasMoreElements()) {
+ final URL u = (URL)e.nextElement();
+ InputStream ins = u.openStream();
+ List<Extension> orig = new ExtensionFragmentParser()
+ .getExtensionsFromText(ins);
+ ins.close();
+
+ if (orig != null && !orig.isEmpty()) {
+ if (list == null) {
+ list = new CopyOnWriteArrayList<Extension>();
+ extensions.put(bundle.getBundleId(), list);
+ }
+ for (Extension ext : orig) {
+ list.add(new OSGiExtension(ext, bundle));
+ }
+ ExtensionRegistry.addExtensions(list);
+ }
+ }
+ }
+ }
+ protected void unregister(final long bundleId) {
+ List<Extension> list = extensions.get(bundleId);
+ if (list != null) {
+ ExtensionRegistry.removeExtensions(list);
+ }
+ }
+ public class OSGiExtension extends Extension {
+ final Bundle bundle;
+ public OSGiExtension(Extension e, Bundle b) {
+ super(e);
+ bundle = b;
+ }
+ public Class<?> getClassObject(ClassLoader cl) {
+ if (clazz == null) {
+ try {
+ clazz = bundle.loadClass(className);
+ } catch (ClassNotFoundException e) {
+ //ignore, fall to super
+ }
+ }
+ return super.getClassObject(cl);
+ }
+ public Class<?> loadInterface(ClassLoader cl) {
+ try {
+ return bundle.loadClass(interfaceName);
+ } catch (ClassNotFoundException e) {
+ //ignore, fall to super
+ }
+ return super.loadInterface(cl);
+ }
+ }
+
+}
Propchange: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/osgi/OSGiExtensionLocator.java
------------------------------------------------------------------------------
svn:keywords = Rev Date