001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.bundles;
006:
007: import org.knopflerfish.framework.Framework;
008: import org.osgi.framework.Bundle;
009: import org.osgi.framework.BundleException;
010: import org.osgi.framework.Constants;
011: import org.osgi.framework.InvalidSyntaxException;
012: import org.osgi.framework.ServiceReference;
013:
014: import com.tc.logging.TCLogger;
015: import com.tc.logging.TCLogging;
016: import com.tc.util.Assert;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.URI;
021: import java.net.URISyntaxException;
022: import java.net.URL;
023: import java.util.Dictionary;
024:
025: /**
026: * Embedded KnopflerFish OSGi implementation, see the <a href="http://www.knopflerfish.org/">Knopflerfish documentation</a>
027: * for more details.
028: */
029: final class KnopflerfishOSGi extends AbstractEmbeddedOSGiRuntime {
030:
031: private static final TCLogger logger = TCLogging
032: .getLogger(KnopflerfishOSGi.class);
033:
034: private static final String KF_BUNDLESTORAGE_PROP = "org.knopflerfish.framework.bundlestorage";
035: private static final String KF_BUNDLESTORAGE_PROP_DEFAULT = "memory";
036:
037: private final URL[] repositories;
038: private final Framework framework;
039:
040: static {
041: System.setProperty(Constants.FRAMEWORK_BOOTDELEGATION, "*");
042: System.setProperty(KF_BUNDLESTORAGE_PROP,
043: KF_BUNDLESTORAGE_PROP_DEFAULT);
044: }
045:
046: KnopflerfishOSGi(final URL[] bundleRepositories) throws Exception {
047: this .repositories = bundleRepositories;
048: System.setProperty(
049: "org.knopflerfish.osgi.registerserviceurlhandler",
050: "false");
051: framework = new Framework(null);
052: framework.launch(0);
053: }
054:
055: public URL[] getRepositories() {
056: return this .repositories;
057: }
058:
059: public void installBundles(final URL[] locations)
060: throws BundleException {
061: for (int i = 0; i < locations.length; i++) {
062: installBundle(locations[i]);
063: }
064: }
065:
066: public void startBundles(final URL[] locations,
067: final EmbeddedOSGiEventHandler handler)
068: throws BundleException {
069: for (int i = 0; i < locations.length; i++) {
070: final long id = framework.getBundleId(locations[i]
071: .toString());
072: startBundle(id, handler);
073: }
074: }
075:
076: private void startBundle(final long id,
077: final EmbeddedOSGiEventHandler handler)
078: throws BundleException {
079: Assert.assertNotNull(handler);
080: final Bundle bundle = framework.bundles.getBundle(id);
081: final boolean isStarting = ((bundle.getState() & Bundle.STARTING) == Bundle.STARTING);
082: final boolean isActive = ((bundle.getState() & Bundle.ACTIVE) == Bundle.ACTIVE);
083:
084: if (isActive || isStarting) {
085: warn(Message.WARN_SKIPPED_ALREADY_ACTIVE,
086: new Object[] { bundle.getSymbolicName() });
087: return;
088: }
089:
090: if (logger.isDebugEnabled()) {
091: info(Message.STARTING_BUNDLE, new Object[] { bundle
092: .getSymbolicName() });
093: }
094: framework.startBundle(bundle.getBundleId());
095: Assert.assertEquals(bundle.getState() & Bundle.ACTIVE, bundle
096: .getState());
097: handler.callback(bundle);
098: }
099:
100: public void installBundle(final URL location)
101: throws BundleException {
102: try {
103: if (logger.isDebugEnabled()) {
104: info(Message.INSTALLING_BUNDLE,
105: new Object[] { location });
106: }
107: final long id = framework.installBundle(
108: location.toString(), location.openStream());
109: final Bundle bundle = framework.getSystemBundleContext()
110: .getBundle(id);
111: final String symname = bundle.getSymbolicName();
112: if (symname == null) {
113: framework.uninstallBundle(id);
114: final File bundleFile = new File(new URI(location
115: .toString()));
116: warn(Message.WARN_SKIPPED_FILE_INSTALLATION,
117: new Object[] { bundleFile.getName() });
118: } else if (logger.isDebugEnabled()) {
119: info(Message.BUNDLE_INSTALLED, new Object[] { symname });
120: }
121: } catch (URISyntaxException e) {
122: throw new RuntimeException(e);
123: } catch (IOException e) {
124: exception(Message.ERROR_BUNDLE_INACCESSIBLE,
125: new Object[] { location.toString() }, e);
126: }
127: }
128:
129: public void registerService(final Object serviceObject,
130: final Dictionary serviceProps) throws BundleException {
131: if (logger.isDebugEnabled()) {
132: info(Message.REGISTERING_SERVICE, new Object[] {
133: serviceObject.getClass().getName(), serviceProps });
134: }
135: framework.getSystemBundleContext().registerService(
136: serviceObject.getClass().getName(), serviceObject,
137: serviceProps);
138: if (logger.isDebugEnabled()) {
139: info(Message.SERVICE_REGISTERED,
140: new Object[] { serviceObject.getClass().getName() });
141: }
142: }
143:
144: public void registerService(final String serviceName,
145: final Object serviceObject, final Dictionary serviceProps)
146: throws BundleException {
147: if (logger.isDebugEnabled()) {
148: info(Message.REGISTERING_SERVICE, new Object[] {
149: serviceName, serviceProps });
150: }
151: framework.getSystemBundleContext().registerService(serviceName,
152: serviceObject, serviceProps);
153: if (logger.isDebugEnabled()) {
154: info(Message.SERVICE_REGISTERED,
155: new Object[] { serviceName });
156: }
157: }
158:
159: public ServiceReference[] getAllServiceReferences(String clazz,
160: java.lang.String filter) throws InvalidSyntaxException {
161: return framework.getSystemBundleContext()
162: .getAllServiceReferences(clazz, filter);
163: }
164:
165: public Object getService(ServiceReference service) {
166: return framework.getSystemBundleContext().getService(service);
167: }
168:
169: public void ungetService(ServiceReference service) {
170: framework.getSystemBundleContext().ungetService(service);
171: }
172:
173: public void shutdown() {
174: if (framework == null)
175: return;
176: if (logger.isDebugEnabled()) {
177: info(Message.STOPPING_FRAMEWORK, new Object[0]);
178: }
179: framework.shutdown();
180: info(Message.SHUTDOWN, new Object[0]);
181: }
182:
183: }
|