1 19 package org.netbeans.modules.j2ee.sun.share.configbean; 20 21 import java.beans.PropertyVetoException ; 22 import java.io.BufferedOutputStream ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.InputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.lang.ref.WeakReference ; 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.LinkedHashMap ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Set ; 39 import java.util.WeakHashMap ; 40 41 import javax.enterprise.deploy.model.DDBean ; 42 import javax.enterprise.deploy.model.DDBeanRoot ; 43 import javax.enterprise.deploy.model.DeployableObject ; 44 import javax.enterprise.deploy.shared.ModuleType ; 45 import javax.enterprise.deploy.spi.DConfigBean ; 46 import javax.enterprise.deploy.spi.DConfigBeanRoot ; 47 import javax.enterprise.deploy.spi.DeploymentManager ; 48 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException ; 49 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 50 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException ; 51 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 52 import org.openide.filesystems.FileSystem; 53 54 import org.xml.sax.SAXException ; 55 56 import org.openide.DialogDisplayer; 57 import org.openide.ErrorManager; 58 import org.openide.NotifyDescriptor; 59 import org.openide.filesystems.FileLock; 60 import org.openide.filesystems.FileObject; 61 import org.openide.filesystems.FileUtil; 62 import org.openide.loaders.DataObject; 63 import org.openide.loaders.DataObjectNotFoundException; 64 import org.openide.util.NbBundle; 65 import org.openide.util.RequestProcessor; 66 67 import org.netbeans.api.project.FileOwnerQuery; 68 import org.netbeans.api.project.Project; 69 import org.netbeans.api.project.ProjectInformation; 70 import org.netbeans.api.project.ProjectUtils; 71 import org.netbeans.modules.schema2beans.Schema2BeansRuntimeException; 72 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 73 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 74 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 75 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 76 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener; 77 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 78 79 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean; 80 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider; 81 import org.netbeans.modules.j2ee.sun.dd.api.DDException; 82 import org.netbeans.modules.j2ee.sun.dd.api.common.MessageDestination; 83 import org.netbeans.modules.j2ee.sun.dd.api.ejb.CmpResource; 84 import org.netbeans.modules.j2ee.sun.dd.api.ejb.MdbConnectionFactory; 85 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp; 86 87 import org.netbeans.modules.j2ee.sun.api.ResourceConfiguratorInterface; 88 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 89 import org.netbeans.modules.j2ee.sun.api.SunDeploymentConfigurationInterface; 90 91 import org.netbeans.modules.j2ee.sun.share.Constants; 92 import org.netbeans.modules.j2ee.sun.share.plan.DeploymentPlan; 93 import org.netbeans.modules.j2ee.sun.share.plan.FileEntry; 94 import org.netbeans.modules.j2ee.sun.share.config.ConfigDataObject; 95 import org.netbeans.modules.j2ee.sun.share.config.ConfigurationStorage; 96 import org.netbeans.modules.j2ee.sun.share.config.DDRoot; 97 import org.netbeans.modules.j2ee.sun.share.config.DDFilesListener; 98 import org.netbeans.modules.j2ee.sun.share.config.StandardDDImpl; 99 100 101 106 public class SunONEDeploymentConfiguration implements Constants, SunDeploymentConfigurationInterface { 108 private static WeakHashMap configurationMap = new WeakHashMap (); 111 112 public static void addConfiguration(File key, SunONEDeploymentConfiguration config) { 113 configurationMap.put(key, new WeakReference (config)); 114 } 115 116 public static void removeConfiguration(File key) { 117 configurationMap.remove(key); 118 } 119 120 public static SunONEDeploymentConfiguration getConfiguration(File key) { 121 SunONEDeploymentConfiguration config = null; 122 WeakReference ref = (WeakReference ) configurationMap.get(key); 123 if(ref != null) { 124 config = (SunONEDeploymentConfiguration) ref.get(); 125 } 126 return config; 127 } 128 129 private DeployableObject dObj; 130 private Map contentMap = new HashMap (); 131 private Map beanMap = new HashMap (); 132 private Map priorBeanMap = new HashMap (); 133 134 private final Object storageMonitor = new Object (); 135 private ConfigurationStorage storage = null; 136 137 142 private String deploymentModuleName="_default_"; 144 146 private File [] configFiles; 147 private File resourceDir; 148 private boolean keepUpdated; 149 150 private DDFilesListener ddFilesListener; 151 152 private static final RequestProcessor resourceProcessor = new RequestProcessor("sun-resource-ref"); 154 156 private ASDDVersion appServerVersion; 157 private ASDDVersion minASVersion; 158 private ASDDVersion maxASVersion; 159 private boolean deferredAppServerChange; 160 161 162 166 public SunONEDeploymentConfiguration(DeployableObject dObj) { 167 this.dObj = dObj; 168 169 this.appServerVersion = ASDDVersion.SUN_APPSERVER_8_1; 172 this.deferredAppServerChange = false; 173 } 174 175 183 public void init(File [] cfgFiles, File resourceDir, boolean keepUpdated) throws ConfigurationException { 184 configFiles = new File [cfgFiles.length]; 186 for(int i = 0; i < cfgFiles.length; i++) { configFiles[i] = cfgFiles[i]; 188 } 189 190 if(configFiles.length == 2 && configFiles[1] != null && "sun-ejb-jar.xml".equals(configFiles[1].getName())) { File tmp = configFiles[0]; 200 configFiles[0] = configFiles[1]; 201 configFiles[1] = tmp; 202 } 203 204 this.resourceDir = resourceDir; 205 this.keepUpdated = keepUpdated; 206 207 addConfiguration(configFiles[0], this); 208 209 J2eeModuleProvider provider = getProvider(configFiles[0].getParentFile()); 213 if(provider == null) { 214 throw new ConfigurationException ("No Project and/or J2eeModuleProvider located for " + configFiles[0].getPath()); } 216 217 224 J2eeModule j2eeModule = provider.getJ2eeModule(); 227 minASVersion = computeMinASVersion((ModuleType ) j2eeModule.getModuleType(), j2eeModule.getModuleVersion()); 228 maxASVersion = computeMaxASVersion(); 229 230 appServerVersion = maxASVersion; 231 232 238 if(keepUpdated) { 239 if(!ensureConfigurationLoaded()) { 241 throw new ConfigurationException ("DConfigBean storage failed initialization for " + configFiles[0].getName()); 242 } else { 243 BaseRoot masterRoot = getMasterDCBRoot(); 245 if(masterRoot != null && 246 J2EEVersion.J2EE_1_4.compareSpecification(masterRoot.getJ2EEModuleVersion()) >= 0 && 247 !configFiles[0].exists() 248 ) { 249 getStorage().setChanged(); 250 } 251 } 252 253 ddFilesListener = new DDFilesListener(this, provider); 260 } 261 } 262 263 public void dispose() { 264 272 SunONEDeploymentConfiguration storedCfg = getConfiguration(configFiles[0]); 273 if(storedCfg != this) { 274 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, 275 "Stored DeploymentConfiguration (" + storedCfg + ") instance not the one being disposed of (" + this + ")."); 276 } 277 278 if(storedCfg != null) { 279 removeConfiguration(configFiles[0]); 280 } 281 } 282 283 public void updateResourceDir(File resourceDir) { 284 this.resourceDir = resourceDir; 285 } 286 287 private void postResourceError(String resourceMsg) { 288 String folderMsg; 291 String projectName = getProjectName(configFiles[0]); 292 if(projectName != null) { 293 folderMsg = NbBundle.getMessage(SunONEDeploymentConfiguration.class, 294 "ERR_NoResourceFolderForProject", projectName); } else { 296 folderMsg = NbBundle.getMessage(SunONEDeploymentConfiguration.class, 297 "ERR_NoResourceFolderUnknown"); } 299 300 final String text = folderMsg + " " + resourceMsg; 301 resourceProcessor.post(new Runnable () { 302 public void run() { 303 NotifyDescriptor.Message msg = new NotifyDescriptor.Message(text, NotifyDescriptor.WARNING_MESSAGE); 304 DialogDisplayer.getDefault().notify(msg); 305 } 306 }); 307 } 308 309 public void ensureResourceDefined(DDBean ddBean) { 310 String xpath = ddBean.getXpath(); 312 int finalSlashIndex = xpath.lastIndexOf('/') + 1; 313 String type = (finalSlashIndex < xpath.length()) ? xpath.substring(finalSlashIndex) : ""; 315 if("message-driven".equals(type)) { BaseEjb theEjbDCB = getEjbDConfigBean(ddBean); 320 321 if(theEjbDCB == null) { 322 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ("EJB DConfigBean cannot be found for DDBean: " + ddBean)); 323 return; 324 } 325 326 ResourceConfiguratorInterface rci = getResourceConfigurator(); 327 String jndiName = theEjbDCB.getJndiName(); 329 if(isEJB3()) { 330 String ejbName = getField(ddBean, "ejb-name"); 331 332 if(!Utils.notEmpty(jndiName)) { 333 jndiName = getField(ddBean, "mapped-name", ejbName); } 337 338 if(!rci.isJMSResourceDefined(jndiName, resourceDir)) { 339 String destinationType = "javax.jms.Queue"; try { 343 DDBean [] activationNameFields = ddBean.getChildBean("activation-config/activation-config-property/activation-config-property-name"); 344 for(int i = 0; i < activationNameFields.length; i++) { 345 if("destinationType".equals(activationNameFields[i].getText())) { 346 DDBean [] activationValueFields = activationNameFields[i].getChildBean("../activation-config-property-value"); 347 if(activationValueFields.length > 0) { 348 String value = activationValueFields[0].getText(); 349 if(Utils.notEmpty(value)) { 350 destinationType = value; 351 break; 352 } 353 } 354 } 355 } 356 } catch(Exception ex) { 357 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 360 } 361 362 String messageDestinationName = getField(ddBean, "message-destination-link", ejbName); String messageDestinationType = getField(ddBean, "message-destination-type", destinationType); 366 if(resourceDir == null) { 367 postResourceError(NbBundle.getMessage(SunONEDeploymentConfiguration.class, 369 "ERR_NoJMSResource", theEjbDCB.getEjbName())); } else { 372 rci.createJMSResource(jndiName, messageDestinationType, messageDestinationName, ejbName, resourceDir); 373 } 374 } 375 } else { 376 if(!rci.isJMSResourceDefined(jndiName, resourceDir)) { 377 String ejbName = getField(ddBean, "ejb-name"); String messageDestinationName = getField(ddBean, "message-destination-link"); String messageDestinationType = getField(ddBean, "message-destination-type"); 381 if(resourceDir == null) { 382 postResourceError(NbBundle.getMessage(SunONEDeploymentConfiguration.class, 384 "ERR_NoJMSResource", theEjbDCB.getEjbName())); } else { 387 rci.createJMSResource(jndiName, messageDestinationType, messageDestinationName, ejbName, resourceDir); 388 } 389 390 MdbConnectionFactory mcf = getStorageFactory().createMdbConnectionFactory(); 391 String connectionFactoryJndiName= "jms/" + messageDestinationName + "Factory"; mcf.setJndiName(connectionFactoryJndiName); 393 try { 394 ((MDEjb) theEjbDCB).setMdbConnectionFactory(mcf); 395 } catch (PropertyVetoException ex) { 396 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 397 } 398 399 MessageDestination md = getStorageFactory().createMessageDestination(); 400 md.setMessageDestinationName(messageDestinationName); 401 md.setJndiName(theEjbDCB.getJndiName()); 402 EjbJarRoot root = (EjbJarRoot) theEjbDCB.getParent(); 403 try { 404 root.addMessageDestination(md); 405 } catch (PropertyVetoException ex) { 406 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 407 } 408 } 409 } 410 } else if("resource-ref".equals(type)) { if(ddBean instanceof StandardDDImpl) { 412 Object o = getDCBCache().get(ddBean); 413 if(o instanceof ResourceRef) { 414 ResourceRef theResRefDCB = (ResourceRef) o; 415 final String refName = getField(ddBean, "res-ref-name"); final String description = getField(ddBean, "description"); final File targetDir = resourceDir; 418 419 if(Utils.notEmpty(description)) { 423 if(resourceDir == null) { 424 postResourceError(NbBundle.getMessage(SunONEDeploymentConfiguration.class, 426 "ERR_NoRefJdbcDataSource", theResRefDCB.getResRefName())); return; 428 } 429 430 435 440 resourceProcessor.post(new Runnable () { 441 public void run() { 442 ResourceConfiguratorInterface rci = getResourceConfigurator(); 443 if(rci != null) { 444 rci.createJDBCDataSourceFromRef(refName, description, targetDir); 445 } 446 } 447 }, 500); 448 } 449 } else { 450 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "No ResourceRef DConfigBean found bound to resource-ref DDBean: " + ddBean); } 452 } else { 453 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "DDBean from wrong tree in ensureResourceDefined: " + ddBean); } 455 } else if("entity".equals(type)) { ensureResourceDefinedForEjb(ddBean, null); 457 } 458 } 459 460 public void ensureResourceDefinedForEjb(DDBean ddBean, String jndiName) { 461 BaseEjb theEjbDCB = getEjbDConfigBean(ddBean); 465 466 if(theEjbDCB == null) { 467 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ("EJB DConfigBean cannot be found for DDBean: " + ddBean)); return; 469 } 470 471 if(theEjbDCB instanceof CmpEntityEjb) { 472 ResourceConfiguratorInterface rci = getResourceConfigurator(); 473 CmpEntityEjb cmpEjbDCB = (CmpEntityEjb) theEjbDCB; 474 475 if(resourceDir == null) { 476 postResourceError(NbBundle.getMessage(SunONEDeploymentConfiguration.class, 479 "ERR_NoCmpOrJdbcDataSource", cmpEjbDCB.getEjbName())); return; 481 } 482 483 if (jndiName == null) { 484 String description = getField(ddBean, "description"); jndiName = rci.createJDBCDataSourceForCmp(cmpEjbDCB.getEjbName(), description, resourceDir); 486 } 487 488 if(jndiName != null) { 490 Base parentDCB = cmpEjbDCB.getParent(); 491 if(parentDCB instanceof EjbJarRoot) { 492 EjbJarRoot ejbJarRoot = (EjbJarRoot) parentDCB; 493 CmpResource cmpResource = null; 494 if(ejbJarRoot.getCmpResource() == null) { 495 cmpResource = getStorageFactory().createCmpResource(); 496 } else { 497 cmpResource = (CmpResource)ejbJarRoot.getCmpResource().clone(); 498 } 499 cmpResource.setJndiName(jndiName); 500 try { 501 ejbJarRoot.setCmpResource(cmpResource); 502 } catch(PropertyVetoException ex) { 503 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 505 } 506 } else { 507 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ("CmpEntityBean DConfigBean parent is of wrong type: " + parentDCB)); } 510 } 511 } 512 } 513 514 private boolean isEJB3() { 515 boolean result = false; 516 517 String j2eeModuleVersion = dObj.getModuleDTDVersion(); 518 EjbJarVersion ejbJarVersion = EjbJarVersion.getEjbJarVersion(j2eeModuleVersion); 519 if(EjbJarVersion.EJBJAR_3_0.compareTo(ejbJarVersion) <= 0) { 520 result = true; 521 } 522 523 return result; 524 } 525 526 private ASDDVersion computeMinASVersion(ModuleType moduleType, String j2eeModuleVersion) { 527 ASDDVersion result = ASDDVersion.SUN_APPSERVER_7_0; 528 529 if(ModuleType.WAR.equals(moduleType)) { 530 ServletVersion servletVersion = ServletVersion.getServletVersion(j2eeModuleVersion); 531 if(ServletVersion.SERVLET_2_4.equals(servletVersion)) { 532 result = ASDDVersion.SUN_APPSERVER_8_1; 533 } else if(ServletVersion.SERVLET_2_5.equals(servletVersion)) { 534 result = ASDDVersion.SUN_APPSERVER_9_0; 535 } 536 } else if(ModuleType.EJB.equals(moduleType)) { 537 EjbJarVersion ejbJarVersion = EjbJarVersion.getEjbJarVersion(j2eeModuleVersion); 538 if(EjbJarVersion.EJBJAR_2_1.equals(ejbJarVersion)) { 539 result = ASDDVersion.SUN_APPSERVER_8_1; 540 } else if(EjbJarVersion.EJBJAR_3_0.equals(ejbJarVersion)) { 541 result = ASDDVersion.SUN_APPSERVER_9_0; 542 } 543 } else if(ModuleType.EAR.equals(moduleType)) { 544 ApplicationVersion applicationVersion = ApplicationVersion.getApplicationVersion(j2eeModuleVersion); 545 if(ApplicationVersion.APPLICATION_1_4.equals(applicationVersion)) { 546 result = ASDDVersion.SUN_APPSERVER_8_1; 547 } else if(ApplicationVersion.APPLICATION_5_0.equals(applicationVersion)) { 548 result = ASDDVersion.SUN_APPSERVER_9_0; 549 } 550 } else if(ModuleType.CAR.equals(moduleType)) { 551 AppClientVersion appClientVersion = AppClientVersion.getAppClientVersion(j2eeModuleVersion); 552 if(AppClientVersion.APP_CLIENT_1_4.equals(appClientVersion)) { 553 result = ASDDVersion.SUN_APPSERVER_8_1; 554 } else if(AppClientVersion.APP_CLIENT_5_0.equals(appClientVersion)) { 555 result = ASDDVersion.SUN_APPSERVER_9_0; 556 } 557 } else { 558 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Unsupported J2EE module type: " + moduleType); result = ASDDVersion.SUN_APPSERVER_8_1; 560 } 561 562 return result; 563 } 564 565 private ASDDVersion computeMaxASVersion() { 566 ASDDVersion result = getTargetAppServerVersion(); 569 if(result == null) { 570 result = ASDDVersion.SUN_APPSERVER_9_0; 571 ErrorManager.getDefault().log(ErrorManager.WARNING, NbBundle.getMessage( 572 SunONEDeploymentConfiguration.class, "ERR_UnidentifiedTargetServer", result.toString())); } 574 return result; 575 } 576 577 public ASDDVersion getMinASVersion() { 578 return minASVersion; 579 } 580 581 public ASDDVersion getMaxASVersion() { 582 return maxASVersion; 583 } 584 585 public StorageBeanFactory getStorageFactory() { 586 return appServerVersion.getStorageFactory(); 587 } 588 589 596 public ASDDVersion getAppServerVersion() { 597 return appServerVersion; 598 } 599 600 607 public void setAppServerVersion(ASDDVersion asVersion) { 608 if(asVersion.compareTo(getMinASVersion()) < 0) { 609 throw new IllegalArgumentException (asVersion.toString() + 610 " is lower than required minimum version " + getMinASVersion().toString()); 611 } 612 613 if(asVersion.compareTo(getMaxASVersion()) > 0) { 614 throw new IllegalArgumentException (asVersion.toString() + 615 " is higher than required maximum version " + getMaxASVersion().toString()); 616 } 617 618 if(!asVersion.equals(appServerVersion) || deferredAppServerChange) { 619 appServerVersion = asVersion; 620 ConfigurationStorage storage = getStorage(); 621 if(storage != null) { 622 deferredAppServerChange = false; 623 storage.setChanged(); 624 } 625 } 626 } 627 628 640 void internalSetAppServerVersion(ASDDVersion asVersion) { 641 if(!asVersion.equals(appServerVersion)) { 642 appServerVersion = asVersion; 643 deferredAppServerChange = true; 644 } 645 } 646 647 651 public File [] getConfigFiles() { 652 return configFiles; 653 } 654 655 private BaseEjb getEjbDConfigBean(DDBean ejbDDBean) { 656 BaseEjb theEjbDCB = null; 657 658 try { 659 DDBean realEjbDDBean = getStorage().normalizeEjbDDBean(ejbDDBean); 660 DDBeanRoot ddBeanRoot = realEjbDDBean.getRoot(); 661 DConfigBeanRoot dcbRoot = getDConfigBeanRoot(ddBeanRoot); 662 DConfigBean dcb = dcbRoot.getDConfigBean(realEjbDDBean); 663 if(dcb instanceof BaseEjb) { 664 theEjbDCB = (BaseEjb) dcb; 665 } 666 } catch(ConfigurationException ex) { 667 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 670 } catch(NullPointerException ex) { 671 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 676 } 677 678 return theEjbDCB; 679 } 680 681 683 private String getField(DDBean bean, String fieldId) { 684 String result = null; 685 686 try { 687 DDBean [] childFields = bean.getChildBean(fieldId); 688 if(childFields.length > 0) { 689 result = childFields[0].getText(); 690 } 691 } catch(Exception ex) { 692 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 697 } 698 699 return result; 700 } 701 702 private String getField(DDBean bean, String fieldId, String defaultValue) { 703 String result = getField(bean, fieldId); 704 705 if(!Utils.notEmpty(result)) { 706 result = defaultValue; 707 } 708 709 return result; 710 } 711 712 private ResourceConfiguratorInterface getResourceConfigurator() { 713 ResourceConfiguratorInterface rci = null; 714 DeploymentManager dm = getDeploymentManager(); 715 if(dm instanceof SunDeploymentManagerInterface) { 716 SunDeploymentManagerInterface sdmi = (SunDeploymentManagerInterface) dm; 717 rci = sdmi.getResourceConfigurator(); 718 } else { 719 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ("Invalid DeploymentManager: " + dm)); 720 } 721 return rci; 722 } 723 724 private DeploymentManager getDeploymentManager() { 725 DeploymentManager dm = null; 726 J2eeModuleProvider provider = getProvider(configFiles[0]); 727 if(provider != null) { 728 InstanceProperties ip = provider.getInstanceProperties(); 729 if(ip != null) { 730 dm = ip.getDeploymentManager(); 731 } else { 732 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new NullPointerException ("Null Server InstanceProperties: " + ip)); 733 } 734 } else { 735 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new NullPointerException ("Null J2eeModuleProvider: " + provider)); 736 } 737 return dm; 738 } 739 740 private J2eeModuleProvider getProvider(File file) { 741 J2eeModuleProvider provider = null; 742 if(file != null) { 743 FileObject fo = FileUtil.toFileObject(file); 744 if(fo != null) { 745 Project project = FileOwnerQuery.getOwner(fo); 746 if (project != null) { 747 org.openide.util.Lookup lookup = project.getLookup(); 748 provider = (J2eeModuleProvider) lookup.lookup(J2eeModuleProvider.class); 749 } 750 } else { 751 File parent = file.getParentFile(); 752 if(parent != null) { 753 provider = getProvider(parent); 754 } 755 } 756 } 757 return provider; 758 } 759 760 private String getProjectName(File file) { 761 String result = null; 762 FileObject fo = FileUtil.toFileObject(file); 763 if(fo != null) { 764 Project project = FileOwnerQuery.getOwner(fo); 765 if (project != null) { 766 ProjectInformation info = ProjectUtils.getInformation(project); 767 if(info != null) { 768 result = info.getName(); 769 } 770 } 771 } 772 return result; 773 } 774 775 776 784 public DConfigBeanRoot getDConfigBeanRoot(DDBeanRoot dDBeanRoot) throws ConfigurationException { 785 793 if (null == dDBeanRoot) { 794 throw Utils.makeCE("ERR_DDBeanIsNull", null, null); 795 } 796 797 if (null == dDBeanRoot.getXpath()) { 798 throw Utils.makeCE("ERR_DDBeanHasNullXpath", null, null); 799 } 800 801 if(!(dDBeanRoot instanceof DDRoot)) { 803 if(getDCBRootCache().entrySet().size() == 0) { 805 throw Utils.makeCE("ERR_CannotNormalizeDDBean", 806 new Object [] { dDBeanRoot.getClass().getName() }, null); 807 } 808 dDBeanRoot = getStorage().normalizeDDBeanRoot(dDBeanRoot); 809 } 810 811 BaseRoot rootDCBean = (BaseRoot) getDCBRootCache().get(dDBeanRoot); 812 813 if(null == rootDCBean) { 814 DCBFactory factory = (DCBFactory) getDCBFactoryMap().get(dDBeanRoot.getXpath()); 815 if(factory != null) { 816 rootDCBean = (BaseRoot) factory.createDCB(dDBeanRoot, null); 817 if(rootDCBean != null) { 818 getDCBCache().put(dDBeanRoot, rootDCBean); 819 getDCBRootCache().put(dDBeanRoot, rootDCBean); 820 } 821 } 822 } 823 824 return rootDCBean; 825 } 826 827 830 public DeployableObject getDeployableObject() { 831 return dObj; 832 } 833 834 839 public void removeDConfigBean(DConfigBeanRoot dConfigBeanRoot) throws BeanNotFoundException { 840 jsr88Logger.entering(this.getClass().toString(), "removeDConfigBean", dConfigBeanRoot); 841 842 if(null != dConfigBeanRoot) { 843 DDBeanRoot key = (DDBeanRoot ) dConfigBeanRoot.getDDBean(); 844 845 BaseRoot deadBean = (BaseRoot) getDCBCache().get(key); 847 if(deadBean != null) { 848 851 Object children[] = deadBean.getChildren().toArray(); 854 for(int i = 0; i < children.length; i++) { 855 try { 856 deadBean.removeDConfigBean((Base) children[i]); 857 } catch(BeanNotFoundException ex) { 858 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 861 } 862 } 863 864 getDCBCache().remove(key); 866 getDCBRootCache().remove(key); 867 868 deadBean.cleanup(); 870 deadBean = null; 871 } else { 872 throw new BeanNotFoundException (NbBundle.getMessage(SunONEDeploymentConfiguration.class, 874 "ERR_DConfigBeanRootNotFoundOnRemove", dConfigBeanRoot.toString())); 875 } 876 } 877 878 jsr88Logger.exiting(this.getClass().toString(), "removeDConfigBean", dConfigBeanRoot); 879 } 880 881 888 public void restore(InputStream inputStream) throws ConfigurationException { 889 jsr88Logger.entering(this.getClass().toString(), "restore", inputStream); 890 891 restoreDConfigBean(inputStream, null); 892 893 jsr88Logger.exiting(this.getClass().toString(), "restore", inputStream); 894 } 895 896 902 public DConfigBeanRoot restoreDConfigBean(InputStream inputStream, DDBeanRoot dDBeanRoot) throws ConfigurationException { 903 jsr88Logger.finest("S1DepConfig:restoreDConfigBean(jiIS,DDBeanRoot)"); 905 909 DeploymentPlan dp = null; 910 911 beanMap.clear(); 913 914 try { 915 if (null != inputStream) { 916 try { 917 if (this.dObj.getType().equals(ModuleType.WAR)) { 918 try { 921 SunWebApp sunW = DDProvider.getDefault().getWebDDRoot(inputStream); 922 dp = DeploymentPlan.createGraph(); 923 FileEntry fe = new FileEntry(); 924 fe.setName("sun-web.xml"); 925 String s = new String (); 926 java.io.StringWriter strWriter = new java.io.StringWriter (); 927 sunW.write(strWriter); 928 fe.setContent(strWriter.toString()); 929 dp.addFileEntry(fe); 930 } catch(DDException ex) { 931 jsr88Logger.finest(ex.getClass().getName() + " while processing sun-web.xml into a deployment plan: " + ex.getLocalizedMessage()); 933 dp = DeploymentPlan.createGraph(); 934 } catch(SAXException ex) { 935 jsr88Logger.finest(ex.getClass().getName() + " while processing sun-web.xml into a deployment plan: " + ex.getLocalizedMessage()); 937 dp = DeploymentPlan.createGraph(); 938 } catch(IOException ex) { 939 jsr88Logger.finest(ex.getClass().getName() + " while processing sun-web.xml into a deployment plan: " + ex.getLocalizedMessage()); 941 dp = DeploymentPlan.createGraph(); 942 } 943 } else { 944 dp = DeploymentPlan.createGraph(inputStream); 945 } 946 } catch (Schema2BeansRuntimeException s2bre) { 947 jsr88Logger.finest("the stream did not have a deployment plan"); 950 dp = DeploymentPlan.createGraph(); 951 } 952 } else { 953 jsr88Logger.finest("the stream was null"); 954 } 955 FileEntry [] entries = new FileEntry[0]; 956 if (null != dp) { 957 FileEntry tentries[] = dp.getFileEntry(); 958 if (null != tentries) { 959 entries = tentries; 960 } 961 } 962 for (int i = 0; i < entries.length ; i++) { 963 String key = Utils.getFQNKey(entries[i].getUri(), entries[i].getName()); contentMap.put(key, entries[i].getContent().getBytes()); 965 } 966 } catch(Schema2BeansRuntimeException ex) { 967 jsr88Logger.finest("Schema2Beans threw a Runtime Exception"); 968 } catch (Exception ex) { 969 jsr88Logger.finest("foo"); 970 ConfigurationException ce = 971 new ConfigurationException ("bad plan stream"); 972 ce.initCause(ex); 973 } 975 976 List pending = new ArrayList (); 980 BaseRoot rootToRestore = null; 981 982 if (null == dDBeanRoot) { 983 Iterator rootIter = getDCBRootCache().entrySet().iterator(); 985 while(rootIter.hasNext()) { 986 pending.add(((Map.Entry ) rootIter.next()).getValue()); 987 } 988 989 rootToRestore = getMasterDCBRoot(); 990 } else { 991 rootToRestore = (BaseRoot) getDCBRootCache().get(dDBeanRoot); 992 if(null != rootToRestore) { 993 pending.add(rootToRestore); 994 } 995 } 996 997 int index = 0; 998 while (index < pending.size()) { 999 Base current = (Base) pending.get(index); 1000 try { 1001 current.loadFromPlanFile(this); 1002 } 1003 catch (java.lang.IllegalStateException ise) { 1004 jsr88Logger.throwing(current.getClass().toString(), "loadFromPlanFile", ise); 1005 assert ise == null; 1006 } 1007 pending.addAll(current.getChildren()); 1008 index++; 1009 } 1010 1011 return rootToRestore; 1012 } 1013 1014 1018 public void save(OutputStream outputStream) throws ConfigurationException { 1019 jsr88Logger.entering(this.getClass().toString(), "save", outputStream); 1020 1021 saveDConfigBean(outputStream, null); 1022 1023 jsr88Logger.exiting(this.getClass().toString(), "save", outputStream); 1024 } 1025 1026 1031 public void saveDConfigBean(OutputStream outputStream, DConfigBeanRoot rootBean) throws ConfigurationException { 1032 Object [] params = new Object [] {outputStream, rootBean} ; 1033 jsr88Logger.entering(this.getClass().toString(), "save", params); 1034 1035 boolean useUriDataAtSave = false; 1036 1040 Map outputGraphs = new LinkedHashMap (); 1041 Map cmpGraphs = new LinkedHashMap (); 1043 try { 1044 if(rootBean == null) { 1048 Iterator rootIter = getDCBRootCache().entrySet().iterator(); 1049 while(rootIter.hasNext()) { 1050 Base dcb = (Base) ((Map.Entry ) rootIter.next()).getValue(); 1051 dcb.addToGraphs(outputGraphs, null, ""); 1052 1053 if(dcb instanceof EjbJarRoot) { 1055 EjbJarRoot ejbJar = (EjbJarRoot) dcb; 1056 Snippet cmpSnippet = ejbJar.getCmpMappingSnippet(); 1057 if(cmpSnippet.hasDDSnippet()) { 1058 String snippetKey = Utils.getFQNKey(ejbJar.getUriText(), cmpSnippet.getFileName()); 1059 cmpGraphs.put(snippetKey, cmpSnippet.getCmpDDSnippet()); 1060 } 1061 } 1062 } 1063 } else { 1064 ((Base) rootBean).addToGraphs(outputGraphs, null, ""); 1065 1066 if(rootBean instanceof EjbJarRoot) { 1068 EjbJarRoot ejbJar = (EjbJarRoot) rootBean; 1069 Snippet cmpSnippet = ejbJar.getCmpMappingSnippet(); 1070 if(cmpSnippet.hasDDSnippet()) { 1071 String snippetKey = Utils.getFQNKey(ejbJar.getUriText(), cmpSnippet.getFileName()); 1072 cmpGraphs.put(snippetKey, cmpSnippet.getCmpDDSnippet()); 1073 } 1074 } 1075 } 1076 1077 Set keys = outputGraphs.keySet(); 1080 Iterator iter = keys.iterator(); 1081 DeploymentPlan dp = new DeploymentPlan(); 1082 CommonDDBean bean = null; 1083 while (iter.hasNext()) { 1084 Object k = iter.next(); 1085 bean = (CommonDDBean) outputGraphs.get(k); 1086 if (null != bean) { 1087 String keyString = (String ) k; 1088 String uri = Utils.getUriFromKey(keyString); 1089 String fname = Utils.getFilenameFromKey(keyString); 1090 FileEntry fe = new FileEntry(); 1091 fe.setName(fname); 1092 if (useUriDataAtSave && uri.length() > 0) { 1093 fe.setUri(uri); 1094 } 1095 String s = new String (); 1096 java.io.StringWriter strWriter = new java.io.StringWriter (); 1097 bean.write(strWriter); 1098 fe.setContent(strWriter.toString()); 1099 dp.addFileEntry(fe); 1100 } else { 1101 jsr88Logger.warning("no bean for key: " + k); 1102 } 1103 } 1104 1105 keys = cmpGraphs.keySet(); 1107 iter = keys.iterator(); 1108 while (iter.hasNext()) { 1109 Object k = iter.next(); 1110 org.netbeans.modules.schema2beans.BaseBean cmpRoot = 1111 (org.netbeans.modules.schema2beans.BaseBean) cmpGraphs.get(k); 1112 if (null != cmpRoot) { 1113 String keyString = (String ) k; 1114 String uri = Utils.getUriFromKey(keyString); 1115 String fname = Utils.getFilenameFromKey(keyString); 1116 FileEntry fe = new FileEntry(); 1117 fe.setName(fname); 1118 if (useUriDataAtSave && uri.length() > 0) { 1119 fe.setUri(uri); 1120 } 1121 java.io.StringWriter strWriter = new java.io.StringWriter (); 1122 cmpRoot.write(strWriter); 1123 fe.setContent(strWriter.toString()); 1124 dp.addFileEntry(fe); 1125 } else { 1126 jsr88Logger.warning("no bean for key: " + k); 1127 } 1128 } 1129 1130 if (this.dObj.getType().equals(ModuleType.WAR)) { 1131 if (null != bean) { 1132 bean.write(outputStream); 1133 } 1134 } else { 1135 dp.write(outputStream); 1136 } 1137 } catch (Exception ex) { 1138 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1139 ConfigurationException ce = new ConfigurationException ("Sun Deployment Descriptor Error"); ce.initCause(ex); 1141 } 1142 jsr88Logger.exiting(this.getClass().toString(), "Save", params); } 1144 1145 1153 Object getBeans(String uri, String fileName, ConfigParser parser, 1154 ConfigFinder finder) { 1155 String key = Utils.getFQNKey(uri, fileName); 1156 Object root = beanMap.get(key); 1157 1158 if(root == null) { 1159 byte[] content = (byte[]) contentMap.get(key); 1161 if(content == null) { 1162 return null; 1163 } 1164 1165 if(parser == null) { 1166 jsr88Logger.severe("Missing parser"); 1167 return null; 1168 } 1169 1170 try { 1171 root = parser.parse(new ByteArrayInputStream (content)); 1172 } catch(Exception ex) { 1173 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1174 root = priorBeanMap.remove(key); 1175 if(root == null) { 1176 return null; 1179 } 1180 } 1181 1182 beanMap.put(key, root); 1183 } 1184 1185 Object result = finder.find(root); 1186 return result; 1187 } 1188 1189 1191 1194 public boolean ensureConfigurationLoaded() { 1195 boolean result = (getStorage() != null); 1197 return result; 1198 } 1199 1200 1205 public ConfigurationStorage getStorage() { 1206 ConfigurationStorage theStorage = null; 1207 1208 1211 synchronized (storageMonitor) { 1212 theStorage = storage; 1213 } 1214 1215 if(theStorage == null) { 1216 J2eeModuleProvider provider = getProvider(configFiles[0].getParentFile()); 1219 if(provider == null) { 1220 throw new IllegalStateException ("No Project and/or J2eeModuleProvider located for " + configFiles[0].getPath()); 1223 } 1224 1225 synchronized (storageMonitor) { 1226 if(storage == null) { 1227 try { 1228 storage = new ConfigurationStorage(provider, this); 1229 } catch (IOException ex) { 1230 ErrorManager.getDefault().notify(ex); 1231 } catch(InvalidModuleException ex) { 1232 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1233 } catch(SAXException ex) { 1234 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1235 } catch(ConfigurationException ex) { 1236 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1237 } 1238 } 1239 theStorage = storage; 1240 } 1241 } 1242 1243 return theStorage; 1244 } 1245 1246 1250 public WebAppRoot getWebAppRoot() { 1251 WebAppRoot war = null; 1252 1253 DDBeanRoot root = dObj.getDDBeanRoot(); 1254 if(null != root) { 1255 try { 1256 ConfigurationStorage storage = getStorage(); 1257 if(storage != null) { 1258 DConfigBeanRoot dcbRoot = getDConfigBeanRoot(storage.normalizeDDBeanRoot(root)); 1259 if(dcbRoot instanceof WebAppRoot) { 1260 war = (WebAppRoot) dcbRoot; 1261 } 1262 } 1263 } catch (ConfigurationException ex) { 1264 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 1265 } 1266 } else { 1267 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "DeployableObject with null DDBeanRoot!!!"); 1268 } 1269 1270 return war; 1271 } 1272 1273 1277 public EjbJarRoot getEjbJarRoot() { 1278 EjbJarRoot ejbJar = null; 1279 1280 DDBeanRoot root = dObj.getDDBeanRoot(); 1281 if(null != root) { 1282 try { 1283 ConfigurationStorage storage = getStorage(); 1284 if(storage != null) { 1285 DConfigBeanRoot dcbRoot = getDConfigBeanRoot(storage.normalizeDDBeanRoot(root)); 1286 if(dcbRoot instanceof EjbJarRoot) { 1287 ejbJar = (EjbJarRoot) dcbRoot; 1288 } 1289 } 1290 } catch (ConfigurationException ex) { 1291 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 1292 } 1293 } else { 1294 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "DeployableObject with null DDBeanRoot!!!"); 1295 } 1296 1297 return ejbJar; 1298 } 1299 1300 1303 public void setContextRoot(String contextRoot){ 1304 if (dObj.getType().equals(ModuleType.WAR)) { 1305 WebAppRoot war = getWebAppRoot(); 1306 if(war != null) { 1307 try { 1308 war.setContextRoot(contextRoot); 1309 } catch(java.beans.PropertyVetoException ex){ 1310 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1312 } 1313 } 1314 } else { 1315 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "SunONEDeploymentConfiguration.setContextRoot() invoked on incorrect module type: " + dObj.getType()); 1316 } 1317 } 1318 1319 1322 public String getContextRoot() { 1323 String contextRoot = null; 1324 if (dObj.getType().equals(ModuleType.WAR)) { 1325 WebAppRoot war = getWebAppRoot(); 1326 if(war != null) { 1327 contextRoot = war.getContextRoot(); 1328 } else { 1329 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "SunONEDeploymentConfiguration.getContextRoot(): No WebAppRoot DConfigBean found for module."); 1330 } 1331 } else { 1332 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "SunONEDeploymentConfiguration.getContextRoot() invoked on incorrect module type: " + dObj.getType()); 1333 } 1334 return contextRoot; 1335 } 1336 1337 1340 public String getDeploymentModuleName(){ 1341 return deploymentModuleName; 1342 } 1343 1344 1347 public void setDeploymentModuleName(String s){ 1348 deploymentModuleName = s; 1349 } 1350 1351 1354 public WebServices getWebServicesRoot() { 1355 WebServices wsr = null; 1356 1357 Map rootMap = getDCBRootCache(); 1358 if(rootMap.size() > 1) { 1359 Iterator rootIterator = rootMap.entrySet().iterator(); 1362 rootIterator.next(); 1363 Object root = ((Map.Entry ) rootIterator.next()).getValue(); 1364 if(root instanceof WebServices) { 1365 wsr = (WebServices) root; 1366 } 1367 } 1368 1369 return wsr; 1370 } 1371 1372 1373 1385 private Map moduleDCBCache = new LinkedHashMap (13); 1386 private Map completeDCBCache = new LinkedHashMap (63); 1387 private Map patchCache = new LinkedHashMap (13); 1388 1389 1393 Map getDCBCache() { 1394 return completeDCBCache; 1395 } 1396 1397 1401 Map getDCBRootCache() { 1402 return moduleDCBCache; 1403 } 1404 1405 1412 Map getPatchList() { 1413 return patchCache; 1414 } 1415 1416 1427 BaseRoot getMasterDCBRoot() { 1428 BaseRoot masterRoot = null; 1429 Iterator rootIterator = moduleDCBCache.entrySet().iterator(); 1430 while(rootIterator.hasNext()) { 1431 BaseRoot tmpRoot = (BaseRoot) ((Map.Entry ) rootIterator.next()).getValue(); 1432 if(!(tmpRoot instanceof WebServices)) { 1436 masterRoot = tmpRoot; 1437 break; 1438 } 1439 } 1440 return masterRoot; 1441 } 1442 1443 1446 private Map dcbFactoryMap = null; 1447 1448 1452 private Map getDCBFactoryMap() { 1453 if(dcbFactoryMap == null) { 1454 dcbFactoryMap = new HashMap (17); 1455 1456 dcbFactoryMap.put("/application", new DCBTopRootFactory(AppRoot.class)); dcbFactoryMap.put("/ejb-jar", new DCBTopRootFactory(EjbJarRoot.class)); dcbFactoryMap.put("/web-app", new DCBTopRootFactory(WebAppRoot.class)); dcbFactoryMap.put("/application-client", new DCBTopRootFactory(AppClientRoot.class)); } 1464 1465 return dcbFactoryMap; 1466 } 1467 1468 1471 private class DCBTopRootFactory implements DCBFactory { 1472 1473 private Class dcbRootClass; 1474 1475 DCBTopRootFactory(Class c) { 1476 dcbRootClass = c; 1477 } 1478 1479 public Base createDCB(DDBean ddBean, Base dcbParent) throws ConfigurationException { 1480 if(ddBean == null) { 1481 throw Utils.makeCE("ERR_RootDDBeanIsNull", null, null); } 1483 1484 if(!(ddBean instanceof DDBeanRoot )) { 1485 Object [] args = new Object [1]; 1486 args[0] = dcbRootClass.getName(); 1487 throw Utils.makeCE("ERR_RootDDBeanWrongType", args, null); } 1489 1490 DDBeanRoot ddbRoot = (DDBeanRoot ) ddBean; 1491 BaseRoot newDCB = null; 1492 1493 try { 1494 newDCB = (BaseRoot) dcbRootClass.newInstance(); 1495 newDCB.init(ddbRoot, SunONEDeploymentConfiguration.this, ddbRoot); 1496 } catch(InstantiationException ex) { 1497 Object [] args = new Object [1]; 1498 args[0] = dcbRootClass.getName(); 1499 throw Utils.makeCE("ERR_UnexpectedInstantiateException", args, ex); } catch(IllegalAccessException ex) { 1501 Object [] args = new Object [1]; 1502 args[0] = dcbRootClass.getName(); 1503 throw Utils.makeCE("ERR_UnexpectedIllegalAccessException", args, ex); } catch (RuntimeException ex) { 1505 throw Utils.makeCE("ERR_UnexpectedRuntimeException", null, ex); } 1507 1508 return newDCB; 1509 } 1510 } 1511 1512 1514 static private int BUF_LEN = 1024; 1515 1516 public void addFileToPlanForModule(File f, DeployableObject mod, ConfigurationStorage storage) throws ConfigurationException { 1518 String uri = getUriForDeployableObject(mod, storage); 1520 String fname = f.getName(); 1522 String key = Utils.getFQNKey(uri,fname); 1523 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 1525 FileInputStream fis = null; 1526 int totalRead = 0; 1527 try { 1528 fis = new FileInputStream (f); 1529 byte[] buf = new byte[BUF_LEN]; 1530 int lastRead = 0; 1531 do { 1532 lastRead = fis.read(buf); 1533 if (lastRead > -1) { 1534 baos.write(buf, 0, lastRead); 1535 totalRead += lastRead; 1536 } 1537 } 1538 while (lastRead > -1); 1539 } 1540 catch (java.io.FileNotFoundException fnfe) { 1541 } 1544 catch (java.io.IOException ioe) { 1545 ConfigurationException ce = new ConfigurationException ("Failed while reading"); ce.initCause(ioe); 1548 throw ce; 1549 } 1550 finally { 1551 if (null != fis) { 1552 try { 1553 fis.close(); 1554 } catch (java.io.IOException ioe) { 1555 } 1557 } 1558 } 1559 if (totalRead > 0) { 1564 contentMap.put(key, baos.toByteArray()); 1565 } 1566 else { 1567 contentMap.remove(key); 1569 } 1570 1571 Object oldMap = beanMap.remove(key); 1574 1575 if(oldMap != null) { 1577 priorBeanMap.put(key, oldMap); 1578 } 1579 } 1580 1581 public void extractFileFromPlanForModule(File f, DeployableObject mod, ConfigurationStorage storage) throws IOException { 1582 String uri = getUriForDeployableObject(mod, storage); 1584 final String fname = f.getName(); 1585 1586 String key = Utils.getFQNKey(uri,fname); 1588 1589 final byte[] content = (byte[]) contentMap.get(key); 1591 1592 if (null != content) { 1594 File parentFile = f.getParentFile(); 1595 if(!parentFile.exists()) { 1596 parentFile.mkdirs(); 1597 } 1598 1599 final FileObject folder = FileUtil.toFileObject(parentFile); 1600 if(folder == null) { 1601 String errorMsg = NbBundle.getMessage(SunONEDeploymentConfiguration.class, 1603 "ERR_SaveError", fname) + " " + NbBundle.getMessage(SunONEDeploymentConfiguration.class, 1605 "ERR_FailedToCreateConfigFolder", parentFile.getAbsolutePath()); throw new IOException (errorMsg); 1607 } else if(!folder.isFolder()) { 1608 String errorMsg = NbBundle.getMessage(SunONEDeploymentConfiguration.class, 1610 "ERR_SaveError", fname) + " " + NbBundle.getMessage(SunONEDeploymentConfiguration.class, 1612 "ERR_ConfigFolderNotDirectory", parentFile.getAbsolutePath()); throw new IOException (errorMsg); 1614 } 1615 1616 FileSystem fs = folder.getFileSystem(); 1617 fs.runAtomicAction(new FileSystem.AtomicAction() { 1618 public void run() throws IOException { 1619 FileLock lock = null; 1620 OutputStream out = null; 1621 try { 1622 FileObject configFO = folder.getFileObject(fname); 1623 if (configFO == null) { 1624 configFO = folder.createData(fname); 1625 } 1626 lock = configFO.lock(); 1627 out = new BufferedOutputStream (configFO.getOutputStream(lock), 4096); 1628 out.write(content); 1629 } finally { 1630 if (out != null) { 1631 try { out.close(); } catch(IOException ioe) {} 1632 } 1633 if (lock != null) { 1634 lock.releaseLock(); 1635 } 1636 } 1637 } 1638 }); 1639 } 1640 } 1641 1642 private String getUriForDeployableObject(DeployableObject mod, ConfigurationStorage storage) { 1643 String rootUri = ""; if(ModuleType.EAR.equals(mod.getType())) { 1649 rootUri = "EAR"; } 1651 return rootUri; 1652 } 1653 1654 void updateContentMap(DConfigBeanRoot rootBean) throws ConfigurationException { 1657 jsr88Logger.entering(this.getClass().toString(), "save", rootBean); 1659 1664 Map outputGraphs = new LinkedHashMap (); 1665 Map cmpGraphs = new LinkedHashMap (); try { 1667 if(rootBean == null) { 1671 Iterator rootIter = getDCBRootCache().entrySet().iterator(); 1672 while(rootIter.hasNext()) { 1673 Base dcb = (Base) ((Map.Entry ) rootIter.next()).getValue(); 1674 dcb.addToGraphs(outputGraphs, null, ""); 1676 if(dcb instanceof EjbJarRoot) { 1678 EjbJarRoot ejbJar = (EjbJarRoot) dcb; 1679 Snippet cmpSnippet = ejbJar.getCmpMappingSnippet(); 1680 if(cmpSnippet.hasDDSnippet()) { 1681 String snippetKey = Utils.getFQNKey(ejbJar.getUriText(), cmpSnippet.getFileName()); 1682 cmpGraphs.put(snippetKey, cmpSnippet.getCmpDDSnippet()); 1683 } 1684 } 1685 } 1686 } else { 1687 ((Base) rootBean).addToGraphs(outputGraphs, null, ""); 1688 if(rootBean instanceof EjbJarRoot) { 1690 EjbJarRoot ejbJar = (EjbJarRoot) rootBean; 1691 Snippet cmpSnippet = ejbJar.getCmpMappingSnippet(); 1692 if(cmpSnippet.hasDDSnippet()) { 1693 String snippetKey = Utils.getFQNKey(ejbJar.getUriText(), cmpSnippet.getFileName()); 1694 cmpGraphs.put(snippetKey, cmpSnippet.getCmpDDSnippet()); 1695 } 1696 } 1697 } 1698 1699 Set keys = outputGraphs.keySet(); 1702 Iterator iter = keys.iterator(); 1703 DeploymentPlan dp = new DeploymentPlan(); 1704 while (iter.hasNext()) { 1705 Object k = iter.next(); 1706 CommonDDBean bean = (CommonDDBean) outputGraphs.get(k); 1707 if (null != bean) { 1708 1717 ByteArrayOutputStream baos = 1718 new ByteArrayOutputStream (); 1719 bean.write(baos); 1720 contentMap.put(k, baos.toByteArray()); 1721 } else { 1724 jsr88Logger.warning("no bean for key: " + k); } 1726 } 1727 1728 keys = cmpGraphs.keySet(); 1730 iter = keys.iterator(); 1731 while (iter.hasNext()) { 1732 Object k = iter.next(); 1733 org.netbeans.modules.schema2beans.BaseBean cmpRoot = 1734 (org.netbeans.modules.schema2beans.BaseBean) cmpGraphs.get(k); 1735 if (null != cmpRoot) { 1736 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 1737 cmpRoot.write(baos); 1738 contentMap.put(k, baos.toByteArray()); 1739 } else { 1740 jsr88Logger.warning("no bean for key: " + k); } 1742 } 1743 1744 1751 } catch (Exception ex) { 1752 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1753 ConfigurationException ce = new ConfigurationException ("Sun Deployment Descriptor Error"); ce.initCause(ex); 1755 } 1756 jsr88Logger.exiting(this.getClass().toString(), "save", rootBean); } 1758 1759 private void refreshGraphFromContentMap(BaseRoot br) { 1760 List pending = new ArrayList (); 1761 BaseRoot rootToRestore = br; 1762 1763 1774 pending.add(rootToRestore); 1775 1776 1777 int index = 0; 1778 while (index < pending.size()) { 1779 Base current = (Base) pending.get(index); 1780 try { 1781 current.loadFromPlanFile(this); 1782 } 1783 catch (java.lang.IllegalStateException ise) { 1784 jsr88Logger.throwing(current.getClass().toString(), "loadFromPlanFile", ise); 1786 assert ise == null; 1787 } 1788 pending.addAll(current.getChildren()); 1789 index++; 1790 } 1791 1792 } 1793 1794 public void readDeploymentPlanFiles(ConfigurationStorage storage, DDBeanRoot masterRootDDBean) throws ConfigurationException { 1796 boolean loadGraph = false; 1797 1798 contentMap.clear(); 1799 BaseRoot masterRoot = (BaseRoot) getDConfigBeanRoot(masterRootDDBean); 1800 1801 int len = getValidatedNumberOfFiles(configFiles); 1802 for (int i = 0; i < len; i++) { 1803 if(configFiles[i].exists()) { 1804 addFileToPlanForModule(configFiles[i], dObj, storage); 1805 loadGraph = true; 1806 } 1807 } 1808 1809 if(loadGraph) { 1811 if(masterRoot != null) { 1812 refreshGraphFromContentMap(masterRoot); 1813 } else { 1814 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 1815 new IllegalStateException ("Loaded configuration data from disk but master DConfigBeanRoot is null.")); } 1817 } 1818 1819 for (int j = 0; j < configFiles.length; j++) { 1820 FileObject fo = FileUtil.toFileObject(configFiles [j]); 1821 if(fo != null) { 1822 fo.refresh(true); 1823 } 1824 } 1825 } 1826 1827 public void writeDeploymentPlanFiles(ConfigurationStorage storage) throws IOException , ConfigurationException { 1828 updateContentMap(null); 1830 1831 int len = getValidatedNumberOfFiles(configFiles); 1832 for (int i = 0; i < len; i++) { 1833 extractFileFromPlanForModule(configFiles[i], dObj, storage); 1834 } 1835 1836 for (int j = 0; j < configFiles.length; j++) { 1837 FileObject fo = FileUtil.toFileObject(configFiles [j]); 1838 if (fo != null) { 1839 fo.refresh(true); 1840 } 1841 } 1842 } 1843 1844 int getValidatedNumberOfFiles(File [] files) throws ConfigurationException { 1845 int len = 0; 1846 if (null != files) { 1847 len = files.length; 1848 } 1849 1850 if (len < 1) { 1852 throw new ConfigurationException ("file list is too short"); } 1854 1855 return len; 1856 } 1857 1858 1891 private ASDDVersion getTargetAppServerVersion() { 1895 ASDDVersion result = null; 1896 J2eeModuleProvider provider = getProvider(configFiles[0].getParentFile()); 1897 String serverType = provider.getServerID(); 1898 1899 1903 if("J2EE".equals(serverType)) { String instance = provider.getServerInstanceID(); 1905 if(Utils.notEmpty(instance)) { 1906 try { 1907 String asInstallPath = instance.substring(1, instance.indexOf("deployer")-1); 1908 File asInstallFolder = new File (asInstallPath); 1909 if(asInstallFolder.exists()) { 1910 result = getInstalledAppServerVersion(asInstallFolder); 1911 } 1912 } catch(IndexOutOfBoundsException ex) { 1913 ErrorManager.getDefault().log(ErrorManager.WARNING, NbBundle.getMessage( 1915 SunONEDeploymentConfiguration.class, "ERR_NoServerInstallLocation", instance)); } catch(NullPointerException ex) { 1917 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 1918 } 1919 } 1920 } else if("SUNWebserver7".equals(serverType)){ result = ASDDVersion.SUN_APPSERVER_8_1; 1922 } 1923 1924 return result; 1925 } 1926 1927 private ASDDVersion getInstalledAppServerVersion(File asInstallFolder) { 1928 File dtdFolder = new File (asInstallFolder, "lib/dtds/"); if(dtdFolder.exists()) { 1930 File domain12dtd = new File (dtdFolder, "sun-domain_1_2.dtd"); if(domain12dtd.exists()) { 1932 return ASDDVersion.SUN_APPSERVER_9_0; 1933 } 1934 File domain11dtd = new File (dtdFolder, "sun-domain_1_1.dtd"); if(domain11dtd.exists()) { 1936 return ASDDVersion.SUN_APPSERVER_8_1; 1937 } 1938 File domain10dtd = new File (dtdFolder, "sun-domain_1_0.dtd"); if(domain10dtd.exists()) { 1940 return ASDDVersion.SUN_APPSERVER_7_0; 1941 } 1942 } 1943 1944 return null; 1945 } 1946 1947 1951 public boolean isDatasourceCreationSupported() { 1952 return true; 1953 } 1954 1955 1960 public Set getDatasources() { 1961 Set datasources = new HashSet (); 1962 ResourceConfiguratorInterface rci = getResourceConfigurator(); 1963 if(resourceDir != null && resourceDir.exists()) { 1964 datasources = rci.getResources(resourceDir); 1965 } 1966 return datasources; 1967 } 1968 1969 1979 public Datasource createDatasource(final String jndiName, final String url, final String username, final String password, final String driver) 1980 throws OperationUnsupportedException , ConfigurationException , DatasourceAlreadyExistsException { 1981 Datasource ds = null; 1982 if(resourceDir == null) { 1983 postResourceError(NbBundle.getMessage(SunONEDeploymentConfiguration.class, 1985 "ERR_NoRefJdbcDataSource", jndiName)); throw new ConfigurationException (NbBundle.getMessage(SunONEDeploymentConfiguration.class, 1987 "ERR_NoRefJdbcDataSource", jndiName)); } 1989 1990 ResourceConfiguratorInterface rci = getResourceConfigurator(); 1991 if(rci != null) { 1992 ds = rci.createDataSource(jndiName, url, username, password, driver, resourceDir); 1993 } 1994 return ds; 1995 } 1996} 1997 | Popular Tags |