001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.connectivity;
008:
009: import java.util.Properties;
010: import java.io.FileInputStream;
011: import java.lang.reflect.Method;
012:
013: import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
014: import org.codehaus.aspectwerkz.util.ContextClassLoader;
015:
016: /**
017: * Manages the remote proxy server.
018: *
019: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
020: */
021: public class RemoteProxyServerManager {
022:
023: /**
024: * The path to the remote proxy server config file.
025: */
026: private static final boolean START_REMOTE_PROXY_SERVER = "true"
027: .equals(java.lang.System.getProperty(
028: "aspectwerkz.remote.server.run", "false"));
029:
030: /**
031: * The sole instance.
032: */
033: private static final RemoteProxyServerManager INSTANCE = new RemoteProxyServerManager();
034:
035: /**
036: * The remote proxy server instance.
037: */
038: private RemoteProxyServer m_remoteProxyServer = null;
039:
040: /**
041: * Returns the sole instance.
042: *
043: * @return the sole instance
044: */
045: public static RemoteProxyServerManager getInstance() {
046: return INSTANCE;
047: }
048:
049: /**
050: * Starts up the remote proxy server.
051: */
052: public void start() {
053: if (START_REMOTE_PROXY_SERVER) {
054: m_remoteProxyServer = new RemoteProxyServer(
055: ContextClassLoader.getLoader(), getInvoker());
056: m_remoteProxyServer.start();
057: }
058: }
059:
060: /**
061: * Returns the Invoker instance to use.
062: *
063: * @return the Invoker
064: */
065: private Invoker getInvoker() {
066: Invoker invoker;
067: try {
068: Properties properties = new Properties();
069: properties.load(new FileInputStream(java.lang.System
070: .getProperty("aspectwerkz.resource.bundle")));
071: String className = properties
072: .getProperty("remote.server.invoker.classname");
073: invoker = (Invoker) ContextClassLoader.forName(className)
074: .newInstance();
075: } catch (Exception e) {
076: invoker = getDefaultInvoker();
077: }
078: return invoker;
079: }
080:
081: /**
082: * Returns the default Invoker.
083: *
084: * @return the default invoker
085: */
086: private Invoker getDefaultInvoker() {
087: return new Invoker() {
088: public Object invoke(final String handle,
089: final String methodName, final Class[] paramTypes,
090: final Object[] args, final Object context) {
091: Object result;
092: try {
093: final Object instance = RemoteProxy
094: .getWrappedInstance(handle);
095: final Method method = instance.getClass()
096: .getMethod(methodName, paramTypes);
097: result = method.invoke(instance, args);
098: } catch (Exception e) {
099: throw new WrappedRuntimeException(e);
100: }
101: return result;
102: }
103: };
104: }
105:
106: /**
107: * Private constructor.
108: */
109: private RemoteProxyServerManager() {
110:
111: }
112: }
|