1 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 ; 28 import javax.naming.NamingException ; 29 import javax.sql.DataSource ; 30 import javax.xml.registry.ConnectionFactory ; 31 import java.io.BufferedReader ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.InputStreamReader ; 35 import java.sql.Connection ; 36 import java.sql.SQLException ; 37 import java.sql.Statement ; 38 39 40 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 datasource = null; 59 60 protected String datasourceurl = null; 61 62 protected String registryOperator = null; 63 64 protected String bindJaxr = null; 65 protected boolean shouldBindJaxr = true; 66 protected boolean dropDB = false; 67 68 private boolean jndiAlreadyBound = false; 69 70 protected Connection getConnection() 71 throws SQLException , NamingException 72 { 73 try 74 { 75 if (datasource == null) 76 { 77 InitialContext ctx = new InitialContext (); 78 Object obj = ctx.lookup(datasourceurl); 79 log.debug(obj.getClass().getName()); 80 datasource = (DataSource ) obj; 81 } } catch (Exception e) 83 { 84 e.printStackTrace(); 85 log.error(e); 86 } 87 return datasource.getConnection(); 88 } 89 90 protected void runDrop() 91 throws SQLException , IOException 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 , IOException 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 name) 108 throws SQLException , IOException 109 { 110 log.debug("JUDDIService: Inside locateScript"); 111 InputStream 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 stream) 126 throws SQLException , IOException 127 { 128 boolean firstError = true; 129 BufferedReader reader = new BufferedReader (new InputStreamReader (stream)); 130 131 Connection connection = null; 132 try 133 { 134 connection = this.getConnection(); 135 if (connection != null) 136 log.debug("Obtained the Connection"); 137 Statement statement = connection.createStatement(); 138 try 139 { 140 String nextStatement = ""; 141 String 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 e) 156 { 157 String 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 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 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 204 public void startService() throws Exception 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 226 public void stopService() 227 throws Exception 228 { 229 log.debug("JUDDIService: Inside stopService with dropOnStop=" + dropOnStop); 230 if (dropOnStop) 231 { 232 runDrop(); 233 } 234 unBindJAXRConnectionFactory(); 235 } 236 237 238 241 public boolean isDropOnStart() 242 { 243 return dropOnStart; 244 } 245 246 251 public void setDropOnStart(boolean dropOnStart) 252 { 253 this.dropOnStart = dropOnStart; 254 } 255 256 259 public boolean isDropOnStop() 260 { 261 return dropOnStop; 262 } 263 264 269 public void setDropOnStop(boolean dropOnStop) 270 { 271 this.dropOnStop = dropOnStop; 272 } 273 274 277 public boolean isCreateOnStart() 278 { 279 return createOnStart; 280 } 281 282 287 public void setCreateOnStart(boolean createOnStart) 288 { 289 this.createOnStart = createOnStart; 290 } 291 292 295 public String getDataSource() 296 { 297 return datasourceurl; 298 } 299 300 305 public void setDataSourceUrl(String ds) 306 { 307 this.datasourceurl = ds; 308 } 309 310 313 public String getRegistryOperator() 314 { 315 return registryOperator; 316 } 317 318 323 public void setRegistryOperator(String ro) 324 { 325 this.registryOperator = ro; 326 } 327 328 331 public String getBindJaxr() 332 { 333 return bindJaxr; 334 } 335 336 337 342 public void setBindJaxr(String str) 343 { 344 this.bindJaxr = str; 345 346 if(this.shouldBindJaxr) 347 { 348 bindJAXRConnectionFactory(); 349 } 350 } 351 352 353 356 public boolean getShouldBindJaxr() 357 { 358 return shouldBindJaxr; 359 } 360 361 366 public void setShouldBindJaxr(boolean str) 367 { 368 this.shouldBindJaxr = str; 369 if (shouldBindJaxr) 370 { 371 bindJAXRConnectionFactory(); 372 } 373 } 374 375 378 public boolean getDropDB() 379 { 380 return dropDB; 381 } 382 383 388 public void setDropDB(boolean b) 389 { 390 this.dropDB = b; 391 try 392 { 393 if (datasource != null) this.runDrop(); 394 } catch (Exception e) 395 { 396 log.error(e.toString()); 397 } 398 } 399 400 private void bindJAXRConnectionFactory() 401 { 402 if( this.bindJaxr == null || jndiAlreadyBound ) return; 404 InitialContext ctx = null; 406 try 407 { 408 ctx = new InitialContext (); 409 } catch (NamingException e) 410 { 411 log.error("JNDI InitialContext Failed"); 412 e.printStackTrace(); 413 } 414 ConnectionFactory factory = (ConnectionFactory ) ConnectionFactoryImpl.newInstance(); 415 416 try 417 { 418 ctx.rebind(bindJaxr, factory); 419 jndiAlreadyBound = true; 420 } catch (NamingException 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; 432 InitialContext ctx = null; 434 try 435 { 436 ctx = new InitialContext (); 437 } catch (NamingException e) 438 { 439 log.error("JNDI InitialContext Failed"); 440 e.printStackTrace(); 441 } 442 try 443 { 444 ctx.unbind(bindJaxr); 445 } catch (NamingException e) 446 { 447 } 449 450 } 451 } 452 453 | Popular Tags |