You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/09/18 01:26:48 UTC
svn commit: r576625 - in /incubator/tuscany/branches/sca-java-1.0/modules:
./ binding-osgi/
contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/
contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/
im...
Author: rfeng
Date: Mon Sep 17 16:26:47 2007
New Revision: 576625
URL: http://svn.apache.org/viewvc?rev=576625&view=rev
Log:
Properly start and stop the OSGi runtime to avoid hang
Added:
incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java (with props)
incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/
incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/
incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
Removed:
incubator/tuscany/branches/sca-java-1.0/modules/binding-osgi/
Modified:
incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java
incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java
incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java
incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java
incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java
incubator/tuscany/branches/sca-java-1.0/modules/pom.xml
Modified: incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java?rev=576625&r1=576624&r2=576625&view=diff
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java (original)
+++ incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java Mon Sep 17 16:26:47 2007
@@ -40,18 +40,20 @@
private Contribution contribution;
protected WeakReference<ClassLoader> classLoader;
private Map<String, ClassReference> map = new HashMap<String, ClassReference>();
-
+
private ModelResolver osgiResolver;
-
+
public ClassReferenceModelResolver(Contribution contribution, ModelFactoryExtensionPoint modelFactories) {
this.contribution = contribution;
//FIXME The classloader should be passed in
this.classLoader = new WeakReference<ClassLoader>(Thread.currentThread().getContextClassLoader());
-
+
try {
- Class osgiResolverClass = Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiClassReferenceModelResolver");
+ Class osgiResolverClass =
+ Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiClassReferenceModelResolver");
if (osgiResolverClass != null) {
- Constructor constructor = osgiResolverClass.getConstructor(Contribution.class, ModelFactoryExtensionPoint.class);
+ Constructor constructor =
+ osgiResolverClass.getConstructor(Contribution.class, ModelFactoryExtensionPoint.class);
this.osgiResolver = (ModelResolver)constructor.newInstance(contribution, modelFactories);
}
} catch (Exception e) {
@@ -62,11 +64,11 @@
ClassReference clazz = (ClassReference)resolved;
map.put(clazz.getClassName(), clazz);
}
-
+
public Object removeModel(Object resolved) {
return map.remove(((ClassReference)resolved).getClassName());
}
-
+
/**
* Handle artifact resolution when the specific class reference is imported from another contribution
* @param unresolved
@@ -75,10 +77,10 @@
private ClassReference resolveImportedModel(ClassReference unresolved) {
ClassReference resolved = unresolved;
- if( this.contribution != null) {
+ if (this.contribution != null) {
for (Import import_ : this.contribution.getImports()) {
if (import_ instanceof JavaImport) {
- JavaImport javaImport = (JavaImport) import_;
+ JavaImport javaImport = (JavaImport)import_;
String packageName = javaImport.getPackage();
if (javaImport.getPackage().equals(packageName)) {
// Delegate the resolution to the import resolver
@@ -86,59 +88,58 @@
}
}
}
-
+
}
return resolved;
}
-
-
+
public <T> T resolveModel(Class<T> modelClass, T unresolved) {
Object resolved = map.get(unresolved);
-
- if (resolved != null ){
+
+ if (resolved != null) {
return modelClass.cast(resolved);
- }
-
+ }
+
//Load a class on demand
Class clazz = null;
-
- if (osgiResolver != null)
- resolved = osgiResolver.resolveModel(modelClass, unresolved);
-
+
if (unresolved == resolved || resolved == null) {
try {
clazz = Class.forName(((ClassReference)unresolved).getClassName(), true, classLoader.get());
} catch (ClassNotFoundException e) {
//we will later try to delegate to imported model resolvers
}
- }
- else
+ } else
clazz = ((ClassReference)resolved).getJavaClass();
-
if (clazz != null) {
//if we load the class
// Store a new ClassReference wrappering the loaded class
ClassReference classReference = new ClassReference(clazz);
map.put(getPackageName(classReference), classReference);
-
+
// Return the resolved ClassReference
- return modelClass.cast(classReference);
+ return modelClass.cast(classReference);
} else {
//delegate resolution of the class
resolved = this.resolveImportedModel((ClassReference)unresolved);
+ if (unresolved == resolved || resolved == null) {
+ if (osgiResolver != null) {
+ resolved = osgiResolver.resolveModel(modelClass, unresolved);
+ }
+ }
+
return modelClass.cast(resolved);
}
-
}
-
+
/***************
* Helper methods
***************/
-
+
private String getPackageName(ClassReference clazz) {
int pos = clazz.getClassName().lastIndexOf(".");
- return clazz.getClassName().substring(0, pos - 1 );
+ return clazz.getClassName().substring(0, pos - 1);
}
}
Modified: incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java?rev=576625&r1=576624&r2=576625&view=diff
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java (original)
+++ incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java Mon Sep 17 16:26:47 2007
@@ -45,9 +45,9 @@
* @version $Rev$ $Date$
*/
public class OSGiImportExportListener implements ContributionListener {
-
+
private OSGiBundleProcessor bundleProcessor;
-
+
public OSGiImportExportListener() {
bundleProcessor = new OSGiBundleProcessor();
}
@@ -57,58 +57,56 @@
* Export model resolvers are same as Contribution model resolver
* Import model resolvers are matched to a specific contribution if a location uri is specified,
* otherwise it try to resolve agains all the other contributions
- */
+ */
public void contributionAdded(ContributionRepository repository, Contribution contribution) {
-
- OSGiRuntime osgiRuntime;
-
+
+ OSGiRuntime osgiRuntime = null;
try {
- osgiRuntime = OSGiRuntime.getRuntime();
-
- if (bundleProcessor.installContributionBundle(contribution) == null)
+ if (bundleProcessor.installContributionBundle(contribution) == null) {
return;
+ } else {
+ osgiRuntime = OSGiRuntime.getRuntime();
+ }
} catch (Exception e) {
return;
}
-
HashSet<Contribution> bundlesToInstall = new HashSet<Contribution>();
// Initialize the contribution imports
- for (Import import_: contribution.getImports()) {
+ for (Import import_ : contribution.getImports()) {
boolean initialized = false;
-
-
- if(import_ instanceof JavaImport) {
- JavaImport javaImport = (JavaImport) import_;
+
+ if (import_ instanceof JavaImport) {
+ JavaImport javaImport = (JavaImport)import_;
String packageName = javaImport.getPackage();
-
+
//Find a matching contribution
- if(javaImport.getLocation() != null) {
+ if (javaImport.getLocation() != null) {
Contribution targetContribution = repository.getContribution(javaImport.getLocation());
if (targetContribution != null) {
-
+
// Find a matching contribution export
- for (Export export: targetContribution.getExports()) {
+ for (Export export : targetContribution.getExports()) {
if (export instanceof JavaExport) {
JavaExport javaExport = (JavaExport)export;
if (packageName.equals(javaExport.getPackage())) {
-
+
if (osgiRuntime.findBundle(targetContribution.getLocation()) == null)
bundlesToInstall.add(targetContribution);
-
+
initialized = true;
-
+
}
}
if (initialized)
break;
}
- }
+ }
}
}
if (!initialized) {
for (Contribution c : repository.getContributions()) {
-
+
// Go over all exports in the contribution
for (Export export : c.getExports()) {
// If the export matches our namespace, try to the resolve the model object
@@ -121,7 +119,7 @@
}
for (Contribution c : bundlesToInstall) {
try {
- installDummyBundle(osgiRuntime, c);
+ installDummyBundle(osgiRuntime, c);
} catch (Exception e) {
}
}
@@ -132,10 +130,12 @@
}
- public void contributionUpdated(ContributionRepository repository, Contribution oldContribution, Contribution contribution) {
+ public void contributionUpdated(ContributionRepository repository,
+ Contribution oldContribution,
+ Contribution contribution) {
}
-
+
private void installDummyBundle(OSGiRuntime osgiRuntime, Contribution contribution) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -143,38 +143,47 @@
String bundleName = contribution.getURI();
String uri = contribution.getURI();
-
+
StringBuffer exportPackageNames = new StringBuffer();
for (Export export : contribution.getExports()) {
if (export instanceof JavaExport) {
- if (exportPackageNames.length() > 0) exportPackageNames.append(",");
+ if (exportPackageNames.length() > 0)
+ exportPackageNames.append(",");
exportPackageNames.append(((JavaExport)export).getPackage());
}
}
StringBuffer importPackageNames = new StringBuffer();
for (Import import_ : contribution.getImports()) {
if (import_ instanceof JavaImport) {
- if (importPackageNames.length() > 0) importPackageNames.append(",");
+ if (importPackageNames.length() > 0)
+ importPackageNames.append(",");
importPackageNames.append(((JavaImport)import_).getPackage());
}
}
- String manifestStr = "Manifest-Version: 1.0" + EOL
- + "Bundle-ManifestVersion: 2" + EOL + "Bundle-Name: "
- + bundleName + EOL + "Bundle-SymbolicName: " + bundleName + EOL
- + "Bundle-Version: " + "1.0.0" + EOL
- + "Bundle-Localization: plugin" + EOL;
-
+ String manifestStr =
+ "Manifest-Version: 1.0" + EOL
+ + "Bundle-ManifestVersion: 2"
+ + EOL
+ + "Bundle-Name: "
+ + bundleName
+ + EOL
+ + "Bundle-SymbolicName: "
+ + bundleName
+ + EOL
+ + "Bundle-Version: "
+ + "1.0.0"
+ + EOL
+ + "Bundle-Localization: plugin"
+ + EOL;
StringBuilder manifestBuf = new StringBuilder();
manifestBuf.append(manifestStr);
- manifestBuf.append("Export-Package: " + exportPackageNames + EOL);
- manifestBuf.append("Import-Package: " + importPackageNames + EOL);
+ manifestBuf.append("Export-Package: " + exportPackageNames + EOL);
+ manifestBuf.append("Import-Package: " + importPackageNames + EOL);
manifestBuf.append("Bundle-ClassPath: .," + uri + EOL);
-
- ByteArrayInputStream manifestStream = new ByteArrayInputStream(
- manifestBuf.toString().getBytes());
+ ByteArrayInputStream manifestStream = new ByteArrayInputStream(manifestBuf.toString().getBytes());
Manifest manifest = new Manifest();
manifest.read(manifestStream);
@@ -184,15 +193,14 @@
jarOut.putNextEntry(ze);
URL url = new URL(contribution.getLocation());
InputStream stream = url.openStream();
-
+
byte[] bytes = new byte[stream.available()];
stream.read(bytes);
jarOut.write(bytes);
stream.close();
-
+
jarOut.close();
out.close();
-
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Modified: incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java?rev=576625&r1=576624&r2=576625&view=diff
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java (original)
+++ incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java Mon Sep 17 16:26:47 2007
@@ -51,7 +51,7 @@
@Override
protected void tearDown() throws Exception {
- OSGiRuntime.getRuntime().shutdown();
+ OSGiRuntime.stop();
}
public void testOSGiComponent() throws Exception {
Modified: incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java?rev=576625&r1=576624&r2=576625&view=diff
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java (original)
+++ incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java Mon Sep 17 16:26:47 2007
@@ -18,28 +18,30 @@
*/
package org.apache.tuscany.sca.osgi.runtime;
-
import java.io.InputStream;
import java.lang.reflect.Method;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.service.packageadmin.PackageAdmin;
-public abstract class OSGiRuntime {
-
+public abstract class OSGiRuntime {
+ private static final Logger logger = Logger.getLogger(OSGiRuntime.class.getName());
private BundleContext bundleContext;
-
+
public abstract BundleContext getBundleContext();
-
+
public abstract void shutdown() throws Exception;
-
+
public abstract boolean supportsBundleFragments();
-
+
private PackageAdmin packageAdmin;
-
-
+
+ private static OSGiRuntime instance;
+
/**
* System property org.apache.tuscany.implementation.osgi.runtime.OSGiRuntime can be set to the
* name of the OSGiRuntime class (eg. EquinoxRuntime). If set, start this runtime and return the
@@ -48,105 +50,106 @@
*
* @throws BundleException
*/
- public static OSGiRuntime getRuntime() throws Exception {
-
+ public synchronized static OSGiRuntime getRuntime() throws Exception {
+ if (instance != null) {
+ return instance;
+ }
String runtimeClassName = System.getProperty(OSGiRuntime.class.getName());
if (runtimeClassName != null) {
try {
Class<?> runtimeClass = OSGiRuntime.class.getClassLoader().loadClass(runtimeClassName);
Method method = runtimeClass.getMethod("getInstance");
- return (OSGiRuntime) method.invoke(null);
-
+ instance = (OSGiRuntime)method.invoke(null);
+ return instance;
+
} catch (Exception e) {
throw new BundleException("Could not start OSGi runtime " + runtimeClassName, e);
}
}
-
+
try {
-
- return EquinoxRuntime.getInstance();
-
+ instance = EquinoxRuntime.getInstance();
+ return instance;
+
} catch (ClassNotFoundException e) {
- } catch (Throwable e) {
- e.printStackTrace();
- }
-
+ // Ignore
+ } catch (Throwable e) {
+ logger.log(Level.SEVERE, e.getMessage(), e);
+ }
+
try {
-
- return FelixRuntime.getInstance();
-
+ instance = FelixRuntime.getInstance();
+ return instance;
} catch (ClassNotFoundException e) {
- } catch (Throwable e) {
- e.printStackTrace();
- }
-
+ // Ignore
+ } catch (Throwable e) {
+ logger.log(Level.SEVERE, e.getMessage(), e);
+ }
+
try {
-
- return KnopflerfishRuntime.getInstance();
-
+ instance = KnopflerfishRuntime.getInstance();
+ return instance;
} catch (ClassNotFoundException e) {
- } catch (Throwable e) {
- e.printStackTrace();
- }
-
+ // Ignore
+ } catch (Throwable e) {
+ logger.log(Level.SEVERE, e.getMessage(), e);
+ }
+
throw new BundleException("Could not start OSGi runtime from the classpath");
}
-
+
private void initialize() {
- if (bundleContext == null)
+ if (bundleContext == null)
bundleContext = getBundleContext();
-
- if (bundleContext != null) {
-
- org.osgi.framework.ServiceReference packageAdminReference =
+
+ if (bundleContext != null) {
+
+ org.osgi.framework.ServiceReference packageAdminReference =
bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin");
if (packageAdminReference != null) {
-
- packageAdmin = (PackageAdmin) bundleContext.getService(packageAdminReference);
+
+ packageAdmin = (PackageAdmin)bundleContext.getService(packageAdminReference);
}
- }
-
+ }
+
}
-
public Bundle findBundle(String bundleSymbolicName, String bundleVersion) {
-
- initialize();
-
+
+ initialize();
+
if (bundleContext != null) {
Bundle[] installedBundles = bundleContext.getBundles();
for (Bundle bundle : installedBundles) {
- if (bundleSymbolicName.equals(bundle.getSymbolicName()) &&
- (bundleVersion == null ||
- bundleVersion.equals(bundle.getHeaders().get("Bundle-Version"))))
- return bundle;
+ if (bundleSymbolicName.equals(bundle.getSymbolicName()) && (bundleVersion == null || bundleVersion
+ .equals(bundle.getHeaders().get("Bundle-Version"))))
+ return bundle;
}
-
+
}
return null;
}
-
public Bundle findBundle(String bundleLocation) {
-
+
initialize();
-
+
if (bundleContext != null) {
Bundle[] installedBundles = bundleContext.getBundles();
for (Bundle bundle : installedBundles) {
if (bundle.getLocation().equals(bundleLocation))
- return bundle;
+ return bundle;
}
-
+
}
return null;
}
-
+
public Bundle installBundle(String bundleLocation, InputStream inputStream) {
-
- initialize();
-
+
+ initialize();
+
try {
if (bundleContext != null) {
Bundle bundle = findBundle(bundleLocation);
@@ -156,15 +159,25 @@
bundle = bundleContext.installBundle(bundleLocation);
else
bundle = bundleContext.installBundle(bundleLocation, inputStream);
-
+
if (bundle != null && packageAdmin != null)
- packageAdmin.refreshPackages(null);
-
+ packageAdmin.refreshPackages(null);
+
return bundle;
}
} catch (BundleException e) {
}
return null;
+ }
+
+ /**
+ * @return the instance
+ */
+ public synchronized static void stop() throws Exception {
+ if (instance != null) {
+ instance.shutdown();
+ instance = null;
+ }
}
}
Added: incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java?rev=576625&view=auto
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java (added)
+++ incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java Mon Sep 17 16:26:47 2007
@@ -0,0 +1,47 @@
+/*
+ * 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 org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ModuleActivator;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class OSGiRuntimeModuleActivator implements ModuleActivator {
+
+ /**
+ * @see org.apache.tuscany.sca.core.ModuleActivator#start(org.apache.tuscany.sca.core.ExtensionPointRegistry)
+ */
+ public void start(ExtensionPointRegistry registry) {
+ }
+
+ /**
+ * @see org.apache.tuscany.sca.core.ModuleActivator#stop(org.apache.tuscany.sca.core.ExtensionPointRegistry)
+ */
+ public void stop(ExtensionPointRegistry registry) {
+ try {
+ OSGiRuntime.stop();
+ } catch (Exception e) {
+ // Ignore the exception
+ }
+ }
+
+}
Propchange: incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator?rev=576625&view=auto
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator (added)
+++ incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator Mon Sep 17 16:26:47 2007
@@ -0,0 +1,36 @@
+# 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.
+# Implementation class for the ModuleActivator
+org.apache.tuscany.sca.osgi.runtime.OSGiRuntimeModuleActivator
+# 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.
+# Implementation class for the ModuleActivator
+org.apache.tuscany.sca.osgi.runtime.OSGiRuntimeModuleActivator
Modified: incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java?rev=576625&r1=576624&r2=576625&view=diff
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java (original)
+++ incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java Mon Sep 17 16:26:47 2007
@@ -19,40 +19,54 @@
package org.apache.tuscany.sca.osgi.runtime;
-import org.apache.tuscany.sca.osgi.runtime.OSGiRuntime;
-import org.osgi.framework.BundleContext;
-
import junit.framework.TestCase;
+import org.osgi.framework.BundleContext;
+
/**
* Test OSGi runtime.
*
*/
public class OSGiRuntimeTestCase extends TestCase {
-
+ private OSGiRuntime runtime;
+
+ /**
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ this.runtime = OSGiRuntime.getRuntime();
+ }
+
+ /**
+ * @see junit.framework.TestCase#tearDown()
+ */
+ @Override
+ protected void tearDown() throws Exception {
+ OSGiRuntime.stop();
+ }
+
public void testRuntime() throws Exception {
-
- BundleContext bc1 = OSGiRuntime.getRuntime().getBundleContext();
-
+
+ BundleContext bc1 = runtime.getBundleContext();
+
assertNotNull(bc1);
-
- BundleContext bc2 = OSGiRuntime.getRuntime().getBundleContext();
-
+
+ BundleContext bc2 = runtime.getBundleContext();
+
assertNotNull(bc2);
-
+
assertTrue(bc1 == bc2);
-
- OSGiRuntime.getRuntime().shutdown();
-
- BundleContext bc3 = OSGiRuntime.getRuntime().getBundleContext();
-
+
+ OSGiRuntime.stop();
+ runtime = OSGiRuntime.getRuntime();
+
+ BundleContext bc3 = runtime.getBundleContext();
+
assertNotNull(bc3);
-
+
assertTrue(bc1 != bc3);
-
-
-
+
}
-
}
Modified: incubator/tuscany/branches/sca-java-1.0/modules/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/pom.xml?rev=576625&r1=576624&r2=576625&view=diff
==============================================================================
--- incubator/tuscany/branches/sca-java-1.0/modules/pom.xml (original)
+++ incubator/tuscany/branches/sca-java-1.0/modules/pom.xml Mon Sep 17 16:26:47 2007
@@ -49,7 +49,6 @@
<module>binding-rmi</module>
<module>binding-sca</module>
<module>binding-sca-axis2</module>
- <module>binding-osgi</module>
<!--
<module>binding-sca-jms</module>
-->
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org
Re: svn commit: r576625 - in /incubator/tuscany/branches/sca-java-1.0/modules: ./ binding-osgi/ contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/ im
Posted by Raymond Feng <en...@gmail.com>.
Sorry, I pressed the button too quickly :-(. A few test cases are failing
and I'll fix them.
Thanks,
Raymond
----- Original Message -----
From: <rf...@apache.org>
To: <tu...@ws.apache.org>
Sent: Monday, September 17, 2007 4:26 PM
Subject: svn commit: r576625 - in
/incubator/tuscany/branches/sca-java-1.0/modules: ./ binding-osgi/
contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/
contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/
im...
> Author: rfeng
> Date: Mon Sep 17 16:26:47 2007
> New Revision: 576625
>
> URL: http://svn.apache.org/viewvc?rev=576625&view=rev
> Log:
> Properly start and stop the OSGi runtime to avoid hang
>
> Added:
>
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
> (with props)
>
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/
>
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/
>
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
> Removed:
> incubator/tuscany/branches/sca-java-1.0/modules/binding-osgi/
> Modified:
>
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java
>
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java
>
> incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java
>
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java
>
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java
> incubator/tuscany/branches/sca-java-1.0/modules/pom.xml
>
> Modified:
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java?rev=576625&r1=576624&r2=576625&view=diff
> ==============================================================================
> ---
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java
> (original)
> +++
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java
> Mon Sep 17 16:26:47 2007
> @@ -40,18 +40,20 @@
> private Contribution contribution;
> protected WeakReference<ClassLoader> classLoader;
> private Map<String, ClassReference> map = new HashMap<String,
> ClassReference>();
> -
> +
> private ModelResolver osgiResolver;
> -
> +
> public ClassReferenceModelResolver(Contribution contribution,
> ModelFactoryExtensionPoint modelFactories) {
> this.contribution = contribution;
> //FIXME The classloader should be passed in
> this.classLoader = new
> WeakReference<ClassLoader>(Thread.currentThread().getContextClassLoader());
> -
> +
> try {
> - Class osgiResolverClass =
> Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiClassReferenceModelResolver");
> + Class osgiResolverClass =
> +
> Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiClassReferenceModelResolver");
> if (osgiResolverClass != null) {
> - Constructor constructor =
> osgiResolverClass.getConstructor(Contribution.class,
> ModelFactoryExtensionPoint.class);
> + Constructor constructor =
> + osgiResolverClass.getConstructor(Contribution.class,
> ModelFactoryExtensionPoint.class);
> this.osgiResolver =
> (ModelResolver)constructor.newInstance(contribution, modelFactories);
> }
> } catch (Exception e) {
> @@ -62,11 +64,11 @@
> ClassReference clazz = (ClassReference)resolved;
> map.put(clazz.getClassName(), clazz);
> }
> -
> +
> public Object removeModel(Object resolved) {
> return map.remove(((ClassReference)resolved).getClassName());
> }
> -
> +
> /**
> * Handle artifact resolution when the specific class reference is
> imported from another contribution
> * @param unresolved
> @@ -75,10 +77,10 @@
> private ClassReference resolveImportedModel(ClassReference unresolved)
> {
> ClassReference resolved = unresolved;
>
> - if( this.contribution != null) {
> + if (this.contribution != null) {
> for (Import import_ : this.contribution.getImports()) {
> if (import_ instanceof JavaImport) {
> - JavaImport javaImport = (JavaImport) import_;
> + JavaImport javaImport = (JavaImport)import_;
> String packageName = javaImport.getPackage();
> if (javaImport.getPackage().equals(packageName)) {
> // Delegate the resolution to the import resolver
> @@ -86,59 +88,58 @@
> }
> }
> }
> -
> +
> }
> return resolved;
> }
> -
> -
> +
> public <T> T resolveModel(Class<T> modelClass, T unresolved) {
> Object resolved = map.get(unresolved);
> -
> - if (resolved != null ){
> +
> + if (resolved != null) {
> return modelClass.cast(resolved);
> - }
> -
> + }
> +
> //Load a class on demand
> Class clazz = null;
> -
> - if (osgiResolver != null)
> - resolved = osgiResolver.resolveModel(modelClass, unresolved);
> -
> +
> if (unresolved == resolved || resolved == null) {
> try {
> clazz =
> Class.forName(((ClassReference)unresolved).getClassName(), true,
> classLoader.get());
> } catch (ClassNotFoundException e) {
> //we will later try to delegate to imported model
> resolvers
> }
> - }
> - else
> + } else
> clazz = ((ClassReference)resolved).getJavaClass();
>
> -
> if (clazz != null) {
> //if we load the class
> // Store a new ClassReference wrappering the loaded class
> ClassReference classReference = new ClassReference(clazz);
> map.put(getPackageName(classReference), classReference);
> -
> +
> // Return the resolved ClassReference
> - return modelClass.cast(classReference);
> + return modelClass.cast(classReference);
> } else {
> //delegate resolution of the class
> resolved =
> this.resolveImportedModel((ClassReference)unresolved);
> + if (unresolved == resolved || resolved == null) {
> + if (osgiResolver != null) {
> + resolved = osgiResolver.resolveModel(modelClass,
> unresolved);
> + }
> + }
> +
> return modelClass.cast(resolved);
> }
> -
>
> }
> -
> +
> /***************
> * Helper methods
> ***************/
> -
> +
> private String getPackageName(ClassReference clazz) {
> int pos = clazz.getClassName().lastIndexOf(".");
> - return clazz.getClassName().substring(0, pos - 1 );
> + return clazz.getClassName().substring(0, pos - 1);
> }
> }
>
> Modified:
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java?rev=576625&r1=576624&r2=576625&view=diff
> ==============================================================================
> ---
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java
> (original)
> +++
> incubator/tuscany/branches/sca-java-1.0/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiImportExportListener.java
> Mon Sep 17 16:26:47 2007
> @@ -45,9 +45,9 @@
> * @version $Rev$ $Date$
> */
> public class OSGiImportExportListener implements ContributionListener {
> -
> +
> private OSGiBundleProcessor bundleProcessor;
> -
> +
> public OSGiImportExportListener() {
> bundleProcessor = new OSGiBundleProcessor();
> }
> @@ -57,58 +57,56 @@
> * Export model resolvers are same as Contribution model resolver
> * Import model resolvers are matched to a specific contribution if a
> location uri is specified,
> * otherwise it try to resolve agains all the other contributions
> - */
> + */
> public void contributionAdded(ContributionRepository repository,
> Contribution contribution) {
> -
> - OSGiRuntime osgiRuntime;
> -
> +
> + OSGiRuntime osgiRuntime = null;
> try {
> - osgiRuntime = OSGiRuntime.getRuntime();
> -
> - if (bundleProcessor.installContributionBundle(contribution)
> == null)
> + if (bundleProcessor.installContributionBundle(contribution)
> == null) {
> return;
> + } else {
> + osgiRuntime = OSGiRuntime.getRuntime();
> + }
> } catch (Exception e) {
> return;
> }
> -
>
> HashSet<Contribution> bundlesToInstall = new
> HashSet<Contribution>();
> // Initialize the contribution imports
> - for (Import import_: contribution.getImports()) {
> + for (Import import_ : contribution.getImports()) {
> boolean initialized = false;
> -
> -
> - if(import_ instanceof JavaImport) {
> - JavaImport javaImport = (JavaImport) import_;
> +
> + if (import_ instanceof JavaImport) {
> + JavaImport javaImport = (JavaImport)import_;
> String packageName = javaImport.getPackage();
> -
> +
> //Find a matching contribution
> - if(javaImport.getLocation() != null) {
> + if (javaImport.getLocation() != null) {
> Contribution targetContribution =
> repository.getContribution(javaImport.getLocation());
> if (targetContribution != null) {
> -
> +
> // Find a matching contribution export
> - for (Export export:
> targetContribution.getExports()) {
> + for (Export export :
> targetContribution.getExports()) {
> if (export instanceof JavaExport) {
> JavaExport javaExport =
> (JavaExport)export;
> if
> (packageName.equals(javaExport.getPackage())) {
> -
> +
> if
> (osgiRuntime.findBundle(targetContribution.getLocation()) == null)
>
> bundlesToInstall.add(targetContribution);
> -
> +
> initialized = true;
> -
> +
> }
> }
> if (initialized)
> break;
> }
> - }
> + }
> }
> }
> if (!initialized) {
> for (Contribution c : repository.getContributions()) {
> -
> +
> // Go over all exports in the contribution
> for (Export export : c.getExports()) {
> // If the export matches our namespace, try to the
> resolve the model object
> @@ -121,7 +119,7 @@
> }
> for (Contribution c : bundlesToInstall) {
> try {
> - installDummyBundle(osgiRuntime, c);
> + installDummyBundle(osgiRuntime, c);
> } catch (Exception e) {
> }
> }
> @@ -132,10 +130,12 @@
>
> }
>
> - public void contributionUpdated(ContributionRepository repository,
> Contribution oldContribution, Contribution contribution) {
> + public void contributionUpdated(ContributionRepository repository,
> + Contribution oldContribution,
> + Contribution contribution) {
>
> }
> -
> +
> private void installDummyBundle(OSGiRuntime osgiRuntime, Contribution
> contribution) throws Exception {
> ByteArrayOutputStream out = new ByteArrayOutputStream();
>
> @@ -143,38 +143,47 @@
>
> String bundleName = contribution.getURI();
> String uri = contribution.getURI();
> -
> +
> StringBuffer exportPackageNames = new StringBuffer();
> for (Export export : contribution.getExports()) {
> if (export instanceof JavaExport) {
> - if (exportPackageNames.length() > 0)
> exportPackageNames.append(",");
> + if (exportPackageNames.length() > 0)
> + exportPackageNames.append(",");
>
> exportPackageNames.append(((JavaExport)export).getPackage());
> }
> }
> StringBuffer importPackageNames = new StringBuffer();
> for (Import import_ : contribution.getImports()) {
> if (import_ instanceof JavaImport) {
> - if (importPackageNames.length() > 0)
> importPackageNames.append(",");
> + if (importPackageNames.length() > 0)
> + importPackageNames.append(",");
>
> importPackageNames.append(((JavaImport)import_).getPackage());
> }
> }
>
> - String manifestStr = "Manifest-Version: 1.0" + EOL
> - + "Bundle-ManifestVersion: 2" + EOL + "Bundle-Name: "
> - + bundleName + EOL + "Bundle-SymbolicName: " + bundleName
> + EOL
> - + "Bundle-Version: " + "1.0.0" + EOL
> - + "Bundle-Localization: plugin" + EOL;
> -
> + String manifestStr =
> + "Manifest-Version: 1.0" + EOL
> + + "Bundle-ManifestVersion: 2"
> + + EOL
> + + "Bundle-Name: "
> + + bundleName
> + + EOL
> + + "Bundle-SymbolicName: "
> + + bundleName
> + + EOL
> + + "Bundle-Version: "
> + + "1.0.0"
> + + EOL
> + + "Bundle-Localization: plugin"
> + + EOL;
>
> StringBuilder manifestBuf = new StringBuilder();
> manifestBuf.append(manifestStr);
> - manifestBuf.append("Export-Package: " + exportPackageNames +
> EOL);
> - manifestBuf.append("Import-Package: " + importPackageNames +
> EOL);
> + manifestBuf.append("Export-Package: " + exportPackageNames +
> EOL);
> + manifestBuf.append("Import-Package: " + importPackageNames +
> EOL);
> manifestBuf.append("Bundle-ClassPath: .," + uri + EOL);
> -
>
> - ByteArrayInputStream manifestStream = new ByteArrayInputStream(
> - manifestBuf.toString().getBytes());
> + ByteArrayInputStream manifestStream = new
> ByteArrayInputStream(manifestBuf.toString().getBytes());
> Manifest manifest = new Manifest();
> manifest.read(manifestStream);
>
> @@ -184,15 +193,14 @@
> jarOut.putNextEntry(ze);
> URL url = new URL(contribution.getLocation());
> InputStream stream = url.openStream();
> -
> +
> byte[] bytes = new byte[stream.available()];
> stream.read(bytes);
> jarOut.write(bytes);
> stream.close();
> -
> +
> jarOut.close();
> out.close();
> -
>
> ByteArrayInputStream in = new
> ByteArrayInputStream(out.toByteArray());
>
>
> Modified:
> incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java?rev=576625&r1=576624&r2=576625&view=diff
> ==============================================================================
> ---
> incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java
> (original)
> +++
> incubator/tuscany/branches/sca-java-1.0/modules/implementation-osgi/src/test/java/org/apache/tuscany/sca/implementation/osgi/invocation/OSGiTestCase.java
> Mon Sep 17 16:26:47 2007
> @@ -51,7 +51,7 @@
>
> @Override
> protected void tearDown() throws Exception {
> - OSGiRuntime.getRuntime().shutdown();
> + OSGiRuntime.stop();
> }
>
> public void testOSGiComponent() throws Exception {
>
> Modified:
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java?rev=576625&r1=576624&r2=576625&view=diff
> ==============================================================================
> ---
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java
> (original)
> +++
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntime.java
> Mon Sep 17 16:26:47 2007
> @@ -18,28 +18,30 @@
> */
> package org.apache.tuscany.sca.osgi.runtime;
>
> -
> import java.io.InputStream;
> import java.lang.reflect.Method;
> +import java.util.logging.Level;
> +import java.util.logging.Logger;
>
> import org.osgi.framework.Bundle;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.BundleException;
> import org.osgi.service.packageadmin.PackageAdmin;
>
> -public abstract class OSGiRuntime {
> -
> +public abstract class OSGiRuntime {
> + private static final Logger logger =
> Logger.getLogger(OSGiRuntime.class.getName());
> private BundleContext bundleContext;
> -
> +
> public abstract BundleContext getBundleContext();
> -
> +
> public abstract void shutdown() throws Exception;
> -
> +
> public abstract boolean supportsBundleFragments();
> -
> +
> private PackageAdmin packageAdmin;
> -
> -
> +
> + private static OSGiRuntime instance;
> +
> /**
> * System property
> org.apache.tuscany.implementation.osgi.runtime.OSGiRuntime can be set to
> the
> * name of the OSGiRuntime class (eg. EquinoxRuntime). If set, start
> this runtime and return the
> @@ -48,105 +50,106 @@
> *
> * @throws BundleException
> */
> - public static OSGiRuntime getRuntime() throws Exception {
> -
> + public synchronized static OSGiRuntime getRuntime() throws Exception
> {
> + if (instance != null) {
> + return instance;
> + }
> String runtimeClassName =
> System.getProperty(OSGiRuntime.class.getName());
>
> if (runtimeClassName != null) {
> try {
> Class<?> runtimeClass =
> OSGiRuntime.class.getClassLoader().loadClass(runtimeClassName);
> Method method = runtimeClass.getMethod("getInstance");
> - return (OSGiRuntime) method.invoke(null);
> -
> + instance = (OSGiRuntime)method.invoke(null);
> + return instance;
> +
> } catch (Exception e) {
> throw new BundleException("Could not start OSGi runtime "
> + runtimeClassName, e);
> }
> }
> -
> +
> try {
> -
> - return EquinoxRuntime.getInstance();
> -
> + instance = EquinoxRuntime.getInstance();
> + return instance;
> +
> } catch (ClassNotFoundException e) {
> - } catch (Throwable e) {
> - e.printStackTrace();
> - }
> -
> + // Ignore
> + } catch (Throwable e) {
> + logger.log(Level.SEVERE, e.getMessage(), e);
> + }
> +
> try {
> -
> - return FelixRuntime.getInstance();
> -
> + instance = FelixRuntime.getInstance();
> + return instance;
> } catch (ClassNotFoundException e) {
> - } catch (Throwable e) {
> - e.printStackTrace();
> - }
> -
> + // Ignore
> + } catch (Throwable e) {
> + logger.log(Level.SEVERE, e.getMessage(), e);
> + }
> +
> try {
> -
> - return KnopflerfishRuntime.getInstance();
> -
> + instance = KnopflerfishRuntime.getInstance();
> + return instance;
> } catch (ClassNotFoundException e) {
> - } catch (Throwable e) {
> - e.printStackTrace();
> - }
> -
> + // Ignore
> + } catch (Throwable e) {
> + logger.log(Level.SEVERE, e.getMessage(), e);
> + }
> +
> throw new BundleException("Could not start OSGi runtime from the
> classpath");
> }
> -
> +
> private void initialize() {
> - if (bundleContext == null)
> + if (bundleContext == null)
> bundleContext = getBundleContext();
> -
> - if (bundleContext != null) {
> -
> - org.osgi.framework.ServiceReference packageAdminReference =
> +
> + if (bundleContext != null) {
> +
> + org.osgi.framework.ServiceReference packageAdminReference =
>
> bundleContext.getServiceReference("org.osgi.service.packageadmin.PackageAdmin");
> if (packageAdminReference != null) {
> -
> - packageAdmin = (PackageAdmin)
> bundleContext.getService(packageAdminReference);
> +
> + packageAdmin =
> (PackageAdmin)bundleContext.getService(packageAdminReference);
> }
> - }
> -
> + }
> +
> }
> -
>
> public Bundle findBundle(String bundleSymbolicName, String
> bundleVersion) {
> -
> - initialize();
> -
> +
> + initialize();
> +
> if (bundleContext != null) {
> Bundle[] installedBundles = bundleContext.getBundles();
> for (Bundle bundle : installedBundles) {
> - if (bundleSymbolicName.equals(bundle.getSymbolicName())
> &&
> - (bundleVersion == null ||
> -
> bundleVersion.equals(bundle.getHeaders().get("Bundle-Version"))))
> - return bundle;
> + if (bundleSymbolicName.equals(bundle.getSymbolicName())
> && (bundleVersion == null || bundleVersion
> + .equals(bundle.getHeaders().get("Bundle-Version"))))
> + return bundle;
> }
> -
> +
> }
> return null;
> }
> -
>
> public Bundle findBundle(String bundleLocation) {
> -
> +
> initialize();
> -
> +
> if (bundleContext != null) {
> Bundle[] installedBundles = bundleContext.getBundles();
> for (Bundle bundle : installedBundles) {
> if (bundle.getLocation().equals(bundleLocation))
> - return bundle;
> + return bundle;
> }
> -
> +
> }
> return null;
> }
> -
> +
> public Bundle installBundle(String bundleLocation, InputStream
> inputStream) {
> -
> - initialize();
> -
> +
> + initialize();
> +
> try {
> if (bundleContext != null) {
> Bundle bundle = findBundle(bundleLocation);
> @@ -156,15 +159,25 @@
> bundle = bundleContext.installBundle(bundleLocation);
> else
> bundle = bundleContext.installBundle(bundleLocation,
> inputStream);
> -
> +
> if (bundle != null && packageAdmin != null)
> - packageAdmin.refreshPackages(null);
> -
> + packageAdmin.refreshPackages(null);
> +
> return bundle;
> }
> } catch (BundleException e) {
> }
> return null;
> + }
> +
> + /**
> + * @return the instance
> + */
> + public synchronized static void stop() throws Exception {
> + if (instance != null) {
> + instance.shutdown();
> + instance = null;
> + }
> }
>
> }
>
> Added:
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java?rev=576625&view=auto
> ==============================================================================
> ---
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
> (added)
> +++
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
> Mon Sep 17 16:26:47 2007
> @@ -0,0 +1,47 @@
> +/*
> + * 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 org.apache.tuscany.sca.core.ExtensionPointRegistry;
> +import org.apache.tuscany.sca.core.ModuleActivator;
> +
> +/**
> + * @version $Rev$ $Date$
> + */
> +public class OSGiRuntimeModuleActivator implements ModuleActivator {
> +
> + /**
> + * @see
> org.apache.tuscany.sca.core.ModuleActivator#start(org.apache.tuscany.sca.core.ExtensionPointRegistry)
> + */
> + public void start(ExtensionPointRegistry registry) {
> + }
> +
> + /**
> + * @see
> org.apache.tuscany.sca.core.ModuleActivator#stop(org.apache.tuscany.sca.core.ExtensionPointRegistry)
> + */
> + public void stop(ExtensionPointRegistry registry) {
> + try {
> + OSGiRuntime.stop();
> + } catch (Exception e) {
> + // Ignore the exception
> + }
> + }
> +
> +}
>
> Propchange:
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
> ------------------------------------------------------------------------------
> svn:eol-style = native
>
> Propchange:
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeModuleActivator.java
> ------------------------------------------------------------------------------
> svn:keywords = Rev Date
>
> Added:
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator?rev=576625&view=auto
> ==============================================================================
> ---
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
> (added)
> +++
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
> Mon Sep 17 16:26:47 2007
> @@ -0,0 +1,36 @@
> +# 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.
> +# Implementation class for the ModuleActivator
> +org.apache.tuscany.sca.osgi.runtime.OSGiRuntimeModuleActivator
> +# 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.
> +# Implementation class for the ModuleActivator
> +org.apache.tuscany.sca.osgi.runtime.OSGiRuntimeModuleActivator
>
> Modified:
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java?rev=576625&r1=576624&r2=576625&view=diff
> ==============================================================================
> ---
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java
> (original)
> +++
> incubator/tuscany/branches/sca-java-1.0/modules/osgi-runtime/src/test/java/org/apache/tuscany/sca/osgi/runtime/OSGiRuntimeTestCase.java
> Mon Sep 17 16:26:47 2007
> @@ -19,40 +19,54 @@
>
> package org.apache.tuscany.sca.osgi.runtime;
>
> -import org.apache.tuscany.sca.osgi.runtime.OSGiRuntime;
> -import org.osgi.framework.BundleContext;
> -
> import junit.framework.TestCase;
>
> +import org.osgi.framework.BundleContext;
> +
> /**
> * Test OSGi runtime.
> *
> */
> public class OSGiRuntimeTestCase extends TestCase {
> -
> + private OSGiRuntime runtime;
> +
> + /**
> + * @see junit.framework.TestCase#setUp()
> + */
> + @Override
> + protected void setUp() throws Exception {
> + this.runtime = OSGiRuntime.getRuntime();
> + }
> +
> + /**
> + * @see junit.framework.TestCase#tearDown()
> + */
> + @Override
> + protected void tearDown() throws Exception {
> + OSGiRuntime.stop();
> + }
> +
> public void testRuntime() throws Exception {
> -
> - BundleContext bc1 = OSGiRuntime.getRuntime().getBundleContext();
> -
> +
> + BundleContext bc1 = runtime.getBundleContext();
> +
> assertNotNull(bc1);
> -
> - BundleContext bc2 = OSGiRuntime.getRuntime().getBundleContext();
> -
> +
> + BundleContext bc2 = runtime.getBundleContext();
> +
> assertNotNull(bc2);
> -
> +
> assertTrue(bc1 == bc2);
> -
> - OSGiRuntime.getRuntime().shutdown();
> -
> - BundleContext bc3 = OSGiRuntime.getRuntime().getBundleContext();
> -
> +
> + OSGiRuntime.stop();
> + runtime = OSGiRuntime.getRuntime();
> +
> + BundleContext bc3 = runtime.getBundleContext();
> +
> assertNotNull(bc3);
> -
> +
> assertTrue(bc1 != bc3);
> -
> -
> -
> +
> }
>
> -
> }
>
> Modified: incubator/tuscany/branches/sca-java-1.0/modules/pom.xml
> URL:
> http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-1.0/modules/pom.xml?rev=576625&r1=576624&r2=576625&view=diff
> ==============================================================================
> --- incubator/tuscany/branches/sca-java-1.0/modules/pom.xml (original)
> +++ incubator/tuscany/branches/sca-java-1.0/modules/pom.xml Mon Sep 17
> 16:26:47 2007
> @@ -49,7 +49,6 @@
> <module>binding-rmi</module>
> <module>binding-sca</module>
> <module>binding-sca-axis2</module>
> - <module>binding-osgi</module>
> <!--
> <module>binding-sca-jms</module>
> -->
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
> For additional commands, e-mail: tuscany-commits-help@ws.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org