From 9bed5ae38c581999db465b42b504026a7097af95 Mon Sep 17 00:00:00 2001
From: lresende tuscany:type=server,name=tuscanyServer
+ *
. It also allows a runtime to be booted given a bootpath.
+ * The JMX domain in which the runtime is registered si definied in
+ * the file $bootPath/etc/runtime.properties
. The properties
+ * defined are jmx.domain
and offline
.
+ *
tuscany.installDir
. If not specified it is asumed to
+ * be the directory from where the JAR file containing the main class
+ * is loaded.
+ *
+ *
+ *
+ * The administration port can be specified using the system property
+ * tuscany.adminPort.If not specified the default port
+ * that is used is 1099
+ *
+ * @version $Rev$ $Date$
+ */
+public class TuscanyServer implements TuscanyServerMBean {
+
+ /**
+ * Agent
+ */
+ private final Agent agent;
+
+ /**
+ * Install directory
+ */
+ private final File installDirectory;
+
+ /**
+ * Started runtimes.
+ */
+ private final Map bootedRuntimes = new ConcurrentHashMap();
+
+ /**
+ * @param args Commandline arguments.
+ */
+ public static void main(String[] args) throws Exception {
+ new TuscanyServer().start();
+ }
+
+ /**
+ * Constructor initializes all the required classloaders.
+ *
+ * @throws MalformedURLException
+ */
+ private TuscanyServer() throws MalformedURLException {
+ installDirectory = DirectoryHelper.getInstallDirectory(TuscanyServer.class);
+ agent = RmiAgent.getInstance();
+ }
+
+ /**
+ * Starts a runtime specified by the bootpath.
+ *
+ * @param profileName Profile for the runtime.
+ */
+ public final void startRuntime(final String profileName) {
+
+ try {
+
+ final File profileDirectory = DirectoryHelper.getProfileDirectory(installDirectory, profileName);
+ final File bootDirectory = DirectoryHelper.getBootDirectory(installDirectory, profileDirectory, null);
+
+ final MBeanServer mBeanServer = agent.getMBeanServer();
+ final StandaloneRuntimeInfo runtimeInfo = createRuntimeInfo(profileName);
+ final ManagementService> managementService = new JmxManagementService(mBeanServer, profileName);
+ final TuscanyRuntime runtime = createRuntime(bootDirectory, runtimeInfo);
+ runtime.setManagementService(managementService);
+ runtime.initialize();
+
+ bootedRuntimes.put(profileName, runtime);
+
+ } catch (InitializationException ex) {
+ ex.printStackTrace();
+ throw new TuscanyServerException(ex);
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ throw new TuscanyServerException(ex);
+ } catch (ClassNotFoundException ex) {
+ ex.printStackTrace();
+ throw new TuscanyServerException(ex);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ throw new TuscanyServerException(ex);
+ }
+
+ System.err.println("Started");
+
+ }
+
+ /**
+ * @see org.apache.tuscany.standalone.server.TuscanyServerMBean#shutdownRuntime(java.lang.String)
+ */
+ public final void shutdownRuntime(String bootPath) {
+
+ try {
+ TuscanyRuntime runtime = bootedRuntimes.get(bootPath);
+ if (runtime != null) {
+ runtime.destroy();
+ bootedRuntimes.remove(runtime);
+ runtime = null;
+ }
+ } catch (ShutdownException ex) {
+ throw new TuscanyServerException(ex);
+ }
+
+ }
+
+ /**
+ * Starts the server.
+ */
+ public final void shutdown() {
+
+ for (String bootPath : bootedRuntimes.keySet()) {
+ shutdownRuntime(bootPath);
+ }
+ agent.shutdown();
+ System.err.println("Shutdown");
+ System.exit(0);
+
+ }
+
+ /**
+ * TODO Share this code with launcher.
+ *
+ * Creates the runtime info.
+ *
+ * @param profile profile for which runtime info is created.
+ * @return Runtime info.
+ * @throws IOException If unable to read the runtime properties.
+ */
+ private StandaloneRuntimeInfo createRuntimeInfo(String profile) throws IOException {
+
+ File profileDir = DirectoryHelper.getProfileDirectory(installDirectory, profile);
+
+ // load properties for this runtime
+ File propFile = new File(profileDir, "etc/runtime.properties");
+ Properties props = DirectoryHelper.loadProperties(propFile, System.getProperties());
+
+ // online unless the offline property is set
+ boolean online = !Boolean.parseBoolean(props.getProperty("offline", "false"));
+
+ return new StandaloneRuntimeInfoImpl(null, profile, installDirectory, profileDir, null, online, props);
+ }
+
+ /**
+ * TODO Share this code with launcher.
+ *
+ * Creates the runtime.
+ *
+ * @param bootDirectory Boot directory for the runtime.
+ * @param runtimeInfo Runtime info.
+ * @return Runtime.
+ */
+ private TuscanyRuntime createRuntime(final File bootDirectory, final StandaloneRuntimeInfo runtimeInfo) throws IOException, ClassNotFoundException {
+
+ final URL profileUrl = runtimeInfo.getProfileDirectory().toURL();
+ final ClassLoader hostClassLoader = ClassLoader.getSystemClassLoader();
+ final ClassLoader bootClassLoader = DirectoryHelper.createClassLoader(hostClassLoader, bootDirectory);
+
+ final URL systemScdl = getSystemScdl(profileUrl, runtimeInfo);
+ if (systemScdl == null) {
+ throw new TuscanyServerException("Unable to find system scdl");
+ }
+
+ final String className =
+ runtimeInfo.getProperty("tuscany.launcherClass",
+ "org.apache.tuscany.runtime.standalone.host.StandaloneRuntimeImpl");
+ final TuscanyRuntime runtime = (TuscanyRuntime) Beans.instantiate(bootClassLoader, className);
+ runtime.setSystemScdl(systemScdl);
+ runtime.setHostClassLoader(hostClassLoader);
+
+ runtime.setRuntimeInfo(runtimeInfo);
+ return runtime;
+
+ }
+
+ /**
+ * Gets the system SCDL.
+ *
+ * @param bootClassLoader Boot classloader.
+ * @return URL to the system SCDL.
+ * @throws MalformedURLException
+ */
+ private URL getSystemScdl(URL profileUrl, StandaloneRuntimeInfo runtimeInfo) throws MalformedURLException {
+ return new URL(profileUrl, runtimeInfo.getProperty("tuscany.systemSCDL", "system.scdl"));
+ }
+
+ /**
+ * Starts the server and starts the JMX agent.
+ */
+ private void start() {
+ agent.start();
+ agent.register(this, "tuscany:type=server,name=tuscanyServer");
+ }
+
+}
diff --git a/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServerException.java b/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServerException.java
new file mode 100644
index 0000000000..1c72196145
--- /dev/null
+++ b/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServerException.java
@@ -0,0 +1,31 @@
+package org.apache.tuscany.standalone.server;
+
+import org.apache.tuscany.api.TuscanyRuntimeException;
+
+/**
+ * Exception thrown by the tuscany server during startup and shutdown.
+ *
+ * @version $Revisiion$ $Date$
+ *
+ */
+
+@SuppressWarnings("serial")
+public class TuscanyServerException extends TuscanyRuntimeException {
+
+ /**
+ * Initializes the cause.
+ * @param cause Root cause of the exception.
+ */
+ public TuscanyServerException(Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * Initializes the message.
+ * @param message Message of the exception.
+ */
+ public TuscanyServerException(String message) {
+ super(message);
+ }
+
+}
diff --git a/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServerMBean.java b/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServerMBean.java
new file mode 100644
index 0000000000..a3bc6291b2
--- /dev/null
+++ b/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServerMBean.java
@@ -0,0 +1,49 @@
+/*
+ * 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.standalone.server;
+
+/**
+ * Management interface for the tuscany server.
+ *
+ * @version $Revision$ $Date$
+ *
+ */
+public interface TuscanyServerMBean {
+
+ /**
+ * Starts a runtime specified by the bootpath.
+ *
+ * @param profileName Profile for the runtime.
+ */
+ public void startRuntime(String profileName);
+
+ /**
+ * Shuts down a runtime specified by the bootpath.
+ *
+ * @param bootPath Bootpath for the runtime.
+ */
+ public void shutdownRuntime(String bootPath);
+
+ /**
+ * Starts the server.
+ *
+ */
+ public void shutdown();
+
+}
--
cgit v1.2.3