1 19 20 package org.netbeans.modules.web.project; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.*; 27 import javax.swing.SwingUtilities ; 28 import org.netbeans.api.java.project.JavaProjectConstants; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectUtils; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.Sources; 33 import org.netbeans.api.project.ui.OpenProjects; 34 import org.netbeans.modules.j2ee.common.Util; 35 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 36 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 37 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 38 import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor; 39 import org.netbeans.spi.project.support.ant.AntProjectHelper; 40 import org.netbeans.spi.project.support.ant.EditableProperties; 41 import org.openide.filesystems.FileObject; 42 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 43 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 44 import org.netbeans.spi.java.classpath.ClassPathProvider; 45 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.NotifyDescriptor; 48 import org.openide.DialogDisplayer; 49 import org.openide.util.NbBundle; 50 import org.netbeans.modules.j2ee.dd.api.webservices.*; 51 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport; 52 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants; 53 import org.openide.ErrorManager; 54 55 56 60 public final class ProjectWebModule extends J2eeModuleProvider 61 implements WebModuleImplementation, J2eeModule, ModuleChangeReporter, 62 EjbChangeDescriptor, PropertyChangeListener { 63 64 public static final String FOLDER_WEB_INF = "WEB-INF"; public static final String FOLDER_CLASSES = "classes"; public static final String FOLDER_LIB = "lib"; public static final String FILE_DD = "web.xml"; 69 private WebProject project; 70 private UpdateHelper helper; 71 private Set versionListeners = null; 72 private String fakeServerInstId = null; 74 private long notificationTimeout = 0; 76 ProjectWebModule (WebProject project, UpdateHelper helper) { 77 this.project = project; 78 this.helper = helper; 79 project.evaluator ().addPropertyChangeListener (this); 80 } 81 82 public FileObject getDeploymentDescriptor() { 83 return getDeploymentDescriptor(false); 84 } 85 86 public FileObject getDeploymentDescriptor(boolean silent) { 87 FileObject webInfFo = getWebInf(silent); 88 if (webInfFo==null) { 89 return null; 90 } 91 FileObject dd = webInfFo.getFileObject (FILE_DD); 92 if (dd == null && !silent 93 && (J2eeModule.J2EE_13.equals(getJ2eePlatformVersion ()) || 94 J2eeModule.J2EE_14.equals(getJ2eePlatformVersion ()))) { 95 showErrorMessage(NbBundle.getMessage(ProjectWebModule.class,"MSG_WebXmlNotFound", webInfFo.getPath())); 97 } 98 return dd; 99 } 100 101 public String getContextPath () { 102 if(getDeploymentDescriptor() == null) { 103 return null; 104 } 105 return getConfigSupport ().getWebContextRoot (); 106 } 107 108 public void setContextPath (String path) { 109 if (getDeploymentDescriptor() != null) { 110 getConfigSupport ().setWebContextRoot (path); 111 } 112 } 113 114 public String getContextPath (String serverInstId) { 115 fakeServerInstId = serverInstId; 116 String result = getContextPath(); 117 fakeServerInstId = null; 118 return result; 119 } 120 121 public void setContextPath (String serverInstId, String path) { 122 fakeServerInstId = serverInstId; 123 setContextPath(path); 124 fakeServerInstId = null; 125 } 126 127 private void showErrorMessage(final String message) { 128 synchronized (this) { 129 if(new Date().getTime() > notificationTimeout && isProjectOpened()) { 130 notificationTimeout = new Date().getTime() + 20000; 133 } else { 134 return; 135 } 136 } 137 Runnable r = new Runnable () { 141 public void run() { 142 if (!SwingUtilities.isEventDispatchThread()) { 143 SwingUtilities.invokeLater(this); 144 } else { 145 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 146 } 147 } 148 }; 149 r.run(); 150 } 151 152 public FileObject getDocumentBase () { 153 return getDocumentBase(false); 154 } 155 156 public FileObject getDocumentBase (boolean silent) { 157 FileObject docBase = getFileObject(WebProjectProperties.WEB_DOCBASE_DIR); 158 if (docBase == null && !silent) { 159 String relativePath = helper.getAntProjectHelper().getStandardPropertyEvaluator().getProperty(WebProjectProperties.WEB_DOCBASE_DIR); 160 String path = (relativePath != null ? helper.getAntProjectHelper().resolvePath(relativePath) : null); 161 String errorMessage; 162 if (path != null) { 163 errorMessage = NbBundle.getMessage(ProjectWebModule.class, "MSG_DocBase_Corrupted", project.getName(), path); 164 } else { 165 errorMessage = NbBundle.getMessage(ProjectWebModule.class, "MSG_DocBase_Corrupted_Unknown", project.getName()); 166 } 167 showErrorMessage(errorMessage); 168 } 169 return docBase; 170 } 171 172 public FileObject[] getJavaSources() { 173 return project.getSourceRoots().getRoots(); 174 } 175 176 184 public FileObject getWebInf () { 185 return getWebInf(false); 186 } 187 188 public File getEnterpriseResourceDirectory() { 189 return getFile(WebProjectProperties.RESOURCE_DIR); 190 } 191 192 public FileObject getWebInf (boolean silent) { 193 FileObject documentBase = getDocumentBase(silent); 194 if (documentBase == null) { 195 return null; 196 } 197 FileObject webInf = documentBase.getFileObject (FOLDER_WEB_INF); 198 if (webInf == null && !silent) { 199 showErrorMessage(NbBundle.getMessage(ProjectWebModule.class,"MSG_WebInfCorrupted", documentBase.getPath())); 201 } 202 return webInf; 203 } 204 205 public FileObject getConfDir() { 206 return getFileObject(WebProjectProperties.CONF_DIR); 207 } 208 209 public File getConfDirAsFile() { 210 return getFile(WebProjectProperties.CONF_DIR); 211 } 212 213 public ClassPathProvider getClassPathProvider () { 214 return (ClassPathProvider) project.getLookup ().lookup (ClassPathProvider.class); 215 } 216 217 public FileObject getArchive () { 218 return getFileObject ("dist.war"); } 220 221 private FileObject getFileObject(String propname) { 222 String prop = helper.getAntProjectHelper().getStandardPropertyEvaluator().getProperty(propname); 223 if (prop != null) { 224 return helper.getAntProjectHelper().resolveFileObject(prop); 225 } else { 226 return null; 227 } 228 } 229 230 private File getFile(String propname) { 231 String prop = helper.getAntProjectHelper().getStandardPropertyEvaluator().getProperty(propname); 232 if (prop != null) { 233 return helper.getAntProjectHelper().resolveFile(prop); 234 } else { 235 return null; 236 } 237 } 238 239 public org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule getJ2eeModule () { 240 return this; 241 } 242 243 public org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter getModuleChangeReporter () { 244 return this; 245 } 246 247 public FileObject findDeploymentConfigurationFile(String name) { 248 if (name == null) { 249 return null; 250 } 251 name = getConfigSupport().getContentRelativePath(name); 252 if (name == null) { 253 return null; 254 } 255 FileObject documentBase = getDocumentBase(); 256 if (documentBase == null) { 257 return null; 258 } 259 return documentBase.getFileObject(name); 260 } 261 262 public File getDeploymentConfigurationFile(String name) { 263 assert name != null : "File name of the deployement configuration file can't be null"; 265 String path = getConfigSupport().getContentRelativePath(name); 266 if (path == null) { 267 path = name; 268 } 269 FileObject documentBase = getDocumentBase(); 270 if (documentBase == null) { 271 return new File (getConfDirAsFile(), name); 273 } 274 return new File (FileUtil.toFile(documentBase), path); 275 } 276 277 public FileObject getModuleFolder () { 278 return getDocumentBase (); 279 } 280 281 public boolean useDefaultServer () { 282 return false; 283 } 284 285 public String getServerID () { 286 String inst = getServerInstanceID (); 287 if (inst != null) { 288 String id = Deployment.getDefault().getServerID(inst); 289 if (id != null) { 290 return id; 291 } 292 } 293 return helper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_TYPE); 294 } 295 296 public String getServerInstanceID () { 297 if (fakeServerInstId != null) 298 return fakeServerInstId; 299 return helper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_SERVER_INSTANCE); 300 } 301 302 public void setServerInstanceID(String severInstanceID) { 303 WebProjectProperties.setServerInstance(project, helper, severInstanceID); 304 } 305 306 public Iterator getArchiveContents () throws java.io.IOException { 307 return new IT (getContentDirectory ()); 308 } 309 310 public FileObject getContentDirectory() { 311 return getFileObject ("build.web.dir"); } 313 314 public FileObject getBuildDirectory() { 315 return getFileObject ("build.dir"); } 317 318 public File getContentDirectoryAsFile() { 319 return getFile ("build.web.dir"); } 321 322 public org.netbeans.modules.schema2beans.BaseBean getDeploymentDescriptor (String location) { 323 if (J2eeModule.WEB_XML.equals(location)){ 324 325 WebApp webApp = getWebApp (); 326 if (webApp != null) { 327 return DDProvider.getDefault ().getBaseBean (webApp); 329 } 330 } 331 else if(J2eeModule.WEBSERVICES_XML.equals(location)){ 332 Webservices webServices = getWebservices(); 333 if(webServices != null){ 334 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getBaseBean(webServices); 335 } 336 } 337 return null; 338 } 339 340 341 public void uncacheDescriptors() { 342 notificationTimeout = 0; 345 } 346 347 private WebApp getWebApp () { 348 try { 349 FileObject deploymentDescriptor = getDeploymentDescriptor (); 350 if(deploymentDescriptor != null) { 351 return DDProvider.getDefault ().getMergedDDRoot (deploymentDescriptor); 352 } 353 } catch (java.io.IOException e) { 354 org.openide.ErrorManager.getDefault ().log (e.getLocalizedMessage ()); 355 } 356 return null; 357 } 358 359 private Webservices getWebservices() { 360 if (Util.isJavaEE5orHigher(project)) { 361 WebServicesSupport wss = WebServicesSupport.getWebServicesSupport(project.getProjectDirectory()); 362 try { 363 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getMergedDDRoot(wss); 364 } catch (IOException ex) { 365 ErrorManager.getDefault().notify(ex); 366 } 367 } else { 368 FileObject wsdd = getDD(); 369 if(wsdd != null) { 370 try { 371 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault() 372 .getDDRoot(getDD()); 373 } catch (java.io.IOException e) { 374 org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage()); 375 } 376 } 377 } 378 return null; 379 } 380 381 public org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor getEjbChanges (long timestamp) { 382 return this; 383 } 384 385 public Object getModuleType () { 386 return J2eeModule.WAR; 387 } 388 389 public String getModuleVersion () { 390 WebApp wapp = getWebApp (); 391 String version = "2.5"; if (wapp != null) 393 version = wapp.getVersion(); 394 return version; 395 } 396 397 private Set versionListeners() { 398 if (versionListeners == null) { 399 versionListeners = new HashSet(); 400 org.netbeans.modules.j2ee.dd.api.web.WebApp webApp = getWebApp(); 401 if (webApp != null) { 402 PropertyChangeListener l = (PropertyChangeListener ) org.openide.util.WeakListeners.create(PropertyChangeListener .class, this, webApp); 403 webApp.addPropertyChangeListener(l); 404 } 405 } 406 return versionListeners; 407 } 408 409 public void addVersionListener(J2eeModule.VersionListener vl) { 410 versionListeners().add(vl); 411 } 412 413 public void removeVersionListener(J2eeModule.VersionListener vl) { 414 if (versionListeners != null) 415 versionListeners.remove(vl); 416 } 417 418 public void propertyChange(PropertyChangeEvent evt) { 419 if (evt.getPropertyName().equals(org.netbeans.modules.j2ee.dd.api.web.WebApp.PROPERTY_VERSION)) { 420 for (Iterator i=versionListeners.iterator(); i.hasNext();) { 421 J2eeModule.VersionListener vl = (J2eeModule.VersionListener) i.next(); 422 String oldVersion = (String ) evt.getOldValue(); 423 String newVersion = (String ) evt.getNewValue(); 424 vl.versionChanged(oldVersion, newVersion); 425 } 426 } else if (evt.getPropertyName ().equals (WebProjectProperties.J2EE_SERVER_INSTANCE)) { 427 Deployment d = Deployment.getDefault (); 428 String oldServerID = evt.getOldValue () == null ? null : d.getServerID ((String ) evt.getOldValue ()); 429 String newServerID = evt.getNewValue () == null ? null : d.getServerID ((String ) evt.getNewValue ()); 430 fireServerChange (oldServerID, newServerID); 431 } else if (WebProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) { 432 String oldValue = (String )evt.getOldValue(); 433 String newValue = (String )evt.getNewValue(); 434 firePropertyChange( 435 PROP_ENTERPRISE_RESOURCE_DIRECTORY, 436 oldValue == null ? null : new File (oldValue), 437 newValue == null ? null : new File (newValue)); 438 } 439 } 440 441 public String getUrl () { 442 EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 443 String warName = ep.getProperty(WebProjectProperties.WAR_NAME); 444 return warName == null ? "" : ("/"+warName); } 446 447 public boolean isManifestChanged (long timestamp) { 448 return false; 449 } 450 451 public void setUrl (String url) { 452 throw new UnsupportedOperationException ("Cannot customize URL of web module"); } 454 455 public boolean ejbsChanged () { 456 return false; 457 } 458 459 public String [] getChangedEjbs () { 460 return new String [] {}; 461 } 462 463 public String getJ2eePlatformVersion () { 464 return helper.getAntProjectHelper().getStandardPropertyEvaluator ().getProperty (WebProjectProperties.J2EE_PLATFORM); 465 } 466 467 public FileObject getDD() { 468 FileObject webInfFo = getWebInf(); 469 if (webInfFo==null) { 470 showErrorMessage(NbBundle.getMessage(ProjectWebModule.class,"MSG_WebInfCorrupted")); 471 return null; 472 } 473 return getWebInf().getFileObject(WebServicesConstants.WEBSERVICES_DD, "xml"); } 475 476 public FileObject[] getSourceRoots() { 477 Sources sources = ProjectUtils.getSources(project); 478 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 479 480 List roots = new LinkedList(); 481 FileObject documentBase = getDocumentBase(); 482 if (documentBase != null) 483 roots.add(documentBase); 484 485 for (int i = 0; i < groups.length; i++) { 486 roots.add(groups[i].getRootFolder()); 487 } 488 489 FileObject[] rootArray = new FileObject[roots.size()]; 490 return (FileObject[])roots.toArray(rootArray); 491 } 492 493 private boolean isProjectOpened() { 494 if (OpenProjects.getDefault() == null) 497 return true; 498 499 Project[] projects = OpenProjects.getDefault().getOpenProjects(); 500 for (int i = 0; i < projects.length; i++) { 501 if (projects[i].equals(project)) 502 return true; 503 } 504 return false; 505 } 506 507 private static class IT implements Iterator { 508 ArrayList ch; 509 FileObject root; 510 511 private IT (FileObject f) { 512 this.ch = new ArrayList (); 513 ch.add (f); 514 this.root = f; 515 } 516 517 public boolean hasNext () { 518 return ! ch.isEmpty(); 519 } 520 521 public Object next () { 522 FileObject f = (FileObject) ch.get(0); 523 ch.remove(0); 524 if (f.isFolder()) { 525 f.refresh(); 526 FileObject chArr[] = f.getChildren (); 527 for (int i = 0; i < chArr.length; i++) { 528 ch.add(chArr [i]); 529 } 530 } 531 return new FSRootRE (root, f); 532 } 533 534 public void remove () { 535 throw new UnsupportedOperationException (); 536 } 537 538 } 539 540 private static final class FSRootRE implements J2eeModule.RootedEntry { 541 FileObject f; 542 FileObject root; 543 544 FSRootRE (FileObject root, FileObject f) { 545 this.f = f; 546 this.root = root; 547 } 548 549 public FileObject getFileObject () { 550 return f; 551 } 552 553 public String getRelativePath () { 554 return FileUtil.getRelativePath (root, f); 555 } 556 } 557 } 558 | Popular Tags |