diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:11:56 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:11:56 +0000 |
commit | 9bed5ae38c581999db465b42b504026a7097af95 (patch) | |
tree | a32b086a857a4556b8aec1aa9ed3a586efd9461e /sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src | |
parent | ece4fd35da7b7fc76264776f81705e6b5b52d3e0 (diff) |
Moving 1.x branches
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835141 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src')
3 files changed, 329 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServer.java b/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServer.java new file mode 100644 index 0000000000..325978c6ea --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/runtime/standalone/server.start/src/main/java/org/apache/tuscany/standalone/server/TuscanyServer.java @@ -0,0 +1,249 @@ +/* + * 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; + +import java.beans.Beans; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import javax.management.MBeanServer; + +import org.apache.tuscany.host.management.ManagementService; +import org.apache.tuscany.host.runtime.InitializationException; +import org.apache.tuscany.host.runtime.ShutdownException; +import org.apache.tuscany.host.runtime.TuscanyRuntime; +import org.apache.tuscany.runtime.standalone.DirectoryHelper; +import org.apache.tuscany.runtime.standalone.StandaloneRuntimeInfo; +import org.apache.tuscany.runtime.standalone.StandaloneRuntimeInfoImpl; +import org.apache.tuscany.service.management.jmx.JmxManagementService; +import org.apache.tuscany.service.management.jmx.agent.Agent; +import org.apache.tuscany.service.management.jmx.agent.RmiAgent; + +/** + * This class provides the commandline interface for starting the + * tuscany standalone server. + * <p/> + * <p/> + * The class boots the tuscany server and also starts a JMX server + * and listens for shutdown command. The server itself is available + * by the object name <code>tuscany:type=server,name=tuscanyServer + * </code>. 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 <code>$bootPath/etc/runtime.properties</code>. The properties + * defined are <code>jmx.domain</code> and <code>offline</code>. + * </p> + * <p/> + * <p/> + * The install directory can be specified using the system property + * <code>tuscany.installDir</code>. If not specified it is asumed to + * be the directory from where the JAR file containing the main class + * is loaded. + * </p> + * <p/> + * <p/> + * The administration port can be specified using the system property + * <code>tuscany.adminPort</tuscany>.If not specified the default port + * that is used is <code>1099</code> + * + * @version $Rev$ $Date$ + */ +public class TuscanyServer implements TuscanyServerMBean { + + /** + * Agent + */ + private final Agent agent; + + /** + * Install directory + */ + private final File installDirectory; + + /** + * Started runtimes. + */ + private final Map<String, TuscanyRuntime> bootedRuntimes = new ConcurrentHashMap<String, TuscanyRuntime>(); + + /** + * @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(); + +} |