1 19 20 package org.netbeans.modules.j2ee.ejbjarproject; 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.classpath.ClassPath; 29 import org.netbeans.api.java.project.JavaProjectConstants; 30 import org.netbeans.api.project.*; 31 import org.netbeans.api.project.ui.OpenProjects; 32 import org.netbeans.modules.j2ee.metadata.ClassPathSupport; 33 import org.netbeans.modules.j2ee.common.Util; 34 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 35 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 37 import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor; 38 import org.netbeans.modules.j2ee.metadata.MetadataUnit; 39 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport; 40 import org.netbeans.spi.project.support.ant.AntProjectHelper; 41 import org.openide.ErrorManager; 42 import org.openide.filesystems.FileObject; 43 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 44 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 45 import org.netbeans.spi.java.classpath.ClassPathProvider; 46 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarImplementation; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.NotifyDescriptor; 49 import org.openide.DialogDisplayer; 50 import org.openide.util.NbBundle; 51 import org.netbeans.spi.project.support.ant.EditableProperties; 52 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices; 53 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathProviderImpl; 54 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants; 55 56 57 61 public final class EjbJarProvider extends J2eeModuleProvider 62 implements EjbJarImplementation, J2eeModule, ModuleChangeReporter, EjbChangeDescriptor, PropertyChangeListener { 63 64 public static final String FILE_DD = "ejb-jar.xml"; 66 private final EjbJarProject project; 67 private final AntProjectHelper helper; 68 private Set versionListeners; 69 private MetadataUnit metadataUnit; 70 private ClassPath metadataClassPath; 71 72 private long notificationTimeout = 0; 74 EjbJarProvider(EjbJarProject project, AntProjectHelper helper) { 75 this.project = project; 76 this.helper = helper; 77 project.evaluator().addPropertyChangeListener (this); 78 } 79 80 public FileObject getDeploymentDescriptor() { 81 FileObject ddFO = null; 82 FileObject metaInfFo = getMetaInf(); 83 if (metaInfFo != null) { 84 ddFO = metaInfFo.getFileObject(FILE_DD); 85 } 86 if (ddFO == null && !EjbJarProjectProperties.JAVA_EE_5.equals(getJ2eePlatformVersion())) { 87 } 89 return ddFO; 90 } 91 92 93 public ClassPath getClassPath() { 94 ClassPathProvider cpp = (ClassPathProvider) project.getLookup().lookup(ClassPathProvider.class); 95 if (cpp != null) { 96 return cpp.findClassPath(getFileObject(EjbJarProjectProperties.SRC_DIR), ClassPath.SOURCE); 97 } 98 return null; 99 } 100 101 public FileObject[] getJavaSources() { 102 return project.getSourceRoots().getRoots(); 103 } 104 105 public FileObject getMetaInf() { 106 FileObject metaInf = getFileObject(EjbJarProjectProperties.META_INF); 107 if (metaInf == null) { 108 String version = project.getAPIEjbJar().getJ2eePlatformVersion(); 109 if (needConfigurationFolder(version)) { 110 String relativePath = helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.META_INF); 111 String path = (relativePath != null ? helper.resolvePath(relativePath) : ""); 112 showErrorMessage(NbBundle.getMessage(EjbJarProvider.class,"MSG_MetaInfCorrupted", project.getName(), path)); 113 } 114 } 115 return metaInf; 116 } 117 118 119 static boolean needConfigurationFolder(final String version) { 120 return EjbJarProjectProperties.J2EE_1_3.equals(version) || 121 EjbJarProjectProperties.J2EE_1_4.equals(version); 122 } 123 124 public File getMetaInfAsFile() { 125 return getFile(EjbJarProjectProperties.META_INF); 126 } 127 128 public File getEnterpriseResourceDirectory() { 129 return getFile(EjbJarProjectProperties.RESOURCE_DIR); 130 } 131 132 public FileObject findDeploymentConfigurationFile(String name) { 133 FileObject metaInf = getMetaInf(); 134 if (metaInf == null) { 135 return null; 136 } 137 return metaInf.getFileObject(name); 138 } 139 140 public File getDeploymentConfigurationFile(String name) { 141 return new File (getMetaInfAsFile(), name); 142 } 143 144 public ClassPathProvider getClassPathProvider() { 145 return (ClassPathProvider) project.getLookup().lookup(ClassPathProvider.class); 146 } 147 148 public FileObject getArchive() { 149 return getFileObject(EjbJarProjectProperties.DIST_JAR); 150 } 151 152 private FileObject getFileObject(String propname) { 153 String prop = helper.getStandardPropertyEvaluator().getProperty(propname); 154 if (prop != null) { 155 return helper.resolveFileObject(prop); 156 } 157 158 return null; 159 } 160 161 private File getFile(String propname) { 162 String prop = helper.getStandardPropertyEvaluator().getProperty(propname); 163 if (prop != null) { 164 return helper.resolveFile(prop); 165 } 166 return null; 167 } 168 169 public org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule getJ2eeModule() { 170 return this; 171 } 172 173 public org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter getModuleChangeReporter() { 174 return this; 175 } 176 177 public boolean useDefaultServer() { 178 return false; 179 } 180 181 public String getServerID() { 182 return helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.J2EE_SERVER_TYPE); 183 } 184 185 public String getServerInstanceID() { 186 return helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.J2EE_SERVER_INSTANCE); 187 } 188 189 public void setServerInstanceID(String serverInstanceID) { 190 assert serverInstanceID != null : "passed serverInstanceID cannot be null"; 191 EjbJarProjectProperties.setServerInstance(project, helper, serverInstanceID); 192 } 193 194 public Iterator getArchiveContents() throws java.io.IOException { 195 return new IT(getContentDirectory()); 196 } 197 198 public FileObject getContentDirectory() { 199 return getFileObject(EjbJarProjectProperties.BUILD_CLASSES_DIR); 200 } 201 202 public FileObject getBuildDirectory() { 203 return getFileObject(EjbJarProjectProperties.BUILD_DIR); 204 } 205 206 public File getContentDirectoryAsFile() { 207 return getFile(EjbJarProjectProperties.BUILD_CLASSES_DIR); 208 } 209 210 public org.netbeans.modules.schema2beans.BaseBean getDeploymentDescriptor(String location) { 211 if (J2eeModule.EJBJAR_XML.equals(location)){ 212 EjbJar ejbJar = getEjbJar(); 213 if (ejbJar != null) { 214 return DDProvider.getDefault().getBaseBean(ejbJar); 216 } 217 } 218 else if(J2eeModule.EJBSERVICES_XML.equals(location)){ 219 Webservices webServices = getWebservices(); 220 if(webServices != null){ 221 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getBaseBean(webServices); 222 } 223 } 224 return null; 225 } 226 227 private EjbJar getEjbJar() { 228 try { 229 org.netbeans.modules.j2ee.api.ejbjar.EjbJar apiEjbJar = project.getAPIEjbJar(); 230 if (apiEjbJar.getDeploymentDescriptor() == null && !EjbJarProjectProperties.JAVA_EE_5.equals(apiEjbJar.getJ2eePlatformVersion())) { 231 return null; 232 } else { 233 return DDProvider.getDefault().getMergedDDRoot(apiEjbJar.getMetadataUnit()); 234 } 235 } catch (java.io.IOException e) { 236 org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage()); 237 } 238 return null; 239 } 240 241 private Webservices getWebservices() { 242 if (Util.isJavaEE5orHigher(project)) { 243 WebServicesSupport wss = WebServicesSupport.getWebServicesSupport(project.getProjectDirectory()); 244 try { 245 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getMergedDDRoot(wss); 246 } catch (IOException ex) { 247 ErrorManager.getDefault().notify(ex); 248 } 249 } else { 250 FileObject wsdd = getDD(); 251 if(wsdd != null) { 252 try { 253 return org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(wsdd); 254 } catch (java.io.IOException e) { 255 org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage()); 256 } 257 } 258 } 259 return null; 260 } 261 262 public FileObject getDD() { 263 FileObject metaInfFo = getMetaInf(); 264 if (metaInfFo==null) { 265 return null; 266 } 267 return metaInfFo.getFileObject(WebServicesConstants.WEBSERVICES_DD, "xml"); } 269 270 public org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor getEjbChanges(long timestamp) { 271 return this; 272 } 273 274 public Object getModuleType() { 275 return J2eeModule.EJB; 276 } 277 278 public String getModuleVersion() { 279 EjbJar ejbJar = getEjbJar(); 280 return (ejbJar == null) ? EjbJar.VERSION_2_1 : ejbJar.getVersion().toString(); 281 } 282 283 private Set versionListeners() { 284 if (versionListeners == null) { 285 versionListeners = new HashSet(); 286 org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJar = getEjbJar(); 287 if (ejbJar != null) { 288 PropertyChangeListener l = (PropertyChangeListener ) org.openide.util.WeakListeners.create(PropertyChangeListener .class, this, ejbJar); 289 ejbJar.addPropertyChangeListener(l); 290 } 291 } 292 return versionListeners; 293 } 294 295 public void addVersionListener(J2eeModule.VersionListener vl) { 296 versionListeners().add(vl); 297 } 298 299 public void removeVersionListener(J2eeModule.VersionListener vl) { 300 if (versionListeners != null) { 301 versionListeners.remove(vl); 302 } 303 } 304 305 public void propertyChange(PropertyChangeEvent evt) { 306 if (evt.getPropertyName().equals(org.netbeans.modules.j2ee.dd.api.ejb.EjbJar.PROPERTY_VERSION)) { 307 for (Iterator i=versionListeners.iterator(); i.hasNext();) { 308 J2eeModule.VersionListener vl = (J2eeModule.VersionListener) i.next(); 309 String oldVersion = (String ) evt.getOldValue(); 310 String newVersion = (String ) evt.getNewValue(); 311 vl.versionChanged(oldVersion, newVersion); 312 } 313 } else if (evt.getPropertyName ().equals (EjbJarProjectProperties.J2EE_SERVER_INSTANCE)) { 314 Deployment d = Deployment.getDefault (); 315 String oldServerID = evt.getOldValue () == null ? null : d.getServerID ((String ) evt.getOldValue ()); 316 String newServerID = evt.getNewValue () == null ? null : d.getServerID ((String ) evt.getNewValue ()); 317 fireServerChange (oldServerID, newServerID); 318 } else if (EjbJarProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) { 319 String oldValue = (String )evt.getOldValue(); 320 String newValue = (String )evt.getNewValue(); 321 firePropertyChange( 322 PROP_ENTERPRISE_RESOURCE_DIRECTORY, 323 oldValue == null ? null : new File (oldValue), 324 newValue == null ? null : new File (newValue)); 325 } 326 } 327 328 public String getUrl() { 329 EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 330 String name = ep.getProperty(EjbJarProjectProperties.JAR_NAME); 331 return name == null ? "" : ('/' + name); 332 } 333 334 public boolean isManifestChanged(long timestamp) { 335 return false; 336 } 337 338 public void setUrl(String url) { 339 throw new UnsupportedOperationException ("Cannot customize URL of EJB module"); } 341 342 public boolean ejbsChanged() { 343 return false; 344 } 345 346 public String [] getChangedEjbs() { 347 return new String [] {}; 348 } 349 350 public String getJ2eePlatformVersion() { 351 return helper.getStandardPropertyEvaluator().getProperty(EjbJarProjectProperties.J2EE_PLATFORM); 352 } 353 354 public MetadataUnit getMetadataUnit() { 355 synchronized (this) { 356 if (metadataUnit == null) { 357 metadataUnit = new MetadataUnitImpl(); 358 } 359 return metadataUnit; 360 } 361 } 362 363 public FileObject[] getSourceRoots() { 364 Sources sources = ProjectUtils.getSources(project); 365 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 366 List roots = new LinkedList(); 367 FileObject metaInf = getMetaInf(); 368 if (metaInf != null) { 369 roots.add(metaInf); 370 } 371 372 for (int i = 0; i < groups.length; i++) { 373 roots.add(groups[i].getRootFolder()); 374 } 375 FileObject[] rootArray = new FileObject[roots.size()]; 376 return (FileObject[])roots.toArray(rootArray); 377 } 378 379 private void showErrorMessage(final String message) { 380 if(new Date().getTime() > notificationTimeout && isProjectOpen()) { 382 Runnable r = new Runnable () { 386 public void run() { 387 if (!SwingUtilities.isEventDispatchThread()) { 388 SwingUtilities.invokeLater(this); 389 } else { 390 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 391 } 392 } 393 }; 394 r.run(); 395 396 notificationTimeout = new Date().getTime() + 20000; 399 } 400 } 401 402 private boolean isProjectOpen() { 403 if (OpenProjects.getDefault() != null) { 406 Project[] projects = OpenProjects.getDefault().getOpenProjects(); 407 for (int i = 0; i < projects.length; i++) { 408 if (projects[i].equals(project)) { 409 return true; 410 } 411 } 412 return false; 413 } else { 414 return true; 417 } 418 } 419 420 private ClassPath getMetadataClassPath() { 421 synchronized (this) { 422 if (metadataClassPath == null) { 423 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class); 424 metadataClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] { 425 cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE), 426 cpProvider.getJ2eePlatformClassPath(), 427 }); 428 } 429 return metadataClassPath; 430 } 431 } 432 433 private class MetadataUnitImpl implements MetadataUnit { 434 public ClassPath getClassPath() { 435 return getMetadataClassPath(); 436 } 437 public FileObject getDeploymentDescriptor() { 438 return EjbJarProvider.this.getDeploymentDescriptor(); 439 } 440 } 441 442 private static class IT implements Iterator { 443 java.util.Enumeration ch; 444 FileObject root; 445 446 private IT(FileObject f) { 447 this.ch = f.getChildren(true); 448 this.root = f; 449 } 450 451 public boolean hasNext() { 452 return ch.hasMoreElements(); 453 } 454 455 public Object next() { 456 FileObject f = (FileObject) ch.nextElement(); 457 return new FSRootRE(root, f); 458 } 459 460 public void remove() { 461 throw new UnsupportedOperationException (); 462 } 463 464 } 465 466 private static final class FSRootRE implements J2eeModule.RootedEntry { 467 FileObject f; 468 FileObject root; 469 470 FSRootRE(FileObject root, FileObject f) { 471 this.f = f; 472 this.root = root; 473 } 474 475 public FileObject getFileObject() { 476 return f; 477 } 478 479 public String getRelativePath() { 480 return FileUtil.getRelativePath(root, f); 481 } 482 } 483 } 484 | Popular Tags |