1 19 24 25 package org.netbeans.modules.j2ee.sun.ide.sunresources.beans; 26 27 import java.io.File ; 28 import java.io.Writer ; 29 import java.io.FileInputStream ; 30 import java.io.OutputStreamWriter ; 31 import java.util.Arrays ; 32 import java.util.Collections ; 33 34 import java.util.Map ; 35 import java.util.List ; 36 import java.util.Vector ; 37 import java.util.ArrayList ; 38 import java.util.Properties ; 39 import java.text.MessageFormat ; 40 import java.util.HashMap ; 41 import java.util.HashSet ; 42 import java.util.ResourceBundle ; 43 44 import javax.management.Attribute ; 45 import javax.management.ObjectName ; 46 import javax.management.AttributeList ; 47 import org.netbeans.api.project.FileOwnerQuery; 48 import org.netbeans.api.project.Project; 49 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 50 import org.netbeans.modules.j2ee.sun.api.SunURIManager; 51 52 import org.openide.util.NbBundle; 53 import org.openide.ErrorManager; 54 55 import org.openide.filesystems.FileLock; 56 import org.openide.filesystems.FileUtil; 57 import org.openide.filesystems.FileObject; 58 import org.openide.filesystems.FileSystem; 59 60 import javax.enterprise.deploy.spi.DeploymentManager ; 61 import org.netbeans.api.db.explorer.ConnectionManager; 62 import org.netbeans.api.db.explorer.DatabaseConnection; 63 64 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePair; 65 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants; 66 import org.netbeans.modules.j2ee.sun.ide.editors.IsolationLevelEditor; 67 import org.netbeans.modules.j2ee.sun.ide.sunresources.wizards.ResourceConfigData; 68 69 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 70 71 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 72 import org.netbeans.modules.j2ee.sun.api.ServerInterface; 73 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 74 75 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider; 76 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.*; 77 import org.netbeans.modules.j2ee.sun.share.serverresources.SunDatasource; 78 import org.netbeans.modules.j2ee.sun.sunresources.beans.DatabaseUtils; 79 80 84 public class ResourceUtils implements WizardConstants{ 85 86 static final ResourceBundle bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.sunresources.beans.Bundle"); static final String [] sysDatasources = {"jdbc/__TimerPool", "jdbc/__CallFlowPool", "jdbc/__default"}; static final String [] sysConnpools = {"__CallFlowPool", "__TimerPool", "DerbyPool"}; static final String SAMPLE_DATASOURCE = "jdbc/sample"; 90 static final String SAMPLE_CONNPOOL = "SamplePool"; 91 92 93 public ResourceUtils() { 94 } 95 96 public static void saveNodeToXml(FileObject resFile, Resources res){ 97 try { 98 res.write(FileUtil.toFile(resFile)); 99 }catch(Exception ex){ 100 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 101 } 102 } 103 104 public static void register(Resources resource, SunDeploymentManagerInterface sunDm, boolean update, String resType) throws Exception { 105 if(sunDm.isRunning()){ 106 ServerInterface mejb = sunDm.getManagement(); 107 if(resType.equals(__JdbcConnectionPool)){ 108 register(resource.getJdbcConnectionPool(0), mejb, update); 109 }else if(resType.equals(__JdbcResource)){ 110 register(resource.getJdbcResource(0), mejb, update); 111 }else if(resType.equals(__PersistenceManagerFactoryResource)){ 112 register(resource.getPersistenceManagerFactoryResource(0), mejb, update); 113 }else if(resType.equals(__MailResource)){ 114 register(resource.getMailResource(0), mejb, update); 115 }else if(resType.equals(__JmsResource)){ 116 if(resource.getAdminObjectResource().length != 0){ 117 register(resource.getAdminObjectResource(0), mejb, update); 118 }else{ 119 if(resource.getConnectorResource().length != 0 && resource.getConnectorConnectionPool().length != 0) { 120 register(resource.getConnectorConnectionPool(0), mejb, update); 121 register(resource.getConnectorResource(0), mejb, update); 122 } 123 } 124 } 125 }else{ 126 throw new Exception (bundle.getString("Err_RegResServerStopped")); } 128 } 129 130 public static void register(JdbcConnectionPool resource, ServerInterface mejb, boolean update) throws Exception { 131 AttributeList attrList = ResourceUtils.getResourceAttributes(resource, mejb); 132 PropertyElement[] props = resource.getPropertyElement(); 133 Properties propsList = getProperties(props); 134 Object [] params = new Object []{attrList, propsList, null}; 135 String resourceName = resource.getName(); 136 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, __GetJdbcConnectionPool)){ 137 createResource(__CreateCP, params, mejb); 138 } 139 } 140 141 public static void register(JdbcResource resource, ServerInterface mejb, boolean update) throws Exception { 142 AttributeList attrList = ResourceUtils.getResourceAttributes(resource); 143 PropertyElement[] props = resource.getPropertyElement(); 144 Properties propsList = getProperties(props); 145 Object [] params = new Object []{attrList, propsList, null}; 146 String resourceName = resource.getJndiName(); 147 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, __GetJdbcResource)){ 148 createResource(__CreateDS, params, mejb); 149 } 150 } 151 152 public static void register(PersistenceManagerFactoryResource resource, ServerInterface mejb, boolean update) throws Exception { 153 AttributeList attrList = ResourceUtils.getResourceAttributes(resource); 154 PropertyElement[] props = resource.getPropertyElement(); 155 Properties propsList = getProperties(props); 156 Object [] params = new Object []{attrList, propsList, null}; 157 String resourceName = resource.getJndiName(); 158 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, __GetPMFResource)){ 159 createResource(__CreatePMF, params, mejb); 160 } 161 } 162 163 public static void register(AdminObjectResource resource, ServerInterface mejb, boolean update) throws Exception { 164 AttributeList attrList = ResourceUtils.getResourceAttributes(resource); 165 PropertyElement[] props = resource.getPropertyElement(); 166 Properties propsList = getProperties(props); 167 Object [] params = new Object []{attrList, propsList, null}; 168 String resourceName = resource.getJndiName(); 169 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, __GetAdmObjResource)){ 170 createResource(__CreateAdmObj, params, mejb); 171 } 172 } 173 174 public static void register(ConnectorResource resource, ServerInterface mejb, boolean update) throws Exception { 175 AttributeList attrList = ResourceUtils.getResourceAttributes(resource); 176 Properties propsList = new Properties (); 177 Object [] params = new Object []{attrList, propsList, null}; 178 String resourceName = resource.getJndiName(); 179 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, __GetConnectorResource)){ 180 createResource(__CreateConnector, params, mejb); 181 } 182 } 183 184 public static void register(ConnectorConnectionPool resource, ServerInterface mejb, boolean update) throws Exception { 185 AttributeList attrList = ResourceUtils.getResourceAttributes(resource); 186 PropertyElement[] props = resource.getPropertyElement(); 187 Properties propsList = getProperties(props); 188 Object [] params = new Object []{attrList, propsList, null}; 189 String resourceName = resource.getName(); 190 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, __GetConnPoolResource)){ 191 createResource(__CreateConnPool, params, mejb); 192 } 193 } 194 195 public static void register(MailResource resource, ServerInterface mejb, boolean update) throws Exception { 196 AttributeList attrList = ResourceUtils.getResourceAttributes(resource); 197 PropertyElement[] props = resource.getPropertyElement(); 198 Properties propsList = getProperties(props); 199 Object [] params = new Object []{attrList, propsList, null}; 200 String resourceName = resource.getJndiName(); 201 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, __GetMailResource)){ 202 createResource(__CreateMail, params, mejb); 203 } 204 } 205 206 public static void register(JmsResource resource, ServerInterface mejb, boolean update) throws Exception { 207 AttributeList attrList = ResourceUtils.getResourceAttributes(resource); 208 PropertyElement[] props = resource.getPropertyElement(); 209 Properties propsList = getProperties(props); 210 Object [] params = new Object []{attrList, propsList, null}; 211 String operName = NbBundle.getMessage(ResourceUtils.class, "CreateJMS"); String resourceName = resource.getJndiName(); 213 if(!isResourceUpdated(resourceName, mejb, attrList, propsList, WizardConstants.__GetJmsResource)){ 214 createResource(operName, params, mejb); 215 } 216 } 217 218 private static boolean isResourceUpdated(String resourceName, ServerInterface mejb, AttributeList attrList, Properties props, String operName ){ 219 boolean isResUpdated = false; 220 try{ 221 ObjectName objName = new ObjectName (MAP_RESOURCES); 222 ObjectName [] resourceObjects = null; 223 if(operName.equals(__GetPMFResource) || operName.equals(__GetJmsResource)){ 224 String [] signature = new String []{"java.lang.String"}; Object [] params = new Object []{null}; 226 resourceObjects = (ObjectName []) mejb.invoke(objName, operName, params, signature); 227 }else{ 228 resourceObjects = (ObjectName []) mejb.invoke(objName, operName, null, null); 229 } 230 if(resourceObjects != null){ 231 ObjectName resOnServer = null; 232 if(operName.equals(__GetJdbcConnectionPool) || operName.equals(__GetConnPoolResource)) 233 resOnServer = getResourceDeployed(resourceObjects, resourceName, false); 234 else 235 resOnServer = getResourceDeployed(resourceObjects, resourceName, true); 236 if(resOnServer != null){ 237 isResUpdated = true; 238 updateResourceAttributes(resOnServer, attrList, mejb); 239 updateResourceProperties(resOnServer, props, mejb); 240 } 241 } }catch(Exception ex){ 243 String errorMsg = MessageFormat.format(bundle.getString("Err_ResourceUpdate"), new Object []{resourceName}); System.out.println(errorMsg); 245 } 246 return isResUpdated; 247 } 248 249 private static ObjectName getResourceDeployed(ObjectName [] resourceObjects, String resourceName, boolean useJndi){ 250 for(int i=0; i<resourceObjects.length; i++){ 251 ObjectName resObj = resourceObjects[i]; 252 String jndiName = null; 253 if(useJndi) 254 jndiName = resObj.getKeyProperty(__JndiName); 255 else 256 jndiName = resObj.getKeyProperty(__Name); 257 258 if(jndiName.equals(resourceName)){ 259 return resObj; 260 } 261 } 262 return null; 263 } 264 265 public static void updateResourceAttributes(ObjectName objName, AttributeList attrList, ServerInterface mejb) throws Exception { 266 try{ 267 Map attributeInfos = getResourceAttributeNames(objName, mejb); 268 String [] attrNames = (String []) attributeInfos.keySet().toArray(new String [attributeInfos.size()]); 269 270 AttributeList existAttrList = mejb.getAttributes(objName, attrNames); 272 for(int i=0; i<existAttrList.size(); i++){ 273 Attribute existAttr = (Attribute )existAttrList.get(i); 274 String existAttrName = existAttr.getName(); 275 for(int j=0; j<attrList.size(); j++){ 276 Attribute resAttr = (Attribute )attrList.get(j); 277 String resAttrName = resAttr.getName(); 278 if(existAttrName.equals(resAttrName)){ 279 if(resAttr.getValue() == null && existAttr.getValue() != null) { 280 mejb.setAttribute(objName, resAttr); 281 }else if(existAttr.getValue() == null) { if((resAttr.getValue() != null) && (! resAttr.getValue().toString().equals(""))) 283 mejb.setAttribute(objName, resAttr); 284 }else{ 285 if(! resAttr.getValue().toString().equals(existAttr.getValue().toString())){ 286 mejb.setAttribute(objName, resAttr); 287 } 288 } 289 } } } 292 }catch(Exception ex){ 293 throw new Exception (ex.getLocalizedMessage()); 294 } 295 } 296 public static void updateResourceProperties(ObjectName objName, Properties props, ServerInterface mejb) throws Exception { 297 try{ 298 String [] signature = new String []{"javax.management.Attribute"}; Object [] params = null; 300 AttributeList attrList = (AttributeList )mejb.invoke(objName, WizardConstants.__GetProperties, null, null); 302 for(int i=0; i<attrList.size(); i++){ 303 Attribute oldAttr = (Attribute )attrList.get(i); 304 String oldAttrName = oldAttr.getName(); 305 if(props.containsKey(oldAttrName)){ 306 if(oldAttr.getValue() != null){ 307 String oldAttrValue = oldAttr.getValue().toString(); 308 if(! props.getProperty(oldAttrName).equals(oldAttrValue)){ 309 Attribute attr = new Attribute (oldAttrName, props.getProperty(oldAttrName)); 310 params = new Object []{attr}; 311 mejb.invoke(objName, WizardConstants.__SetProperty, params, signature); 312 } 313 }else{ if(props.getProperty(oldAttrName) != null){ 315 Attribute attr = new Attribute (oldAttrName, props.getProperty(oldAttrName)); 316 params = new Object []{attr}; 317 mejb.invoke(objName, WizardConstants.__SetProperty, params, signature); 318 } 319 } 320 }else{ 321 Attribute removeAttr = new Attribute (oldAttrName, null); 324 params = new Object []{removeAttr}; 325 mejb.invoke(objName, WizardConstants.__SetProperty, params, signature); 326 } 327 } addNewExtraProperties(objName, props, attrList, mejb); 329 }catch(Exception ex){ 330 throw new Exception (ex.getLocalizedMessage()); 331 } 332 } 333 334 private static Map getResourceAttributeNames(ObjectName objName, ServerInterface mejb) throws Exception { 335 try{ 336 Map attributeInfos = new java.util.HashMap (); 337 javax.management.MBeanInfo info = mejb.getMBeanInfo(objName); 338 javax.management.MBeanAttributeInfo [] attrs = info.getAttributes(); 339 for (int i=0; i<attrs.length; i++) { 340 if(attrs[i] != null){ 341 attributeInfos.put(attrs[i].getName(), attrs[i]); 342 } 343 } 344 return attributeInfos; 345 }catch(Exception ex){ 346 throw new Exception (ex.getLocalizedMessage()); 347 } 348 } 349 350 private static void addNewExtraProperties(ObjectName objName, Properties props, AttributeList attrList, ServerInterface mejb) throws Exception { 351 try{ 352 String [] signature = new String []{"javax.management.Attribute"}; Object [] params = null; 354 if(props.size() > attrList.size()){ 355 java.util.Enumeration listProps = props.propertyNames(); 356 while(listProps.hasMoreElements()){ 357 String propName = listProps.nextElement().toString(); 358 if(! attrList.contains(propName)){ 359 Attribute attr = new Attribute (propName, props.getProperty(propName)); 360 params = new Object []{attr}; 361 mejb.invoke(objName, WizardConstants.__SetProperty, params, signature); 362 } 363 } } 365 }catch(Exception ex){ 366 throw new Exception (ex.getLocalizedMessage()); 367 } 368 } 369 370 371 372 static final String MAP_RESOURCES = "com.sun.appserv:type=resources,category=config"; public static void createResource(String operName, Object [] params, ServerInterface mejb) throws Exception { 374 try{ 375 ObjectName objName = new ObjectName (MAP_RESOURCES); 376 String [] signature = new String []{"javax.management.AttributeList", "java.util.Properties", "java.lang.String"}; mejb.invoke(objName, operName, params, signature); 378 }catch(Exception ex){ 379 throw new Exception (ex.getLocalizedMessage()); 380 } 381 } 382 383 public static AttributeList getResourceAttributes(JdbcConnectionPool connPool, ServerInterface mejb){ 384 AttributeList attrs = new AttributeList (); 385 attrs.add(new Attribute (__Name, connPool.getName())); 386 attrs.add(new Attribute (__DatasourceClassname, connPool.getDatasourceClassname())); 387 attrs.add(new Attribute (__ResType, connPool.getResType())); 388 attrs.add(new Attribute (__SteadyPoolSize, connPool.getSteadyPoolSize())); 389 attrs.add(new Attribute (__MaxPoolSize, connPool.getMaxPoolSize())); 390 attrs.add(new Attribute (__MaxWaitTimeInMillis, connPool.getMaxWaitTimeInMillis())); 391 attrs.add(new Attribute (__PoolResizeQuantity, connPool.getPoolResizeQuantity())); 392 attrs.add(new Attribute (__IdleTimeoutInSeconds, connPool.getIdleTimeoutInSeconds())); 393 String isolation = connPool.getTransactionIsolationLevel(); 394 if (isolation != null && (isolation.length() == 0 || isolation.equals(NbBundle.getMessage(IsolationLevelEditor.class, "LBL_driver_default"))) ){ isolation = null; 396 } 397 attrs.add(new Attribute (__TransactionIsolationLevel, isolation)); 398 attrs.add(new Attribute (__IsIsolationLevelGuaranteed, connPool.getIsIsolationLevelGuaranteed())); 399 attrs.add(new Attribute (__IsConnectionValidationRequired, connPool.getIsConnectionValidationRequired())); 400 attrs.add(new Attribute (__ConnectionValidationMethod, connPool.getConnectionValidationMethod())); 401 attrs.add(new Attribute (__ValidationTableName, connPool.getValidationTableName())); 402 attrs.add(new Attribute (__FailAllConnections, connPool.getFailAllConnections())); 403 attrs.add(new Attribute (__Description, connPool.getDescription())); 404 405 if(is90Server(mejb)){ 406 attrs.add(new Attribute (__NonTransactionalConnections, connPool.getNonTransactionalConnections())); 407 attrs.add(new Attribute (__AllowNonComponentCallers, connPool.getAllowNonComponentCallers())); 408 } 409 return attrs; 410 } 411 412 public static AttributeList getResourceAttributes(JdbcResource jdbcResource){ 413 AttributeList attrs = new AttributeList (); 414 attrs.add(new Attribute (__JndiName, jdbcResource.getJndiName())); 415 attrs.add(new Attribute (__PoolName, jdbcResource.getPoolName())); 416 attrs.add(new Attribute (__JdbcObjectType, jdbcResource.getObjectType())); 417 attrs.add(new Attribute (__Enabled, jdbcResource.getEnabled())); 418 attrs.add(new Attribute (__Description, jdbcResource.getDescription())); 419 return attrs; 420 } 421 422 public static AttributeList getResourceAttributes(PersistenceManagerFactoryResource pmResource){ 423 AttributeList attrs = new AttributeList (); 424 attrs.add(new Attribute (__JndiName, pmResource.getJndiName())); 425 attrs.add(new Attribute (__FactoryClass, pmResource.getFactoryClass())); 426 attrs.add(new Attribute (__JdbcResourceJndiName, pmResource.getJdbcResourceJndiName())); 427 attrs.add(new Attribute (__Enabled, pmResource.getEnabled())); 428 attrs.add(new Attribute (__Description, pmResource.getDescription())); 429 return attrs; 430 } 431 432 public static AttributeList getResourceAttributes(AdminObjectResource aoResource){ 433 AttributeList attrs = new AttributeList (); 434 attrs.add(new Attribute (__JndiName, aoResource.getJndiName())); 435 attrs.add(new Attribute (__Description, aoResource.getDescription())); 436 attrs.add(new Attribute (__Enabled, aoResource.getEnabled())); 437 attrs.add(new Attribute (__JavaMessageResType, aoResource.getResType())); 438 attrs.add(new Attribute (__AdminObjResAdapterName, aoResource.getResAdapter())); 439 return attrs; 440 } 441 442 public static AttributeList getResourceAttributes(ConnectorResource connResource){ 443 AttributeList attrs = new AttributeList (); 444 attrs.add(new Attribute (__JndiName, connResource.getJndiName())); 445 attrs.add(new Attribute (__PoolName, connResource.getPoolName())); 446 attrs.add(new Attribute (__Description, connResource.getDescription())); 447 attrs.add(new Attribute (__Enabled, connResource.getEnabled())); 448 return attrs; 449 } 450 451 public static AttributeList getResourceAttributes(ConnectorConnectionPool connPoolResource){ 452 AttributeList attrs = new AttributeList (); 453 attrs.add(new Attribute (__Name, connPoolResource.getName())); 454 attrs.add(new Attribute (__ConnectorPoolResAdName, connPoolResource.getResourceAdapterName())); 455 attrs.add(new Attribute (__ConnectorPoolConnDefName, connPoolResource.getConnectionDefinitionName())); 456 return attrs; 457 } 458 459 public static AttributeList getResourceAttributes(MailResource mailResource){ 460 AttributeList attrs = new AttributeList (); 461 attrs.add(new Attribute (__JndiName, mailResource.getJndiName())); 462 attrs.add(new Attribute (__StoreProtocol, mailResource.getStoreProtocol())); 463 attrs.add(new Attribute (__StoreProtocolClass, mailResource.getStoreProtocolClass())); 464 attrs.add(new Attribute (__TransportProtocol, mailResource.getTransportProtocol())); 465 attrs.add(new Attribute (__TransportProtocolClass, mailResource.getTransportProtocolClass())); 466 attrs.add(new Attribute (__Host, mailResource.getHost())); 467 attrs.add(new Attribute (__MailUser, mailResource.getUser())); 468 attrs.add(new Attribute (__From, mailResource.getFrom())); 469 attrs.add(new Attribute (__Debug, mailResource.getDebug())); 470 attrs.add(new Attribute (__Enabled, mailResource.getEnabled())); 471 attrs.add(new Attribute (__Description, mailResource.getDescription())); 472 return attrs; 473 } 474 475 public static AttributeList getResourceAttributes(JmsResource jmsResource){ 476 AttributeList attrs = new AttributeList (); 477 attrs.add(new Attribute (__JavaMessageJndiName, jmsResource.getJndiName())); 478 attrs.add(new Attribute (__JavaMessageResType, jmsResource.getResType())); 479 attrs.add(new Attribute (__Enabled, jmsResource.getEnabled())); 480 attrs.add(new Attribute (__Description, jmsResource.getDescription())); 481 return attrs; 482 } 483 484 private static Properties getProperties(PropertyElement[] props) throws Exception { 485 Properties propList = new Properties (); 486 for(int i=0; i<props.length; i++){ 487 String name = props[i].getName(); 488 String value = props[i].getValue(); 489 if(value != null && value.trim().length() != 0){ 490 propList.put(name, value); 491 } 492 } 493 return propList; 494 } 495 496 public List getTargetServers(){ 497 String instances [] = InstanceProperties.getInstanceList(); 498 List targets = new ArrayList (); 499 for (int i=0; i < instances.length; i++) { 500 if (instances[i].startsWith(SunURIManager.SUNSERVERSURI)) { 501 targets.add(InstanceProperties.getInstanceProperties(instances[i]).getDeploymentManager()); 502 } 503 else if (instances[i].startsWith("[")) { 504 targets.add(InstanceProperties.getInstanceProperties(instances[i]).getDeploymentManager()); 505 } 506 } 507 return targets; 512 } 513 514 public static void saveConnPoolDatatoXml(ResourceConfigData data) { 515 try{ 516 Vector vec = data.getProperties(); 517 Resources res = getResourceGraph(); 518 JdbcConnectionPool connPool = res.newJdbcConnectionPool(); 519 520 String [] keys = data.getFieldNames(); 521 for (int i = 0; i < keys.length; i++) { 522 String key = keys[i]; 523 if (key.equals(__Properties)){ 524 Vector props = (Vector )data.getProperties(); 525 for (int j = 0; j < props.size(); j++) { 526 NameValuePair pair = (NameValuePair)props.elementAt(j); 527 PropertyElement prop = connPool.newPropertyElement(); 528 prop = populatePropertyElement(prop, pair); 529 connPool.addPropertyElement(prop); 530 } 531 }else{ 532 String value = data.getString(key); 533 if (key.equals(__Name)){ 534 connPool.setName(value); 535 data.setTargetFile(value); 536 }else if (key.equals(__DatasourceClassname)) 537 connPool.setDatasourceClassname(value); 538 else if (key.equals(__ResType)) 539 connPool.setResType(value); 540 else if (key.equals(__SteadyPoolSize)) 541 connPool.setSteadyPoolSize(value); 542 else if (key.equals(__MaxPoolSize)) 543 connPool.setMaxPoolSize(value); 544 else if (key.equals(__MaxWaitTimeInMillis)) 545 connPool.setMaxWaitTimeInMillis(value); 546 else if (key.equals(__PoolResizeQuantity)) 547 connPool.setPoolResizeQuantity(value); 548 else if (key.equals(__IdleTimeoutInSeconds)) 549 connPool.setIdleTimeoutInSeconds(value); 550 else if (key.equals(__TransactionIsolationLevel)){ 551 if (value.equals(NbBundle.getMessage(IsolationLevelEditor.class, "LBL_driver_default"))){ value = null; 553 } 554 connPool.setTransactionIsolationLevel(value); 555 }else if (key.equals(__IsIsolationLevelGuaranteed)) 556 connPool.setIsIsolationLevelGuaranteed(value); 557 else if (key.equals(__IsConnectionValidationRequired)) 558 connPool.setIsConnectionValidationRequired(value); 559 else if (key.equals(__ConnectionValidationMethod)) 560 connPool.setConnectionValidationMethod(value); 561 else if (key.equals(__ValidationTableName)) 562 connPool.setValidationTableName(value); 563 else if (key.equals(__FailAllConnections)) 564 connPool.setFailAllConnections(value); 565 else if (key.equals(__Description)) 566 connPool.setDescription(value); 567 else if (key.equals(__NonTransactionalConnections)) 568 connPool.setNonTransactionalConnections(value); 569 else if (key.equals(__AllowNonComponentCallers)) 570 connPool.setAllowNonComponentCallers(value); 571 } 572 573 } res.addJdbcConnectionPool(connPool); 575 createFile(data.getTargetFileObject(), data.getTargetFile(), res); 576 }catch(Exception ex){ 577 System.out.println("Unable to saveConnPoolDatatoXml "); 578 } 579 } 580 581 public static void saveJDBCResourceDatatoXml(ResourceConfigData dsData, ResourceConfigData cpData) { 582 try{ 583 Resources res = getResourceGraph(); 584 JdbcResource datasource = res.newJdbcResource(); 585 586 String [] keys = dsData.getFieldNames(); 587 for (int i = 0; i < keys.length; i++) { 588 String key = keys[i]; 589 if (key.equals(__Properties)){ 590 Vector props = (Vector )dsData.getProperties(); 591 for (int j = 0; j < props.size(); j++) { 592 NameValuePair pair = (NameValuePair)props.elementAt(j); 593 PropertyElement prop = datasource.newPropertyElement(); 594 prop = populatePropertyElement(prop, pair); 595 datasource.addPropertyElement(prop); 596 } 597 }else{ 598 String value = dsData.getString(key); 599 if (key.equals(__JndiName)){ 600 datasource.setJndiName(value); 601 dsData.setTargetFile(value); 602 }else if (key.equals(__PoolName)) 603 datasource.setPoolName(value); 604 else if (key.equals(__JdbcObjectType)) 605 datasource.setObjectType(value); 606 else if (key.equals(__Enabled)) 607 datasource.setEnabled(value); 608 else if (key.equals(__Description)) 609 datasource.setDescription(value); 610 } 611 612 } res.addJdbcResource(datasource); 614 if(cpData != null){ 615 saveConnPoolDatatoXml(cpData); 616 } 617 createFile(dsData.getTargetFileObject(), dsData.getTargetFile(), res); 618 }catch(Exception ex){ 619 ex.printStackTrace(); 620 System.out.println("Unable to saveJDBCResourceDatatoXml "); 621 } 622 } 623 624 public static void savePMFResourceDatatoXml(ResourceConfigData pmfData, ResourceConfigData dsData, ResourceConfigData cpData) { 625 try{ 626 Resources res = getResourceGraph(); 627 PersistenceManagerFactoryResource pmfresource = res.newPersistenceManagerFactoryResource(); 628 629 String [] keys = pmfData.getFieldNames(); 630 for (int i = 0; i < keys.length; i++) { 631 String key = keys[i]; 632 if (key.equals(__Properties)){ 633 Vector props = (Vector )pmfData.getProperties(); 634 for (int j = 0; j < props.size(); j++) { 635 NameValuePair pair = (NameValuePair)props.elementAt(j); 636 PropertyElement prop = pmfresource.newPropertyElement(); 637 prop = populatePropertyElement(prop, pair); 638 pmfresource.addPropertyElement(prop); 639 } 640 }else{ 641 String value = pmfData.getString(key); 642 if (key.equals(__JndiName)){ 643 pmfresource.setJndiName(value); 644 pmfData.setTargetFile(value); 645 }else if (key.equals(__FactoryClass)) 646 pmfresource.setFactoryClass(value); 647 else if (key.equals(__JdbcResourceJndiName)) 648 pmfresource.setJdbcResourceJndiName(value); 649 else if (key.equals(__Enabled)) 650 pmfresource.setEnabled(value); 651 else if (key.equals(__Description)) 652 pmfresource.setDescription(value); 653 } 654 655 } res.addPersistenceManagerFactoryResource(pmfresource); 657 createFile(pmfData.getTargetFileObject(), pmfData.getTargetFile(), res); 658 659 if(dsData != null){ 660 saveJDBCResourceDatatoXml(dsData, cpData); 661 } 662 }catch(Exception ex){ 663 System.out.println("Unable to savePMFResourceDatatoXml "); 664 } 665 } 666 667 public static void saveJMSResourceDatatoXml(ResourceConfigData jmsData) { 668 try{ 669 Resources res = getResourceGraph(); 670 String type = jmsData.getString(__ResType); 671 if(type.equals(__QUEUE) || type.equals(__TOPIC)){ 672 AdminObjectResource aoresource = res.newAdminObjectResource(); 673 aoresource.setDescription(jmsData.getString(__Description)); 674 aoresource.setEnabled(jmsData.getString(__Enabled)); 675 aoresource.setJndiName(jmsData.getString(__JndiName)); 676 aoresource.setResType(jmsData.getString(__ResType)); 677 aoresource.setResAdapter(__JmsResAdapter); 678 Vector props = (Vector )jmsData.getProperties(); 679 for (int j = 0; j < props.size(); j++) { 680 NameValuePair pair = (NameValuePair)props.elementAt(j); 681 PropertyElement prop = aoresource.newPropertyElement(); 682 prop = populatePropertyElement(prop, pair); 683 aoresource.addPropertyElement(prop); 684 } 685 686 res.addAdminObjectResource(aoresource); 687 }else{ 688 ConnectorResource connresource = res.newConnectorResource(); 689 connresource.setDescription(jmsData.getString(__Description)); 690 connresource.setEnabled(jmsData.getString(__Enabled)); 691 connresource.setJndiName(jmsData.getString(__JndiName)); 692 connresource.setPoolName(jmsData.getString(__JndiName)); 693 694 ConnectorConnectionPool connpoolresource = res.newConnectorConnectionPool(); 695 connpoolresource.setName(jmsData.getString(__JndiName)); 696 connpoolresource.setConnectionDefinitionName(jmsData.getString(__ResType)); 697 connpoolresource.setResourceAdapterName(__JmsResAdapter); 698 699 Vector props = (Vector )jmsData.getProperties(); 700 for (int j = 0; j < props.size(); j++) { 701 NameValuePair pair = (NameValuePair)props.elementAt(j); 702 PropertyElement prop = connpoolresource.newPropertyElement(); 703 prop = populatePropertyElement(prop, pair); 704 connpoolresource.addPropertyElement(prop); 705 } 706 707 res.addConnectorResource(connresource); 708 res.addConnectorConnectionPool(connpoolresource); 709 } 710 711 createFile(jmsData.getTargetFileObject(), jmsData.getTargetFile(), res); 712 }catch(Exception ex){ 713 ex.printStackTrace(); 714 System.out.println("Unable to saveJMSResourceDatatoXml "); 715 } 716 } 717 718 public static void saveMailResourceDatatoXml(ResourceConfigData data) { 719 try{ 720 Vector vec = data.getProperties(); 721 Resources res = getResourceGraph(); 722 MailResource mlresource = res.newMailResource(); 723 724 String [] keys = data.getFieldNames(); 725 for (int i = 0; i < keys.length; i++) { 726 String key = keys[i]; 727 if (key.equals(__Properties)) { 728 Vector props = (Vector )data.getProperties(); 729 for (int j = 0; j < props.size(); j++) { 730 NameValuePair pair = (NameValuePair)props.elementAt(j); 731 PropertyElement prop = mlresource.newPropertyElement(); 732 prop = populatePropertyElement(prop, pair); 733 mlresource.addPropertyElement(prop); 734 } 735 }else{ 736 String value = data.getString(key); 737 if (key.equals(__JndiName)){ 738 mlresource.setJndiName(value); 739 data.setTargetFile(value); 740 }else if (key.equals(__StoreProtocol)) 741 mlresource.setStoreProtocol(value); 742 else if (key.equals(__StoreProtocolClass)) 743 mlresource.setStoreProtocolClass(value); 744 else if (key.equals(__TransportProtocol)) 745 mlresource.setTransportProtocol(value); 746 else if (key.equals(__TransportProtocolClass)) 747 mlresource.setTransportProtocolClass(value); 748 else if (key.equals(__Host)) 749 mlresource.setHost(value); 750 else if (key.equals(__MailUser)) 751 mlresource.setUser(value); 752 else if (key.equals(__From)) 753 mlresource.setFrom(value); 754 else if (key.equals(__Debug)) 755 mlresource.setDebug(value); 756 else if (key.equals(__Description)) 757 mlresource.setDescription(value); 758 } 759 } 761 res.addMailResource(mlresource); 762 createFile(data.getTargetFileObject(), data.getTargetFile(), res); 763 }catch(Exception ex){ 764 System.out.println("Unable to saveMailResourceDatatoXml "); 765 } 766 } 767 768 public static void createFile(FileObject targetFolder, String filename, final Resources res){ 769 try{ 770 if(filename.indexOf("/") != -1){ filename = filename.substring(0, filename.indexOf("/")) + "_" + filename.substring(filename.indexOf("/")+1, filename.length()); } 774 if(filename.indexOf("\\") != -1){ filename = filename.substring(0, filename.indexOf("\\")) + "_" + filename.substring(filename.indexOf("\\")+1, filename.length()); } 777 String oldName = filename; 778 targetFolder = setUpExists(targetFolder); 779 filename = FileUtil.findFreeFileName(targetFolder, filename, __SunResourceExt); 780 781 final String resFileName = filename; 782 final FileObject resTargetFolder = targetFolder; 783 FileSystem fs = targetFolder.getFileSystem(); 784 fs.runAtomicAction(new FileSystem.AtomicAction() { 785 public void run() throws java.io.IOException { 786 FileObject newfile = resTargetFolder.createData(resFileName, "sun-resource"); 788 FileLock lock = newfile.lock(); 789 Writer w = null; 790 try { 791 Writer out = new OutputStreamWriter (newfile.getOutputStream(lock), "UTF8"); 792 res.write(out); 793 out.flush(); 794 out.close(); 795 } catch(Exception ex){ 796 } finally { 798 lock.releaseLock(); 799 } 800 } 801 }); 802 }catch(Exception ex){ 803 System.out.println("Error while creating file"); 805 } 806 } 807 808 public static String createUniqueFileName(String in_targetName, FileObject fo, String defName){ 809 String targetName = in_targetName; 810 if (targetName == null || targetName.length() == 0) 811 targetName = defName; 812 813 targetName = makeLegalFilename(targetName); 814 targetName = FileUtil.findFreeFileName(fo, targetName, __SunResourceExt); 815 targetName = revertToResName(targetName); 816 return targetName; 817 } 818 819 public static List getRegisteredConnectionPools(ResourceConfigData data){ 820 List connPools = new ArrayList (); 821 try { 822 String OPER_OBJ_ConnPoolResource = "getJdbcConnectionPool"; String keyProp = "name"; InstanceProperties instanceProperties = getTargetServer(data.getTargetFileObject()); 825 if(instanceProperties != null) 826 connPools = getResourceNames(instanceProperties, OPER_OBJ_ConnPoolResource, keyProp); 827 connPools.removeAll(Arrays.asList(sysConnpools)); 828 List projectCP = getProjectResources(data, __ConnectionPoolResource); 829 for(int i=0; i<projectCP.size(); i++){ 830 String localCP = projectCP.get(i).toString(); 831 if(! connPools.contains(localCP)) 832 connPools.add(localCP); 833 } 834 } catch (java.lang.NoClassDefFoundError ncdfe) { 835 } 837 return connPools; 838 } 839 840 public static List getRegisteredJdbcResources(ResourceConfigData data){ 841 List dataSources = new ArrayList (); 842 try { 843 String keyProp = "jndi-name"; InstanceProperties instanceProperties = getTargetServer(data.getTargetFileObject()); 845 if(instanceProperties != null) 846 dataSources = getResourceNames(instanceProperties, WizardConstants.__GetJdbcResource, keyProp); 847 dataSources.removeAll(Arrays.asList(sysDatasources)); 848 List projectDS = getProjectResources(data, __JDBCResource); 849 for(int i=0; i<projectDS.size(); i++){ 850 String localDS = projectDS.get(i).toString(); 851 if(! dataSources.contains(localDS)) 852 dataSources.add(localDS); 853 } 854 } catch (java.lang.NoClassDefFoundError ncdfe) { 855 } 857 return dataSources; 858 } 859 860 private static List getResourceNames(InstanceProperties instProps, String query, String keyProperty){ 861 Object tmp = instProps.getDeploymentManager(); 862 List retVal; 863 if (tmp instanceof SunDeploymentManagerInterface) { 864 SunDeploymentManagerInterface eightDM = (SunDeploymentManagerInterface)tmp; 865 retVal = getResourceNames(eightDM, query, keyProperty); 866 } else { 867 retVal = Collections.EMPTY_LIST; 868 } 869 return retVal; 870 } 871 872 private static List getResourceNames(SunDeploymentManagerInterface eightDM, String query, String keyProperty){ 873 List resList = new ArrayList (); 874 String MAP_RESOURCES = "com.sun.appserv:type=resources,category=config"; try{ 876 ServerInterface mejb = (ServerInterface)eightDM.getManagement(); 877 ObjectName objName = new ObjectName (MAP_RESOURCES); 878 ObjectName [] beans = (ObjectName [])mejb.invoke(objName, query, null, null); 879 for(int i=0; i<beans.length; i++){ 880 String resName = ((ObjectName )beans[i]).getKeyProperty(keyProperty); 881 resList.add(resName); 882 } 883 }catch(Exception ex){ 884 } 888 return resList; 889 } 890 891 private static List getProjectResources(ResourceConfigData data, String resourceType){ 892 List projectResources = new ArrayList (); 893 FileObject targetFolder = data.getTargetFileObject(); 894 if(targetFolder != null){ 895 FileObject setUpFolder = setUpExists(targetFolder); 896 java.util.Enumeration en = setUpFolder.getData(false); 897 while(en.hasMoreElements()){ 898 FileObject resourceFile = (FileObject)en.nextElement(); 899 File resource = FileUtil.toFile(resourceFile); 900 if(resourceType.equals(__ConnectionPoolResource)) 901 projectResources = filterConnectionPools(resource, projectResources); 902 else 903 projectResources = filterDataSources(resource, projectResources); 904 } 905 } 906 return projectResources; 907 } 908 909 private static List filterConnectionPools(File primaryFile, List projectCP){ 910 try{ 911 if(! primaryFile.isDirectory()){ 912 FileInputStream in = new FileInputStream (primaryFile); 913 Resources resources = DDProvider.getDefault().getResourcesGraph(in); 914 915 JdbcConnectionPool[] pools = resources.getJdbcConnectionPool(); 917 for(int i=0; i<pools.length; i++){ 918 projectCP.add(pools[i].getName()); 919 } 920 } 921 }catch(Exception exception){ 922 } 924 return projectCP; 925 } 926 927 private static List filterDataSources(File primaryFile, List projectDS){ 928 try{ 929 if(! primaryFile.isDirectory()){ 930 FileInputStream in = new FileInputStream (primaryFile); 931 Resources resources = DDProvider.getDefault().getResourcesGraph(in); 932 933 JdbcResource[] dataSources = resources.getJdbcResource(); 935 for(int i=0; i<dataSources.length; i++){ 936 projectDS.add(dataSources[i].getJndiName()); 937 } 938 } 939 }catch(Exception exception){ 940 } 942 return projectDS; 943 } 944 945 public static FileObject setUpExists(FileObject targetFolder){ 946 FileObject pkgLocation = getResourceDirectory(targetFolder); 947 if(pkgLocation == null){ 948 return targetFolder; 950 }else{ 951 return pkgLocation; 952 } 953 } 954 955 private static Resources getResourceGraph(){ 956 return DDProvider.getDefault().getResourcesGraph(); 957 } 958 959 private static PropertyElement populatePropertyElement(PropertyElement prop, NameValuePair pair){ 960 prop.setName(pair.getParamName()); 961 prop.setValue(pair.getParamValue()); 962 return prop; 963 } 964 965 public static boolean isLegalFilename(String filename) { 967 for(int i = 0; i < ILLEGAL_FILENAME_CHARS.length; i++) 968 if(filename.indexOf(ILLEGAL_FILENAME_CHARS[i]) >= 0) 969 return false; 970 971 return true; 972 } 973 974 public static boolean isFriendlyFilename(String filename) { 975 if(filename.indexOf(BLANK) >= 0 || filename.indexOf(DOT) >= 0) 976 return false; 977 978 return isLegalFilename(filename); 979 } 980 981 public static String makeLegalFilename(String filename) { 982 for(int i = 0; i < ILLEGAL_FILENAME_CHARS.length; i++) 983 filename = filename.replace(ILLEGAL_FILENAME_CHARS[i], REPLACEMENT_CHAR); 984 985 return filename; 986 } 987 988 public static boolean isLegalResourceName(String filename) { 989 for(int i = 0; i < ILLEGAL_RESOURCE_NAME_CHARS.length; i++) 990 if(filename.indexOf(ILLEGAL_RESOURCE_NAME_CHARS[i]) >= 0) 991 return false; 992 993 return true; 994 } 995 996 public static FileObject getResourceDirectory(FileObject fo){ 997 Project holdingProj = FileOwnerQuery.getOwner(fo); 998 FileObject resourceDir = null; 999 if (holdingProj != null){ 1000 J2eeModuleProvider provider = (J2eeModuleProvider) holdingProj.getLookup().lookup(J2eeModuleProvider.class); 1001 if(provider != null){ 1002 File resourceLoc = provider.getEnterpriseResourceDirectory (); 1003 if(resourceLoc != null){ 1004 if(resourceLoc.exists ()){ 1005 resourceDir = FileUtil.toFileObject (resourceLoc); 1006 }else{ 1007 resourceLoc.mkdirs (); 1008 resourceDir = FileUtil.toFileObject (resourceLoc); 1009 } 1010 } 1011 } else { resourceDir = fo.getFileObject("setup"); 1013 } 1014 } 1015 return resourceDir; 1016 } 1017 1018 1019 1020 public static HashSet getServerDataSources(DeploymentManager dm){ 1021 HashSet datasources = new HashSet (); 1022 try { 1023 ObjectName configObjName = new ObjectName (WizardConstants.MAP_RESOURCES); 1024 SunDeploymentManagerInterface eightDM = (SunDeploymentManagerInterface)dm; 1025 ServerInterface mejb = (ServerInterface)eightDM.getManagement(); 1026 List systemDS = Arrays.asList(sysDatasources); 1027 if(eightDM.isRunning()){ 1028 updateSampleDatasource(eightDM, configObjName); 1029 ObjectName [] resourceObjects = (ObjectName []) mejb.invoke(configObjName, WizardConstants.__GetJdbcResource, null, null); 1030 for(int i=0; i<resourceObjects.length; i++){ 1031 ObjectName objName = resourceObjects[i]; 1032 String dsJndiName = (String )mejb.getAttribute(objName, "jndi-name"); if(! systemDS.contains(dsJndiName)){ 1035 String poolName = (String )mejb.getAttribute(objName, "pool-name"); HashMap poolValues = fillInPoolValues(eightDM, configObjName, poolName); 1037 if(! poolValues.isEmpty()){ 1038 String username = (String )poolValues.get(WizardConstants.__User); 1039 String password = (String )poolValues.get(WizardConstants.__Password); 1040 String url = (String )poolValues.get(WizardConstants.__Url); 1041 String driverClassName = (String )poolValues.get(WizardConstants.__DriverClassName); 1042 1043 SunDatasource ds = new SunDatasource(dsJndiName, url, username, password, driverClassName); 1044 datasources.add(ds); 1045 } 1046 } 1047 } } else{ 1049 if(eightDM.isLocal()) { 1050 datasources = formatXmlSunDatasources(eightDM.getSunDatasourcesFromXml()); 1051 } 1052 } } catch (Exception ex) { 1054 } 1056 return datasources; 1057 } 1058 1059 private static void updateSampleDatasource(SunDeploymentManagerInterface eightDM, ObjectName configObjName){ 1060 try{ 1061 if(! eightDM.isLocal()) 1062 return; 1063 List datasources = getResourceNames(eightDM, __GetJdbcResource, "jndi-name"); if(! datasources.contains(SAMPLE_DATASOURCE)){ 1065 ServerInterface mejb = (ServerInterface)eightDM.getManagement(); 1066 1067 if(getConnectionPoolObjByName(mejb, configObjName, SAMPLE_CONNPOOL) == null){ 1068 AttributeList poolAttrs = new AttributeList (); 1069 Attribute attr = new Attribute ("name", SAMPLE_CONNPOOL); poolAttrs.add(attr); 1071 attr = new Attribute ("datasource-classname", "org.apache.derby.jdbc.ClientDataSource"); poolAttrs.add(attr); 1073 attr = new Attribute ("res-type", "javax.sql.DataSource"); poolAttrs.add(attr); 1075 1076 Properties propsList = new Properties (); 1077 propsList.put(__User, "app"); propsList.put(__Password, "app"); propsList.put(__ServerName, "localhost"); propsList.put(__DerbyPortNumber, "1527"); 1081 propsList.put(__DerbyDatabaseName, "sample"); Object [] poolParams = new Object []{poolAttrs, propsList, null}; 1083 createResource(__CreateCP, poolParams, mejb); 1084 } 1085 1086 AttributeList attrs = new AttributeList (); 1087 attrs.add(new Attribute (__JndiName, SAMPLE_DATASOURCE)); 1088 attrs.add(new Attribute (__PoolName, SAMPLE_CONNPOOL)); 1089 attrs.add(new Attribute (__JdbcObjectType, "user")); attrs.add(new Attribute (__Enabled, "true")); Object [] params = new Object []{attrs, new Properties (), null}; 1092 createResource(__CreateDS, params, mejb); 1093 } 1094 }catch(Exception ex){} 1095 } 1096 1097 public static HashMap fillInPoolValues(SunDeploymentManagerInterface eightDM, ObjectName configObjName, String poolName) throws Exception { 1098 HashMap connPoolAttrs = new HashMap (); 1099 ServerInterface mejb = (ServerInterface)eightDM.getManagement(); 1100 ObjectName connPoolObj = getConnectionPoolByName(mejb, configObjName, poolName); 1102 String driverClassName = (String )mejb.getAttribute(connPoolObj, "datasource-classname"); String url = ""; String username = ""; String password = ""; String serverName = ""; String portNo = ""; String dbName = ""; String sid = ""; 1111 AttributeList attrList = (AttributeList )mejb.invoke(connPoolObj, WizardConstants.__GetProperties, null, null); 1112 HashMap attrs = getObjMap(attrList); 1113 Object [] keys = attrs.keySet().toArray(); 1114 for(int i=0; i<keys.length; i++){ 1115 String keyName = (String )keys[i]; 1116 if(keyName.equalsIgnoreCase(WizardConstants.__DatabaseName)){ 1117 if(driverClassName.indexOf("pointbase") != -1){ url = getStringVal(attrs.get(keyName)); 1119 }else{ 1120 dbName = getStringVal(attrs.get(keyName)); 1121 } 1122 }else if(keyName.equalsIgnoreCase(WizardConstants.__User)) { 1123 username = getStringVal(attrs.get(keyName)); 1124 }else if(keyName.equalsIgnoreCase(WizardConstants.__Password)) { 1125 password = getStringVal(attrs.get(keyName)); 1126 }else if(keyName.equalsIgnoreCase(WizardConstants.__Url)) { 1127 url = getStringVal(attrs.get(keyName)); 1128 }else if(keyName.equalsIgnoreCase(WizardConstants.__ServerName)) { 1129 serverName = getStringVal(attrs.get(keyName)); 1130 }else if(keyName.equalsIgnoreCase(WizardConstants.__DerbyPortNumber)) { 1131 portNo = getStringVal(attrs.get(keyName)); 1132 }else if(keyName.equalsIgnoreCase(WizardConstants.__SID)) { 1133 sid = getStringVal(attrs.get(keyName)); 1134 } 1135 } 1136 1137 if(driverClassName.indexOf("derby") != -1){ url = "jdbc:derby://"; if(serverName != null){ 1140 url = url + serverName; 1141 if(portNo != null) { 1142 url = url + ":" + portNo; } 1144 url = url + "/" + dbName ; } 1146 }else if(url.equals("")) { String urlPrefix = DatabaseUtils.getUrlPrefix(driverClassName); 1148 String vName = ResourceConfigurator.getDatabaseVendorName(urlPrefix, null); 1149 if(serverName != null){ 1150 if(vName.equals("sybase2")){ url = urlPrefix + serverName; 1152 } else{ 1153 url = urlPrefix + "//" + serverName; } 1155 if(portNo != null) { 1156 url = url + ":" + portNo; } 1158 } 1159 if(vName.equals("sun_oracle") || vName.equals("datadirect_oracle")) { url = url + ";SID=" + sid; }else if(Arrays.asList(WizardConstants.Reqd_DBName).contains(vName)) { 1162 url = url + ";databaseName=" + dbName; }else if(Arrays.asList(WizardConstants.VendorsDBNameProp).contains(vName)) { 1164 url = url + "/" + dbName ; } 1166 } 1167 1168 if((! eightDM.isLocal()) && (url.indexOf("localhost") != -1)){ String hostName = eightDM.getHost(); 1170 url = url.replaceFirst("localhost", hostName); } 1172 DatabaseConnection databaseConnection = getDatabaseConnection(url); 1173 if(databaseConnection != null){ 1174 driverClassName = databaseConnection.getDriverClass(); 1175 }else{ 1176 String drivername = DatabaseUtils.getDriverName(url); 1178 if(drivername != null) { 1179 driverClassName = drivername; 1180 } 1181 } 1182 1183 connPoolAttrs.put(__User, username); 1184 connPoolAttrs.put(__Password, password); 1185 connPoolAttrs.put(__Url, url); 1186 connPoolAttrs.put(__DriverClassName, driverClassName); 1187 return connPoolAttrs; 1188 } 1189 1190 private static ObjectName getConnectionPoolByName(ServerInterface mejb, ObjectName configObjName, String poolName) throws Exception { 1191 String [] signature = new String []{"java.lang.String"}; Object [] params = new Object []{poolName}; 1193 ObjectName connPoolObj = (ObjectName ) mejb.invoke(configObjName, WizardConstants.__GetJdbcConnectionPoolByName, params, signature); 1194 return connPoolObj; 1195 } 1196 1197 private static ObjectName getConnectionPoolObjByName(ServerInterface mejb, ObjectName configObjName, String poolName) { 1198 ObjectName connPoolObj = null; 1199 try{ 1200 connPoolObj = getConnectionPoolByName(mejb, configObjName, poolName); 1201 }catch(Exception ex){} 1202 return connPoolObj; 1203 } 1204 1205 1206 private static String getStringVal(Object val){ 1207 String value = null; 1208 if (val != null) 1209 value = val.toString(); 1210 return value; 1211 } 1212 1213 private static HashMap getObjMap(AttributeList attrList){ 1214 HashMap attrs = new HashMap (); 1215 for(int k=0; k<attrList.size(); k++){ 1216 Attribute currAttr = (Attribute )attrList.get(k); 1217 String pname = currAttr.getName(); 1218 Object pObjvalue = currAttr.getValue(); 1219 attrs.put(pname, pObjvalue); 1220 } 1221 return attrs; 1222 } 1223 1224 public static String revertToResName(String filename) { 1225 if(filename.indexOf("jdbc_") != -1) 1226 filename = filename.replaceFirst("jdbc_", "jdbc/"); 1227 if(filename.indexOf("mail_") != -1) 1228 filename = filename.replaceFirst("mail_", "mail/"); 1229 if(filename.indexOf("jms_") != -1) 1230 filename = filename.replaceFirst("jms_", "jms/"); 1231 return filename; 1232 } 1233 1234 public static boolean isUniqueFileName(String in_targetName, FileObject fo, String defName){ 1235 boolean isUniq = true; 1236 String targetName = in_targetName; 1237 if (targetName != null && targetName.length() != 0) { 1238 targetName = makeLegalFilename(targetName); 1239 targetName = targetName + "." + __SunResourceExt; File targFile = new File (fo.getPath(), targetName); 1241 if(targFile.exists()) 1242 isUniq = false; 1243 } 1244 return isUniq; 1245 } 1246 1247 public static DatabaseConnection getDatabaseConnection(String url) { 1248 DatabaseConnection[] dbConns = ConnectionManager.getDefault().getConnections(); 1249 for(int i=0; i<dbConns.length; i++){ 1250 String dbConnUrl = ((DatabaseConnection)dbConns[i]).getDatabaseURL(); 1251 if(dbConnUrl.startsWith(url)) 1252 return ((DatabaseConnection)dbConns[i]); 1253 } 1254 return null; 1255 } 1256 1257 public static InstanceProperties getTargetServer(FileObject fo){ 1258 InstanceProperties serverName = null; 1259 Project holdingProj = FileOwnerQuery.getOwner(fo); 1260 if (holdingProj != null){ 1261 J2eeModuleProvider modProvider = (J2eeModuleProvider) holdingProj.getLookup().lookup(J2eeModuleProvider.class); 1262 if(modProvider != null) 1263 serverName = modProvider.getInstanceProperties(); 1264 } 1265 return serverName; 1266 } 1267 1268 public static HashMap getConnPoolValues(File resourceDir, String poolName){ 1269 HashMap poolValues = new HashMap (); 1270 try{ 1271 ObjectName configObjName = new ObjectName (WizardConstants.MAP_RESOURCES); 1272 InstanceProperties instanceProperties = getTargetServer(FileUtil.toFileObject(resourceDir)); 1273 if(instanceProperties != null){ 1274 SunDeploymentManagerInterface eightDM = (SunDeploymentManagerInterface)instanceProperties.getDeploymentManager(); 1275 if(eightDM.isRunning()){ 1276 ServerInterface mejb = (ServerInterface)eightDM.getManagement(); 1277 poolValues = fillInPoolValues(eightDM, configObjName, poolName); 1278 }else{ 1279 if(eightDM.isLocal()){ 1280 HashMap poolMap = eightDM.getConnPoolsFromXml(); 1281 poolValues = formatPoolMap((HashMap )poolMap.get(poolName)); 1282 } 1283 } 1284 } 1285 }catch(Exception ex){ } 1286 return poolValues; 1287 } 1288 1289 public static HashSet formatXmlSunDatasources(HashMap dsMap){ 1290 HashSet datasources = new HashSet (); 1291 String [] keys = (String [])dsMap.keySet().toArray(new String [dsMap.size()]); 1292 for(int i=0; i<keys.length; i++){ 1293 String jndiName = keys[i]; 1294 HashMap poolValues = (HashMap )dsMap.get(jndiName); 1295 poolValues = formatPoolMap(poolValues); 1296 1297 String url = getStringVal(poolValues.get(__Url)); 1298 String username = getStringVal(poolValues.get(__User)); 1299 String password = getStringVal(poolValues.get(__Password)); 1300 String driverClassName = getStringVal(poolValues.get(__DriverClassName)); if((url != null) && (! url.equals (""))) { SunDatasource ds = new SunDatasource (jndiName, url, username, password, driverClassName); 1303 datasources.add (ds); 1304 } 1305 } 1306 1307 return datasources; 1308 } 1309 1310 private static HashMap formatPoolMap(HashMap poolValues){ 1311 String driverClassName = getStringVal(poolValues.get("dsClassName")); 1313 String url = ""; String serverName = getStringVal(poolValues.get(__ServerName)); 1315 String portNo = getStringVal(poolValues.get(__DerbyPortNumber)); 1316 String dbName = getStringVal(poolValues.get(__DerbyDatabaseName)); 1317 String dbVal = getStringVal(poolValues.get(__DatabaseName)); 1318 String portVal = getStringVal(poolValues.get(__PortNumber)); 1319 String sid = getStringVal(poolValues.get(__SID)); 1320 if(driverClassName.indexOf("pointbase") != -1){ 1321 url = getStringVal(poolValues.get(__DatabaseName)); 1322 }else if(driverClassName.indexOf("derby") != -1){ 1323 if(serverName != null){ 1324 url = "jdbc:derby://" + serverName; 1325 if(portNo != null) { 1326 url = url + ":" + portNo; } 1328 url = url + "/" + dbName ; } 1330 }else{ 1331 String in_url = getStringVal(poolValues.get(__Url)); 1332 if(in_url != null) { 1333 url = in_url; 1334 } 1335 if(url.equals("")) { String urlPrefix = DatabaseUtils.getUrlPrefix(driverClassName); 1337 String vName = ResourceConfigurator.getDatabaseVendorName(urlPrefix, null); 1338 if(serverName != null){ 1339 if(vName.equals("sybase2")){ url = urlPrefix + serverName; 1341 }else{ 1342 url = urlPrefix + "//" + serverName; } 1344 if(portVal != null) { 1345 url = url + ":" + portVal; } 1347 } 1348 if(vName.equals("sun_oracle") || vName.equals("datadirect_oracle")) { url = url + ";SID=" + sid; }else if(Arrays.asList(WizardConstants.Reqd_DBName).contains(vName)) { 1351 url = url + ";databaseName=" + dbVal; }else if(Arrays.asList(WizardConstants.VendorsDBNameProp).contains(vName)) { 1353 url = url + "/" + dbVal ; } 1355 } 1356 } 1357 1358 DatabaseConnection databaseConnection = getDatabaseConnection(url); 1359 if(databaseConnection != null) { 1360 driverClassName = databaseConnection.getDriverClass(); 1361 }else{ 1362 String drivername = DatabaseUtils.getDriverName(url); 1364 if(drivername != null) { 1365 driverClassName = drivername; 1366 } 1367 } 1368 1369 poolValues.put(__Url, url); 1370 poolValues.put(__DriverClassName, driverClassName); 1371 1372 return poolValues; 1373 } 1374 1375 public static boolean is90Server(ServerInterface mejb){ 1376 boolean is90Server = true; 1377 SunDeploymentManagerInterface sunDm = (SunDeploymentManagerInterface)mejb.getDeploymentManager(); 1378 if(sunDm.isLocal()){ 1379 is90Server = is90ServerLocal(sunDm); 1380 }else{ 1381 try{ 1382 ObjectName serverObj = new ObjectName ("com.sun.appserv:j2eeType=J2EEServer,name=server,category=runtime"); String serverName = (String )mejb.getAttribute(serverObj, "serverVersion"); if((serverName != null) && (serverName.indexOf("8.") != -1)) is90Server = false; 1386 }catch(Exception ex){ } 1387 } 1388 return is90Server; 1389 } 1390 1391 private static boolean is90ServerLocal(SunDeploymentManagerInterface sunDm){ 1392 boolean isGlassfish = true; 1393 try{ 1394 isGlassfish = ServerLocationManager.isGlassFish(sunDm.getPlatformRoot()); 1395 }catch(Exception ex){ } 1396 return isGlassfish; 1397 1398 } 1399 1400 private final static char BLANK = ' '; 1401 private final static char DOT = '.'; 1402 private final static char REPLACEMENT_CHAR = '_'; 1403 private final static char[] ILLEGAL_FILENAME_CHARS = {'/', '\\', ':', '*', '?', '"', '<', '>', '|', ',' }; 1404 private final static char[] ILLEGAL_RESOURCE_NAME_CHARS = {':', '*', '?', '"', '<', '>', '|', ',' }; 1405} 1406 | Popular Tags |