KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jaxr > juddi > JUDDIService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.jaxr.juddi;
23
24 import org.apache.ws.scout.registry.ConnectionFactoryImpl;
25 import org.jboss.system.ServiceMBeanSupport;
26
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30 import javax.xml.registry.ConnectionFactory JavaDoc;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.sql.Connection JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.Statement JavaDoc;
38
39
40 /**
41  * A JBoss MBean for the JUDDI Service
42  * MBean for the JUDDI open source project integrated as part of the JAXR requirements
43  * for J2EE 1.4 compliance
44  *
45  * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
46  * extends="org.jboss.system.ServiceMBean"
47  * @since Nov 8, 2004
48  */

49 public class JUDDIService extends ServiceMBeanSupport
50         implements JUDDIServiceMBean
51 {
52     protected boolean dropOnStart = false;
53     protected boolean createOnStart = false;
54     protected boolean dropOnStop = false;
55     protected boolean loadNAICS = false;
56     protected boolean loadUNSPSC = false;
57
58     protected DataSource JavaDoc datasource = null;
59
60     protected String JavaDoc datasourceurl = null;
61
62     protected String JavaDoc registryOperator = null;
63
64     protected String JavaDoc bindJaxr = null;
65     protected boolean shouldBindJaxr = true;
66     protected boolean dropDB = false;
67
68     private boolean jndiAlreadyBound = false;
69
70     protected Connection JavaDoc getConnection()
71             throws SQLException JavaDoc, NamingException JavaDoc
72     {
73         try
74         {
75             if (datasource == null)
76             {
77                 InitialContext JavaDoc ctx = new InitialContext JavaDoc();
78                 Object JavaDoc obj = ctx.lookup(datasourceurl);
79                 log.debug(obj.getClass().getName());
80                 datasource = (DataSource JavaDoc) obj;
81             }//end if
82
} catch (Exception JavaDoc e)
83         {
84             e.printStackTrace();
85             log.error(e);
86         }
87         return datasource.getConnection();
88     }
89
90     protected void runDrop()
91             throws SQLException JavaDoc, IOException JavaDoc
92     {
93         log.debug("JUDDIService: Inside runDrop");
94
95         locateAndRunScript("juddi_drop_db.ddl");
96         log.debug("JUDDIService: Exit runDrop");
97     }
98
99     protected void runCreate()
100             throws SQLException JavaDoc, IOException JavaDoc
101     {
102         log.debug("JUDDIService: Inside runCreate");
103         locateAndRunScript("juddi_create_db.ddl");
104         locateAndRunScript("juddi_data.ddl");
105     }
106
107     private void locateAndRunScript(String JavaDoc name)
108             throws SQLException JavaDoc, IOException JavaDoc
109     {
110         log.debug("JUDDIService: Inside locateScript");
111         InputStream JavaDoc input =
112                 getClass().getClassLoader().getResourceAsStream("META-INF/ddl/" + name);
113         if (input != null)
114         {
115             try
116             {
117                 runScript(input);
118             } finally
119             {
120                 input.close();
121             }
122         }
123     }
124
125     protected void runScript(InputStream JavaDoc stream)
126             throws SQLException JavaDoc, IOException JavaDoc
127     {
128         boolean firstError = true;
129         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stream));
130
131         Connection JavaDoc connection = null;
132         try
133         {
134             connection = this.getConnection();
135             if (connection != null)
136                 log.debug("Obtained the Connection");
137             Statement JavaDoc statement = connection.createStatement();
138             try
139             {
140                 String JavaDoc nextStatement = "";
141                 String JavaDoc nextLine;
142                 while ((nextLine = reader.readLine()) != null)
143                 {
144                     log.debug("Statement Obtained=" + nextLine);
145                     nextLine = nextLine.trim();
146                     if (nextLine.indexOf("--") != -1) continue;
147                     int semicolon = nextLine.indexOf(";");
148                     if (semicolon != -1)
149                     {
150                         nextStatement += nextLine.substring(0, semicolon);
151                         try
152                         {
153                             log.debug("Statement to execute:" + nextStatement);
154                             statement.execute(nextStatement);
155                         } catch (SQLException JavaDoc e)
156                         {
157                             String JavaDoc err = "Could not execute a statement of juddi script::";
158
159                             if (firstError)
160                             {
161                                 log.debug(err + e.getLocalizedMessage() + " " + nextStatement);
162                                 log.debug("Your settings are:dropOnStart =" + dropOnStart + ";createOnStart =" + createOnStart);
163                                 log.debug("dropOnStop = " + dropOnStop);
164
165                                 firstError = false;
166                             }
167                         }
168                         nextStatement = nextLine.substring(semicolon + 1);
169                     } else
170                     {
171                         nextStatement += nextLine;
172                     }
173                 }
174                 if (!nextStatement.equals(""))
175                 {
176                     try
177                     {
178                         log.debug("Statement to execute:" + nextStatement);
179                         statement.execute(nextStatement);
180                     } catch (SQLException JavaDoc e)
181                     {
182                         log.debug("Could not execute last statement of a juddi init script: " + e.getLocalizedMessage());
183                         log.debug("Your settings are:dropOnStart =" + dropOnStart + ";createOnStart =" + createOnStart);
184                         log.debug("dropOnStop = " + dropOnStop);
185                     }
186                 }
187             } finally
188             {
189                 if (statement != null) statement.close();
190             }
191         } catch (NamingException JavaDoc nm)
192         {
193             log.error("Looks like DataSource was tried with the wrong JNDI name");
194             log.error(nm);
195         } finally
196         {
197             if (connection != null) connection.close();
198         }
199     }
200
201     /**
202      * starts the jUDDI service
203      */

