You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by da...@apache.org on 2010/01/19 15:33:29 UTC
svn commit: r900795 - in /incubator/aries/trunk/spi-fly/src:
main/java/org/apache/aries/spifly/ test/java/org/apache/aries/spifly/
Author: davidb
Date: Tue Jan 19 14:33:29 2010
New Revision: 900795
URL: http://svn.apache.org/viewvc?rev=900795&view=rev
Log:
Refactored the SPIBundleTracker following feedback from Jarek Gawor.
Added:
incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTrackerCustomizer.java (with props)
incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerCustomizerTest.java (with props)
Removed:
incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTracker.java
incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerTest.java
Modified:
incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/Activator.java
incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/ActivatorTest.java
Modified: incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/Activator.java?rev=900795&r1=900794&r2=900795&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/Activator.java (original)
+++ incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/Activator.java Tue Jan 19 14:33:29 2010
@@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@@ -38,7 +39,8 @@
lst = new LogServiceTracker(context, LogService.class.getName(), null);
lst.open();
- bt = new SPIBundleTracker(context, this);
+ bt = new BundleTracker(context, Bundle.ACTIVE,
+ new SPIBundleTrackerCustomizer(this, context.getBundle()));
bt.open();
}
Added: incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTrackerCustomizer.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTrackerCustomizer.java?rev=900795&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTrackerCustomizer.java (added)
+++ incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTrackerCustomizer.java Tue Jan 19 14:33:29 2010
@@ -0,0 +1,123 @@
+/**
+ * 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.aries.spifly;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.log.LogService;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
+
+public class SPIBundleTrackerCustomizer implements BundleTrackerCustomizer {
+ public static final String SPI_PROVIDER_URL = "spi.provider.url";
+ public static final String OPT_IN_HEADER = "SPI-Provider";
+
+ final Activator activator;
+ final Bundle spiBundle;
+
+ public SPIBundleTrackerCustomizer(Activator a, Bundle b) {
+ activator = a;
+ spiBundle = b;
+ }
+
+ public Object addingBundle(Bundle bundle, BundleEvent event) {
+ log(LogService.LOG_INFO, "Bundle Considered for SPI providers: " + bundle.getSymbolicName());
+ if (bundle.equals(spiBundle)) {
+ return null;
+ }
+
+ if (bundle.getHeaders().get(OPT_IN_HEADER) == null) {
+ log(LogService.LOG_INFO, "No '" + OPT_IN_HEADER +
+ "' Manifest header. Skipping bundle: " + bundle.getSymbolicName());
+ return null;
+ } else {
+ log(LogService.LOG_INFO, "Examining bundle for SPI provider: " + bundle.getSymbolicName());
+ }
+
+ Enumeration<?> entries = bundle.findEntries("META-INF/services", "*", false);
+ if (entries == null) {
+ return null;
+ }
+
+ List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();
+ while(entries.hasMoreElements()) {
+ URL url = (URL) entries.nextElement();
+ log(LogService.LOG_INFO, "Found SPI resource: " + url);
+
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
+ String className = reader.readLine();
+ // do we need to read more than one class name?
+
+ Class<?> cls = bundle.loadClass(className);
+ Object o = cls.newInstance();
+ log(LogService.LOG_INFO, "Instantiated SPI provider: " + o);
+
+ Hashtable<String, Object> props = new Hashtable<String, Object>();
+ props.put(SPI_PROVIDER_URL, url);
+
+ String s = url.toExternalForm();
+ int idx = s.lastIndexOf('/');
+ String registrationClassName = className;
+ if (s.length() > idx) {
+ registrationClassName = s.substring(idx + 1);
+ }
+
+ ServiceRegistration reg = bundle.getBundleContext().registerService(registrationClassName, o, props);
+ registrations.add(reg);
+ log(LogService.LOG_INFO, "Registered service: " + reg);
+ } catch (Exception e) {
+ log(LogService.LOG_INFO, "Could not load SPI implementation referred from " + url, e);
+ }
+ }
+
+ return registrations;
+ }
+
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+ // nothing to do here
+ }
+
+ public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
+ if (object instanceof List<?>) {
+ for (Object reg : (List<?>) object) {
+ if (reg instanceof ServiceRegistration) {
+ ((ServiceRegistration) reg).unregister();
+ log(LogService.LOG_INFO, "Unregistered: " + reg);
+ }
+ }
+ }
+ }
+
+ private void log(int level, String message) {
+ activator.log(level, message);
+ }
+
+ private void log(int level, String message, Throwable th) {
+ activator.log(level, message, th);
+ }
+}
Propchange: incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTrackerCustomizer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/aries/trunk/spi-fly/src/main/java/org/apache/aries/spifly/SPIBundleTrackerCustomizer.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/ActivatorTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/ActivatorTest.java?rev=900795&r1=900794&r2=900795&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/ActivatorTest.java (original)
+++ incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/ActivatorTest.java Tue Jan 19 14:33:29 2010
@@ -20,8 +20,6 @@
import junit.framework.TestCase;
-import org.apache.aries.spifly.Activator;
-import org.apache.aries.spifly.SPIBundleTracker;
import org.easymock.IAnswer;
import org.easymock.classextension.EasyMock;
import org.osgi.framework.BundleContext;
@@ -51,7 +49,7 @@
Activator a = new Activator();
a.start(bc);
- assertTrue(a.bt instanceof SPIBundleTracker);
+ assertNotNull(a.bt);
assertNotNull(a.lst);
assertEquals(0, a.logServices.size());
Added: incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerCustomizerTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerCustomizerTest.java?rev=900795&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerCustomizerTest.java (added)
+++ incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerCustomizerTest.java Tue Jan 19 14:33:29 2010
@@ -0,0 +1,196 @@
+/**
+ * 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.aries.spifly;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+
+import junit.framework.TestCase;
+
+import org.easymock.EasyMock;
+import org.easymock.IAnswer;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class SPIBundleTrackerCustomizerTest extends TestCase {
+ public void testAddingBundle() throws Exception {
+ Bundle spiBundle = EasyMock.createMock(Bundle.class);
+ EasyMock.replay(spiBundle);
+ SPIBundleTrackerCustomizer sbt = new SPIBundleTrackerCustomizer(new Activator(), spiBundle);
+
+ URL jarURL = getClass().getResource("TestSPIBundle2_1.0.0.jar");
+ Dictionary<String, Object> headers = getManifestHeaders(jarURL);
+ URL url = new URL("jar:" + jarURL + "!/META-INF/services/javax.xml.parsers.DocumentBuilderFactory");
+ final ClassLoader mockBundleLoader = new URLClassLoader(new URL[] {jarURL});
+
+ Bundle b = EasyMock.createMock(Bundle.class);
+ EasyMock.expect(b.getSymbolicName()).andReturn("x.y.z").anyTimes();
+ EasyMock.expect(b.findEntries("META-INF/services", "*", false))
+ .andReturn(Collections.enumeration(Collections.singleton(url)));
+ EasyMock.expect(b.getHeaders()).andReturn(headers).anyTimes();
+ EasyMock.expect(b.loadClass((String) EasyMock.anyObject())).andAnswer(new IAnswer<Class<?>>() {
+ public Class<?> answer() throws Throwable {
+ return mockBundleLoader.loadClass((String) EasyMock.getCurrentArguments()[0]);
+ }
+ });
+
+ BundleContext bc2 = EasyMock.createMock(BundleContext.class);
+ EasyMock.expect(bc2.registerService(EasyMock.eq("javax.xml.parsers.DocumentBuilderFactory"),
+ EasyMock.anyObject(), (Dictionary<?, ?>) EasyMock.anyObject())).andAnswer(new IAnswer<ServiceRegistration>() {
+ public ServiceRegistration answer() throws Throwable {
+ Object impl = EasyMock.getCurrentArguments()[1];
+ assertEquals("org.example.test.Test2DomBuilderFactory", impl.getClass().getName());
+ assertNotNull(((Dictionary<?, ?>) EasyMock.getCurrentArguments()[2])
+ .get(SPIBundleTrackerCustomizer.SPI_PROVIDER_URL));
+ return EasyMock.createMock(ServiceRegistration.class);
+ }
+ });
+ EasyMock.replay(bc2);
+
+ EasyMock.expect(b.getBundleContext()).andReturn(bc2);
+ EasyMock.replay(b);
+
+ assertEquals(1, ((List<?>) sbt.addingBundle(b, null)).size());
+
+ EasyMock.verify(bc2);
+ EasyMock.verify(b);
+ EasyMock.verify(spiBundle);
+ }
+
+ public void testAddingNonMarkedBundle() throws Exception {
+ Bundle spiBundle = EasyMock.createMock(Bundle.class);
+ EasyMock.replay(spiBundle);
+ SPIBundleTrackerCustomizer sbt = new SPIBundleTrackerCustomizer(new Activator(), spiBundle);
+
+ URL jarURL = getClass().getResource("TestSPIBundle_1.0.0.jar");
+ Dictionary<String, Object> headers = getManifestHeaders(jarURL);
+ URL url = new URL("jar:" + jarURL + "!/META-INF/services/javax.xml.parsers.DocumentBuilderFactory");
+ final ClassLoader mockBundleLoader = new URLClassLoader(new URL[] {jarURL});
+
+ Bundle b = EasyMock.createMock(Bundle.class);
+ EasyMock.expect(b.getSymbolicName()).andReturn("x.y.z").anyTimes();
+ EasyMock.expect(b.findEntries("META-INF/services", "*", false))
+ .andReturn(Collections.enumeration(Collections.singleton(url))).anyTimes();
+ EasyMock.expect(b.getHeaders()).andReturn(headers).anyTimes();
+ EasyMock.expect(b.loadClass((String) EasyMock.anyObject())).andAnswer(new IAnswer<Class<?>>() {
+ public Class<?> answer() throws Throwable {
+ return mockBundleLoader.loadClass((String) EasyMock.getCurrentArguments()[0]);
+ }
+ }).anyTimes();
+
+ BundleContext bc2 = EasyMock.createMock(BundleContext.class);
+ // no services are expected to be registered.
+ EasyMock.replay(bc2);
+
+ EasyMock.expect(b.getBundleContext()).andReturn(bc2).anyTimes();
+ EasyMock.replay(b);
+
+ assertNull(sbt.addingBundle(b, null));
+
+ EasyMock.verify(bc2); // verify that bc2.registerService() was never called
+ EasyMock.verify(b);
+ EasyMock.verify(spiBundle);
+ }
+
+ public void testAddingUnrelatedButMarkedBundle() {
+ Bundle spiBundle = EasyMock.createMock(Bundle.class);
+ EasyMock.replay(spiBundle);
+ SPIBundleTrackerCustomizer sbt = new SPIBundleTrackerCustomizer(new Activator(), spiBundle);
+
+ Dictionary<String, Object> headers = new Hashtable<String, Object>();
+ headers.put(SPIBundleTrackerCustomizer.OPT_IN_HEADER, "somevalue");
+
+ Bundle b = EasyMock.createMock(Bundle.class);
+ EasyMock.expect(b.getSymbolicName()).andReturn("x.y.z").anyTimes();
+ EasyMock.expect(b.getHeaders()).andReturn(headers).anyTimes();
+ EasyMock.expect(b.findEntries("META-INF/services", "*", false)).andReturn(null);
+ EasyMock.replay(b);
+
+ assertNull(sbt.addingBundle(b, null));
+ EasyMock.verify(b);
+ EasyMock.verify(spiBundle);
+ }
+
+ public void testAddingSelf() {
+ Bundle spiBundle = EasyMock.createMock(Bundle.class);
+ EasyMock.expect(spiBundle.getSymbolicName()).andReturn("a.b.c").anyTimes();
+ EasyMock.replay(spiBundle);
+ SPIBundleTrackerCustomizer sbt = new SPIBundleTrackerCustomizer(new Activator(), spiBundle);
+
+ assertNull(sbt.addingBundle(spiBundle, null));
+ EasyMock.verify(spiBundle);
+ }
+
+ public void testRemovedBundle() {
+ Bundle spiBundle = EasyMock.createMock(Bundle.class);
+ EasyMock.replay(spiBundle);
+ SPIBundleTrackerCustomizer sbt = new SPIBundleTrackerCustomizer(new Activator(), spiBundle);
+
+ Bundle b = EasyMock.createMock(Bundle.class);
+ EasyMock.replay(b);
+
+ ServiceRegistration sr1 = EasyMock.createMock(ServiceRegistration.class);
+ sr1.unregister();
+ EasyMock.replay(sr1);
+ ServiceRegistration sr2 = EasyMock.createMock(ServiceRegistration.class);
+ sr2.unregister();
+ EasyMock.replay(sr2);
+ List<ServiceRegistration> regs = Arrays.asList(sr1, sr2);
+ sbt.removedBundle(b, null, regs);
+
+ EasyMock.verify(sr1);
+ EasyMock.verify(sr2);
+ }
+
+ public void testRemovedUnrelatedBundle() {
+ Bundle spiBundle = EasyMock.createMock(Bundle.class);
+ EasyMock.replay(spiBundle);
+ SPIBundleTrackerCustomizer sbt = new SPIBundleTrackerCustomizer(new Activator(), spiBundle);
+
+ Bundle b = EasyMock.createMock(Bundle.class);
+ EasyMock.replay(b);
+
+ sbt.removedBundle(b, null, b);
+ EasyMock.verify(b);
+ }
+
+ private Dictionary<String, Object> getManifestHeaders(URL jarURL) throws IOException {
+ JarFile jf = new JarFile(jarURL.getFile());
+ try {
+ Attributes attrs = jf.getManifest().getMainAttributes();
+ Hashtable<String, Object> headers = new Hashtable<String, Object>();
+ for (Map.Entry<Object, Object> entry : attrs.entrySet()) {
+ headers.put(entry.getKey().toString(), entry.getValue());
+ }
+ return headers;
+ } finally {
+ jf.close();
+ }
+ }
+}
Propchange: incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerCustomizerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/aries/trunk/spi-fly/src/test/java/org/apache/aries/spifly/SPIBundleTrackerCustomizerTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date