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