1 5 package com.tc.bundles; 6 7 import org.osgi.framework.Bundle; 8 import org.osgi.framework.BundleException; 9 import org.osgi.framework.InvalidSyntaxException; 10 import org.osgi.framework.ServiceReference; 11 12 import com.tc.config.Directories; 13 import com.tc.logging.TCLogger; 14 import com.tc.logging.TCLogging; 15 import com.terracottatech.config.Modules; 16 17 import java.io.File ; 18 import java.io.FileNotFoundException ; 19 import java.net.URL ; 20 import java.util.ArrayList ; 21 import java.util.Dictionary ; 22 import java.util.List ; 23 24 27 public interface EmbeddedOSGiRuntime { 28 29 public static final String MODULES_URL_PROPERTY_NAME = "tc.tests.configuration.modules.url"; 30 31 void installBundle(final String bundleName, final String bundleVersion) throws BundleException; 32 33 void startBundle(final String bundleName, final String bundleVersion) throws BundleException; 34 35 Bundle getBundle(final String bundleName, final String bundleVersion); 36 37 void registerService(final Object serviceObject, final Dictionary serviceProps) throws BundleException; 38 39 void stopBundle(final String bundleName, final String bundleVersion) throws BundleException; 40 41 void uninstallBundle(final String bundleName, final String bundleVersion) throws BundleException; 42 43 ServiceReference[] getAllServiceReferences(final String clazz, final String filter) throws InvalidSyntaxException; 44 45 Object getService(final ServiceReference service); 46 47 void ungetService(final ServiceReference service); 48 49 52 void shutdown() throws BundleException; 53 54 static class Factory { 55 56 private static final TCLogger logger = TCLogging.getLogger(EmbeddedOSGiRuntime.Factory.class); 57 58 public static EmbeddedOSGiRuntime createOSGiRuntime(final Modules modules) throws Exception { 59 final List prependLocations = new ArrayList (); 60 try { 63 if (Directories.getInstallationRoot() != null) { 64 logger.debug("Prepending bundle repository: " + new File (Directories.getInstallationRoot(), "modules").toURL().toString()); 65 prependLocations.add(new File (Directories.getInstallationRoot(), "modules").toURL()); 66 } 67 } catch (FileNotFoundException fnfe) { 68 } 70 if (System.getProperty(MODULES_URL_PROPERTY_NAME) != null) { 71 prependLocations.add(new URL (System.getProperty(MODULES_URL_PROPERTY_NAME))); 72 } 73 final URL [] prependURLs = new URL [prependLocations.size()]; 74 prependLocations.toArray(prependURLs); 75 76 final URL [] bundleRepositories = new URL [modules.sizeOfRepositoryArray() + prependURLs.length]; 77 for (int pos = 0; pos < prependURLs.length; pos++) { 78 bundleRepositories[pos] = prependURLs[pos]; 79 } 80 for (int pos = prependURLs.length; pos < bundleRepositories.length; pos++) { 81 bundleRepositories[pos] = new URL (modules.getRepositoryArray(pos - prependURLs.length)); 82 } 83 84 logger.info("OSGi Bundle Repositories:"); 85 for (int i = 0; i < bundleRepositories.length; i++) { 86 logger.info("\t" + bundleRepositories[i]); 87 } 88 return new KnopflerfishOSGi(bundleRepositories); 89 } 90 } 91 92 } 93 | Popular Tags |