1 19 20 package org.netbeans.modules.j2ee.clientproject; 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.Date ; 27 import java.util.Enumeration ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Set ; 33 import javax.swing.SwingUtilities ; 34 import org.netbeans.api.java.project.JavaProjectConstants; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.api.project.ProjectUtils; 37 import org.netbeans.api.project.SourceGroup; 38 import org.netbeans.api.project.Sources; 39 import org.netbeans.api.project.ui.OpenProjects; 40 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties; 41 import org.netbeans.modules.j2ee.dd.api.client.AppClient; 43 import org.netbeans.modules.j2ee.dd.api.client.DDProvider; 44 import org.netbeans.modules.j2ee.deployment.common.api.EjbChangeDescriptor; 46 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 47 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 48 import org.netbeans.modules.j2ee.deployment.devmodules.api.ModuleChangeReporter; 49 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 50 import org.netbeans.modules.j2ee.spi.ejbjar.CarImplementation; 51 import org.netbeans.modules.schema2beans.BaseBean; 52 import org.netbeans.modules.websvc.api.client.WebServicesClientConstants; 54 import org.netbeans.spi.java.classpath.ClassPathProvider; 55 import org.netbeans.spi.project.support.ant.AntProjectHelper; 56 import org.netbeans.spi.project.support.ant.EditableProperties; 57 import org.openide.DialogDisplayer; 58 import org.openide.NotifyDescriptor; 60 import org.openide.filesystems.FileObject; 61 import org.openide.filesystems.FileUtil; 62 import org.openide.util.NbBundle; 63 import org.openide.util.WeakListeners; 64 65 68 public final class AppClientProvider extends J2eeModuleProvider 69 implements CarImplementation, J2eeModule, ModuleChangeReporter, EjbChangeDescriptor, PropertyChangeListener { 70 71 public static final String FILE_DD = "application-client.xml"; 73 private final AppClientProject project; 74 private final AntProjectHelper helper; 75 private Set <J2eeModule.VersionListener> versionListeners; 76 77 private long notificationTimeout = 0; 79 AppClientProvider(AppClientProject project, AntProjectHelper helper) { 80 this.project = project; 81 this.helper = helper; 82 project.evaluator().addPropertyChangeListener(this); 83 } 84 85 public FileObject getDeploymentDescriptor() { 86 FileObject metaInfFo = getMetaInf(); 87 if (metaInfFo == null) { 88 return null; 89 } 90 return metaInfFo.getFileObject(FILE_DD); 91 } 92 93 public FileObject[] getJavaSources() { 94 return project.getSourceRoots().getRoots(); 95 } 96 97 public FileObject getMetaInf() { 98 FileObject metaInf = getFileObject(AppClientProjectProperties.META_INF); 99 if (metaInf == null) { 100 String version = project.getAPICar().getJ2eePlatformVersion(); 101 if (needConfigurationFolder(version)) { 102 String relativePath = helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.META_INF); 103 String path = (relativePath != null ? helper.resolvePath(relativePath) : ""); 104 showErrorMessage(NbBundle.getMessage(AppClientProvider.class, "MSG_MetaInfCorrupted", project.getName(), path)); 105 } 106 } 107 return metaInf; 108 } 109 110 111 static boolean needConfigurationFolder(final String version) { 112 return AppClientProjectProperties.J2EE_1_3.equals(version) || 113 AppClientProjectProperties.J2EE_1_4.equals(version); 114 } 115 116 public File getMetaInfAsFile() { 117 return getFile(AppClientProjectProperties.META_INF); 118 } 119 120 public File getEnterpriseResourceDirectory() { 121 return getFile(AppClientProjectProperties.RESOURCE_DIR); 122 } 123 124 public FileObject findDeploymentConfigurationFile(String name) { 125 FileObject metaInf = getMetaInf(); 126 if (metaInf == null) { 127 return null; 128 } 129 return metaInf.getFileObject(name); 130 } 131 132 public File getDeploymentConfigurationFile(String name) { 133 return new File (getMetaInfAsFile(), name); 134 } 135 136 public ClassPathProvider getClassPathProvider() { 137 return (ClassPathProvider) project.getLookup().lookup(ClassPathProvider.class); 138 } 139 140 public FileObject getArchive() { 141 return getFileObject(AppClientProjectProperties.DIST_JAR); 142 } 143 144 private FileObject getFileObject(String propname) { 145 String prop = helper.getStandardPropertyEvaluator().getProperty(propname); 146 if (prop != null) { 147 return helper.resolveFileObject(prop); 148 } 149 150 return null; 151 } 152 153 private File getFile(String propname) { 154 String prop = helper.getStandardPropertyEvaluator().getProperty(propname); 155 if (prop != null) { 156 return helper.resolveFile(prop); 157 } 158 return null; 159 } 160 161 public J2eeModule getJ2eeModule() { 162 return this; 163 } 164 165 public ModuleChangeReporter getModuleChangeReporter() { 166 return this; 167 } 168 169 public boolean useDefaultServer() { 170 return true; 171 } 172 173 public String getServerID() { 174 return helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.J2EE_SERVER_TYPE); 175 } 176 177 public String getServerInstanceID() { 178 return helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.J2EE_SERVER_INSTANCE); 179 } 180 181 public void setServerInstanceID(String serverInstanceID) { 182 assert serverInstanceID != null : "passed serverInstanceID cannot be null"; 183 AppClientProjectProperties.setServerInstance(project, helper, serverInstanceID); 184 } 185 186 public Iterator getArchiveContents() throws IOException { 187 return new IT(getContentDirectory()); 188 } 189 190 public FileObject getContentDirectory() { 191 return getFileObject(AppClientProjectProperties.BUILD_CLASSES_DIR); 192 } 193 194 public FileObject getBuildDirectory() { 195 return getFileObject(AppClientProjectProperties.BUILD_DIR); 196 } 197 198 public File getContentDirectoryAsFile() { 199 return getFile(AppClientProjectProperties.BUILD_CLASSES_DIR); 200 } 201 202 public BaseBean getDeploymentDescriptor(String location) { 203 if (J2eeModule.CLIENT_XML.equals(location)){ 204 AppClient appClient = Utils.getAppClient(project); 205 if (appClient != null) { 206 return DDProvider.getDefault().getBaseBean(appClient); 208 } 209 } 215 return null; 216 } 217 241 public FileObject getDD() { 242 FileObject metaInfFo = getMetaInf(); 243 if (metaInfFo==null) { 244 return null; 245 } 246 return metaInfFo.getFileObject(WebServicesClientConstants.WEBSERVICES_DD, "xml"); } 248 249 public EjbChangeDescriptor getEjbChanges(long timestamp) { 250 return this; 251 } 252 253 public Object getModuleType() { 254 return J2eeModule.CLIENT; 255 } 256 257 public String getModuleVersion() { 258 AppClient ac = Utils.getAppClient(project); 259 return (ac == null) ? AppClient.VERSION_1_4 : ac.getVersion().toString(); 260 } 261 262 private Set <J2eeModule.VersionListener> versionListeners() { 263 if (versionListeners == null) { 264 versionListeners = new HashSet <J2eeModule.VersionListener>(); 265 AppClient appClient = Utils.getAppClient(project); 266 if (appClient != null) { 267 PropertyChangeListener l = (PropertyChangeListener ) WeakListeners.create(PropertyChangeListener .class, this, appClient); 268 appClient.addPropertyChangeListener(l); 269 } 270 } 271 return versionListeners; 272 } 273 274 public void addVersionListener(J2eeModule.VersionListener vl) { 275 versionListeners().add(vl); 276 } 277 278 public void removeVersionListener(J2eeModule.VersionListener vl) { 279 if (versionListeners != null) { 280 versionListeners.remove(vl); 281 } 282 } 283 284 public void propertyChange(PropertyChangeEvent evt) { 285 if (evt.getPropertyName().equals(AppClient.PROPERTY_VERSION)) { 286 for (J2eeModule.VersionListener vl : versionListeners) { 287 String oldVersion = (String ) evt.getOldValue(); 288 String newVersion = (String ) evt.getNewValue(); 289 vl.versionChanged(oldVersion, newVersion); 290 } 291 } else if (evt.getPropertyName().equals(AppClientProjectProperties.J2EE_SERVER_INSTANCE)) { 292 Deployment d = Deployment.getDefault(); 293 String oldServerID = evt.getOldValue() == null ? null : d.getServerID((String ) evt.getOldValue()); 294 String newServerID = evt.getNewValue() == null ? null : d.getServerID((String ) evt.getNewValue()); 295 fireServerChange(oldServerID, newServerID); 296 } else if (AppClientProjectProperties.RESOURCE_DIR.equals(evt.getPropertyName())) { 297 String oldValue = (String )evt.getOldValue(); 298 String newValue = (String )evt.getNewValue(); 299 firePropertyChange( 300 PROP_ENTERPRISE_RESOURCE_DIRECTORY, 301 oldValue == null ? null : new File (oldValue), 302 newValue == null ? null : new File (newValue)); 303 } 304 } 305 306 public String getUrl() { 307 EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 308 String name = ep.getProperty(AppClientProjectProperties.JAR_NAME); 309 return name == null ? "" : ('/' + name); 310 } 311 312 public boolean isManifestChanged(long timestamp) { 313 return false; 314 } 315 316 public void setUrl(String url) { 317 throw new UnsupportedOperationException ("Cannot customize URL of Application Client module"); } 319 320 public boolean ejbsChanged() { 321 return false; 322 } 323 324 public String [] getChangedEjbs() { 325 return new String [] {}; 326 } 327 328 public String getJ2eePlatformVersion() { 329 return helper.getStandardPropertyEvaluator().getProperty(AppClientProjectProperties.J2EE_PLATFORM); 330 } 331 332 public FileObject[] getSourceRoots() { 333 Sources sources = ProjectUtils.getSources(project); 334 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 335 List <FileObject> roots = new LinkedList <FileObject>(); 336 FileObject metaInf = getMetaInf(); 337 if (metaInf != null) { 338 roots.add(metaInf); 339 } 340 for (int i = 0; i < groups.length; i++) { 341 roots.add(groups[i].getRootFolder()); 342 } 343 FileObject[] rootArray = new FileObject[roots.size()]; 344 return roots.toArray(rootArray); 345 } 346 347 private void showErrorMessage(final String message) { 348 if(new Date ().getTime() > notificationTimeout && isProjectOpen()) { 350 Runnable r = new Runnable () { 354 public void run() { 355 if (!SwingUtilities.isEventDispatchThread()) { 356 SwingUtilities.invokeLater(this); 357 } else { 358 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 359 } 360 } 361 }; 362 r.run(); 363 364 notificationTimeout = new Date ().getTime() + 20000; 367 } 368 } 369 370 private boolean isProjectOpen() { 371 if (OpenProjects.getDefault() != null) { 374 Project[] projects = OpenProjects.getDefault().getOpenProjects(); 375 for (int i = 0; i < projects.length; i++) { 376 if (projects[i].equals(project)) { 377 return true; 378 } 379 } 380 return false; 381 } else { 382 return true; 385 } 386 } 387 388 private static class IT implements Iterator { 389 390 Enumeration ch; 391 FileObject root; 392 393 private IT(FileObject f) { 394 this.ch = f.getChildren(true); 395 this.root = f; 396 } 397 398 public boolean hasNext() { 399 return ch.hasMoreElements(); 400 } 401 402 public Object next() { 403 FileObject f = (FileObject) ch.nextElement(); 404 return new FSRootRE(root, f); 405 } 406 407 public void remove() { 408 throw new UnsupportedOperationException (); 409 } 410 411 } 412 413 private static final class FSRootRE implements J2eeModule.RootedEntry { 414 415 FileObject f; 416 FileObject root; 417 418 FSRootRE(FileObject root, FileObject f) { 419 this.f = f; 420 this.root = root; 421 } 422 423 public FileObject getFileObject() { 424 return f; 425 } 426 427 public String getRelativePath() { 428 return FileUtil.getRelativePath(root, f); 429 } 430 431 } 432 433 } 434 | Popular Tags |