1 19 20 package org.netbeans.modules.j2ee.oc4j.config; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import javax.enterprise.deploy.spi.DeploymentManager ; 28 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 29 import javax.management.MBeanServerConnection ; 30 import javax.management.ObjectInstance ; 31 import javax.management.ObjectName ; 32 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 33 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 34 import org.netbeans.modules.j2ee.deployment.plugins.api.DatasourceManager; 35 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager; 36 import org.netbeans.modules.j2ee.oc4j.config.gen.ConnectionPool; 37 import org.netbeans.modules.j2ee.oc4j.config.gen.DataSources; 38 import org.netbeans.modules.j2ee.oc4j.config.gen.ManagedDataSource; 39 import org.netbeans.modules.j2ee.oc4j.config.gen.NativeDataSource; 40 import org.netbeans.modules.j2ee.oc4j.ide.OC4JErrorManager; 41 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils; 42 import org.openide.ErrorManager; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.util.NbBundle; 46 47 51 public class OC4JDatasourceManager implements DatasourceManager { 52 53 private static final String DATA_SOURCES_XML = "data-sources.xml"; private static final String CONFIG_DIR = File.separator + "j2ee" + File.separator + 55 "home" + File.separator + "config"; 57 private OC4JDeploymentManager dm; 59 60 private FileObject serverDir; 62 63 64 public OC4JDatasourceManager(DeploymentManager dm) { 65 if (!(dm instanceof OC4JDeploymentManager)) { 66 throw new IllegalArgumentException ("Only OC4JDeplomentManager is supported"); } 68 69 this.dm = (OC4JDeploymentManager) dm; 70 71 serverDir = FileUtil.toFileObject(new File (this.dm.getProperties().getOC4JHomeLocation())); 72 } 73 74 public Set <Datasource> getDatasources() { 75 if(OC4JPluginUtils.isLocalServer(dm.getInstanceProperties())) 76 return getLocalDatasources(); 77 78 return getRemoteDatasources(); 79 } 80 81 public void deployDatasources(Set <Datasource> datasources) throws ConfigurationException , 82 DatasourceAlreadyExistsException { 83 Set <Datasource> deployedDS = getDatasources(); 85 86 for (Datasource d : datasources) { 87 if(deployedDS.contains(d)) 88 continue; 89 90 OC4JDatasource ds = (OC4JDatasource) d; 91 92 try { 93 String cpName = ds.getJndiName() + " Connection Pool"; 94 String cpClazz = ds.getDriverClassName(); 95 String cpUsername = ds.getUsername(); 96 String cpPassword = ds.getPassword(); 97 String cpUrl = ds.getUrl(); 98 99 deployConnectionPool(cpName, cpClazz, cpUsername, cpPassword, cpUrl); 101 102 String mdsJndiName = ds.getJndiName(); 103 Integer mdsIdleTimeout = new Integer (ds.getIdleTimeoutMinutes()); 104 105 deployManagedDataSource(mdsJndiName, cpName, mdsIdleTimeout); 107 108 } catch(Exception e) { 109 OC4JErrorManager.getInstance(dm).error(dm.getUri(), e, OC4JErrorManager.GENERIC_FAILURE); 110 } 111 } 112 } 113 114 123 public void deployConnectionPool(String name, String clazz, String username, String password, String url) { 124 if (null == name || null == clazz || null == username || 125 null == password || null == url) 126 throw new NullPointerException (); 127 128 try { 129 if (OC4JPluginUtils.isLocalServer(dm.getInstanceProperties()) && 131 !OC4JPluginUtils.checkClass(clazz, dm)) 132 return; 133 134 MBeanServerConnection server = dm.getJMXConnector(); 136 137 Iterator i = server.queryMBeans(new ObjectName ("oc4j:j2eeType=JDBCResource,*"), null).iterator(); 139 140 while(i.hasNext()) { 141 ObjectName elem = ((ObjectInstance ) i.next()).getObjectName(); 142 String s = elem.getKeyProperty("name").substring(1, elem.getKeyProperty("name").length()-1); 143 144 if (name.equals(s)) 145 return; 146 } 147 148 Iterator j = server.queryMBeans(new ObjectName ("oc4j:j2eeType=J2EEApplication,name=default,J2EEServer=standalone"), null).iterator(); 149 ObjectName elem = ((ObjectInstance ) j.next()).getObjectName(); 150 151 server.invoke(elem, "createJDBCConnectionPool", new Object [] { 153 name, clazz, username, password, url}, new String [] { 154 "java.lang.String", 155 "java.lang.String", 156 "java.lang.String", 157 "java.lang.String", 158 "java.lang.String"}); 159 } catch (Exception e) { 160 OC4JErrorManager.getInstance(dm).error(dm.getUri(), e, OC4JErrorManager.GENERIC_FAILURE); 161 } 162 } 163 164 171 public void deployManagedDataSource(String jndiName, String connectionPoolName, Integer idleTimeout) { 172 if (null == jndiName || null == connectionPoolName || null == idleTimeout) 173 throw new NullPointerException (); 174 175 try { 176 MBeanServerConnection server = dm.getJMXConnector(); 178 179 Iterator i = server.queryMBeans(new ObjectName ("oc4j:j2eeType=JDBCDataSource,*"), null).iterator(); 181 182 while(i.hasNext()) { 183 ObjectName elem = ((ObjectInstance ) i.next()).getObjectName(); 184 String name = elem.getKeyProperty("name").substring(1, elem.getKeyProperty("name").length()-1); 185 String pool = elem.getKeyProperty("JDBCResource").substring(1, elem.getKeyProperty("JDBCResource").length()-1); 186 187 if(pool.length() == 0) 188 continue; 189 190 String s = (String ) server.getAttribute(elem, "jndiName"); 191 192 if (jndiName.equals(s) || jndiName.equals(name)) 193 return; 194 } 195 196 Iterator j = server.queryMBeans(new ObjectName ("oc4j:j2eeType=JDBCResource,name=\"" + connectionPoolName + "\",*"), null).iterator(); 198 199 if (!j.hasNext()) 200 return; 201 202 Iterator k = server.queryMBeans(new ObjectName ("oc4j:j2eeType=J2EEApplication,name=default,J2EEServer=standalone"), null).iterator(); 203 ObjectName elem = ((ObjectInstance ) k.next()).getObjectName(); 204 205 server.invoke(elem, "createManagedDataSource", new Object [] { 207 jndiName, "", "", jndiName, idleTimeout, connectionPoolName, "", 208 ""}, new String [] { 209 "java.lang.String", 210 "java.lang.String", 211 "java.lang.String", 212 "java.lang.String", 213 "java.lang.Integer", 214 "java.lang.String", 215 "java.lang.String", 216 "java.lang.String",}); 217 } catch(Exception e) { 218 OC4JErrorManager.getInstance(dm).error(dm.getUri(), e, OC4JErrorManager.GENERIC_FAILURE); 219 } 220 } 221 222 227 public void undeployNativeDataSource(String name) { 228 if (null == name) 229 throw new NullPointerException (); 230 231 try { 232 MBeanServerConnection server = dm.getJMXConnector(); 234 235 Iterator i = server.queryMBeans(new ObjectName ("oc4j:j2eeType=J2EEApplication,name=default,J2EEServer=standalone"), null).iterator(); 236 ObjectName elem = ((ObjectInstance ) i.next()).getObjectName(); 237 238 server.invoke(elem, "removeNativeDataSource", new Object [] { 240 name,}, new String [] {"java.lang.String"}); 241 } catch (Exception e) { 242 OC4JErrorManager.getInstance(dm).error(dm.getUri(), e, OC4JErrorManager.GENERIC_FAILURE); 243 } 244 } 245 246 public void undeployManagedDataSource(String name) { 247 if (null == name) 248 throw new NullPointerException (); 249 250 try { 251 MBeanServerConnection server = dm.getJMXConnector(); 253 254 Iterator i = server.queryMBeans(new ObjectName ("oc4j:j2eeType=J2EEApplication,name=default,J2EEServer=standalone"), null).iterator(); 255 ObjectName elem = ((ObjectInstance ) i.next()).getObjectName(); 256 257 server.invoke(elem, "removeManagedDataSource", new Object [] { 259 name,}, new String [] {"java.lang.String"}); 260 } catch (Exception e) { 261 OC4JErrorManager.getInstance(dm).error(dm.getUri(), e, OC4JErrorManager.GENERIC_FAILURE); 262 } 263 } 264 265 public void undeployConnectionPool(String name) { 266 if (null == name) 267 throw new NullPointerException (); 268 269 try { 270 MBeanServerConnection server = dm.getJMXConnector(); 272 273 Iterator i = server.queryMBeans(new ObjectName ("oc4j:j2eeType=J2EEApplication,name=default,J2EEServer=standalone"), null).iterator(); 274 ObjectName elem = ((ObjectInstance ) i.next()).getObjectName(); 275 276 Iterator j = server.queryMBeans(new ObjectName ("oc4j:j2eeType=JDBCResource,name=\"" + name + "\",*"), null).iterator(); 278 ObjectName pool = ((ObjectInstance ) j.next()).getObjectName(); 279 String [] array = (String []) server.getAttribute(pool, "jdbcDataSources"); 280 281 for (String s : array) { 282 String dsName = new ObjectName (s).getKeyProperty("name"); 283 284 dsName = dsName.substring(1, dsName.length() - 1); 286 287 undeployManagedDataSource(dsName); 289 } 290 291 server.invoke(elem, "removeDataSourceConnectionPool", new Object [] { 293 name,}, new String [] {"java.lang.String"}); 294 } catch (Exception e) { 295 OC4JErrorManager.getInstance(dm).error(dm.getUri(), e, OC4JErrorManager.GENERIC_FAILURE); 296 } 297 } 298 299 private Set <Datasource> getLocalDatasources() { 300 301 Set <Datasource> globalDS = new HashSet <Datasource>(); 302 303 if (serverDir == null || !serverDir.isValid() || !serverDir.isFolder() || !serverDir.canRead()) { 304 ErrorManager.getDefault().log(ErrorManager.USER, 305 NbBundle.getMessage(OC4JDatasourceManager.class, "ERR_WRONG_DEPLOY_DIR")); 306 return globalDS; 307 } 308 309 File dsFile = new File (FileUtil.toFile(serverDir), CONFIG_DIR + File.separator + DATA_SOURCES_XML); 310 311 if(dsFile.exists()) { 312 try { 313 DataSources datasources = DataSources.createGraph(dsFile); 314 315 NativeDataSource nds[] = datasources.getNativeDataSource(); 316 for (NativeDataSource ds : nds) { 317 if (ds.getJndiName().length() > 0) { 318 globalDS.add(new OC4JDatasource( 319 ds.getJndiName(), 320 ds.getUrl(), 321 ds.getUser(), 322 ds.getPassword(), 323 ds.getDataSourceClass())); 324 } 325 } 326 327 ManagedDataSource mds[] = datasources.getManagedDataSource(); 328 for (ManagedDataSource ds : mds) { 329 if (ds.getJndiName().length() > 0) { 330 String cpName = ds.getConnectionPoolName(); 331 ConnectionPool[] cps = datasources.getConnectionPool(); 332 for (ConnectionPool cp : cps) { 333 if(cpName.equals(cp.getName())) { 334 globalDS.add(new OC4JDatasource( 335 ds.getJndiName(), 336 cp.getConnectionFactory().getUrl(), 337 cp.getConnectionFactory().getUser(), 338 cp.getConnectionFactory().getPassword(), 339 cp.getConnectionFactory().getFactoryClass())); 340 } 341 } 342 } 343 } 344 } catch(IOException ex) { 345 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 346 } 347 } 348 349 return globalDS; 350 } 351 352 private Set <Datasource> getRemoteDatasources() { 353 Set <Datasource> datasources = new HashSet <Datasource>(); 354 355 MBeanServerConnection serverConnection = dm.getJMXConnector(); 356 357 try { 358 Iterator i = serverConnection.queryMBeans(new ObjectName ("oc4j:j2eeType=JDBCDataSource,*"), null).iterator(); 359 while(i.hasNext()) { 360 ObjectName dsElem = ((ObjectInstance ) i.next()).getObjectName(); 361 362 String jndiName = (String ) serverConnection.getAttribute(dsElem, "jndiName"); 363 String pool = dsElem.getKeyProperty("JDBCResource").substring(1, dsElem.getKeyProperty("JDBCResource").length()-1); 364 String username = (String ) serverConnection.getAttribute(dsElem, "user"); 365 String password = (String ) serverConnection.getAttribute(dsElem, "password"); 366 367 Iterator j = serverConnection.queryMBeans(new ObjectName ("oc4j:j2eeType=JDBCResource,name=\""+pool+"\",*"), null).iterator(); 368 369 if(!j.hasNext()) 370 continue; 371 372 ObjectName poolElem = ((ObjectInstance ) j.next()).getObjectName(); 373 374 String url = (String ) serverConnection.getAttribute(poolElem, "url"); 375 String factoryClassName = (String ) serverConnection.getAttribute(poolElem, "factoryClass"); 376 377 Datasource ds = new OC4JDatasource(jndiName, url, username, password, factoryClassName); 378 379 datasources.add(ds); 380 } 381 } catch(Exception ex) { 382 } 384 385 return datasources; 386 } 387 } | Popular Tags |