summaryrefslogtreecommitdiffstats
path: root/branches/sca-equinox/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/EquinoxLauncherBundleHelper.java
blob: 222ac73868f634044165baf89390473ec5eeb8a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.apache.tuscany.sca.node.equinox.launcher;

import static java.lang.System.currentTimeMillis;
import static java.lang.System.getProperty;
import static java.lang.System.setProperty;
import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.installBundle;
import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.libraryBundle;
import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.string;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;

/**
 * Bundle activator which installs Tuscany modules into an OSGi runtime.
 *
 */
public class EquinoxLauncherBundleHelper implements BundleListener {
    private static Logger logger = Logger.getLogger(EquinoxLauncherBundleHelper.class.getName());

    private List<Bundle> installedBundles = new ArrayList<Bundle>();
    private BundleContext bundleContext;

    public EquinoxLauncherBundleHelper() {
        super();
    }

    public void start(BundleContext bundleContext) throws Exception {
        this.bundleContext = bundleContext;
        this.bundleContext.addBundleListener(this);

        // Install the Tuscany bundles
        long start = currentTimeMillis();

        // FIXME: SDO bundles dont have the correct dependencies
        setProperty("commonj.sdo.impl.HelperProvider", "org.apache.tuscany.sdo.helper.HelperProviderImpl");

        // Get the list of JAR files to install
        String jarFilesProperty = getProperty("org.apache.tuscany.sca.node.launcher.equinox.jarFiles");
        String[] jarFiles = jarFilesProperty.split(";");
        
        // Create a single 'library' bundle for them
        logger.info("Generating third-party library bundle.");
        for (String jarFile: jarFiles) {
            logger.info("Adding third-party jar: " + jarFile);
        }
        long libraryStart = currentTimeMillis();
        InputStream library = libraryBundle(jarFiles);
        logger.info("Third-party library bundle generated in " + (currentTimeMillis() - libraryStart) + " ms.");
        libraryStart = currentTimeMillis();
        Bundle libraryBundle = bundleContext.installBundle("org.apache.tuscany.sca.node.launcher.equinox.libraries", library);
        logger.info("Third-party library bundle installed in " + (currentTimeMillis() - libraryStart) + " ms: " + string(libraryBundle, false));
        installedBundles.add(libraryBundle);
        
        // Get the set of already installed bundles
        Set<String> alreadyInstalledBundleNames = new HashSet<String>();
        for (Bundle bundle: bundleContext.getBundles()) {
            alreadyInstalledBundleNames.add(bundle.getSymbolicName());
        }

        // Get the list of bundle files and names to install
        String bundleFilesProperty = getProperty("org.apache.tuscany.sca.node.launcher.equinox.bundleFiles");
        String[] bundleFiles = bundleFilesProperty.split(";");
        String bundleNamesProperty = getProperty("org.apache.tuscany.sca.node.launcher.equinox.bundleNames");
        String[] bundleNames = bundleNamesProperty.split(";");
        
        // Install all the bundles that are not already installed
        for (int i =0, n = bundleFiles.length; i < n; i++) {
            String bundleFile = bundleFiles[i];
            String bundleName = bundleNames[i];
            if (!alreadyInstalledBundleNames.contains(bundleName)) {
                if (bundleName.contains("org.eclipse.jdt.junit")) {
                    continue;
                }
                long installStart = currentTimeMillis();
                Bundle bundle = installBundle(bundleContext, bundleFile);
                logger.info("Bundle installed in " + (currentTimeMillis() - installStart) + " ms: " + string(bundle, false));
                installedBundles.add(bundle);
                alreadyInstalledBundleNames.add(bundleName);
            }
        }

        long end = currentTimeMillis();
        logger.info("Tuscany bundles are installed in " + (end - start) + " ms.");
    }

    public void stop(BundleContext bundleContext) throws Exception {
        
        // Uninstall all the bundles we've installed
        for (int i = installedBundles.size() -1; i >= 0; i--) {
            Bundle bundle = installedBundles.get(i);
            try {
                //if (logger.isLoggable(Level.FINE)) {
                logger.info("Uninstalling bundle: " + string(bundle, false));
                //}
                bundle.uninstall();
            } catch (Exception e) {
                logger.log(Level.SEVERE, e.getMessage(), e);
            }
        }
        installedBundles.clear();

        this.bundleContext.removeBundleListener(this);
        this.bundleContext = null;
    }

    public void bundleChanged(BundleEvent event) {
    }

}