1 19 package org.netbeans.modules.j2ee.clientproject.classpath; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.IOException ; 24 import java.io.File ; 25 import java.net.URI ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties; 30 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 31 import org.openide.ErrorManager; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.Mutex; 35 import org.netbeans.api.project.libraries.Library; 36 import org.netbeans.api.project.ant.AntArtifact; 37 import org.netbeans.api.project.ProjectManager; 38 import org.netbeans.api.project.Project; 39 import org.netbeans.api.project.libraries.LibraryManager; 40 import org.netbeans.spi.project.support.ant.AntProjectHelper; 41 import org.netbeans.spi.project.support.ant.ReferenceHelper; 42 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 43 import org.netbeans.spi.project.support.ant.EditableProperties; 44 import org.netbeans.modules.j2ee.clientproject.UpdateHelper; 45 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AntArtifactChooser; 46 import org.openide.util.RequestProcessor; 47 48 public class AppClientProjectClassPathExtender implements ProjectClassPathExtender, PropertyChangeListener { 49 50 private static final String DEFAULT_CLASS_PATH = AppClientProjectProperties.JAVAC_CLASSPATH; 51 private static final String DEFAULT_INCLUDED_LIBS_ELEMENT = ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES; 52 53 private Project project; 54 private UpdateHelper helper; 55 private ReferenceHelper refHelper; 56 private PropertyEvaluator eval; 57 58 59 private ClassPathSupport cs; 60 61 private volatile boolean projectDeleted; 62 63 public AppClientProjectClassPathExtender (Project project, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper) { 64 this.project = project; 65 this.helper = helper; 66 this.eval = eval; 67 this.refHelper = refHelper; 68 69 this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(), 70 AppClientProjectProperties.WELL_KNOWN_PATHS, 71 AppClientProjectProperties.LIBRARY_PREFIX, 72 AppClientProjectProperties.LIBRARY_SUFFIX, 73 AppClientProjectProperties.ANT_ARTIFACT_PREFIX ); 74 eval.addPropertyChangeListener(this); registerLibraryListeners(); 76 } 77 78 public boolean addLibrary(final Library library) throws IOException { 79 return addLibraries(DEFAULT_CLASS_PATH, new Library[] { library }, DEFAULT_INCLUDED_LIBS_ELEMENT); 80 } 81 82 114 115 public boolean addLibraries(final String classPathId, final Library[] libraries, final String includedLibrariesElement) throws IOException { 116 assert libraries != null : "Parameter cannot be null"; try { 118 return ((Boolean )ProjectManager.mutex().writeAccess( 119 new Mutex.ExceptionAction () { 120 public Object run() throws Exception { 121 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 122 String raw = props.getProperty(classPathId); 123 List <ClassPathSupport.Item> resources = cs.itemsList( raw, includedLibrariesElement ); 124 boolean added = false; 125 for (int i = 0; i < libraries.length; i++) { 126 ClassPathSupport.Item item = ClassPathSupport.Item.create( libraries[i], null, includedLibrariesElement != null ); 127 if (!resources.contains(item)) { 128 resources.add (item); 129 added = true; 130 } 131 } 132 if (added) { 133 String itemRefs[] = cs.encodeToStrings( resources.iterator(), includedLibrariesElement ); 134 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty(classPathId, itemRefs); 136 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 137 138 EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH); 140 List <ClassPathSupport.Item> l = new ArrayList <ClassPathSupport.Item>(); 141 l.addAll(resources); 142 AppClientProjectProperties.storeLibrariesLocations(l.iterator(), privateProps); 143 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps); 144 145 ProjectManager.getDefault().saveProject(project); 146 return Boolean.TRUE; 147 } 148 return Boolean.FALSE; 149 } 150 } 151 )).booleanValue(); 152 } catch (Exception e) { 153 if (e instanceof IOException ) { 154 throw (IOException ) e; 155 } 156 else { 157 Exception t = new IOException (); 158 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 159 } 160 } 161 } 162 163 public boolean addArchiveFile(final FileObject archiveFile) throws IOException { 164 return addArchiveFiles(DEFAULT_CLASS_PATH, new FileObject[] { archiveFile }, DEFAULT_INCLUDED_LIBS_ELEMENT); 165 } 166 167 public boolean addArchiveFiles(final String classPathId, final FileObject[] archiveFiles, final String includedLibrariesElement) throws IOException { 168 assert archiveFiles != null : "Parameter cannot be null"; try { 170 return ((Boolean )ProjectManager.mutex().writeAccess( 171 new Mutex.ExceptionAction () { 172 public Object run() throws Exception { 173 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 174 String raw = props.getProperty(classPathId); 175 List <ClassPathSupport.Item> resources = cs.itemsList( raw, includedLibrariesElement ); 176 boolean added = false; 177 for (int i = 0; i < archiveFiles.length; i++) { 178 File f = FileUtil.toFile (archiveFiles[i]); 179 if (f == null ) { 180 throw new IllegalArgumentException ("The file must exist on disk"); } 182 ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null, includedLibrariesElement != null ); 183 if (!resources.contains(item)) { 184 resources.add (item); 185 added = true; 186 } 187 } 188 if (added) { 189 String itemRefs[] = cs.encodeToStrings( resources.iterator(), includedLibrariesElement ); 190 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty(classPathId, itemRefs); 192 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 193 ProjectManager.getDefault().saveProject(project); 194 return Boolean.TRUE; 195 } 196 return Boolean.FALSE; 197 } 198 } 199 )).booleanValue(); 200 } catch (Exception e) { 201 if (e instanceof IOException ) { 202 throw (IOException ) e; 203 } 204 else { 205 Exception t = new IOException (); 206 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 207 } 208 } 209 } 210 211 251 252 254 public boolean addAntArtifact(AntArtifact artifact, URI artifactElement) throws IOException { 255 return addAntArtifacts(DEFAULT_CLASS_PATH, new AntArtifactChooser.ArtifactItem[] { new AntArtifactChooser.ArtifactItem(artifact, artifactElement) }, DEFAULT_INCLUDED_LIBS_ELEMENT); 256 } 257 258 public boolean addAntArtifacts(final String classPathId, final AntArtifactChooser.ArtifactItem[] artifactItems, final String includedLibrariesElement) throws IOException { 259 assert artifactItems != null : "Parameter cannot be null"; try { 261 return ((Boolean )ProjectManager.mutex().writeAccess( 262 new Mutex.ExceptionAction () { 263 public Object run() throws Exception { 264 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 265 String raw = props.getProperty (classPathId); 266 List <ClassPathSupport.Item> resources = cs.itemsList( raw, includedLibrariesElement ); 267 boolean added = false; 268 for (int i = 0; i < artifactItems.length; i++) { 269 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, includedLibrariesElement != null ); 270 if (!resources.contains(item)) { 271 resources.add (item); 272 added = true; 273 } 274 } 275 if (added) { 276 String itemRefs[] = cs.encodeToStrings( resources.iterator(), includedLibrariesElement ); 277 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty (classPathId, itemRefs); 279 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 280 ProjectManager.getDefault().saveProject(project); 281 return Boolean.TRUE; 282 } 283 return Boolean.FALSE; 284 } 285 } 286 )).booleanValue(); 287 } catch (Exception e) { 288 if (e instanceof IOException ) { 289 throw (IOException ) e; 290 } 291 else { 292 Exception t = new IOException (); 293 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 294 } 295 } 296 } 297 298 333 334 private void registerLibraryListeners () { 335 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); Library libs [] = LibraryManager.getDefault().getLibraries(); 337 for (int i = 0; i < libs.length; i++) { 338 libs [i].removePropertyChangeListener(this); 339 } 340 Iterator i = cs.itemsIterator(props.getProperty(AppClientProjectProperties.JAVAC_CLASSPATH), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES); 341 while (i.hasNext()) { 342 ClassPathSupport.Item item = (ClassPathSupport.Item)i.next(); 343 if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken()) { 344 item.getLibrary().addPropertyChangeListener(this); 345 } 346 } 347 } 348 349 public void propertyChange (PropertyChangeEvent e) { 350 if (projectDeleted) { 351 return; 352 } 353 if (e.getSource().equals(eval) && (e.getPropertyName().equals(AppClientProjectProperties.JAVAC_CLASSPATH))) { 354 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); String javacCp = props.getProperty(AppClientProjectProperties.JAVAC_CLASSPATH); 356 if (javacCp != null) { 357 registerLibraryListeners(); 358 storeLibLocations(); 359 } 360 } else if (e.getPropertyName().equals(Library.PROP_CONTENT)) { 361 storeLibLocations(); 362 } 363 } 364 365 private void storeLibLocations() { 366 RequestProcessor.getDefault().post(new Runnable () { 367 public void run() { 368 ProjectManager.mutex().writeAccess(new Runnable () { 369 public void run() { 370 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH); 373 List <ClassPathSupport.Item> wmLibs = cs.itemsList(props.getProperty(AppClientProjectProperties.JAVAC_CLASSPATH), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES); 374 cs.encodeToStrings(wmLibs.iterator(), ClassPathSupport.ELEMENT_INCLUDED_LIBRARIES); 375 AppClientProjectProperties.storeLibrariesLocations(wmLibs.iterator(), privateProps); 376 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps); 377 378 try { 379 ProjectManager.getDefault().saveProject(project); 380 } catch (IOException e) { 381 ErrorManager.getDefault().notify(e); 382 } 383 } 384 }); 385 } 386 }); 387 } 388 389 public void notifyDeleting() { 390 projectDeleted = true; 391 eval.removePropertyChangeListener(this); 392 } 393 } 394 | Popular Tags |