204     public void startService() throws Exception JavaDoc
205     {
206         log.debug("JUDDIService: Inside startService with dropOnStart="
207                 + dropOnStart + " createOnStart=" + createOnStart);
208         if (shouldBindJaxr && !jndiAlreadyBound)
209         {
210             bindJAXRConnectionFactory();
211         }
212
213         if (dropOnStart)
214         {
215             runDrop();
216         }
217         if (createOnStart)
218         {
219             runCreate();
220         }
221     }
222
223     /**
224      * stop the service
225      */

226     public void stopService()
227             throws Exception JavaDoc
228     {
229         log.debug("JUDDIService: Inside stopService with dropOnStop=" + dropOnStop);
230         if (dropOnStop)
231         {
232             runDrop();
233         }
234         unBindJAXRConnectionFactory();
235     }
236
237
238     /**
239      * @return boolean
240      */

241     public boolean isDropOnStart()
242     {
243         return dropOnStart;
244     }
245
246     /**
247      * Sets the dropOnStart.
248      *
249      * @param dropOnStart The dropOnStart to set
250      */

251     public void setDropOnStart(boolean dropOnStart)
252     {
253         this.dropOnStart = dropOnStart;
254     }
255
256     /**
257      * @return boolean
258      */

259     public boolean isDropOnStop()
260     {
261         return dropOnStop;
262     }
263
264     /**
265      * Sets the dropOnStop.
266      *
267      * @param dropOnStop The dropOnStop to set
268      */

269     public void setDropOnStop(boolean dropOnStop)
270     {
271         this.dropOnStop = dropOnStop;
272     }
273
274     /**
275      * @return boolean
276      */

277     public boolean isCreateOnStart()
278     {
279         return createOnStart;
280     }
281
282     /**
283      * Sets the createOnStart.
284      *
285      * @param createOnStart The createOnStart to set
286      */

287     public void setCreateOnStart(boolean createOnStart)
288     {
289         this.createOnStart = createOnStart;
290     }
291
292     /**
293      * @return String
294      */

