KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > connectivity > RemoteProxyServerManager


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.connectivity;
5
6
7 import com.tc.aspectwerkz.exception.WrappedRuntimeException;
8 import com.tc.aspectwerkz.util.ContextClassLoader;
9
10 import java.io.FileInputStream JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 /**
15  * Manages the remote proxy server.
16  *
17  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
18  */

19 public class RemoteProxyServerManager {
20
21   /**
22    * The path to the remote proxy server config file.
23    */

24   private static final boolean START_REMOTE_PROXY_SERVER = "true".equals(
25           java.lang.System.getProperty(
26                   "aspectwerkz.remote.server.run",
27                   "false"
28           )
29   );
30
31   /**
32    * The sole instance.
33    */

34   private static final RemoteProxyServerManager INSTANCE = new RemoteProxyServerManager();
35
36   /**
37    * The remote proxy server instance.
38    */

39   private RemoteProxyServer m_remoteProxyServer = null;
40
41   /**
42    * Returns the sole instance.
43    *
44    * @return the sole instance
45    */

46   public static RemoteProxyServerManager getInstance() {
47     return INSTANCE;
48   }
49
50   /**
51    * Starts up the remote proxy server.
52    */

53   public void start() {
54     if (START_REMOTE_PROXY_SERVER) {
55       m_remoteProxyServer = new RemoteProxyServer(ContextClassLoader.getLoader(), getInvoker());
56       m_remoteProxyServer.start();
57     }
58   }
59
60   /**
61    * Returns the Invoker instance to use.
62    *
63    * @return the Invoker
64    */

65   private Invoker getInvoker() {
66     Invoker invoker;
67     try {
68       Properties JavaDoc properties = new Properties JavaDoc();
69       properties.load(new FileInputStream JavaDoc(java.lang.System.getProperty("aspectwerkz.resource.bundle")));
70       String JavaDoc className = properties.getProperty("remote.server.invoker.classname");
71       invoker = (Invoker) ContextClassLoader.forName(className).newInstance();
72     } catch (Exception JavaDoc e) {
73       invoker = getDefaultInvoker();
74     }
75     return invoker;
76   }
77
78   /**
79    * Returns the default Invoker.
80    *
81    * @return the default invoker
82    */

83   private Invoker getDefaultInvoker() {
84     return new Invoker() {
85       public Object JavaDoc invoke(final String JavaDoc handle,
86                            final String JavaDoc methodName,
87                            final Class JavaDoc[] paramTypes,
88                            final Object JavaDoc[] args,
89                            final Object JavaDoc context) {
90         Object JavaDoc result;
91         try {
92           final Object JavaDoc instance = RemoteProxy.getWrappedInstance(handle);
93           final Method JavaDoc method = instance.getClass().getMethod(methodName, paramTypes);
94           result = method.invoke(instance, args);
95         } catch (Exception JavaDoc e) {
96           throw new WrappedRuntimeException(e);
97         }
98         return result;
99       }
100     };
101   }
102
103   /**
104    * Private constructor.
105    */

106   private RemoteProxyServerManager() {
107
108   }
109 }
110
Popular Tags