summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/1.6.1-TUSCANY-3909/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/KnopflerfishRuntime.java
blob: 19041daf505147278bbb4655930f60be90bbceb4 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.sca.osgi.runtime;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Hashtable;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

/**
 * Implementation of an OSGi Runtime using Knopflerfish.
 *
 * @version $Rev$ $Date$
 */
public class KnopflerfishRuntime extends OSGiRuntime  {
    
    private static BundleContext bundleContext;
    
    private static KnopflerfishRuntime instance;
    

    private static Class<?> frameworkClass;
    
    private static Object framework;
    
    public static OSGiRuntime getInstance() throws Exception {
        if (instance == null) {
            frameworkClass = KnopflerfishRuntime.class.getClassLoader().loadClass("org.knopflerfish.framework.Framework");
            KnopflerfishRuntime runtime = new KnopflerfishRuntime();
            instance = runtime;
        }
        return instance;
    }
    
   

    // FIXME: Knopflerfish does not expose the methods used for configuration as public methods
    //        It may be worth using the private methods in org.knopflerfish.framework.Main for
    //        configuring using reflection if security policies allow it.
    //        For now, a simple configuration routine reads sca.xargs from the directory in 
    //        the classpath which contains framework.jar. The entries in init.xargs starting with
    //        -install are assumed to be single-line entries with full bundle location.
    //
    @Override
    protected BundleContext startRuntime(boolean tuscanyRunningInOSGiContainer) throws Exception {
        
        if (bundleContext != null)
            return bundleContext;
        
        
        
        System.setProperty("org.knopflerfish.framework.bundlestorage", "memory");
                    
        Constructor frameworkConstructor = frameworkClass.getConstructor(Object.class);
        framework = frameworkConstructor.newInstance(new KnopflerfishRuntime());
        Method launchMethod = frameworkClass.getMethod("launch", long.class);
        launchMethod.invoke(framework, 0);
        Method getContextMethod = frameworkClass.getMethod("getSystemBundleContext");
        bundleContext = (BundleContext)getContextMethod.invoke(framework);
       
        System.setProperty("com.gatespace.bundle.cm.store", "knopflerfish.store");
        File xargsFile = null;
        String classpath = System.getProperty("java.class.path");
        String[] classpathEntries = classpath.split(System.getProperty("path.separator"));
        for (int i = 0; i < classpathEntries.length; i++) {
            if (classpathEntries[i].endsWith("framework.jar")) {
                String path = classpathEntries[i].substring(0, classpathEntries[i].length() - "framework.jar".length());
                path = path + "sca.xargs";
                xargsFile = new File(path);
                if (!xargsFile.exists())
                    xargsFile = null;
                break;
            }
        }
        if (xargsFile != null) {
            BufferedReader reader = new BufferedReader(new FileReader(xargsFile));
            String line;
            Hashtable<String, Bundle> bundles = new Hashtable<String, Bundle>();
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.startsWith("-install")) {
                    try {

                        String bundleLocation = line.substring("-install".length()).trim();
                        Bundle bundle = bundleContext.installBundle(bundleLocation);
                        bundles.put(bundleLocation, bundle);
                        
                    } catch (BundleException e) {
                        e.printStackTrace();
                    }
                    
                }
                if (line.startsWith("-start")) {

                    try {
                        String bundleLocation = line.substring("-start".length()).trim();
                        Bundle bundle = bundles.get(bundleLocation);
                        bundle.start();
                    } catch (BundleException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        return bundleContext;
        
    }
    
    @Override
    public BundleContext getBundleContext() {
        return bundleContext;
    }
    
    @Override
    protected void setBundleContext(BundleContext bundleContext) {
        super.setBundleContext(bundleContext);
        KnopflerfishRuntime.bundleContext = bundleContext;
    }

    @Override
    public void shutdown() throws Exception {

        if (bundleContext == null)
            return;
        bundleContext = null;
        instance = null;
        if (framework != null) {
            Method shutdownMethod = frameworkClass.getMethod("shutdown");
            try {
                shutdownMethod.invoke(framework);
            } catch (Exception e) {
                // Ignore errors
            }
            framework = null;
        }
        super.shutdown();
        
    }
    
    @Override
    public boolean supportsBundleFragments() {
        return true;
    }
}