1 | package omq.server; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.lang.reflect.Method; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.HashMap; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Map; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import omq.Remote; |
---|
12 | import omq.common.broker.Broker; |
---|
13 | import omq.common.util.ParameterQueue; |
---|
14 | |
---|
15 | import org.apache.log4j.Logger; |
---|
16 | |
---|
17 | /** |
---|
18 | * A RemoteObject when it's started will be waiting for requests and will invoke |
---|
19 | * them. When a RemoteObject is started it listens two queues, the first one has |
---|
20 | * the same name as its reference and the second one is its multiqueue -this |
---|
21 | * name can be set using a property, be aware to use a name not used by another |
---|
22 | * object!!!-. |
---|
23 | * |
---|
24 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
25 | * |
---|
26 | */ |
---|
27 | public abstract class RemoteObject implements Remote { |
---|
28 | |
---|
29 | private static final long serialVersionUID = -1778953938739846450L; |
---|
30 | private static final Logger logger = Logger.getLogger(RemoteObject.class.getName()); |
---|
31 | |
---|
32 | private String reference; |
---|
33 | private String UID; |
---|
34 | private Properties env; |
---|
35 | private transient Broker broker; |
---|
36 | private transient RemoteThreadPool pool; |
---|
37 | private transient Map<String, List<Class<?>>> params; |
---|
38 | |
---|
39 | private static final Map<String, Class<?>> primitiveClasses = new HashMap<String, Class<?>>(); |
---|
40 | |
---|
41 | static { |
---|
42 | primitiveClasses.put("byte", Byte.class); |
---|
43 | primitiveClasses.put("short", Short.class); |
---|
44 | primitiveClasses.put("char", Character.class); |
---|
45 | primitiveClasses.put("int", Integer.class); |
---|
46 | primitiveClasses.put("long", Long.class); |
---|
47 | primitiveClasses.put("float", Float.class); |
---|
48 | primitiveClasses.put("double", Double.class); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * This method starts a remoteObject. |
---|
53 | * |
---|
54 | * @param reference |
---|
55 | * - broker's binding referece |
---|
56 | * @param broker |
---|
57 | * - broker that binds this remoteObject |
---|
58 | * @param env |
---|
59 | * - properties of this remoteObject |
---|
60 | * @throws Exception |
---|
61 | */ |
---|
62 | public void startRemoteObject(String reference, Broker broker, Properties env) throws Exception { |
---|
63 | this.broker = broker; |
---|
64 | this.reference = reference; |
---|
65 | this.env = env; |
---|
66 | |
---|
67 | this.params = new HashMap<String, List<Class<?>>>(); |
---|
68 | for (Method m : this.getClass().getMethods()) { |
---|
69 | List<Class<?>> list = new ArrayList<Class<?>>(); |
---|
70 | for (Class<?> clazz : m.getParameterTypes()) { |
---|
71 | list.add(clazz); |
---|
72 | } |
---|
73 | this.params.put(m.getName(), list); |
---|
74 | } |
---|
75 | |
---|
76 | // Get pool information |
---|
77 | int minPoolThreads = Integer.parseInt(env.getProperty(ParameterQueue.MIN_POOL_THREADS, "1")); |
---|
78 | int maxPoolThreads = Integer.parseInt(env.getProperty(ParameterQueue.MAX_POOL_THREADS, "1")); |
---|
79 | long refresh = Long.parseLong(env.getProperty(ParameterQueue.POOL_REFRESH_TIME, "60000")); |
---|
80 | long keepAliveTime = Long.parseLong(env.getProperty(ParameterQueue.KEEP_ALIVE_TIME, "30000")); |
---|
81 | int maxMessagesPerThread = Integer.parseInt(env.getProperty(ParameterQueue.MAX_MESSAGES_PER_THREAD, "5")); |
---|
82 | |
---|
83 | // Create the pool & start it |
---|
84 | pool = new RemoteThreadPool(minPoolThreads, maxPoolThreads, refresh, keepAliveTime, maxMessagesPerThread, this, broker); |
---|
85 | pool.start(); |
---|
86 | } |
---|
87 | |
---|
88 | public void startRemoteObject(String reference, String UID, Broker broker, Properties env) throws Exception { |
---|
89 | this.UID = UID; |
---|
90 | startRemoteObject(reference, broker, env); |
---|
91 | } |
---|
92 | |
---|
93 | @Override |
---|
94 | public String getRef() { |
---|
95 | return reference; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * This method kills all the threads waiting for requests |
---|
100 | * |
---|
101 | * @throws IOException |
---|
102 | * - If an operation failed. |
---|
103 | */ |
---|
104 | public void kill() throws IOException { |
---|
105 | logger.info("Killing objectmq: " + this.getRef()); |
---|
106 | pool.kill(); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * This method invokes the method specified by methodName and arguments |
---|
111 | * |
---|
112 | * @param methodName |
---|
113 | * @param arguments |
---|
114 | * @return result |
---|
115 | * @throws Exception |
---|
116 | */ |
---|
117 | public Object invokeMethod(String methodName, Object[] arguments) throws Exception { |
---|
118 | |
---|
119 | // Get the specific method identified by methodName and its arguments |
---|
120 | Method method = loadMethod(methodName, arguments); |
---|
121 | |
---|
122 | return method.invoke(this, arguments); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * This method loads the method specified by methodName and args |
---|
127 | * |
---|
128 | * @param methodName |
---|
129 | * @param args |
---|
130 | * @return method |
---|
131 | * @throws NoSuchMethodException |
---|
132 | * - If the method cannot be found |
---|
133 | */ |
---|
134 | private Method loadMethod(String methodName, Object[] args) throws NoSuchMethodException { |
---|
135 | Method m = null; |
---|
136 | |
---|
137 | // Obtain the class reference |
---|
138 | Class<?> clazz = this.getClass(); |
---|
139 | Class<?>[] argArray = null; |
---|
140 | |
---|
141 | if (args != null) { |
---|
142 | argArray = new Class<?>[args.length]; |
---|
143 | for (int i = 0; i < args.length; i++) { |
---|
144 | argArray[i] = args[i].getClass(); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | try { |
---|
149 | m = clazz.getMethod(methodName, argArray); |
---|
150 | } catch (NoSuchMethodException nsm) { |
---|
151 | m = loadMethodWithPrimitives(methodName, argArray); |
---|
152 | } |
---|
153 | return m; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * This method loads a method which uses primitives as arguments |
---|
158 | * |
---|
159 | * @param methodName |
---|
160 | * - name of the method wanted to invoke |
---|
161 | * @param argArray |
---|
162 | * - arguments |
---|
163 | * @return method |
---|
164 | * @throws NoSuchMethodException |
---|
165 | * - If the method cannot be found |
---|
166 | */ |
---|
167 | private Method loadMethodWithPrimitives(String methodName, Class<?>[] argArray) throws NoSuchMethodException { |
---|
168 | if (argArray != null) { |
---|
169 | Method[] methods = this.getClass().getMethods(); |
---|
170 | int length = argArray.length; |
---|
171 | |
---|
172 | for (Method method : methods) { |
---|
173 | String name = method.getName(); |
---|
174 | int argsLength = method.getParameterTypes().length; |
---|
175 | |
---|
176 | if (name.equals(methodName) && length == argsLength) { |
---|
177 | // This array can have primitive types inside |
---|
178 | Class<?>[] params = method.getParameterTypes(); |
---|
179 | |
---|
180 | boolean found = true; |
---|
181 | |
---|
182 | for (int i = 0; i < length; i++) { |
---|
183 | if (params[i].isPrimitive()) { |
---|
184 | Class<?> paramWrapper = primitiveClasses.get(params[i].getName()); |
---|
185 | |
---|
186 | if (!paramWrapper.equals(argArray[i])) { |
---|
187 | found = false; |
---|
188 | break; |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | if (found) { |
---|
193 | return method; |
---|
194 | } |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | throw new NoSuchMethodException(methodName); |
---|
199 | } |
---|
200 | |
---|
201 | public List<Class<?>> getParams(String methodName) { |
---|
202 | return params.get(methodName); |
---|
203 | } |
---|
204 | |
---|
205 | public Broker getBroker() { |
---|
206 | return broker; |
---|
207 | } |
---|
208 | |
---|
209 | public RemoteThreadPool getPool() { |
---|
210 | return pool; |
---|
211 | } |
---|
212 | |
---|
213 | public Properties getEnv() { |
---|
214 | return env; |
---|
215 | } |
---|
216 | |
---|
217 | public String getUID() { |
---|
218 | return UID; |
---|
219 | } |
---|
220 | |
---|
221 | public void setUID(String uID) { |
---|
222 | UID = uID; |
---|
223 | } |
---|
224 | |
---|
225 | } |
---|