1 | package omq.common.broker; |
---|
2 | |
---|
3 | import java.util.Properties; |
---|
4 | |
---|
5 | import omq.Remote; |
---|
6 | import omq.client.proxy.Proxymq; |
---|
7 | import omq.client.remote.response.ResponseListener; |
---|
8 | import omq.common.remote.OmqConnectionFactory; |
---|
9 | import omq.exception.RemoteException; |
---|
10 | import omq.server.remote.request.RemoteObject; |
---|
11 | |
---|
12 | import com.rabbitmq.client.Channel; |
---|
13 | import com.rabbitmq.client.Connection; |
---|
14 | |
---|
15 | public class Broker { |
---|
16 | private static Connection connection; |
---|
17 | private static Channel channel; |
---|
18 | private static Properties environment; |
---|
19 | |
---|
20 | public static void initBroker(Properties env) throws Exception { |
---|
21 | if (environment == null) { |
---|
22 | environment = env; |
---|
23 | connection = OmqConnectionFactory.getConnection(env); |
---|
24 | channel = connection.createChannel(); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | // TODO: what happens if the connection is not set |
---|
29 | public static Connection getConnection() throws Exception { |
---|
30 | return connection; |
---|
31 | } |
---|
32 | |
---|
33 | public static Channel getChannel() throws Exception { |
---|
34 | return channel; |
---|
35 | } |
---|
36 | |
---|
37 | public static Remote lookup(String reference, Class<?> contract) throws RemoteException { |
---|
38 | try { |
---|
39 | if (ResponseListener.isVoid()) { |
---|
40 | ResponseListener.init(environment); |
---|
41 | } |
---|
42 | if (!Proxymq.containsProxy(reference)) { |
---|
43 | Proxymq proxy = new Proxymq(reference, contract, environment); |
---|
44 | Class<?>[] array = { contract }; |
---|
45 | return (Remote) Proxymq.newProxyInstance(contract.getClassLoader(), array, proxy); |
---|
46 | } |
---|
47 | return (Remote) Proxymq.getInstance(reference); |
---|
48 | |
---|
49 | } catch (Exception e) { |
---|
50 | throw new RemoteException(e); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | public static void bind(String reference, RemoteObject remote) throws RemoteException { |
---|
55 | try { |
---|
56 | remote.start(reference, environment); |
---|
57 | } catch (Exception e) { |
---|
58 | throw new RemoteException(e); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | public static void unbind(String reference) throws RemoteException { |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | public void rebind(String name, Remote obj) throws RemoteException { |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | } |
---|