295     public String JavaDoc getDataSource()
296     {
297         return datasourceurl;
298     }
299
300     /**
301      * Sets the Datasource Url.
302      *
303      * @param ds The datasourceurl to set
304      */

305     public void setDataSourceUrl(String JavaDoc ds)
306     {
307         this.datasourceurl = ds;
308     }
309
310     /**
311      * @return String
312      */

313     public String JavaDoc getRegistryOperator()
314     {
315         return registryOperator;
316     }
317
318     /**
319      * Sets the RegistryOperator.
320      *
321      * @param ro The datasourceurl to set
322      */

323     public void setRegistryOperator(String JavaDoc ro)
324     {
325         this.registryOperator = ro;
326     }
327
328     /**
329      * gets the JAXR ConnectionFactory.
330      */

331     public String JavaDoc getBindJaxr()
332     {
333         return bindJaxr;
334     }
335
336
337     /**
338      * Sets the JAXR ConnectionFactory.
339      *
340      * @param str The context to bind the Jaxr factory to set
341      */

342     public void setBindJaxr(String JavaDoc str)
343     {
344         this.bindJaxr = str;
345
346         if(this.shouldBindJaxr)
347         {
348            bindJAXRConnectionFactory();
349         }
350     }
351
352
353     /**
354      * gets the JAXR ConnectionFactory.
355      */

356     public boolean getShouldBindJaxr()
357     {
358         return shouldBindJaxr;
359     }
360
361     /**
362      * Sets the JAXR ConnectionFactory.
363      *
364      * @param str Should a Jaxr Connection Factory bound
365      */

366     public void setShouldBindJaxr(boolean str)
367     {
368         this.shouldBindJaxr = str;
369         if (shouldBindJaxr)
370         {
371             bindJAXRConnectionFactory();
372         }
373     }
374
375     /**
376      * gets the JAXR ConnectionFactory.
377      */

378     public boolean getDropDB()
379     {
380         return dropDB;
381     }
382
383     /**
384      * Sets the JAXR ConnectionFactory.
385      *
386      * @param b Should a Jaxr Connection Factory bound
387      */

388     public void setDropDB(boolean b)
389     {
390         this.dropDB = b;
391         try
392         {
393             if (datasource != null) this.runDrop();
394         } catch (Exception JavaDoc e)
395         {
396             log.error(e.toString());
397         }
398     }
399
400     private void bindJAXRConnectionFactory()
401     {
402         if( this.bindJaxr == null || jndiAlreadyBound ) return;//We will wait for it to be set
403

404         //Bind a ConnectionFactory object to JNDI
405
InitialContext JavaDoc ctx = null;
406         try
407         {
408             ctx = new InitialContext JavaDoc();
409         } catch (NamingException JavaDoc e)
410         {
411             log.error("JNDI InitialContext Failed");
412             e.printStackTrace();
413         }
414         ConnectionFactory JavaDoc factory = (ConnectionFactory JavaDoc) ConnectionFactoryImpl.newInstance();
415
416         try
417         {
418             ctx.rebind(bindJaxr, factory);
419             jndiAlreadyBound = true;
420         } catch (NamingException JavaDoc e)
421         {
422             log.error("JNDI Bind Failed:" + bindJaxr);
423             e.printStackTrace();
424         }
425
426     }
427
428     private void unBindJAXRConnectionFactory()
429     {
430         if( this.bindJaxr == null || jndiAlreadyBound ) return;//We will wait for it to be set
431

432         //Bind a ConnectionFactory object to JNDI
433
InitialContext JavaDoc ctx = null;
434         try
435         {
436             ctx = new InitialContext JavaDoc();
437         } catch (NamingException JavaDoc e)
438         {
439             log.error("JNDI InitialContext Failed");
440             e.printStackTrace();
441         }
442         try
443         {
444             ctx.unbind(bindJaxr);
445         } catch (NamingException JavaDoc e)
446         {
447             //Ignore
448
}
449
450     }
451 }
452
453
Popular Tags