1 /***************************************************************************************
2 * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz.connectivity;
9
10 import java.util.Properties;
11 import java.io.FileInputStream;
12 import java.lang.reflect.Method;
13
14 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
15 import org.codehaus.aspectwerkz.util.ContextClassLoader;
16
17 /***
18 * Manages the remote proxy server.
19 *
20 * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
21 */
22 public class RemoteProxyServerManager {
23
24 /***
25 * The path to the remote proxy server config file.
26 */
27 private static final boolean START_REMOTE_PROXY_SERVER = "true".equals(
28 java.lang.System.getProperty(
29 "aspectwerkz.remote.server.run",
30 "false"
31 )
32 );
33
34 /***
35 * The sole instance.
36 */
37 private static final RemoteProxyServerManager INSTANCE = new RemoteProxyServerManager();
38
39 /***
40 * The remote proxy server instance.
41 */
42 private RemoteProxyServer m_remoteProxyServer = null;
43
44 /***
45 * Returns the sole instance.
46 *
47 * @return the sole instance
48 */
49 public static RemoteProxyServerManager getInstance() {
50 return INSTANCE;
51 }
52
53 /***
54 * Starts up the remote proxy server.
55 */
56 public void start() {
57 if (START_REMOTE_PROXY_SERVER) {
58 m_remoteProxyServer = new RemoteProxyServer(ContextClassLoader.getLoader(), getInvoker());
59 m_remoteProxyServer.start();
60 }
61 }
62
63 /***
64 * Returns the Invoker instance to use.
65 *
66 * @return the Invoker
67 */
68 private Invoker getInvoker() {
69 Invoker invoker;
70 try {
71 Properties properties = new Properties();
72 properties.load(new FileInputStream(java.lang.System.getProperty("aspectwerkz.resource.bundle")));
73 String className = properties.getProperty("remote.server.invoker.classname");
74 invoker = (Invoker) ContextClassLoader.forName(className).newInstance();
75 } catch (Exception e) {
76 invoker = getDefaultInvoker();
77 }
78 return invoker;
79 }
80
81 /***
82 * Returns the default Invoker.
83 *
84 * @return the default invoker
85 */
86 private Invoker getDefaultInvoker() {
87 return new Invoker() {
88 public Object invoke(final String handle,
89 final String methodName,
90 final Class[] paramTypes,
91 final Object[] args,
92 final Object context) {
93 Object result;
94 try {
95 final Object instance = RemoteProxy.getWrappedInstance(handle);
96 final Method method = instance.getClass().getMethod(methodName, paramTypes);
97 result = method.invoke(instance, args);
98 } catch (Exception e) {
99 throw new WrappedRuntimeException(e);
100 }
101 return result;
102 }
103 };
104 }
105
106 /***
107 * Private constructor.
108 */
109 private RemoteProxyServerManager() {
110
111 }
112 }