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