1 | package de.uka.ipd.sdq.prototype.framework.utils; |
2 | |
3 | import java.io.Serializable; |
4 | import java.net.MalformedURLException; |
5 | import java.rmi.AlreadyBoundException; |
6 | import java.rmi.Naming; |
7 | import java.rmi.NotBoundException; |
8 | import java.rmi.Remote; |
9 | import java.rmi.RemoteException; |
10 | import java.rmi.registry.LocateRegistry; |
11 | import java.rmi.registry.Registry; |
12 | import java.rmi.server.UnicastRemoteObject; |
13 | import java.util.Scanner; |
14 | |
15 | import de.uka.ipd.sdq.prototype.framework.AbstractScenarioThread; |
16 | |
17 | public class RmiRegistry extends UnicastRemoteObject implements IRmiRegistry, Serializable { |
18 | |
19 | /** |
20 | * |
21 | */ |
22 | private static final long serialVersionUID = 1L; |
23 | |
24 | public static final String LOCALHOST = "localhost"; |
25 | |
26 | private static RmiRegistry singleton; |
27 | |
28 | protected static org.apache.log4j.Logger logger = org.apache.log4j.Logger |
29 | .getLogger(AbstractScenarioThread.class); |
30 | |
31 | private RmiRegistry() throws RemoteException |
32 | { |
33 | // Locate java RMI registry or create one |
34 | try { |
35 | LocateRegistry.createRegistry(Registry.REGISTRY_PORT); |
36 | logger.info("Java RMI registry started"); |
37 | |
38 | } catch (RemoteException e) { |
39 | LocateRegistry.getRegistry(Registry.REGISTRY_PORT); |
40 | logger.info("Java RMI registry already running"); |
41 | } |
42 | |
43 | // Create registry binding service if it doesn't exist yet |
44 | try { |
45 | Naming.lookup("//localhost/" + PCM_RMI_REGISTRY); |
46 | |
47 | logger.info("RMI binding service already running"); |
48 | System.exit(0); |
49 | |
50 | } catch (MalformedURLException e) { |
51 | } catch (NotBoundException e) { |
52 | logger.info("Starting RMI binding service"); |
53 | |
54 | try { |
55 | Naming.bind(PCM_RMI_REGISTRY, this); |
56 | } catch (MalformedURLException e2) { |
57 | logger.error("RMI registry failed", e2); |
58 | } catch (RemoteException e2) { |
59 | logger.error("RMI registry failed", e2); |
60 | } catch (AlreadyBoundException e2) { |
61 | logger.error("RMI registry failed: Registry already bound on this port", e2); |
62 | } |
63 | } |
64 | } |
65 | |
66 | /* (non-Javadoc) |
67 | * @see de.uka.ipd.sdq.prototype.framework.IRmiRegistry#bindComponent(java.lang.String, java.rmi.server.UnicastRemoteObject) |
68 | */ |
69 | public void bindComponent(String name, Remote component) throws RemoteException |
70 | { |
71 | logger.info("Binding " + name + " to RMI registry."); |
72 | try { |
73 | java.rmi.Naming.rebind(name, component); |
74 | } catch (MalformedURLException e) { |
75 | logger.error("RMI registry failed", e); |
76 | // } catch (AlreadyBoundException e) { |
77 | // logger.error("RMI Registry failed: Component " + name + " already bound",e); |
78 | } |
79 | } |
80 | |
81 | public static void startRegistry() |
82 | { |
83 | if (singleton == null) { |
84 | try { |
85 | singleton = new RmiRegistry(); |
86 | } catch (RemoteException e) { |
87 | logger.error("RMI Registry failed", e); |
88 | } |
89 | } |
90 | } |
91 | |
92 | /** |
93 | * Registers a component to a RMI registry at the given IP. |
94 | * @param registryIP IP of the registry |
95 | * @param component instance of the component |
96 | * @param componentName unique name of the component |
97 | */ |
98 | public static void registerComponent(String registryIP, java.rmi.Remote component, String componentName) { |
99 | |
100 | try { |
101 | Registry reg = LocateRegistry.getRegistry(registryIP); |
102 | |
103 | de.uka.ipd.sdq.prototype.framework.utils.IRmiRegistry pcmRegistry = null; |
104 | |
105 | while (true) { |
106 | try { |
107 | pcmRegistry = (IRmiRegistry) reg.lookup(PCM_RMI_REGISTRY); |
108 | pcmRegistry.bindComponent(componentName, component); |
109 | break; |
110 | } catch (RemoteException e) { |
111 | logger.info("RMI registry not found. Next attempt in 3 seconds."); |
112 | Thread.sleep(3000); |
113 | } catch (NotBoundException e) { |
114 | logger.info("RMI binding service not found. Next attempt in 3 seconds."); |
115 | Thread.sleep(3000); |
116 | } |
117 | } |
118 | |
119 | logger.info(componentName + " bound in registry"); |
120 | |
121 | } catch (Exception e) { |
122 | logger.error(componentName + " err: " + e.getMessage()); |
123 | } |
124 | |
125 | } |
126 | |
127 | /** |
128 | * Returns an IP from an argument string array or LOCALHOST instead |
129 | * @param args |
130 | * @return |
131 | */ |
132 | public static String getIpFromArguments(String[] args) { |
133 | if (args != null && args.length > 0) { |
134 | return args[0]; |
135 | } |
136 | return LOCALHOST; |
137 | } |
138 | |
139 | /** |
140 | * |
141 | * @param name |
142 | * @return |
143 | */ |
144 | public static Remote lookup(String name) { |
145 | Remote result = null; |
146 | |
147 | while (true) { |
148 | try { |
149 | |
150 | result = java.rmi.Naming.lookup(name); |
151 | |
152 | } catch (java.net.MalformedURLException e) { |
153 | logger.error("Remote URI malformed. This should never happen, strange model names used?"); |
154 | } catch (java.rmi.RemoteException e) { |
155 | logger.error("Error while waiting for system. " + e); |
156 | } catch (java.rmi.NotBoundException e) { |
157 | logger.info("System missing: " + e.getMessage()); |
158 | try { |
159 | Thread.sleep(3000); |
160 | } catch (InterruptedException innerE) { |
161 | logger.error("Error while waiting for system. " + e); |
162 | } |
163 | continue; |
164 | } |
165 | |
166 | return result; |
167 | } |
168 | } |
169 | |
170 | |
171 | public static void main(String[] args) |
172 | { |
173 | startRegistry(); |
174 | |
175 | logger.info("RMI registry started on port " + Registry.REGISTRY_PORT); |
176 | |
177 | new Thread() { |
178 | public void run() { |
179 | Scanner input = new Scanner(System.in); |
180 | if (input.hasNext()) { |
181 | input.nextLine(); |
182 | } |
183 | } |
184 | }.run(); |
185 | } |
186 | } |