1 | package omq.common.broker; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.Properties; |
---|
5 | |
---|
6 | import omq.Remote; |
---|
7 | import omq.client.proxy.Proxymq; |
---|
8 | import omq.client.remote.response.ResponseListener; |
---|
9 | import omq.common.event.EventDispatcher; |
---|
10 | import omq.common.util.Environment; |
---|
11 | import omq.common.util.OmqConnectionFactory; |
---|
12 | import omq.exception.EnvironmentException; |
---|
13 | import omq.exception.RemoteException; |
---|
14 | import omq.server.remote.request.RemoteObject; |
---|
15 | |
---|
16 | import com.rabbitmq.client.Channel; |
---|
17 | import com.rabbitmq.client.Connection; |
---|
18 | |
---|
19 | public class Broker { |
---|
20 | private static Connection connection; |
---|
21 | private static Channel channel; |
---|
22 | private static boolean clientStarted = false; |
---|
23 | |
---|
24 | public static void initBroker(Properties env) throws Exception { |
---|
25 | try { |
---|
26 | Environment.getEnvironment(); |
---|
27 | } catch (EnvironmentException ex) { // environment not set. |
---|
28 | Environment.setEnvironment(env); |
---|
29 | connection = OmqConnectionFactory.getNewConnection(env); |
---|
30 | channel = connection.createChannel(); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | // TODO: what happens if the connection is not set |
---|
35 | public static Connection getConnection() throws Exception { |
---|
36 | return connection; |
---|
37 | } |
---|
38 | |
---|
39 | public static Channel getChannel() throws Exception { |
---|
40 | return channel; |
---|
41 | } |
---|
42 | |
---|
43 | public static Channel getNewChannel() throws IOException { |
---|
44 | return connection.createChannel(); |
---|
45 | } |
---|
46 | |
---|
47 | public static Remote lookup(String reference, Class<?> contract) throws RemoteException { |
---|
48 | try { |
---|
49 | Properties environment = Environment.getEnvironment(); |
---|
50 | |
---|
51 | if (!clientStarted) { |
---|
52 | initClient(environment); |
---|
53 | clientStarted = true; |
---|
54 | } |
---|
55 | |
---|
56 | if (!Proxymq.containsProxy(reference)) { |
---|
57 | Proxymq proxy = new Proxymq(reference, contract, environment); |
---|
58 | Class<?>[] array = { contract }; |
---|
59 | return (Remote) Proxymq.newProxyInstance(contract.getClassLoader(), array, proxy); |
---|
60 | } |
---|
61 | return (Remote) Proxymq.getInstance(reference); |
---|
62 | |
---|
63 | } catch (Exception e) { |
---|
64 | throw new RemoteException(e); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | public static void bind(String reference, RemoteObject remote) throws RemoteException { |
---|
69 | try { |
---|
70 | Properties environment = Environment.getEnvironment(); |
---|
71 | remote.start(reference, environment); |
---|
72 | } catch (Exception e) { |
---|
73 | throw new RemoteException(e); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | public static void unbind(String reference) throws RemoteException { |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | public void rebind(String name, Remote obj) throws RemoteException { |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | private static void initClient(Properties environment) throws Exception { |
---|
86 | if (ResponseListener.isVoid()) { |
---|
87 | ResponseListener.init(environment); |
---|
88 | } |
---|
89 | if (EventDispatcher.isVoid()) { |
---|
90 | EventDispatcher.init(environment); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | } |
---|