1 19 package org.netbeans.modules.web.project.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.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.openide.ErrorManager; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.Mutex; 35 36 import org.netbeans.api.project.libraries.Library; 37 import org.netbeans.api.project.libraries.LibraryManager; 38 import org.netbeans.api.project.ant.AntArtifact; 39 import org.netbeans.api.project.ProjectManager; 40 import org.netbeans.api.project.Project; 41 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.netbeans.spi.project.support.ant.ReferenceHelper; 44 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 45 import org.netbeans.spi.project.support.ant.EditableProperties; 46 47 import org.netbeans.modules.web.project.UpdateHelper; 48 import org.netbeans.modules.web.project.ui.customizer.AntArtifactChooser; 49 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 50 import org.openide.util.RequestProcessor; 51 52 53 public class WebProjectClassPathExtender implements ProjectClassPathExtender, PropertyChangeListener { 54 55 private static final String CP_CLASS_PATH = "javac.classpath"; private static final String DEFAULT_WEB_MODULE_ELEMENT_NAME = ClassPathSupport.TAG_WEB_MODULE_LIBRARIES; 57 58 private Project project; 59 private UpdateHelper helper; 60 private PropertyEvaluator eval; 61 62 private ClassPathSupport cs; 63 64 private volatile boolean projectDeleted; 65 66 public WebProjectClassPathExtender (Project project, UpdateHelper helper, PropertyEvaluator eval, ReferenceHelper refHelper) { 67 this.project = project; 68 this.helper = helper; 69 this.eval = eval; 70 71 this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(), 72 WebProjectProperties.WELL_KNOWN_PATHS, 73 WebProjectProperties.LIBRARY_PREFIX, 74 WebProjectProperties.LIBRARY_SUFFIX, 75 WebProjectProperties.ANT_ARTIFACT_PREFIX ); 76 eval.addPropertyChangeListener(this); registerLibraryListeners(); 78 } 79 80 public boolean addLibrary(final Library library) throws IOException { 81 return addLibraries(CP_CLASS_PATH, new Library[] { library }, DEFAULT_WEB_MODULE_ELEMENT_NAME); 82 } 83 84 public boolean addLibraries(final String classPathId, final Library[] libraries, final String webModuleElementName) throws IOException { 85 assert libraries != null : "Parameter cannot be null"; try { 87 return ((Boolean )ProjectManager.mutex().writeAccess( 88 new Mutex.ExceptionAction () { 89 public Object run() throws Exception { 90 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 91 String raw = props.getProperty(classPathId); 92 List resources = cs.itemsList( raw, webModuleElementName ); 93 boolean added = false; 94 for (int i = 0; i < libraries.length; i++) { 95 ClassPathSupport.Item item = ClassPathSupport.Item.create( libraries[i], null, ClassPathSupport.Item.PATH_IN_WAR_LIB); 96 if (!resources.contains(item)) { 97 resources.add (item); 98 added = true; 99 } 100 } 101 if (added) { 102 String itemRefs[] = cs.encodeToStrings( resources.iterator(), webModuleElementName ); 103 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty(classPathId, itemRefs); 105 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 106 EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH); 108 ArrayList l = new ArrayList (); 109 l.addAll(resources); 110 l.addAll(cs.itemsList(props.getProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES)); 111 WebProjectProperties.storeLibrariesLocations(l.iterator(), privateProps); 112 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps); 113 ProjectManager.getDefault().saveProject(project); 114 return Boolean.TRUE; 115 } 116 return Boolean.FALSE; 117 } 118 } 119 )).booleanValue(); 120 } catch (Exception e) { 121 if (e instanceof IOException ) { 122 throw (IOException ) e; 123 } 124 else { 125 Exception t = new IOException (); 126 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 127 } 128 } 129 } 130 131 public boolean addArchiveFile(final FileObject archiveFile) throws IOException { 132 return addArchiveFiles(CP_CLASS_PATH, new FileObject[] { archiveFile }, DEFAULT_WEB_MODULE_ELEMENT_NAME); 133 } 134 135 public boolean addArchiveFiles(final String classPathId, final FileObject[] archiveFiles, final String webModuleElementName) throws IOException { 136 assert archiveFiles != null : "Parameter cannot be null"; try { 138 return ((Boolean )ProjectManager.mutex().writeAccess( 139 new Mutex.ExceptionAction () { 140 public Object run() throws Exception { 141 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 142 String raw = props.getProperty(classPathId); 143 List resources = cs.itemsList( raw, webModuleElementName ); 144 boolean added = false; 145 for (int i = 0; i < archiveFiles.length; i++) { 146 File f = FileUtil.toFile (archiveFiles[i]); 147 if (f == null ) { 148 throw new IllegalArgumentException ("The file must exist on disk"); } 150 ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null, archiveFiles[i].isFolder() ? ClassPathSupport.Item.PATH_IN_WAR_DIR : ClassPathSupport.Item.PATH_IN_WAR_LIB); 151 if (!resources.contains(item)) { 152 resources.add (item); 153 added = true; 154 } 155 } 156 if (added) { 157 String itemRefs[] = cs.encodeToStrings( resources.iterator(), webModuleElementName ); 158 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty(classPathId, itemRefs); 160 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 161 ProjectManager.getDefault().saveProject(project); 162 return Boolean.TRUE; 163 } 164 return Boolean.FALSE; 165 } 166 } 167 )).booleanValue(); 168 } catch (Exception e) { 169 if (e instanceof IOException ) { 170 throw (IOException ) e; 171 } 172 else { 173 Exception t = new IOException (); 174 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 175 } 176 } 177 } 178 179 181 public boolean addAntArtifact (AntArtifact artifact, URI artifactElement) throws IOException { 182 return addAntArtifacts(CP_CLASS_PATH, new AntArtifactChooser.ArtifactItem[] { new AntArtifactChooser.ArtifactItem(artifact, artifactElement) }, DEFAULT_WEB_MODULE_ELEMENT_NAME); 183 } 184 185 public boolean addAntArtifacts(final String classPathId, final AntArtifactChooser.ArtifactItem[] artifactItems, final String webModuleElementName) throws IOException { 186 assert artifactItems != null : "Parameter cannot be null"; try { 188 return ((Boolean )ProjectManager.mutex().writeAccess( 189 new Mutex.ExceptionAction () { 190 public Object run() throws Exception { 191 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 192 String raw = props.getProperty (classPathId); 193 List resources = cs.itemsList( raw, webModuleElementName ); 194 boolean added = false; 195 for (int i = 0; i < artifactItems.length; i++) { 196 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, ClassPathSupport.Item.PATH_IN_WAR_LIB); 197 if (!resources.contains(item)) { 198 resources.add (item); 199 added = true; 200 } 201 } 202 if (added) { 203 String itemRefs[] = cs.encodeToStrings( resources.iterator(), webModuleElementName ); 204 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty (classPathId, itemRefs); 206 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 207 ProjectManager.getDefault().saveProject(project); 208 return Boolean.TRUE; 209 } 210 return Boolean.FALSE; 211 } 212 } 213 )).booleanValue(); 214 } catch (Exception e) { 215 if (e instanceof IOException ) { 216 throw (IOException ) e; 217 } 218 else { 219 Exception t = new IOException (); 220 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 221 } 222 } 223 } 224 225 public ClassPathSupport getClassPathSupport () { 226 return cs; 227 } 228 229 private void registerLibraryListeners () { 230 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); Library libs [] = LibraryManager.getDefault().getLibraries(); 232 for (int i = 0; i < libs.length; i++) { 233 libs [i].removePropertyChangeListener(this); 234 } 235 HashSet set = new HashSet (); 236 set.addAll(cs.itemsList(props.getProperty(WebProjectProperties.JAVAC_CLASSPATH), WebProjectProperties.TAG_WEB_MODULE_LIBRARIES)); 237 set.addAll(cs.itemsList(props.getProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES)); 238 Iterator i = set.iterator(); 239 while (i.hasNext()) { 240 ClassPathSupport.Item item = (ClassPathSupport.Item)i.next(); 241 if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken()) { 242 item.getLibrary().addPropertyChangeListener(this); 243 } 244 } 245 } 246 247 public void propertyChange (PropertyChangeEvent e) { 248 if (projectDeleted) { 249 return; 250 } 251 if (e.getSource().equals(eval) && (e.getPropertyName().equals(WebProjectProperties.JAVAC_CLASSPATH) 252 || e.getPropertyName().equals(WebProjectProperties.WAR_CONTENT_ADDITIONAL))) { 253 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); String javacCp = props.getProperty(WebProjectProperties.JAVAC_CLASSPATH); 255 if (javacCp != null) { 256 registerLibraryListeners(); 257 if (ProjectManager.getDefault().isValid(project)) 258 storeLibLocations(); 259 } 260 } else if (e.getPropertyName().equals(Library.PROP_CONTENT)) { 261 storeLibLocations(); 262 } 263 } 264 265 private void storeLibLocations() { 266 RequestProcessor.getDefault().post(new Runnable () { 267 public void run() { 268 ProjectManager.mutex().writeAccess(new Runnable () { 269 public void run() { 270 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH); 273 List wmLibs = cs.itemsList(props.getProperty(WebProjectProperties.JAVAC_CLASSPATH), WebProjectProperties.TAG_WEB_MODULE_LIBRARIES); 274 List additionalLibs = cs.itemsList(props.getProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES); 275 cs.encodeToStrings(wmLibs.iterator(), WebProjectProperties.TAG_WEB_MODULE_LIBRARIES); 276 cs.encodeToStrings(additionalLibs.iterator(), WebProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES); 277 HashSet set = new HashSet (); 278 set.addAll(wmLibs); 279 set.addAll(additionalLibs); 280 WebProjectProperties.storeLibrariesLocations(set.iterator(), privateProps); 281 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProps); 282 283 try { 284 ProjectManager.getDefault().saveProject(project); 285 } catch (IOException e) { 286 ErrorManager.getDefault().notify(e); 287 } 288 } 289 }); 290 } 291 }); 292 } 293 294 public void notifyDeleting() { 295 projectDeleted = true; 296 eval.removePropertyChangeListener(this); 297 } 298 } 299 | Popular Tags |