KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Dictionary JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * For OSGi information please refer to the documentation at the <a HREF="http://www.osgi.org/">OSGi web page</a>
26  */

27 public interface EmbeddedOSGiRuntime {
28
29   public static final String JavaDoc MODULES_URL_PROPERTY_NAME = "tc.tests.configuration.modules.url";
30
31   void installBundle(final String JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException;
32
33   void startBundle(final String JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException;
34
35   Bundle getBundle(final String JavaDoc bundleName, final String JavaDoc bundleVersion);
36
37   void registerService(final Object JavaDoc serviceObject, final Dictionary JavaDoc serviceProps) throws BundleException;
38
39   void stopBundle(final String JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException;
40
41   void uninstallBundle(final String JavaDoc bundleName, final String JavaDoc bundleVersion) throws BundleException;
42
43   ServiceReference[] getAllServiceReferences(final String JavaDoc clazz, final String JavaDoc filter) throws InvalidSyntaxException;
44
45   Object JavaDoc getService(final ServiceReference service);
46
47   void ungetService(final ServiceReference service);
48
49   /**
50    * This should shut down the OSGi framework itself and all running bundles.
51    */

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 JavaDoc {
59       final List JavaDoc prependLocations = new ArrayList JavaDoc();
60       // There are two repositories that we [optionally] prepend: a system property (used by tests) and the installation
61
// root (which is not set when running tests)
62
try {
63         if (Directories.getInstallationRoot() != null) {
64           logger.debug("Prepending bundle repository: " + new File JavaDoc(Directories.getInstallationRoot(), "modules").toURL().toString());
65           prependLocations.add(new File JavaDoc(Directories.getInstallationRoot(), "modules").toURL());
66         }
67       } catch (FileNotFoundException JavaDoc fnfe) {
68         // Ignore, tc.install-dir is not set so we must be in a test environment
69
}
70       if (System.getProperty(MODULES_URL_PROPERTY_NAME) != null) {
71         prependLocations.add(new URL JavaDoc(System.getProperty(MODULES_URL_PROPERTY_NAME)));
72       }
73       final URL JavaDoc[] prependURLs = new URL JavaDoc[prependLocations.size()];
74       prependLocations.toArray(prependURLs);
75
76       final URL JavaDoc[] bundleRepositories = new URL JavaDoc[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 JavaDoc(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