1 19 20 package org.netbeans.modules.java.j2seproject.classpath; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.api.java.project.JavaProjectConstants; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectManager; 32 import org.netbeans.api.project.SourceGroup; 33 import org.netbeans.api.project.Sources; 34 import org.netbeans.api.project.ant.AntArtifact; 35 import org.netbeans.api.project.libraries.Library; 36 import org.netbeans.modules.java.j2seproject.UpdateHelper; 37 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 38 import org.netbeans.spi.java.project.classpath.ProjectClassPathModifierImplementation; 39 import org.netbeans.spi.project.support.ant.AntProjectHelper; 40 import org.netbeans.spi.project.support.ant.EditableProperties; 41 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 42 import org.netbeans.spi.project.support.ant.ReferenceHelper; 43 import org.openide.ErrorManager; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.util.Mutex; 46 import org.openide.util.MutexException; 47 48 52 public class J2SEProjectClassPathModifier extends ProjectClassPathModifierImplementation { 53 54 static final int ADD = 1; 55 static final int REMOVE = 2; 56 57 private final Project project; 58 private final UpdateHelper helper; 59 private final ReferenceHelper refHelper; 60 private final PropertyEvaluator eval; 61 private final ClassPathSupport cs; 62 63 64 public J2SEProjectClassPathModifier(final Project project, final UpdateHelper helper, final PropertyEvaluator eval, final ReferenceHelper refHelper) { 65 assert project != null; 66 assert helper != null; 67 assert eval != null; 68 assert refHelper != null; 69 this.project = project; 70 this.helper = helper; 71 this.eval = eval; 72 this.refHelper = refHelper; 73 this.cs = new ClassPathSupport( eval, refHelper, helper.getAntProjectHelper(), 74 J2SEProjectProperties.WELL_KNOWN_PATHS, 75 J2SEProjectProperties.LIBRARY_PREFIX, 76 J2SEProjectProperties.LIBRARY_SUFFIX, 77 J2SEProjectProperties.ANT_ARTIFACT_PREFIX ); 78 } 79 80 protected SourceGroup[] getExtensibleSourceGroups() { 81 Sources s = (Sources) this.project.getLookup().lookup(Sources.class); 82 assert s != null; 83 return s.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 84 } 85 86 protected String [] getExtensibleClassPathTypes (SourceGroup sg) { 87 return new String [] { 88 ClassPath.COMPILE, 89 ClassPath.EXECUTE 90 }; 91 } 92 93 protected boolean removeRoots(final URL [] classPathRoots, final SourceGroup sourceGroup, final String type) throws IOException , UnsupportedOperationException { 94 return handleRoots (classPathRoots, getClassPathProperty(sourceGroup, type), REMOVE); 95 } 96 97 protected boolean addRoots (final URL [] classPathRoots, final SourceGroup sourceGroup, final String type) throws IOException , UnsupportedOperationException { 98 return handleRoots (classPathRoots, getClassPathProperty(sourceGroup, type), ADD); 99 } 100 101 boolean handleRoots (final URL [] classPathRoots, final String classPathProperty, final int operation) throws IOException , UnsupportedOperationException { 102 assert classPathRoots != null : "The classPathRoots cannot be null"; assert classPathProperty != null; 104 try { 105 return ProjectManager.mutex().writeAccess( 106 new Mutex.ExceptionAction<Boolean >() { 107 public Boolean run() throws Exception { 108 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 109 String raw = props.getProperty(classPathProperty); 110 List <ClassPathSupport.Item> resources = cs.itemsList(raw); 111 boolean changed = false; 112 for (int i=0; i< classPathRoots.length; i++) { 113 assert classPathRoots[i] != null; 114 assert classPathRoots[i].toExternalForm().endsWith("/"); URL toAdd = FileUtil.getArchiveFile(classPathRoots[i]); 116 if (toAdd == null) { 117 toAdd = classPathRoots[i]; 118 } 119 File f = FileUtil.normalizeFile( new File (URI.create(toAdd.toExternalForm()))); 120 if (f == null ) { 121 throw new IllegalArgumentException ("The file must exist on disk"); } 123 ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null ); 124 if (operation == ADD && !resources.contains(item)) { 125 resources.add (item); 126 changed = true; 127 } 128 else if (operation == REMOVE && resources.contains(item)) { 129 resources.remove(item); 130 changed = true; 131 } 132 } 133 if (changed) { 134 String itemRefs[] = cs.encodeToStrings( resources.iterator() ); 135 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty(classPathProperty, itemRefs); 137 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 138 ProjectManager.getDefault().saveProject(project); 139 return true; 140 } 141 return false; 142 } 143 } 144 ); 145 } catch (Exception e) { 146 if (e instanceof IOException ) { 147 throw (IOException ) e; 148 } 149 else { 150 Exception t = new IOException (); 151 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 152 } 153 } 154 } 155 156 protected boolean removeAntArtifacts(final AntArtifact[] artifacts, final URI [] artifactElements, final SourceGroup sourceGroup, final String type) throws IOException , UnsupportedOperationException { 157 return handleAntArtifacts (artifacts, artifactElements, getClassPathProperty(sourceGroup, type), REMOVE); 158 } 159 160 protected boolean addAntArtifacts(final AntArtifact[] artifacts, final URI [] artifactElements, final SourceGroup sourceGroup, final String type) throws IOException , UnsupportedOperationException { 161 return handleAntArtifacts (artifacts, artifactElements, getClassPathProperty(sourceGroup, type), ADD); 162 } 163 164 boolean handleAntArtifacts (final AntArtifact[] artifacts, final URI [] artifactElements, final String classPathProperty, final int operation) throws IOException , UnsupportedOperationException { 165 assert artifacts != null : "Artifacts cannot be null"; assert artifactElements != null : "ArtifactElements cannot be null"; assert artifacts.length == artifactElements.length : "Each artifact has to have corresponding artifactElement"; assert classPathProperty != null; 169 try { 170 return ProjectManager.mutex().writeAccess( 171 new Mutex.ExceptionAction<Boolean >() { 172 public Boolean run() throws Exception { 173 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 174 String raw = props.getProperty (classPathProperty); 175 List <ClassPathSupport.Item> resources = cs.itemsList(raw); 176 boolean changed = false; 177 for (int i=0; i<artifacts.length; i++) { 178 assert artifacts[i] != null; 179 assert artifactElements[i] != null; 180 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifacts[i], artifactElements[i], null ); 181 if (operation == ADD && !resources.contains(item)) { 182 resources.add (item); 183 changed = true; 184 } 185 else if (operation == REMOVE && resources.contains(item)) { 186 resources.remove(item); 187 changed = true; 188 } 189 } 190 if (changed) { 191 String itemRefs[] = cs.encodeToStrings( resources.iterator() ); 192 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty (classPathProperty, itemRefs); 194 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 195 ProjectManager.getDefault().saveProject(project); 196 return true; 197 } 198 return false; 199 } 200 } 201 ); 202 } catch (Exception e) { 203 if (e instanceof IOException ) { 204 throw (IOException ) e; 205 } 206 else { 207 Exception t = new IOException (); 208 throw (IOException ) ErrorManager.getDefault().annotate(t,e); 209 } 210 } 211 } 212 213 214 protected boolean removeLibraries(final Library[] libraries, final SourceGroup sourceGroup, final String type) throws IOException , UnsupportedOperationException { 215 return handleLibraries (libraries, getClassPathProperty(sourceGroup, type), REMOVE); 216 } 217 218 protected boolean addLibraries(final Library[] libraries, final SourceGroup sourceGroup, final String type) throws IOException , UnsupportedOperationException { 219 return handleLibraries (libraries, getClassPathProperty(sourceGroup, type), ADD); 220 } 221 222 boolean handleLibraries (final Library[] libraries, final String classPathProperty, final int operation) throws IOException , UnsupportedOperationException { 223 assert libraries != null : "Libraries cannot be null"; assert classPathProperty != null; 225 try { 226 return ProjectManager.mutex().writeAccess( 227 new Mutex.ExceptionAction<Boolean >() { 228 public Boolean run() throws IOException { 229 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 230 String raw = props.getProperty(classPathProperty); 231 List <ClassPathSupport.Item> resources = cs.itemsList(raw); 232 List <ClassPathSupport.Item> changed = new ArrayList <ClassPathSupport.Item>(libraries.length); 233 for (int i=0; i< libraries.length; i++) { 234 assert libraries[i] != null; 235 ClassPathSupport.Item item = ClassPathSupport.Item.create( libraries[i], null ); 236 if (operation == ADD && !resources.contains(item)) { 237 resources.add (item); 238 changed.add(item); 239 } 240 else if (operation == REMOVE && resources.contains(item)) { 241 resources.remove(item); 242 changed.add(item); 243 } 244 } 245 if (!changed.isEmpty()) { 246 String itemRefs[] = cs.encodeToStrings( resources.iterator() ); 247 props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); props.setProperty(classPathProperty, itemRefs); 249 if (operation == ADD) { 250 for (ClassPathSupport.Item item : changed) { 251 String prop = cs.getLibraryReference(item); 252 prop = prop.substring(2, prop.length()-1); ClassPathSupport.relativizeLibraryClassPath(props, helper.getAntProjectHelper(), prop); 254 } 255 } 256 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 257 ProjectManager.getDefault().saveProject(project); 258 return true; 259 } 260 return false; 261 } 262 } 263 ); 264 } catch (MutexException e) { 265 throw (IOException ) e.getException(); 266 } 267 } 268 269 private String getClassPathProperty (final SourceGroup sg, final String type) throws UnsupportedOperationException { 270 assert sg != null : "SourceGroup cannot be null"; assert type != null : "Type cannot be null"; final String classPathProperty = ((ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class)).getPropertyName (sg, type); 273 if (classPathProperty == null) { 274 throw new UnsupportedOperationException ("Modification of [" + sg.getRootFolder().getPath() +", " + type + "] is not supported"); } 276 return classPathProperty; 277 } 278 } 279 | Popular Tags |