1 5 package com.tc.bundles; 6 7 import org.knopflerfish.framework.Framework; 8 import org.osgi.framework.Bundle; 9 import org.osgi.framework.BundleException; 10 import org.osgi.framework.Constants; 11 import org.osgi.framework.InvalidSyntaxException; 12 import org.osgi.framework.ServiceReference; 13 14 import com.tc.net.util.URLUtil; 15 16 import java.io.IOException ; 17 import java.net.MalformedURLException ; 18 import java.net.URL ; 19 import java.text.MessageFormat ; 20 import java.util.Dictionary ; 21 22 26 final class KnopflerfishOSGi extends AbstractEmbeddedOSGiRuntime { 27 28 private static String KF_BUNDLESTORAGE_PROP = "org.knopflerfish.framework.bundlestorage"; 29 private static String KF_BUNDLESTORAGE_PROP_DEFAULT = "memory"; 30 31 private static final String BUNDLE_PATH = "{0}-{1}.jar"; 33 34 private final URL [] bundleRepositories; 35 private final Framework framework; 36 37 static { 38 System.setProperty(Constants.FRAMEWORK_BOOTDELEGATION, "*"); 39 System.setProperty(KF_BUNDLESTORAGE_PROP, KF_BUNDLESTORAGE_PROP_DEFAULT); 40 49 } 50 51 54 public KnopflerfishOSGi(final URL [] bundleRepositories) throws Exception { 55 this.bundleRepositories = bundleRepositories; 56 System.setProperty("org.knopflerfish.osgi.registerserviceurlhandler", "false"); 57 framework = new Framework(null); 58 framework.launch(0); 59 } 60 61 public void installBundle(final String bundleName, final String bundleVersion) throws BundleException { 62 final URL bundleLocation = getBundleURL(bundleName, bundleVersion); 63 if (bundleLocation != null) { 64 try { 65 framework.installBundle(bundleLocation.toString(), bundleLocation.openStream()); 66 info(Message.BUNDLE_INSTALLED, new Object [] { getSymbolicName(bundleName, bundleVersion) }); 67 } catch (IOException ioe) { 68 throw new BundleException("Unable to open URL[" + bundleLocation + "]", ioe); 69 } 70 } else { 71 throw new BundleException("Unable to find bundle '" + bundleName + "', version '" + bundleVersion 72 + "' in any repository"); 73 } 74 } 75 76 public void startBundle(final String bundleName, final String bundleVersion) throws BundleException { 77 final long bundleID = getBundleID(bundleName, bundleVersion); 78 framework.startBundle(bundleID); 79 info(Message.BUNDLE_STARTED, new Object [] { getSymbolicName(bundleName, bundleVersion) }); 80 } 81 82 public Bundle getBundle(String bundleName, String bundleVersion) { 83 return framework.bundles.getBundle(getBundleID(bundleName, bundleVersion)); 84 } 85 86 public void registerService(final Object serviceObject, final Dictionary serviceProps) throws BundleException { 87 framework.getSystemBundleContext().registerService(serviceObject.getClass().getName(), serviceObject, serviceProps); 88 info(Message.SERVICE_REGISTERED, new Object [] { serviceObject.getClass().getName() }); 89 } 90 91 public ServiceReference[] getAllServiceReferences(String clazz, java.lang.String filter) 92 throws InvalidSyntaxException { 93 return framework.getSystemBundleContext().getAllServiceReferences(clazz, filter); 94 } 95 96 public Object getService(ServiceReference service) { 97 return framework.getSystemBundleContext().getService(service); 98 } 99 100 public void ungetService(ServiceReference service) { 101 framework.getSystemBundleContext().ungetService(service); 102 } 103 104 public void stopBundle(final String bundleName, final String bundleVersion) throws BundleException { 105 final long bundleID = getBundleID(bundleName, bundleVersion); 106 framework.stopBundle(bundleID); 107 info(Message.BUNDLE_STOPPED, new Object [] { getSymbolicName(bundleName, bundleVersion) }); 108 } 109 110 public void uninstallBundle(final String bundleName, final String bundleVersion) throws BundleException { 111 framework.uninstallBundle(getBundleID(bundleName, bundleVersion)); 112 info(Message.BUNDLE_UNINSTALLED, new Object [] { getSymbolicName(bundleName, bundleVersion) }); 113 } 114 115 public void shutdown() throws BundleException { 116 framework.shutdown(); 117 info(Message.SHUTDOWN, new Object [0]); 118 } 119 120 private URL getBundleURL(final String bundleName, final String bundleVersion) { 121 final String path = MessageFormat.format(BUNDLE_PATH, new String [] { bundleName, bundleVersion }); 122 try { 123 return URLUtil.resolve(bundleRepositories, path); 124 } catch (MalformedURLException murle) { 125 throw new RuntimeException ("Unable to resolve bundle '" + path 126 + "', please check that your repositories are correctly configured", murle); 127 } 128 } 129 130 private long getBundleID(final String bundleName, final String bundleVersion) { 131 final URL bundleURL = getBundleURL(bundleName, bundleVersion); 132 return framework.getBundleId(bundleURL.toString()); 133 } 134 135 private String getSymbolicName(final String bundleName, final String bundleVersion) { 136 final Bundle bundle = framework.getSystemBundleContext().getBundle(getBundleID(bundleName, bundleVersion)); 137 return bundle.getSymbolicName(); 138 } 139 140 } 141 | Popular Tags |