KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > components > ComponentManager


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.components;
26
27 import java.io.IOException JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * This class defines a ComponentManager
35  *
36  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
37  * @version 1.0
38  */

39 public abstract class ComponentManager implements ComponentManagerInterface
40 {
41   protected Hashtable JavaDoc processes = new Hashtable JavaDoc();
42   
43   
44   /**
45    * Instaciate a process managed by this component manager on the given port
46    * This used the default configuration file returned by the
47    * <code>getDefaultConfigurationFile</code> method
48    * @param port port to start the process on
49    * @return a reference to the newly started component
50    * @throws Exception if fails
51    */

52   public ComponentInterface instanciateProcess(String JavaDoc port) throws Exception JavaDoc
53   {
54     return instanciateProcess(port,getDefaultConfigurationFile());
55   }
56   
57   /**
58    * Instaciate a process managed by this component manager on the given port
59    *
60    * @param port port to start the process on
61    * @param configurationFile used to instanciate the process
62    * @return a reference to the newly started component
63    * @throws Exception if fails
64    */

65   public abstract ComponentInterface instanciateProcess(String JavaDoc port,String JavaDoc configurationFile) throws Exception JavaDoc;
66   
67   /**
68    * The default configuration file to use with this component manager.
69    * This should be specific to each component manager
70    *
71    * @return the default filename
72    */

73   public abstract String JavaDoc getDefaultConfigurationFile();
74   
75   
76   
77   /**
78    * Starts component on the given port with the given database
79    *
80    * @param port to run hsql on
81    * @param database to load component with
82    * @return created process
83    * @throws Exception if fails to create process
84    */

85   public ComponentInterface startComponent(String JavaDoc port, String JavaDoc database) throws Exception JavaDoc
86   {
87     return start(port, database);
88   }
89
90   /**
91    * Starts component on the given port
92    *
93    * @param port to run component on
94    * @return created process
95    * @throws Exception if fails to create process
96    */

97   public ComponentInterface startComponent(String JavaDoc port) throws Exception JavaDoc
98   {
99     return start(port);
100   }
101
102   /**
103    * Check if can open a connection on localhost on the given port
104    *
105    * @param port to open a socket for check
106    * @throws Exception if fails
107    */

108   public void waitForStarted(String JavaDoc port) throws Exception JavaDoc
109   {
110     int retry = 10;
111     while (!isStarted(port))
112     {
113       retry--;
114       if (retry == 0)
115         throw new IOException JavaDoc("I think the component is not started");
116       synchronized (this)
117       {
118         this.wait(1000);
119       }
120     }
121   }
122
123   /**
124    * Wait for the component to stop
125    *
126    * @param port to check the connection on
127    * @throws Exception if fails
128    */

129   public void waitForStopped(String JavaDoc port) throws Exception JavaDoc
130   {
131     int retry = 5;
132     while (isStarted(port))
133     {
134       retry--;
135       if (retry == 0)
136         throw new IOException JavaDoc("I think the component is still started");
137       synchronized (this)
138       {
139         this.wait(2000);
140       }
141     }
142   }
143
144   /**
145    * Check if can open a connection on localhost on the given port
146    *
147    * @param port to open a socket for check
148    * @return true if can connect, false if exception or can't connect
149    */

150   public boolean isStarted(String JavaDoc port)
151   {
152     try
153     {
154       Socket JavaDoc socket = new Socket JavaDoc(InetAddress.getLocalHost().getHostAddress(),
155           Integer.parseInt(port));
156       socket.close();
157       return true;
158     }
159     catch (Exception JavaDoc e)
160     {
161       return false;
162     }
163   }
164
165   /**
166    * fill the database with raidb1 default configuration
167    *
168    * @param port of the database
169    * @throws Exception if fails
170    */

171   public void loaddatabase(String JavaDoc port) throws Exception JavaDoc
172   {
173     ((ComponentInterface) processes.get(port)).loadDatabase();
174   }
175   
176   /**
177    * Load the database with a given input file
178    *
179    * @param port of the database to load
180    * @param templateName name of the file to load(NO PATH!)
181    * @throws Exception if fails
182    */

183   public void loaddatabase(String JavaDoc port, String JavaDoc templateName) throws Exception JavaDoc
184   {
185     ((ComponentInterface) processes.get(port)).loadDatabase(templateName);
186   }
187   
188   /**
189    * Load the database with a given input file
190    *
191    * @param port of the database to load
192    * @param templateName name of the file to load(NO PATH!)
193    * @param target the target database to load
194    * @throws Exception if fails
195    */

196   public void loaddatabase(String JavaDoc port, String JavaDoc templateName,String JavaDoc target) throws Exception JavaDoc
197   {
198     ((ComponentInterface) processes.get(port)).loadDatabase(templateName,target);
199   }
200   
201   /**
202    * Stop Hsql. Destroy the process so it looks like a failure.
203    *
204    * @param process to stop
205    */

206   public void stop(ComponentInterface process)
207   {
208     // Kill the process and remove files
209
if (process != null)
210     {
211       processes.remove(process.getPort());
212       process.release();
213     }
214     try
215     {
216       waitForStopped(process.getPort());
217     }
218     catch (Exception JavaDoc e)
219     {
220       e.printStackTrace();
221     }
222   }
223   
224   /**
225    * Stop a component from its unique port number
226    *
227    * @param componentOnPort port number of the component to stop
228    */

229   public void stop(String JavaDoc componentOnPort)
230   {
231     stop((ComponentInterface)processes.get(componentOnPort));
232   }
233   
234   /**
235    * Same as stop(String)
236    *
237    * @param port port number
238    */

239   public void stop(int port)
240   {
241     stop(""+port);
242   }
243   
244   /**
245    * Stops all process contained in this manager
246    */

247   public void stopAll()
248   {
249    this.release();
250   }
251   
252   /**
253    * Starts database component on the given port with the given database
254    *
255    * @param port to run component on
256    * @param database to load component with
257    * @return created process
258    * @throws Exception if fails to create process
259    */

260   public ComponentInterface start(String JavaDoc port, String JavaDoc database) throws Exception JavaDoc
261   {
262     ComponentInterface hs = instanciateProcess(port, database);
263     waitForStarted(port);
264     processes.put(port, hs);
265     return hs;
266   }
267   
268   /**
269    *
270    * Starts database component on the given port with the default database
271    *
272    * @param port to run component on
273    * @return created process
274    * @throws Exception if fails to create process
275    */

276   public ComponentInterface start(String JavaDoc port) throws Exception JavaDoc
277   {
278     return start(port,getDefaultConfigurationFile());
279   }
280   
281
282   /**
283    * release files locked by manager
284    */

285   public void release()
286   {
287     Iterator JavaDoc iter = processes.keySet().iterator();
288     ComponentInterface component = null;
289     while (iter.hasNext())
290     {
291       try
292       {
293         component = ((ComponentInterface) processes.get(iter.next()));
294         component.release();
295       }
296       catch(Exception JavaDoc e)
297       {
298         e.printStackTrace();
299       }
300     }
301   }
302   
303   /**
304    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentManagerInterface#simulateFailure(int, long, boolean)
305    */

306   public void simulateFailure(final int port, final long wait, boolean rand)
307   {
308     simulateFailure(""+port,wait,rand);
309   }
310   
311   /**
312    * @see org.objectweb.cjdbc.scenario.tools.components.ComponentManagerInterface#simulateFailure(java.lang.String, long, boolean)
313    */

314   public void simulateFailure(final String JavaDoc port, final long wait, final boolean rand)
315   {
316     Thread JavaDoc t = new Thread JavaDoc()
317     {
318       public void run()
319       {
320         synchronized(this)
321         {
322           try
323           {
324             wait(wait);
325           }
326           catch (InterruptedException JavaDoc e)
327           {
328             e.printStackTrace();
329           }
330           ((ComponentInterface)processes.get(port)).release();
331         }
332       }
333     };
334     t.start();
335   }
336 }
Popular Tags