1 19 20 package org.netbeans.modules.j2ee.deployment.config; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 35 import org.netbeans.modules.j2ee.deployment.common.api.OriginalCMPMapping; 36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 37 import org.netbeans.modules.j2ee.deployment.impl.Server; 38 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 39 import javax.enterprise.deploy.model.*; 40 import javax.enterprise.deploy.shared.ModuleType ; 41 import javax.enterprise.deploy.spi.*; 42 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException ; 43 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException ; 44 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 45 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance; 46 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 47 import org.netbeans.modules.j2ee.deployment.plugins.api.ConfigurationSupport; 48 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 49 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 50 import org.openide.ErrorManager; 51 import org.openide.filesystems.FileLock; 52 import org.openide.filesystems.FileObject; 53 import org.openide.filesystems.FileUtil; 54 import org.openide.util.NbBundle; 55 import org.netbeans.api.project.FileOwnerQuery; 56 import org.netbeans.api.project.Project; 57 import org.netbeans.modules.j2ee.deployment.execution.DeploymentConfigurationProvider; 58 59 70 73 public final class ConfigSupportImpl implements J2eeModuleProvider.ConfigSupport, 74 DeploymentConfigurationProvider, PropertyChangeListener { 75 76 private static final File [] EMPTY_FILE_LIST = new File [0]; 77 private static final String GENERIC_EXTENSION = ".dpf"; 79 private String configurationPrimaryFileName = null; 80 private Map relativePaths = null; 81 private Map allRelativePaths = null; 82 83 private final J2eeModuleProvider provider; 84 private final ModuleDeploymentSupport mds; 85 private DeploymentConfiguration deploymentConfiguration; 86 87 private Server server; 88 private ServerInstance instance; 89 90 91 public ConfigSupportImpl (J2eeModuleProvider provider) { 92 this.provider = provider; 93 mds = new ModuleDeploymentSupport(provider, true); 94 String serverInstanceId = provider.getServerInstanceID(); 95 if (serverInstanceId != null) { 96 instance = ServerRegistry.getInstance().getServerInstance(serverInstanceId); 97 if (instance != null) { 98 server = instance.getServer(); 100 } 101 } 102 if (server == null) { 103 String serverID = provider.getServerID(); 105 if (serverID != null) { 106 server = ServerRegistry.getInstance().getServer(serverID); 108 } 109 } 110 if (server != null) { 112 provider.addPropertyChangeListener(this); 113 } 114 } 115 116 121 public File getConfigurationFile() { 122 try { 123 return getDeploymentPlanFileForDistribution(); 124 } catch (Exception ex) { 125 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 126 } 127 return null; 128 } 129 130 131 public void createDeploymentConfiguration(Server server) { 132 if (server == this.server) { 133 createInitialConfiguration(); 134 } else { 135 ModuleDeploymentSupport mds = new ModuleDeploymentSupport(provider, false); 136 DeployableObject dobj = mds.getDeployableObject(); 137 try { 138 DeploymentConfiguration deployConf = server.getDisconnectedDeploymentManager().createConfiguration(dobj); 139 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 140 File [] files = getDeploymentConfigurationFiles(getProvider(), server); 141 serverConfig.initConfiguration(deployConf, files, getProvider().getEnterpriseResourceDirectory(), false); 142 } catch(InvalidModuleException ime) { 143 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ime); 144 } catch (ConfigurationException ce) { 145 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ce); 146 } catch (DeploymentManagerCreationException dmce) { 147 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, dmce); 148 } 149 } 150 } 151 152 155 public static File [] getDeploymentConfigurationFiles (J2eeModuleProvider provider, Server server) { 156 return getDeploymentConfigurationFiles(provider, server, false); 157 } 158 159 public static FileObject[] getConfigurationFiles(J2eeModuleProvider jmp) { 160 Collection servers = ServerRegistry.getInstance().getServers(); 161 ArrayList files = new ArrayList (); 162 for (Iterator i=servers.iterator(); i.hasNext();) { 163 Server s = (Server) i.next(); 164 File [] configs = getDeploymentConfigurationFiles(jmp, s, true); 165 for (int j=0; j<configs.length; j++) { 166 files.add(FileUtil.toFileObject(configs[j])); 167 } 168 } 169 return (FileObject[]) files.toArray(new FileObject[files.size()]); 170 } 171 172 public String getDeploymentName() { 173 try { 174 FileObject fo = getProvider().getJ2eeModule().getContentDirectory(); 175 if (fo == null) { 176 fo = findPrimaryConfigurationFO(); 177 } 178 if (fo == null) 179 return null; 180 Project owner = FileOwnerQuery.getOwner(fo); 181 if (owner != null) 182 return owner.getProjectDirectory().getName(); 183 184 } catch (IOException ioe) { 185 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 186 } 187 return null; 188 } 189 190 191 public void dispose() { 192 if (server != null) { 193 provider.removePropertyChangeListener(this); 194 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 195 if (deploymentConfiguration != null && serverConfig != null) { 196 serverConfig.disposeConfiguration(deploymentConfiguration); 197 } 198 } 199 if (mds != null) { 200 mds.cleanup(); 201 } 202 } 203 204 206 public boolean createInitialConfiguration() { 207 return getDeploymentConfiguration() != null; 208 } 209 210 public boolean ensureConfigurationReady() { 211 return getDeploymentConfiguration() != null; 212 } 213 214 219 public String getWebContextRoot() { 220 if (!getProvider().getJ2eeModule().getModuleType().equals(J2eeModule.WAR)) { 221 ErrorManager.getDefault().log("getWebContextRoot called on other module type then WAR"); return null; 223 } 224 if (server == null) { 225 return null; 226 } 227 DeploymentConfiguration config = getDeploymentConfiguration(); 228 if (config == null) { 229 return null; 230 } 231 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 232 if (serverConfig == null) { 233 return null; 234 } 235 try { 236 return serverConfig.getWebContextRoot(config, config.getDeployableObject()); 237 } catch (ConfigurationException ce) { 238 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ce); 239 return null; 240 } 241 } 242 243 246 public void setWebContextRoot(String contextRoot) { 247 if (!getProvider().getJ2eeModule().getModuleType().equals(J2eeModule.WAR)) { 248 ErrorManager.getDefault().log("setWebContextRoot called on other module type then WAR"); return; 250 } 251 if (server == null) { 252 return; 253 } 254 DeploymentConfiguration config = getDeploymentConfiguration(); 255 if (config == null) { 256 return; 257 } 258 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 259 if (serverConfig == null) { 260 return; 261 } 262 try { 263 serverConfig.setWebContextRoot(config, config.getDeployableObject(), contextRoot); 264 } catch (ConfigurationException ce) { 265 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ce); 266 } 267 } 268 269 public String [] getDeploymentConfigurationFileNames() { 270 if (server == null) { 271 return new String []{}; 272 } 273 if (hasCustomSupport()) { 274 return (String []) getRelativePaths().keySet().toArray(new String [relativePaths.size()]); 275 } 276 return new String [] { getStandardDeploymentPlanName() }; 277 } 278 279 public String getContentRelativePath(String configName) { 280 if (! hasCustomSupport()) { 281 return configName; } 283 return (String ) getAllRelativePaths().get(configName); 284 } 285 286 public void setCMPMappingInfo(final OriginalCMPMapping[] mappings) { 287 if (server == null) { 288 return; 290 } 291 DeploymentConfiguration config = getDeploymentConfiguration(); 292 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 293 serverConfig.setMappingInfo(config, mappings); 294 } 295 296 public void ensureResourceDefinedForEjb(final String ejbname, final String ejbtype) { 297 if (ejbname == null) { 298 throw new NullPointerException ("EJB name cannot be null"); } 300 if (ejbtype == null) { 301 throw new NullPointerException ("EJB type cannot be null"); } 303 if (server == null) { 304 return; 306 } 307 DDBean ejbBean = findDDBean(ejbname, ejbtype); 308 if (ejbBean != null) { 309 DeploymentConfiguration config = getDeploymentConfiguration(); 310 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 311 serverConfig.ensureResourceDefined(config, ejbBean); 312 } 313 } 314 315 public void ensureResourceDefinedForEjb(String ejbname, String ejbtype, String jndiName) { 316 if (ejbname == null) { 317 throw new NullPointerException ("EJB name cannot be null"); } 319 if (ejbtype == null) { 320 throw new NullPointerException ("EJB type cannot be null"); } 322 if (jndiName == null) { 323 throw new NullPointerException ("JNDI name cannot be null"); } 325 if (server == null) { 326 return; 328 } 329 DDBean ejbBean = findDDBean(ejbname, ejbtype); 330 if (ejbBean != null) { 331 DeploymentConfiguration config = getDeploymentConfiguration(); 332 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 333 serverConfig.ensureResourceDefined(config, ejbBean, jndiName); 334 } 335 } 336 337 341 private DDBean findDDBean(String ejbname, String ejbtype) { 342 if (!J2eeModule.EJB.equals(provider.getJ2eeModule().getModuleType())) { 343 throw new IllegalArgumentException ("Trying to get config bean for ejb on non ejb module!"); } 345 DDBean ejbBean = null; 346 DDRoot ddroot = mds.getDDBeanRoot(J2eeModule.EJBJAR_XML); 347 StandardDDImpl[] ddbeans = (StandardDDImpl[]) ddroot.getChildBean( 348 "/enterprise-beans/" + ejbtype); for (int i=0; i<ddbeans.length; i++) { 350 String ejbName = (String ) ddbeans[i].proxy.bean.getValue("EjbName"); if (ejbname.equals(ejbName)) { 352 ejbBean = ddbeans[i]; 353 break; 354 } 355 } 356 if (ejbBean == null) { 357 if (ddbeans != null) { 358 for (int i=0; i<ddbeans.length; i++) { 359 String msg = ddbeans[i].proxy.bean.dumpBeanNode(); 360 ErrorManager.getDefault().log(ErrorManager.ERROR, msg); 361 } 362 } 363 Exception e = new Exception ("Failed to lookup: "+ejbname+" type "+ejbtype); 364 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 365 return null; 366 } 367 368 return ejbBean; 369 } 370 371 public Set <Datasource> getDatasources() { 372 373 Set <Datasource> projectDS = Collections.<Datasource>emptySet(); 374 375 if (server != null) { 376 ConfigurationSupport configSupport = server.getConfigurationSupport(); 377 if (configSupport != null) { 378 DeploymentConfiguration config = getDeploymentConfiguration(); 379 if (config != null) { 380 projectDS = configSupport.getDatasources(config); 381 } 382 } 383 } 384 385 return projectDS; 386 } 387 388 public boolean isDatasourceCreationSupported() { 389 if (server == null) { 390 return false; 392 } 393 ConfigurationSupport configSupport = server.getConfigurationSupport(); 394 395 if (configSupport == null) 396 return false; 397 398 return configSupport.isDatasourceCreationSupported(); 399 } 400 401 public Datasource createDatasource(String jndiName, String url, String username, String password, String driver) 402 throws OperationUnsupportedException , DatasourceAlreadyExistsException { 403 Datasource ds = null; 404 if (server != null) { 405 ConfigurationSupport configSupport = server.getConfigurationSupport(); 406 if (configSupport != null) { 407 DeploymentConfiguration config = getDeploymentConfiguration(); 408 if (config != null) { 409 try { 410 ds = configSupport.createDatasource(config, jndiName, url, username, password, driver); 411 } catch (ConfigurationException ce) { 412 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ce); 413 } 414 } 415 } 416 } 417 return ds; 418 } 419 420 422 425 public DeploymentConfiguration getDeploymentConfiguration() { 426 if (deploymentConfiguration == null) { 427 DeployableObject dobj = mds.getDeployableObject(); 428 try { 429 if (instance != null) { 430 deploymentConfiguration = instance.getDeploymentManagerForConfiguration().createConfiguration(dobj); 431 } else if (server != null) { 432 deploymentConfiguration = server.getDisconnectedDeploymentManager().createConfiguration(dobj); 433 } else { 434 return null; 436 } 437 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 438 File [] files = getDeploymentConfigurationFiles(getProvider(), server); 439 serverConfig.initConfiguration(deploymentConfiguration, files, 440 getProvider().getEnterpriseResourceDirectory(), true); 441 } catch(InvalidModuleException ime) { 442 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ime); 443 return null; 444 } catch (ConfigurationException ce) { 445 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ce); 446 return null; 447 } catch (DeploymentManagerCreationException dmce) { 448 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, dmce); 449 return null; 450 } 451 } 452 return deploymentConfiguration; 453 } 454 455 public DeployableObject getDeployableObject(String moduleUri) { 456 DeployableObject deplObj = mds.getDeployableObject(); 457 if (deplObj instanceof J2eeApplicationObject) { 458 return ((J2eeApplicationObject)deplObj).getDeployableObject(moduleUri); 459 } 460 return mds.getDeployableObject(); 461 } 462 463 465 468 private static File [] getDeploymentConfigurationFiles (J2eeModuleProvider provider, Server server, boolean existingOnly) { 469 if (provider == null || server == null) 470 return new File [0]; 471 472 ModuleType type = (ModuleType ) provider.getJ2eeModule().getModuleType(); 473 String [] fnames; 474 if (hasCustomSupport(server, type)) { 475 fnames = server.getDeploymentPlanFiles(type); 476 } else if (server.supportsModuleType(type)) { 477 fnames = new String [] { getStandardDeploymentPlanName(server) }; 478 } else { 479 return EMPTY_FILE_LIST; 480 } 481 482 ArrayList files = new ArrayList (); 483 for (int i = 0; i < fnames.length; i++) { 484 File path = new File (fnames[i]); 485 String fname = path.getName(); 486 File file = null; 487 if (existingOnly) { 488 FileObject fo = provider.findDeploymentConfigurationFile(fname); 489 if (fo != null) { 490 file = FileUtil.toFile(fo); 491 } 492 } else { 493 file = provider.getDeploymentConfigurationFile(fname); 494 } 495 496 if (file != null) { 497 files.add(file); 498 } 499 } 500 return (File [])files.toArray(new File [files.size()]); 501 } 502 503 509 private File getDeploymentPlanFileForDistribution() throws IOException , ConfigurationException { 510 if (server == null) { 511 String msg = NbBundle.getMessage(ConfigSupportImpl.class, "MSG_NoTargetSelected"); 512 throw new ConfigurationException (msg); 513 } 514 515 FileLock lock = null; 516 OutputStream out = null; 517 try { 518 FileObject dist = getProvider().getJ2eeModule().getContentDirectory(); 519 String planName = getStandardDeploymentPlanName(); 520 FileObject plan = null; 521 if (dist != null) { 522 plan = dist.getFileObject(planName); 523 if (plan == null) { 524 plan = dist.createData(planName); 525 } 526 } else { 527 return null; 528 } 529 lock = plan.lock(); 530 out = plan.getOutputStream(lock); 531 DeploymentConfiguration conf = getDeploymentConfiguration(); 532 if (conf != null) { 533 conf.save(out); 534 return FileUtil.toFile(plan); 535 } 536 return null; 537 } finally { 538 if (lock != null) lock.releaseLock(); 539 try { 540 if (out != null) out.close(); 541 } catch(IOException ioe) { 542 ErrorManager.getDefault().log(ioe.toString()); 543 } 544 } 545 } 546 547 private String getPrimaryConfigurationFileName() { 548 getRelativePaths(); 549 550 if (configurationPrimaryFileName == null) 551 return getStandardDeploymentPlanName(); 552 else 553 return configurationPrimaryFileName; 554 } 555 556 private String getStandardDeploymentPlanName() { 557 return getStandardDeploymentPlanName(server); 558 559 } 560 561 private static String getStandardDeploymentPlanName(Server server) { 562 return server.getShortName() + GENERIC_EXTENSION; 563 } 564 565 private FileObject findPrimaryConfigurationFO() throws IOException { 566 String configFileName = getPrimaryConfigurationFileName(); 567 return getProvider().findDeploymentConfigurationFile(configFileName); 568 } 569 570 private ModuleType getModuleType() { 571 return (ModuleType ) getProvider().getJ2eeModule().getModuleType(); 572 } 573 574 private boolean hasCustomSupport() { 575 return hasCustomSupport(server, getModuleType()); 576 } 577 578 private static boolean hasCustomSupport(Server server, ModuleType type) { 579 if (server == null || server.getConfigurationSupport() == null) { 580 return false; 581 } 582 return server.getDeploymentPlanFiles(type) != null; 583 } 584 585 private J2eeModuleProvider getProvider () { 586 return provider; 587 } 588 589 private Map getRelativePaths() { 590 if (relativePaths != null) 591 return relativePaths; 592 593 relativePaths = new HashMap (); 594 if (hasCustomSupport()) { 595 String [] paths = server.getDeploymentPlanFiles(getModuleType()); 596 configurationPrimaryFileName = paths[0].substring(paths[0].lastIndexOf("/")+1); 597 598 collectData(server, relativePaths); 599 } 600 601 return relativePaths; 602 } 603 604 private void collectData(Server server, Map map) { 605 if (! this.hasCustomSupport(server, getModuleType())) 606 return; 607 608 String [] paths = server.getDeploymentPlanFiles(getModuleType()); 609 paths = (paths == null) ? new String [0] : paths; 610 for (int i=0; i<paths.length; i++) { 611 String name = paths[i].substring(paths[i].lastIndexOf("/")+1); 612 map.put(name, paths[i]); 613 } 614 } 615 616 private Map getAllRelativePaths() { 617 if (allRelativePaths != null) 618 return allRelativePaths; 619 620 allRelativePaths = new HashMap (); 621 Collection servers = ServerRegistry.getInstance().getServers(); 622 for (Iterator i=servers.iterator(); i.hasNext();) { 623 Server server = (Server) i.next(); 624 collectData(server, allRelativePaths); 625 } 626 return allRelativePaths; 627 } 628 629 public void propertyChange(PropertyChangeEvent evt) { 630 if (J2eeModuleProvider.PROP_ENTERPRISE_RESOURCE_DIRECTORY.equals(evt.getPropertyName())) { 631 DeploymentConfiguration config = getDeploymentConfiguration(); 632 ConfigurationSupport serverConfig = server.getConfigurationSupport(); 633 Object newValue = evt.getNewValue(); 634 if (newValue != null && !(newValue instanceof File )) { 635 throw new IllegalArgumentException ("Enterprise resource directory property value is not a File"); } 637 serverConfig.updateResourceDir(config, (File )newValue); 638 } 639 } 640 } 641 | Popular Tags |