1 19 20 package org.netbeans.modules.j2ee.earproject; 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.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import org.netbeans.api.java.project.JavaProjectConstants; 35 import org.netbeans.api.project.FileOwnerQuery; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectUtils; 38 import org.netbeans.api.project.SourceGroup; 39 import org.netbeans.api.project.Sources; 40 import org.netbeans.modules.j2ee.api.ejbjar.Car; 41 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; 42 import org.netbeans.modules.j2ee.dd.api.application.Application; 43 import org.netbeans.modules.j2ee.dd.api.application.DDProvider; 44 import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor; 45 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 46 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 47 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModuleContainer; 48 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter; 49 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleListener; 50 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider; 51 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 52 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties; 53 import org.netbeans.modules.j2ee.spi.ejbjar.EarImplementation; 54 import org.netbeans.modules.schema2beans.BaseBean; 55 import org.netbeans.modules.web.api.webmodule.WebModule; 56 import org.netbeans.spi.java.classpath.ClassPathProvider; 57 import org.netbeans.spi.project.support.ant.AntProjectHelper; 58 import org.openide.ErrorManager; 59 import org.openide.filesystems.FileObject; 60 import org.openide.filesystems.FileUtil; 61 import org.openide.util.WeakListeners; 62 63 69 public final class ProjectEar extends J2eeAppProvider 70 implements 71 J2eeModule, 72 ModuleChangeReporter, 73 EjbChangeDescriptor, 74 PropertyChangeListener , 75 EarImplementation, 76 J2eeModuleContainer { 77 78 public static final String FILE_DD = "application.xml"; 80 private final EarProject project; 81 private Set <J2eeModule.VersionListener> versionListeners; 82 83 ProjectEar (EarProject project) { this.project = project; 85 AntProjectHelper helper = project.getAntProjectHelper(); 86 helper.getStandardPropertyEvaluator().addPropertyChangeListener(this); 87 } 88 89 public FileObject getDeploymentDescriptor() { 90 FileObject dd = null; 91 FileObject metaInf = getMetaInf(); 92 if (metaInf != null) { 93 dd = metaInf.getFileObject(FILE_DD); 94 if (dd == null) { 95 try { 96 dd = EarProjectGenerator.setupDD(J2eeModule.JAVA_EE_5, metaInf, project); 97 } catch (IOException ioe) { 98 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 99 } 100 } 101 } 102 return dd; 103 } 104 105 public FileObject getMetaInf() { 106 return project.getOrCreateMetaInfDir(); 107 } 108 109 public File getEnterpriseResourceDirectory() { 110 return project.getFile(EarProjectProperties.RESOURCE_DIR); 111 } 112 113 public ClassPathProvider getClassPathProvider () { 114 return (ClassPathProvider) project.getLookup ().lookup (ClassPathProvider.class); 115 } 116 117 public FileObject getArchive () { 118 return project.getFileObject (EarProjectProperties.DIST_JAR); } 120 121 public J2eeModule getJ2eeModule () { 122 return this; 123 } 124 125 public ModuleChangeReporter getModuleChangeReporter () { 126 return this; 127 } 128 129 public boolean useDefaultServer () { 130 return false; 131 } 132 133 public String getServerID () { 134 return project.getServerID(); } 136 137 public void setServerInstanceID(String severInstanceID) { 138 } 140 141 public String getServerInstanceID () { 142 return project.getServerInstanceID(); } 144 145 public Iterator getArchiveContents () throws IOException { 146 return new IT (getContentDirectory ()); 147 } 148 149 public FileObject getContentDirectory() { 150 return project.getFileObject (EarProjectProperties.BUILD_DIR); } 152 153 public FileObject getBuildDirectory() { 154 return project.getFileObject (EarProjectProperties.BUILD_DIR); } 156 157 public BaseBean getDeploymentDescriptor (String location) { 158 if (! J2eeModule.APP_XML.equals(location)) { 159 return null; 160 } 161 162 Application earApp = getApplication(); 163 if (earApp != null) { 164 return DDProvider.getDefault ().getBaseBean (earApp); 166 } 167 return null; 168 } 169 170 private Application getApplication () { 171 try { 172 return DDProvider.getDefault ().getDDRoot (getDeploymentDescriptor ()); 173 } catch (IOException e) { 174 ErrorManager.getDefault ().log (e.getLocalizedMessage ()); 175 } 176 return null; 177 178 } 179 180 public EjbChangeDescriptor getEjbChanges (long timestamp) { 181 return this; 182 } 183 184 public Object getModuleType () { 185 return J2eeModule.EAR; 186 } 187 188 public String getModuleVersion () { 189 Application app = getApplication(); 190 return (app == null) ? Application.VERSION_5 : app.getVersion().toString(); 191 } 192 193 private Set <J2eeModule.VersionListener> versionListeners() { 194 if (versionListeners == null) { 195 versionListeners = new HashSet <J2eeModule.VersionListener>(); 196 Application app = getApplication(); 197 if (app != null) { 198 PropertyChangeListener l = (PropertyChangeListener ) WeakListeners.create(PropertyChangeListener .class, this, app); 199 app.addPropertyChangeListener(l); 200 } 201 } 202 return versionListeners; 203 } 204 205 public void addVersionListener(J2eeModule.VersionListener vl) { 206 try { 207 versionListeners().add(vl); 208 } catch (UnsupportedOperationException uoe) { 209 } 211 } 212 213 public void removeVersionListener(J2eeModule.VersionListener vl) { 214 if (versionListeners != null) { 215 versionListeners.remove(vl); 216 } 217 } 218 219 public void propertyChange(PropertyChangeEvent evt) { 220 if (evt.getPropertyName().equals(Application.PROPERTY_VERSION)) { 221 for (J2eeModule.VersionListener vl : versionListeners) { 222 String oldVersion = (String ) evt.getOldValue(); 223 String newVersion = (String ) evt.getNewValue(); 224 vl.versionChanged(oldVersion, newVersion); 225 } 226 } else if (EarProjectProperties.J2EE_SERVER_INSTANCE.equals(evt.getPropertyName())) { 227 Deployment d = Deployment.getDefault(); 228 String oldServerID = evt.getOldValue() == null ? null : d.getServerID((String )evt.getOldValue ()); 229 String newServerID = evt.getNewValue() == null ? null : d.getServerID((String )evt.getNewValue ()); 230 fireServerChange (oldServerID, newServerID); 231 } else if (EarProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) { 232 String oldValue = (String )evt.getOldValue(); 233 String newValue = (String )evt.getNewValue(); 234 firePropertyChange( 235 PROP_ENTERPRISE_RESOURCE_DIRECTORY, 236 oldValue == null ? null : new File (oldValue), 237 newValue == null ? null : new File (newValue)); 238 } 239 } 240 241 public String getUrl () { 242 return ""; 243 } 244 245 public boolean isManifestChanged (long timestamp) { 246 return false; 247 } 248 249 public void setUrl (String url) { 250 throw new UnsupportedOperationException ("Cannot customize URL of web module"); } 252 253 public boolean ejbsChanged () { 254 return false; 255 } 256 257 public String [] getChangedEjbs () { 258 return new String [] {}; 259 } 260 261 public String getJ2eePlatformVersion () { 262 return project.getJ2eePlatformVersion(); } 264 265 public FileObject[] getSourceRoots() { 266 Sources sources = ProjectUtils.getSources(project); 267 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 268 FileObject[] roots = new FileObject[groups.length+1]; 269 roots[0] = getMetaInf(); 270 for (int i=0; i < groups.length; i++) { 271 roots[i+1] = groups[i].getRootFolder(); 272 } 273 274 return roots; 275 } 276 277 private static class IT implements Iterator { 278 Enumeration ch; 279 FileObject root; 280 281 private IT (FileObject f) { 282 this.ch = f.getChildren (true); 283 this.root = f; 284 } 285 286 public boolean hasNext () { 287 return ch.hasMoreElements (); 288 } 289 290 public Object next () { 291 FileObject f = (FileObject) ch.nextElement (); 292 return new FSRootRE (root, f); 293 } 294 295 public void remove () { 296 throw new UnsupportedOperationException (); 297 } 298 299 } 300 301 private static final class FSRootRE implements J2eeModule.RootedEntry { 302 FileObject f; 303 FileObject root; 304 305 FSRootRE (FileObject root, FileObject f) { 306 this.f = f; 307 this.root = root; 308 } 309 310 public FileObject getFileObject () { 311 return f; 312 } 313 314 public String getRelativePath () { 315 return FileUtil.getRelativePath (root, f); 316 } 317 } 318 319 private Map <String , J2eeModuleProvider> mods = new HashMap <String , J2eeModuleProvider>(); 320 321 void setModules(Map <String , J2eeModuleProvider> mods) { 322 if (null == mods) { 323 throw new IllegalArgumentException ("mods"); } 325 this.mods = mods; 326 } 327 328 public J2eeModule[] getModules(ModuleListener ml) { 329 if (null != ml) { 330 addModuleListener(ml); 331 } 332 J2eeModule[] retVal = new J2eeModule[mods.size()]; 333 int i = 0; 334 for (J2eeModuleProvider provider : mods.values()) { 335 retVal[i++] = provider.getJ2eeModule(); 336 } 337 return retVal; 338 } 339 340 public void addModuleProvider(J2eeModuleProvider jmp, String uri) { 341 mods.put(uri, jmp); 342 J2eeModule jm = jmp.getJ2eeModule(); 343 fireAddModule(jm); 344 } 345 346 public void removeModuleProvider(J2eeModuleProvider jmp, String uri) { 347 J2eeModule jm = jmp.getJ2eeModule(); 353 fireRemoveModule(jm); 354 mods.remove(uri); 355 } 356 357 private final List <ModuleListener> modListeners = new ArrayList <ModuleListener>(); 358 359 private void fireAddModule(J2eeModule jm) { 360 for (ModuleListener ml : modListeners) { 361 try { 362 ml.addModule(jm); 363 } catch (RuntimeException rex) { 364 ErrorManager.getDefault().log(rex.getLocalizedMessage()); 365 } 366 } 367 } 368 369 private void fireRemoveModule(J2eeModule jm) { 370 for (ModuleListener ml : modListeners) { 371 try { 372 ml.removeModule(jm); 373 } catch (RuntimeException rex) { 374 ErrorManager.getDefault().log(rex.getLocalizedMessage()); 375 } 376 } 377 } 378 379 public void addModuleListener(ModuleListener ml) { 380 modListeners.add(ml); 381 } 382 383 public void removeModuleListener(ModuleListener ml){ 384 modListeners.remove(ml); 385 } 386 387 392 public J2eeModuleProvider getChildModuleProvider(String uri) { 393 return mods.get(uri); 394 } 395 396 400 public J2eeModuleProvider[] getChildModuleProviders() { 401 return (J2eeModuleProvider[]) mods.values().toArray(new J2eeModuleProvider[mods.size()]); 402 } 403 404 public File getDeploymentConfigurationFile(String name) { 405 FileObject moduleFolder = getMetaInf(); 406 File configFolder = FileUtil.toFile(moduleFolder); 407 return new File (configFolder, name); 408 } 409 410 public FileObject findDeploymentConfigurationFile (String name) { 411 FileObject moduleFolder = getMetaInf(); 412 return moduleFolder == null ? null : moduleFolder.getFileObject(name); 413 } 414 415 public void addEjbJarModule(EjbJar module) { 416 FileObject childFO = module.getDeploymentDescriptor(); 417 if (childFO == null) { 418 childFO = module.getMetaInf(); 419 } 420 addModule(childFO); 421 } 422 423 public void addWebModule(WebModule module) { 424 FileObject childFO = module.getDeploymentDescriptor(); 425 if (childFO == null) { 426 childFO = module.getWebInf(); 427 } 428 addModule(childFO); 429 } 430 431 public void addCarModule(Car module) { 432 FileObject childFO = module.getDeploymentDescriptor(); 433 if (childFO == null) { 434 childFO = module.getMetaInf(); 435 } 436 addModule(childFO); 437 } 438 439 private void addModule(final FileObject childFO) { 440 Project owner = null; 441 if (childFO != null) { 442 owner = FileOwnerQuery.getOwner(childFO); 443 } 444 if (owner == null) { 445 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, 446 "Unable to add module to the Enterpise Application. Owner project not found."); } else { 448 ((EarProjectProperties)project.getProjectProperties()).addJ2eeSubprojects(new Project [] {owner}); 449 } 450 } 451 452 } 453 | Popular Tags |