Rationalized how to locate an SCA contribution given a known Java class contained in that contribution, and how to locate an SCA contribution represented by an OSGi bundle.

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@698815 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
jsdelfino 2008-09-25 04:53:56 +00:00
parent 3913958f54
commit 2aa39a40c3
7 changed files with 80 additions and 36 deletions

View file

@ -0,0 +1,66 @@
/*
* 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.node.equinox.launcher;
import java.io.File;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.osgi.framework.Bundle;
/**
* ContributionLocationHelper
*
* @version $Rev: $ $Date: $
*/
public class ContributionLocationHelper {
/**
* Returns the location of the SCA contribution containing the given class.
*
* @param anchorClass
* @return
*/
public static String getContributionLocation(final Class<?> anchorClass) {
URL url = AccessController.doPrivileged(new PrivilegedAction<URL>() {
public URL run() {
return anchorClass.getProtectionDomain().getCodeSource().getLocation();
}
});
String uri = url.toString();
return uri;
}
/**
* Returns the location of the SCA contribution represented by the given bundle.
*
* @param anchorClass
* @return
*/
public static String getContributionLocation(final Bundle bundle) {
String uri = bundle.getLocation();
uri = uri.substring(uri.indexOf("file:") + 5);
File file = new File(uri);
uri = file.toURI().toString();
return uri;
}
}

View file

@ -48,7 +48,7 @@ import org.osgi.framework.BundleContext;
/**
* Wraps the Equinox runtime.
*/
public class EquinoxHost {
class EquinoxHost {
private static Logger logger = Logger.getLogger(EquinoxHost.class.getName());
private BundleContext bundleContext;
@ -118,7 +118,7 @@ public class EquinoxHost {
*
* @return
*/
public BundleContext start() {
BundleContext start() {
try {
if (!EclipseStarter.isRunning()) {
@ -292,7 +292,7 @@ public class EquinoxHost {
/**
* Stop the Equinox host.
*/
public void stop() {
void stop() {
try {
// Uninstall all the bundles we've installed

View file

@ -65,7 +65,7 @@ public class NodeLauncher {
* @throws LauncherException
*/
public <T> T createNodeFromURL(String configurationURL) throws LauncherException {
return (T)node(configurationURL, null, null, null, null, bundleContext);
return (T)node(configurationURL, null, null, null, bundleContext);
}
/**
@ -80,7 +80,7 @@ public class NodeLauncher {
* @throws LauncherException
*/
public <T> T createNode(String compositeURI, Contribution... contributions) throws LauncherException {
return (T)node(null, compositeURI, null, contributions, null, bundleContext);
return (T)node(null, compositeURI, null, contributions, bundleContext);
}
/**
@ -94,25 +94,7 @@ public class NodeLauncher {
*/
public <T> T createNode(String compositeURI, String compositeContent, Contribution... contributions)
throws LauncherException {
return (T)node(null, compositeURI, compositeContent, contributions, null, bundleContext);
}
/**
* Create a SCA node based on the discovery of the contribution on the classpath for the
* given classloader. This method should be treated a convenient shortcut with the following
* assumptions:
* <ul>
* <li>This is a standalone application and there is a deployable composite file on the classpath.
* <li>There is only one contribution which contains the deployable composite file physically in its packaging hierarchy.
* </ul>
*
* @param compositeURI The URI of the composite file relative to the root of the enclosing contribution
* @param classLoader The ClassLoader used to load the composite file as a resource. If the value is null,
* then thread context classloader will be used
* @return A newly created SCA node
*/
public <T> T createNodeFromClassLoader(String compositeURI, ClassLoader classLoader) throws LauncherException {
return (T)node(null, compositeURI, null, null, classLoader, bundleContext);
return (T)node(null, compositeURI, compositeContent, contributions, bundleContext);
}
public static void main(String[] args) throws Exception {

View file

@ -103,7 +103,6 @@ final class NodeLauncherUtil {
String compositeURI,
String compositeContent,
Contribution[] contributions,
ClassLoader contributionClassLoader,
BundleContext bundleContext) throws LauncherException {
try {
@ -129,12 +128,6 @@ final class NodeLauncherUtil {
// Construct the node with a configuration URI
bootstrap = bootstrapClass.getConstructor(String.class).newInstance(configurationURI);
} else if (contributionClassLoader != null) {
// Construct the node with a compositeURI and a classloader
Constructor<?> constructor = bootstrapClass.getConstructor(String.class, ClassLoader.class);
bootstrap = constructor.newInstance(compositeURI, contributionClassLoader);
} else if (compositeContent != null) {
// Construct the node with a composite URI, the composite content and

View file

@ -50,7 +50,8 @@ public class NodeLauncherTestCase {
@Test
public void testLaunch() throws Exception {
SCANode node = launcher.createNodeFromClassLoader("HelloWorld.composite", getClass().getClassLoader());
String location = ContributionLocationHelper.getContributionLocation(getClass());
SCANode node = launcher.createNode("HelloWorld.composite", new Contribution("test", location));
node.start();
node.stop();
}

View file

@ -21,6 +21,8 @@ package calculator;
import junit.framework.TestCase;
import org.apache.tuscany.sca.node.SCANode;
import org.apache.tuscany.sca.node.equinox.launcher.Contribution;
import org.apache.tuscany.sca.node.equinox.launcher.ContributionLocationHelper;
import org.apache.tuscany.sca.node.equinox.launcher.NodeLauncher;
import org.osoa.sca.annotations.EagerInit;
import org.osoa.sca.annotations.Scope;
@ -38,7 +40,8 @@ public class CalculatorTestCase extends TestCase {
@Override
protected void setUp() throws Exception {
launcher = NodeLauncher.newInstance();
node = launcher.createNodeFromClassLoader("Calculator.composite", getClass().getClassLoader());
String location = ContributionLocationHelper.getContributionLocation(getClass());
node = launcher.createNode("Calculator.composite", new Contribution("test", location));
System.out.println("SCA Node API ClassLoader: " + node.getClass().getClassLoader());
node.start();
}

View file

@ -18,10 +18,9 @@
*/
package calculator.rcp;
import java.io.File;
import org.apache.tuscany.sca.node.SCANode;
import org.apache.tuscany.sca.node.equinox.launcher.Contribution;
import org.apache.tuscany.sca.node.equinox.launcher.ContributionLocationHelper;
import org.apache.tuscany.sca.node.equinox.launcher.NodeLauncher;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
@ -49,7 +48,7 @@ public class Activator extends AbstractUIPlugin {
plugin = this;
launcher = NodeLauncher.newInstance();
String location = getClass().getProtectionDomain().getCodeSource().getLocation().toString();
String location = ContributionLocationHelper.getContributionLocation(getClass());
node = launcher.createNode("Calculator.composite", new Contribution("c1", location));
node.start();
}