KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > bundles > KnopflerfishOSGi


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.text.MessageFormat JavaDoc;
20 import java.util.Dictionary JavaDoc;
21
22 /**
23  * Embedded KnopflerFish OSGi implementation, see the <a HREF="http://www.knopflerfish.org/">Knopflerfish documentation</a>
24  * for more details.
25  */

26 final class KnopflerfishOSGi extends AbstractEmbeddedOSGiRuntime {
27
28   private static String JavaDoc KF_BUNDLESTORAGE_PROP = "org.knopflerfish.framework.bundlestorage";
29   private static String JavaDoc KF_BUNDLESTORAGE_PROP_DEFAULT = "memory";
30
31   // {0} := bundle name, {1} := bundle version (not necessarily numeric)
32
private static final String JavaDoc BUNDLE_PATH = "{0}-{1}.jar";
33
34   private final URL JavaDoc[] 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 /*
41     try {
42       framework = new Framework(null);
43       framework.launch(0);
44     }
45     catch (Exception e) {
46       throw new RuntimeException("Error initializing OSGi framework", e);
47     }
48 */

49   }
50
51   /**
52    * Creates and starts an in-memory OSGi layer using Knopflerfish.
53    */

54   public KnopflerfishOSGi(final URL JavaDoc[] bundleRepositories) throws Exception JavaDoc {
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 JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException {
62     final URL JavaDoc bundleLocation = getBundleURL(bundleName, bundleVersion);
63     if (bundleLocation != null) {
64       try {
65         framework.installBundle(bundleLocation.toString(), bundleLocation.openStream());
66         info(Message.BUNDLE_INSTALLED, new Object JavaDoc[] { getSymbolicName(bundleName, bundleVersion) });
67       } catch (IOException JavaDoc 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 JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException {
77     final long bundleID = getBundleID(bundleName, bundleVersion);
78     framework.startBundle(bundleID);
79     info(Message.BUNDLE_STARTED, new Object JavaDoc[] { getSymbolicName(bundleName, bundleVersion) });
80   }
81
82   public Bundle getBundle(String JavaDoc bundleName, String JavaDoc bundleVersion) {
83     return framework.bundles.getBundle(getBundleID(bundleName, bundleVersion));
84   }
85
86   public void registerService(final Object JavaDoc serviceObject, final Dictionary JavaDoc serviceProps) throws BundleException {
87     framework.getSystemBundleContext().registerService(serviceObject.getClass().getName(), serviceObject, serviceProps);
88     info(Message.SERVICE_REGISTERED, new Object JavaDoc[] { serviceObject.getClass().getName() });
89   }
90
91   public ServiceReference[] getAllServiceReferences(String JavaDoc clazz, java.lang.String JavaDoc filter)
92       throws InvalidSyntaxException {
93     return framework.getSystemBundleContext().getAllServiceReferences(clazz, filter);
94   }
95   
96   public Object JavaDoc 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 JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException {
105     final long bundleID = getBundleID(bundleName, bundleVersion);
106     framework.stopBundle(bundleID);
107     info(Message.BUNDLE_STOPPED, new Object JavaDoc[] { getSymbolicName(bundleName, bundleVersion) });
108   }
109
110   public void uninstallBundle(final String JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException {
111     framework.uninstallBundle(getBundleID(bundleName, bundleVersion));
112     info(Message.BUNDLE_UNINSTALLED, new Object JavaDoc[] { getSymbolicName(bundleName, bundleVersion) });
113   }
114
115   public void shutdown() throws BundleException {
116     framework.shutdown();
117     info(Message.SHUTDOWN, new Object JavaDoc[0]);
118   }
119
120   private URL JavaDoc getBundleURL(final String JavaDoc bundleName, final String JavaDoc bundleVersion) {
121     final String JavaDoc path = MessageFormat.format(BUNDLE_PATH, new String JavaDoc[] { bundleName, bundleVersion });
122     try {
123       return URLUtil.resolve(bundleRepositories, path);
124     } catch (MalformedURLException JavaDoc murle) {
125       throw new RuntimeException JavaDoc("Unable to resolve bundle '" + path
126           + "', please check that your repositories are correctly configured", murle);
127     }
128   }
129
130   private long getBundleID(final String JavaDoc bundleName, final String JavaDoc bundleVersion) {
131     final URL JavaDoc bundleURL = getBundleURL(bundleName, bundleVersion);
132     return framework.getBundleId(bundleURL.toString());
133   }
134
135   private String JavaDoc getSymbolicName(final String JavaDoc bundleName, final String JavaDoc bundleVersion) {
136     final Bundle bundle = framework.getSystemBundleContext().getBundle(getBundleID(bundleName, bundleVersion));
137     return bundle.getSymbolicName();
138   }
139
140 }
141
Popular Tags