1 19 package org.netbeans.modules.j2ee.sun.share.config; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.*; 25 import java.util.*; 26 import javax.enterprise.deploy.model.*; 27 import javax.enterprise.deploy.spi.*; 28 import javax.enterprise.deploy.spi.exceptions.*; 29 30 import org.openide.*; 31 import org.openide.NotifyDescriptor.Confirmation; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.loaders.DataObject; 35 import org.openide.loaders.DataObjectNotFoundException; 36 import org.openide.nodes.*; 37 import org.openide.util.Mutex; 38 import org.openide.util.NbBundle; 39 import org.openide.util.RequestProcessor; 40 import org.openide.util.RequestProcessor.Task; 41 import org.openide.util.WeakListeners; 42 43 import org.xml.sax.SAXException ; 44 45 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 46 import org.netbeans.modules.j2ee.deployment.devmodules.spi.*; 47 import org.netbeans.modules.j2ee.sun.share.config.ui.ConfigBeanTopComponent; 48 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration; 49 50 51 55 public class ConfigurationStorage implements PropertyChangeListener , Node.Cookie { 56 57 public static final String ROOT = "/"; private SunONEDeploymentConfiguration config; 59 60 private ConfigDataObject configDataObject = null; 63 private Object dobjMonitor = new Object (); 64 private PropertyChangeListener weakPropListener = null; 65 66 Map moduleMap = new HashMap(); 68 final Map versionListeners = new HashMap(); 69 private boolean needsSave = false; 70 final J2eeModuleProvider module; 71 boolean loaded = false; 72 private Task autoSaveTask; 73 private int saveInProgress; 74 private int cleanInProgress; 75 private boolean saveFailedDialogDisplayed; 76 77 private PropertyChangeSupport dobjCookieChangeSupport; 80 81 public ConfigurationStorage(J2eeModuleProvider module, SunONEDeploymentConfiguration config) throws ConfigurationException, InvalidModuleException, IOException, SAXException { 82 this.module = module; 83 this.config = config; 84 this.saveInProgress = 0; 85 this.cleanInProgress = 0; 86 this.dobjCookieChangeSupport = new PropertyChangeSupport (this); 87 88 load(); createVersionListeners(); 90 } 91 92 private void init() throws ConfigurationException, InvalidModuleException, IOException { 93 try { 95 cleanInProgress++; 96 for (Iterator i = moduleMap.values().iterator(); i.hasNext();) { 97 ((ModuleDDSupport)i.next()).cleanup(); 98 } 99 moduleMap.clear(); 100 } finally { 101 cleanInProgress--; 102 } 103 104 ModuleDDSupport mds = new ModuleDDSupport(module, config); 106 moduleMap.put(ROOT,mds); 107 117 } 118 119 122 public DDBeanRoot normalizeDDBeanRoot(DDBeanRoot ddBeanRoot) { 123 DDBeanRoot ddNew = ddBeanRoot; 124 125 ModuleDDSupport mds = (ModuleDDSupport) moduleMap.get(ROOT); 128 if(mds != null) { 129 DDBeanRoot newRoot = mds.getDDBeanRoot(); 130 if(newRoot != ddBeanRoot && newRoot != null) { 131 ddNew = newRoot; 132 133 assert ddBeanRoot.getXpath().equals(ddNew.getXpath()) : "Mismatched xpaths in normalizeDDBeanRoot for " + ddBeanRoot; 136 } 137 } 138 139 return ddNew; 140 } 141 142 public DDBean normalizeEjbDDBean(DDBean ejbDDBean) { 143 DDBean result = null; 144 String theEjbName = Utils.getField(ejbDDBean, "ejb-name"); ModuleDDSupport mds = (ModuleDDSupport) moduleMap.get(ROOT); 146 DDRoot ddRoot = mds.getDDBeanRoot(J2eeModule.EJBJAR_XML); 147 StandardDDImpl[] ddBeans = (StandardDDImpl[]) ddRoot.getChildBean(ejbDDBean.getXpath()); 148 149 for(int i = 0; i < ddBeans.length; i++) { 150 String ejbName = (String ) ddBeans[i].proxy.bean.getValue("EjbName"); if (theEjbName.equals(ejbName)) { 152 result = ddBeans[i]; 153 break; 154 } 155 } 156 157 if(result == null) { 158 if (ddBeans != null) { 159 for (int i = 0; i < ddBeans.length; i++) { 160 String msg = ddBeans[i].proxy.bean.dumpBeanNode(); 161 ErrorManager.getDefault().log(ErrorManager.ERROR, msg); 162 } 163 } 164 Exception ex = new Exception ("Failed to lookup: " + theEjbName + ", type " + ejbDDBean.getXpath()); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 166 } 167 168 return result; 169 } 170 171 private boolean ensureLoaded() { 172 if(loaded) { 173 return true; 174 } 175 176 try { 177 load(); 178 return true; 179 } catch (Exception e) { 180 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 181 return false; 182 } 183 } 184 185 public ConfigDataObject getPrimaryDataObject() { 186 ConfigDataObject configDO; 187 188 synchronized (dobjMonitor) { 189 if(configDataObject == null) { 190 FileObject configFO = FileUtil.toFileObject(config.getConfigFiles()[0]); 191 if(configFO != null) { 192 try { 193 DataObject dObj = DataObject.find(configFO); 194 if(dObj instanceof ConfigDataObject) { 195 configDataObject = (ConfigDataObject) dObj; 196 weakPropListener = WeakListeners.propertyChange(this, configDataObject); 197 configDataObject.addPropertyChangeListener(weakPropListener); 198 } 200 } catch (DataObjectNotFoundException ex) { 201 } 203 } 204 } 205 configDO = configDataObject; 206 } 207 return configDO; 208 } 209 210 public void propertyChange(PropertyChangeEvent evt) { 211 if(DataObject.PROP_VALID.equals(evt.getPropertyName())) { 214 if(Boolean.FALSE.equals(evt.getNewValue())) { 215 synchronized (dobjMonitor) { 216 configDataObject.removePropertyChangeListener(weakPropListener); 218 configDataObject = null; 219 weakPropListener = null; 220 } 221 } 222 } 223 else if(DataObject.PROP_COOKIE.equals(evt.getPropertyName())) { 225 dobjCookieChangeSupport.firePropertyChange(evt); 226 } 227 } 228 229 public void addPropertyChangeListener(PropertyChangeListener pCL) { 230 dobjCookieChangeSupport.addPropertyChangeListener(pCL); 231 } 232 233 public void removePropertyChangeListener(PropertyChangeListener pCL) { 234 dobjCookieChangeSupport.removePropertyChangeListener(pCL); 235 } 236 237 238 241 private String filesToString(File[] files) { 242 StringBuffer sb = new StringBuffer (); 243 if (files.length > 0) { 244 sb.append(files[0].getPath()); 245 } 246 for (int i = 1; i < files.length; i++) { 247 sb.append(", "); sb.append(files[i].getPath()); 249 } 250 return sb.toString(); 251 } 252 253 257 public void autoSave() { 258 if (autoSaveTask == null) { 260 autoSaveTask = RequestProcessor.getDefault().post(new Runnable () { 262 private boolean dialogIsDisplayed; 263 public void run() { 264 Mutex.EVENT.readAccess(new Runnable () { 269 public void run() { 270 if(dialogIsDisplayed) { 272 return; } 275 276 ConfigDataObject configDO = getPrimaryDataObject(); 277 if (configDO == null) { 278 FileObject configFO = FileUtil.toFileObject(config.getConfigFiles()[0]); 281 if(configFO == null) { 282 try { 283 save(); 285 } catch (Exception ex) { 287 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 288 } 289 } 290 291 return; 293 } 294 295 if (!configDO.isConfigEditorOpened()) { 297 try { 299 if (configDO.areModified()) { 301 File files[] = ConfigurationStorage.this.config.getConfigFiles(); 302 String serverName = ConfigurationStorage.this.config.getAppServerVersion().toString(); 303 String msg = NbBundle.getMessage(ConfigurationStorage.class, 304 "MSG_SaveGeneratedChanges", serverName, 306 filesToString(files)); 307 Confirmation cf = new Confirmation(msg, NotifyDescriptor.YES_NO_OPTION); 308 dialogIsDisplayed = true; 309 DialogDisplayer.getDefault().notify(cf); 310 if (!NotifyDescriptor.YES_OPTION.equals(cf.getValue())) { 311 return; 312 } 313 } 314 save(); 315 } catch (Exception ex) { 317 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 319 } finally { 320 dialogIsDisplayed = false; 321 } 322 } else { 323 configDO.setChanged(); 325 } 326 } 327 }); 328 } 329 }, 100); 330 } else { 331 autoSaveTask.schedule(100); 333 } 334 } 335 336 public void setChanged() { 337 if(cleanInProgress == 0) { 338 needsSave = true; 339 autoSave(); 340 } 341 } 342 343 public void updateDDRoot(FileObject dd) { 344 if("webservices".equals(dd.getName())) { 345 ModuleDDSupport mds = (ModuleDDSupport) moduleMap.get(ROOT); 346 if(mds != null) { 347 mds.getDDBeanRoot(ModuleDDSupport.filenameToPath(dd.getNameExt(), mds.getType())); 348 } 349 } 350 } 351 352 public DeploymentConfiguration getDeploymentConfiguration() { 353 return config; 354 } 355 356 public static J2eeModuleProvider getChildModuleProvider(J2eeModuleProvider jmp, String uri) { 357 if (uri == null) { 358 return null; 359 } 360 J2eeModuleProvider child = null; 361 if (jmp instanceof J2eeAppProvider) { 362 J2eeAppProvider jap = (J2eeAppProvider) jmp; 363 child = jap.getChildModuleProvider(uri); 364 if (child == null) { 365 if (uri.startsWith(ROOT)) { 366 uri = uri.substring(1); 367 } else { 368 uri = ROOT + uri; 369 } 370 child = jap.getChildModuleProvider(uri); 371 } 372 } 373 return child; 374 } 375 376 408 409 public Node getMainNode() { 410 Node[] nodes = getMainNodes(); 411 return nodes.length > 0 ? nodes[0] : null; 412 } 413 414 public Node[] getMainNodes() { 415 if (! ensureLoaded()) { 416 return new Node[0]; 417 } 418 419 ModuleDDSupport mds = (ModuleDDSupport) moduleMap.get(ROOT); 420 return mds == null? new Node[0] : mds.getNodes(); 421 } 422 423 public Node[] getNodes(J2eeModule mod) { 424 if (! ensureLoaded()) { 425 return new Node[0]; 426 } 427 428 ModuleDDSupport mds = (ModuleDDSupport) moduleMap.get(mod.getUrl()); 429 433 if (mds == null) { 434 return new Node[0]; 435 } else { 436 return mds.getNodes(); 437 } 438 } 439 440 private void createDConfigBean(ModuleDDSupport mod) throws ConfigurationException { 441 mod.createConfigs(this); 442 } 443 444 public void saveOnDemand() throws IOException { 445 if (needsSave) { 446 save(); 447 } 448 } 449 450 public void save() throws IOException { 451 try { 458 saveInProgress++; 459 460 if(config != null) { 461 config.writeDeploymentPlanFiles(this); 462 needsSave = false; 463 ConfigDataObject configDO = getPrimaryDataObject(); 464 if(configDO != null) { 465 configDO.resetChanged(); 466 } 467 } else { 468 throw new IllegalStateException ("Attempted to save configuration when DeploymentConfiguration is null."); 469 } 470 } catch (ConfigurationException ce) { 471 reportExceptionDuringSave(ce); 472 473 IOException ioe = new IOException(ce.getLocalizedMessage()); 477 ioe.initCause(ce); 478 throw ioe; 479 } catch(IOException ioe) { 480 reportExceptionDuringSave(ioe); 481 throw ioe; 482 } finally { 483 saveInProgress--; 484 } 485 } 486 487 private void reportExceptionDuringSave(Exception ex) { 488 if(!saveFailedDialogDisplayed) { try { 490 saveFailedDialogDisplayed = true; 492 493 String errorMsg; 495 String exceptionMsg = ex.getLocalizedMessage(); 496 String appServerVersion = config.getAppServerVersion().toString(); 497 String fileList = filesToString(config.getConfigFiles()); 498 499 if(exceptionMsg != null && exceptionMsg.length() > 0) { 500 if(ex instanceof IOException) { 501 errorMsg = exceptionMsg; 505 } else { 506 errorMsg = NbBundle.getMessage(ConfigurationStorage.class, "MSG_ConfigurationSaveFailedHasMessage", 508 appServerVersion, fileList, exceptionMsg); 509 } 510 } else { 511 errorMsg = NbBundle.getMessage(ConfigurationStorage.class, "MSG_ConfigurationSaveFailed", 513 appServerVersion, fileList); 514 } 515 516 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(errorMsg)); 517 } finally { 518 saveFailedDialogDisplayed = false; 519 } 520 } 521 } 522 523 public void load() throws IOException, InvalidModuleException, ConfigurationException { 524 if(saveInProgress > 0) { return; 526 } 527 528 init(); 529 530 538 539 if(config instanceof SunONEDeploymentConfiguration) { 540 SunONEDeploymentConfiguration s1dc = (SunONEDeploymentConfiguration) config; 541 ModuleDDSupport rootSupport = (ModuleDDSupport) moduleMap.get(ROOT); 542 s1dc.readDeploymentPlanFiles(this, rootSupport.getDDBeanRoot()); 543 createDConfigBean(rootSupport); 544 loaded = true; 545 } else { 546 throw new IllegalArgumentException ("Invalid DeploymentConfiguration: " + config); 547 } 548 } 549 550 public void cleanup() { 551 for(Iterator i = moduleMap.values().iterator();i.hasNext();) { 556 ModuleDDSupport mds = (ModuleDDSupport) i.next(); 557 removeVersionListener(mds.getProvider().getJ2eeModule()); 558 mds.cleanup(); 559 } 560 moduleMap = new HashMap(); 561 565 } 566 567 private String getKey(J2eeModule mod) { 568 String key = mod.getUrl(); 569 if (key == null || key.trim().equals("")) { key = ROOT; 571 } 572 return key; 573 } 574 575 private void createVersionListener(J2eeModule mod) { 576 String key = getKey(mod); 577 J2eeModule.VersionListener listener = new ModuleVersionListener(key); 578 mod.addVersionListener(listener); 579 versionListeners.put(key, listener); 580 } 581 582 private void removeVersionListener(J2eeModule mod) { 583 J2eeModule.VersionListener vl = (J2eeModule.VersionListener) versionListeners.remove(getKey(mod)); 584 if (vl != null) { 585 mod.removeVersionListener(vl); 586 } 587 } 588 589 private void createVersionListeners() { 590 createVersionListener(module.getJ2eeModule()); 591 598 } 599 600 boolean saveInProgress() { 601 return (saveInProgress > 0); 602 } 603 604 private class ModuleVersionListener implements J2eeModule.VersionListener { 605 private String moduleUri; 606 ModuleVersionListener(String moduleUri) { 607 this.moduleUri = moduleUri; 608 } 609 public void versionChanged(String oldVersion, String newVersion) { 610 try { 611 saveOnDemand(); 612 cleanup(); 613 init(); 614 } catch(java.util.NoSuchElementException e) { 615 String msg = NbBundle.getMessage( 616 ConfigurationStorage.class, "MSG_DescriptorError", "TBD", e.getMessage()); 617 NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE); 618 DialogDisplayer.getDefault().notify(nd); 619 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 620 } catch(IOException ex) { 621 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 622 } catch (Exception e2) { 623 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e2); 624 } 625 626 ConfigBeanTopComponent tc = ConfigBeanTopComponent.findByConfigStorage(ConfigurationStorage.this); 627 if (tc != null) { 628 tc.refresh(); 629 } 630 } 631 } 632 } 633 | Popular Tags |