1 19 20 package org.netbeans.modules.j2ee.deployment.devmodules.spi; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import javax.enterprise.deploy.spi.Target ; 31 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 32 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 33 import org.netbeans.modules.j2ee.deployment.common.api.OriginalCMPMapping; 34 import org.netbeans.modules.j2ee.deployment.common.api.ValidationException; 35 import org.netbeans.modules.j2ee.deployment.config.*; 36 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 37 import org.netbeans.modules.j2ee.deployment.impl.DefaultSourceMap; 38 import org.netbeans.modules.j2ee.deployment.impl.Server; 39 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance; 40 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 41 import org.netbeans.modules.j2ee.deployment.impl.ServerString; 42 import org.netbeans.modules.j2ee.deployment.impl.ServerTarget; 43 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 44 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 45 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 46 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo; 47 import org.netbeans.modules.j2ee.deployment.common.api.SourceFileMap; 48 import org.netbeans.modules.j2ee.deployment.plugins.api.StartServer; 49 import org.netbeans.modules.j2ee.deployment.plugins.api.VerifierSupport; 50 import org.openide.ErrorManager; 51 import org.openide.filesystems.FileObject; 52 import org.openide.filesystems.FileUtil; 53 import org.openide.util.WeakListeners; 54 import java.io.File ; 55 import java.util.ArrayList ; 56 import java.util.List ; 57 58 63 public abstract class J2eeModuleProvider { 64 65 private InstanceListener il; 66 private ConfigSupportImpl configSupportImpl; 67 List listeners = new ArrayList (); 68 private ConfigFilesListener configFilesListener = null; 69 70 75 public static final String PROP_ENTERPRISE_RESOURCE_DIRECTORY = "resourceDir"; 77 private PropertyChangeSupport supp = new PropertyChangeSupport (this); 78 79 public J2eeModuleProvider () { 80 il = new IL (); 81 ServerRegistry.getInstance ().addInstanceListener ( 82 (InstanceListener) WeakListeners.create( 83 InstanceListener.class, il, ServerRegistry.getInstance ())); 84 } 85 86 public abstract J2eeModule getJ2eeModule (); 87 88 public abstract ModuleChangeReporter getModuleChangeReporter (); 89 90 public final ConfigSupport getConfigSupport () { 91 ConfigSupportImpl confSupp; 92 synchronized (this) { 93 confSupp = configSupportImpl; 94 } 95 if (confSupp == null) { 96 confSupp = new ConfigSupportImpl(this); 97 synchronized (this) { 98 configSupportImpl = confSupp; 99 } 100 } 101 return confSupp; 102 } 103 104 110 public final ServerDebugInfo getServerDebugInfo () { 111 ServerInstance si = ServerRegistry.getInstance ().getServerInstance (getServerInstanceID ()); 112 StartServer ss = si.getStartServer(); 113 if (ss == null) { 114 return null; 115 } 116 if (! si.isRunningLastCheck() && ss.needsStartForTargetList()) { 120 if (ss.isAlsoTargetServer(null)) { 121 return ss.getDebugInfo(null); 122 } else { 123 return null; 124 } 125 } 126 127 Target target = null; 128 if (si != null) { 129 ServerTarget[] sts = si.getTargets(); 130 for (int i=0; i<sts.length; i++) { 131 if (si.getStartServer().isAlsoTargetServer(sts[i].getTarget())) { 132 target = sts[i].getTarget(); 133 } 134 } 135 if (target == null && sts.length > 0) { 136 target = sts[0].getTarget(); 137 } 138 return si.getStartServer().getDebugInfo(target); 139 } 140 return null; 141 } 142 143 150 public Set <Datasource> getServerDatasources() { 151 ServerInstance si = ServerRegistry.getInstance ().getServerInstance (getServerInstanceID ()); 152 Set <Datasource> deployedDS = Collections.<Datasource>emptySet(); 153 if (si != null) { 154 deployedDS = si.getDatasources(); 155 } 156 else { 157 ErrorManager.getDefault().log(ErrorManager.WARNING, 158 "The server data sources cannot be retrieved because the server instance cannot be found."); 159 } 160 return deployedDS; 161 } 162 163 170 public Set <Datasource> getModuleDatasources() { 171 Set <Datasource> projectDS = getConfigSupport().getDatasources(); 172 return projectDS; 173 } 174 175 182 public boolean isDatasourceCreationSupported() { 183 return getConfigSupport().isDatasourceCreationSupported(); 184 } 185 186 187 202 public final Datasource createDatasource(String jndiName, String url, String username, String password, String driver) 203 throws DatasourceAlreadyExistsException { 204 205 Set <Datasource> deployedDS = getServerDatasources(); 207 if (deployedDS != null) { 208 for (Iterator it = deployedDS.iterator(); it.hasNext();) { 209 Datasource ds = (Datasource) it.next(); 210 if (jndiName.equals(ds.getJndiName())) throw new DatasourceAlreadyExistsException(ds); 212 } 213 } 214 215 Datasource ds = null; 216 try { 217 ds = getConfigSupport().createDatasource(jndiName, url, username, password, driver); 219 } catch (OperationUnsupportedException oue) { 220 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, oue); 221 } 222 223 return ds; 224 } 225 226 235 public void deployDatasources() throws ConfigurationException , DatasourceAlreadyExistsException { 236 ServerInstance si = ServerRegistry.getInstance ().getServerInstance (getServerInstanceID ()); 237 if (si != null) { 238 Set <Datasource> moduleDS = getModuleDatasources(); 239 si.deployDatasources(moduleDS); 240 } 241 else { 242 ErrorManager.getDefault().log(ErrorManager.WARNING, 243 "The data sources cannot be deployed because the server instance cannot be found."); 244 } 245 } 246 247 248 255 public final void addPropertyChangeListener(PropertyChangeListener l) { 256 supp.addPropertyChangeListener(l); 257 } 258 259 265 public final void removePropertyChangeListener(PropertyChangeListener l) { 266 supp.removePropertyChangeListener(l); 267 } 268 269 270 278 protected final void firePropertyChange(String propName, Object oldValue, Object newValue) { 279 supp.firePropertyChange(propName, oldValue, newValue); 280 } 281 282 288 public static interface ConfigSupport { 289 293 public boolean createInitialConfiguration(); 294 298 public boolean ensureConfigurationReady(); 299 300 306 308 311 public void setWebContextRoot(String contextRoot); 312 public String getWebContextRoot(); 313 314 318 public String [] getDeploymentConfigurationFileNames(); 319 325 public String getContentRelativePath(String deploymentConfigurationFileName); 326 330 public void setCMPMappingInfo(OriginalCMPMapping[] mappings); 331 338 @Deprecated 339 public void ensureResourceDefinedForEjb(String ejbname, String ejbtype); 340 341 348 public void ensureResourceDefinedForEjb(String ejbName, String ejbType, String jndiName); 349 350 357 public boolean isDatasourceCreationSupported(); 358 359 366 public Set <Datasource> getDatasources(); 367 368 384 public Datasource createDatasource(String jndiName, String url, String username, String password, String driver) 385 throws OperationUnsupportedException , DatasourceAlreadyExistsException; 386 } 387 388 395 abstract public File getDeploymentConfigurationFile(String name); 396 397 405 abstract public FileObject findDeploymentConfigurationFile (String name); 406 407 411 public File getEnterpriseResourceDirectory() { 412 return null; 413 } 414 415 422 public FileObject[] getSourceRoots() { 423 return new FileObject[0]; 424 } 425 426 431 public SourceFileMap getSourceFileMap() { 432 return new DefaultSourceMap(this); 433 } 434 435 438 public boolean useDefaultServer () { 439 return true; 440 } 441 442 448 public abstract void setServerInstanceID(String severInstanceID); 449 450 455 public String getServerInstanceID () { 456 return ServerRegistry.getInstance ().getDefaultInstance ().getUrl (); 457 } 458 459 462 public InstanceProperties getInstanceProperties(){ 463 return InstanceProperties.getInstanceProperties(getServerInstanceID()); 464 } 465 466 469 public String getServerID () { 470 return ServerRegistry.getInstance ().getDefaultInstance ().getServer ().getShortName (); 471 } 472 473 476 public String getDeploymentName() { 477 return getConfigSupportImpl().getDeploymentName(); 478 } 479 480 483 public boolean hasVerifierSupport() { 484 String serverId = getServerID(); 485 if (serverId != null) { 486 Server server = ServerRegistry.getInstance().getServer(serverId); 487 if (server != null) { 488 return server.canVerify(getJ2eeModule().getModuleType()); 489 } 490 } 491 return false; 492 } 493 494 500 public void verify(FileObject target, OutputStream logger) throws ValidationException { 501 VerifierSupport verifier = ServerRegistry.getInstance().getServer(getServerID()).getVerifierSupport(); 502 if (verifier == null) { 503 throw new ValidationException ("Verification not supported by the selected server"); 504 } 505 Object type = getJ2eeModule().getModuleType(); 506 if (!verifier.supportsModuleType(type)) { 507 throw new ValidationException ("Verification not supported for module type " + type); 508 } 509 ServerRegistry.getInstance().getServer(getServerID()).getVerifierSupport().verify(target, logger); 510 } 511 512 protected final void fireServerChange (String oldServerID, String newServerID) { 513 Server oldServer = ServerRegistry.getInstance ().getServer (oldServerID); 514 Server newServer = ServerRegistry.getInstance ().getServer (newServerID); 515 if (oldServer != null && newServer != null && !oldServer.equals (newServer)) { 516 517 if (J2eeModule.WAR.equals(getJ2eeModule().getModuleType())) { 518 String oldCtxPath = getConfigSupportImpl().getWebContextRoot(); 519 ConfigSupportImpl oldConSupp; 520 synchronized (this) { 521 oldConSupp = configSupportImpl; 522 configSupportImpl = null; 523 } 524 getConfigSupportImpl().ensureConfigurationReady(); 525 if (oldCtxPath == null || oldCtxPath.equals("")) { oldCtxPath = getDeploymentName().replace(' ', '_'); char c [] = oldCtxPath.toCharArray(); 528 for (int i = 0; i < c.length; i++) { 529 if (!Character.UnicodeBlock.BASIC_LATIN.equals(Character.UnicodeBlock.of(c[i])) || 530 !Character.isLetterOrDigit(c[i])) { 531 c[i] = '_'; 532 } 533 } 534 oldCtxPath = "/" + new String (c); } 536 getConfigSupportImpl().setWebContextRoot(oldCtxPath); 537 if (oldConSupp != null) { 538 oldConSupp.dispose(); 539 } 540 } else { 541 ConfigSupportImpl oldConSupp; 542 synchronized (this) { 543 oldConSupp = configSupportImpl; 544 configSupportImpl = null; 545 } 546 getConfigSupportImpl().ensureConfigurationReady(); 547 if (oldConSupp != null) { 548 oldConSupp.dispose(); 549 } 550 } 551 } 552 } 553 554 557 public final FileObject[] getConfigurationFiles() { 558 return getConfigurationFiles(false); 559 } 560 561 public final FileObject[] getConfigurationFiles(boolean refresh) { 562 if (refresh) { 563 configFilesListener.stopListening(); 564 configFilesListener = null; 565 } 566 addCFL(); 567 return ConfigSupportImpl.getConfigurationFiles(this); 568 } 569 570 public final void addConfigurationFilesListener(ConfigurationFilesListener l) { 571 listeners.add(l); 572 } 573 public final void removeConfigurationFilesListener(ConfigurationFilesListener l) { 574 listeners.remove(l); 575 } 576 577 584 public final void addInstanceListener(InstanceListener l) { 585 ServerRegistry.getInstance ().addInstanceListener(l); 586 } 587 588 595 public final void removeInstanceListener(InstanceListener l) { 596 ServerRegistry.getInstance ().removeInstanceListener(l); 597 } 598 599 private void addCFL() { 600 if (configFilesListener != null) 602 return; 603 configFilesListener = new ConfigFilesListener(this, listeners); 604 } 605 606 private final class IL implements InstanceListener { 607 608 public void changeDefaultInstance (String oldInst, String newInst) { 609 ServerInstance oldServerInstance = ServerRegistry.getInstance().getServerInstance(oldInst); 610 ServerInstance newServerInstance = ServerRegistry.getInstance().getServerInstance(newInst); 611 ServerString oldInstance = oldServerInstance != null 612 ? new ServerString(oldServerInstance) 613 : null; 614 ServerString newInstance = newServerInstance != null 615 ? new ServerString(newServerInstance) 616 : null; 617 if (useDefaultServer () && newInstance != null 618 && (oldInstance == null || !oldInstance.getPlugin().equals(newInstance.getPlugin()))) { 619 if (J2eeModule.WAR.equals(getJ2eeModule().getModuleType())) { 620 String oldCtxPath = getConfigSupportImpl().getWebContextRoot(); 621 oldCtxPath = "/"+J2eeModuleProvider.this.getDeploymentName(); ConfigSupportImpl oldConSupp; 623 synchronized (J2eeModuleProvider.this) { 624 oldConSupp = configSupportImpl; 625 configSupportImpl = null; 626 } 627 if (oldConSupp != null) { 628 oldConSupp.dispose(); 629 } 630 getConfigSupportImpl().ensureConfigurationReady(); 631 String ctx = getConfigSupportImpl().getWebContextRoot (); 632 if (ctx == null || ctx.equals ("")) { getConfigSupportImpl().setWebContextRoot(oldCtxPath); 634 } 635 } else { 636 ConfigSupportImpl oldConSupp; 637 synchronized (J2eeModuleProvider.this) { 638 oldConSupp = configSupportImpl; 639 configSupportImpl = null; 640 } 641 if (oldConSupp != null) { 642 oldConSupp.dispose(); 643 } 644 getConfigSupportImpl().ensureConfigurationReady(); 645 } 646 } 647 } 648 649 public void instanceAdded (String instance) { 650 } 651 652 public void instanceRemoved (String instance) { 653 } 654 655 } 656 657 private ConfigSupportImpl getConfigSupportImpl() { 658 return (ConfigSupportImpl) getConfigSupport(); 659 } 660 661 } 662 | Popular Tags |