1 19 20 package org.netbeans.modules.j2ee.ejbfreeform; 21 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Set ; 31 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; 32 import org.netbeans.modules.j2ee.metadata.MetadataUnit; 33 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarFactory; 34 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarImplementation; 35 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarProvider; 36 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarsInProject; 37 import org.netbeans.spi.java.classpath.ClassPathProvider; 38 import org.w3c.dom.Element ; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.filesystems.FileObject; 41 import org.netbeans.spi.project.AuxiliaryConfiguration; 42 import org.netbeans.api.java.classpath.ClassPath; 43 import org.netbeans.api.java.project.JavaProjectConstants; 44 import org.netbeans.api.java.queries.SourceForBinaryQuery; 45 import org.netbeans.api.project.FileOwnerQuery; 46 import org.netbeans.api.project.Project; 47 import org.netbeans.api.project.ProjectUtils; 48 import org.netbeans.api.project.SourceGroup; 49 import org.netbeans.modules.ant.freeform.spi.support.Util; 50 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 51 import org.netbeans.spi.project.support.ant.AntProjectEvent; 52 import org.netbeans.spi.project.support.ant.AntProjectHelper; 53 import org.netbeans.spi.project.support.ant.AntProjectListener; 54 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 55 import org.netbeans.spi.project.support.ant.PropertyUtils; 56 57 58 63 public class EJBModules implements EjbJarProvider, EjbJarsInProject, AntProjectListener, ClassPathProvider { 64 65 private ArrayList modules = new ArrayList (); 66 private HashMap cache = new HashMap (); 67 private Project project; 68 private AntProjectHelper helper; 69 private PropertyEvaluator evaluator; 70 71 public EJBModules (Project project, AntProjectHelper helper, PropertyEvaluator evaluator) { 72 assert project != null; 73 this.project = project; 74 this.helper = helper; 75 this.evaluator = evaluator; 76 helper.addAntProjectListener(this); 77 } 78 79 public EjbJar findEjbJar (FileObject file) { 80 Project owner = FileOwnerQuery.getOwner (file); 81 synchronized (this) { 82 if (project.equals (owner)) { 83 if (modules.isEmpty()) { 84 readAuxData (); 85 } 86 for (Iterator iter = modules.iterator (); iter.hasNext ();) { 87 FFEJBModule wm = (FFEJBModule) iter.next (); 88 if (wm.contais (file)) { 89 if (cache.get (wm) == null) { 90 cache.put (wm, EjbJarFactory.createEjbJar (wm)); 91 } 92 return (EjbJar) cache.get (wm); 93 } 94 } 95 } 96 return null; 97 } 98 } 99 100 public ClassPath findClassPath (FileObject file, String type) { 101 if (!ClassPath.SOURCE.equals (type)) { 104 return null; 105 } 106 Project owner = FileOwnerQuery.getOwner (file); 107 if (owner != null && owner.equals (project)) { 108 if (modules == null) { 109 readAuxData (); 110 } 111 for (Iterator iter = modules.iterator (); iter.hasNext ();) { 112 FFEJBModule wm = (FFEJBModule) iter.next (); 113 if (wm.contais (file)) { 114 return wm.findClassPath (file, type); 115 } 116 } 117 } 118 return null; 119 } 120 121 public synchronized void readAuxData () { 122 modules.clear(); 123 cache.clear(); 124 AuxiliaryConfiguration aux = (AuxiliaryConfiguration)project.getLookup().lookup(AuxiliaryConfiguration.class); 125 assert aux != null; 126 Element ejb = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB_2, true); 127 String namespace = EJBProjectNature.NS_EJB_2; 128 if (ejb == null) { 129 ejb = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB, true); 130 namespace = EJBProjectNature.NS_EJB; 131 } 132 if (ejb == null) { 133 return; 134 } 135 List ejbModules = Util.findSubElements(ejb); 136 Iterator it = ejbModules.iterator(); 137 while (it.hasNext()) { 138 Element ejbModulesEl = (Element )it.next(); 139 assert ejbModulesEl.getLocalName().equals("ejb-module") : ejbModulesEl; 140 FileObject configFilesFO = getFile (ejbModulesEl, "config-files"); Element j2eeSpecEl = Util.findElement (ejbModulesEl, "j2ee-spec-level", namespace); 142 String j2eeSpec = j2eeSpecEl == null ? null : evaluator.evaluate (Util.findText (j2eeSpecEl)); 143 Element classpathEl = Util.findElement (ejbModulesEl, "classpath", namespace); 144 FileObject [] sources = getSources (classpathEl); 145 ClassPath cp = classpathEl == null ? null : createClasspath (classpathEl, sources); 146 File [] j2eePlatformClasspath = org.netbeans.modules.j2ee.common.Util.getJ2eePlatformClasspathEntries(project); 147 modules.add (new FFEJBModule (configFilesFO, j2eeSpec, sources, cp, j2eePlatformClasspath)); 148 } 149 } 150 151 private FileObject getFile (Element parent, String fileElName) { 152 Element el = Util.findElement (parent, fileElName, EJBProjectNature.NS_EJB_2); 153 if (el == null) { 154 el = Util.findElement (parent, fileElName, EJBProjectNature.NS_EJB); 155 } 156 String fname = Util.findText (el); 157 String locationEval = evaluator.evaluate(fname); 158 if (locationEval != null) { 159 File locationFile = helper.resolveFile(locationEval); 160 return FileUtil.toFileObject(locationFile); 161 } 162 return null; 163 } 164 165 private FileObject [] getSources (Element classpathEl) { 166 String cp = Util.findText(classpathEl); 167 if (cp == null) { 168 cp = ""; 169 } 170 String cpEval = evaluator.evaluate(cp); 171 if (cpEval == null) { 172 return null; 173 } 174 String [] path = PropertyUtils.tokenizePath(cpEval); 175 Set srcRootSet = new HashSet (); 176 for (int i = 0; i < path.length; i++) { 177 File entryFile = helper.resolveFile(path[i]); 178 URL entry; 179 try { 180 entry = entryFile.toURI().toURL(); 181 if (!entryFile.exists () && !entry.toExternalForm ().endsWith ("/")) { 182 entry = new URL (entry.toExternalForm () + "/"); 183 } 184 } catch (MalformedURLException x) { 185 throw new AssertionError (x); 186 } 187 if (FileUtil.isArchiveFile(entry)) { 188 entry = FileUtil.getArchiveRoot(entry); 189 } 190 SourceForBinaryQuery.Result res = SourceForBinaryQuery.findSourceRoots (entry); 191 FileObject srcForBin [] = res.getRoots (); 192 for (int j = 0; j < srcForBin.length; j++) { 193 srcRootSet.add (srcForBin [j]); 194 } 195 if (srcForBin.length == 0) { 196 srcRootSet.add(FileUtil.toFileObject(entryFile)); 197 } 198 } 199 SourceGroup sg [] = ProjectUtils.getSources (project).getSourceGroups (JavaProjectConstants.SOURCES_TYPE_JAVA); 200 Set filteredSources = new HashSet (); 201 for (int i = 0; i < sg.length; i++) { 203 if (path.length == 0 || srcRootSet.contains (sg [i].getRootFolder ())) { 204 filteredSources.add (sg [i].getRootFolder ()); 205 } 206 } 207 return (FileObject []) filteredSources.toArray (new FileObject [filteredSources.size ()]); 209 } 210 211 214 private ClassPath createClasspath(Element classpathEl, FileObject[] sources) { 215 String cp = Util.findText(classpathEl); 216 if (cp == null) { 217 cp = ""; 218 } 219 String cpEval = evaluator.evaluate(cp); 220 if (cpEval == null) { 221 return null; 222 } 223 String [] path = PropertyUtils.tokenizePath(cpEval); 224 Set entries = new HashSet (); 225 for (int i = 0; i < path.length; i++) { 226 entries.add(helper.resolveFile(path[i])); 227 } 228 if (entries.size() == 0) { 229 for (int i = 0; i < sources.length; i++) { 232 entries.add(FileUtil.toFile(sources[i])); 233 } 234 } 235 URL [] pathURL = new URL [entries.size()]; 236 int i = 0; 237 for (Iterator it = entries.iterator(); it.hasNext();) { 238 File entryFile = (File )it.next(); 239 URL entry; 240 try { 241 entry = entryFile.toURI().toURL(); 242 if (FileUtil.isArchiveFile(entry)) { 243 entry = FileUtil.getArchiveRoot(entry); 244 } else { 245 String s = entry.toExternalForm(); 246 if (!s.endsWith("/")) { entry = new URL (s + '/'); 249 } 250 } 251 } catch (MalformedURLException x) { 252 throw new AssertionError (x); 253 } 254 pathURL[i++] = entry; 255 } 256 return ClassPathSupport.createClassPath(pathURL); 257 } 258 259 public void configurationXmlChanged(AntProjectEvent ev) { 260 readAuxData(); 261 } 262 263 public void propertiesChanged(AntProjectEvent ev) { 264 } 266 267 public EjbJar[] getEjbJars() { 268 if (modules.isEmpty()) { 269 readAuxData (); 270 } 271 EjbJar results [] = new EjbJar[modules.size()]; 272 int i = 0; 273 for (Iterator iter = modules.iterator (); iter.hasNext ();) { 274 FFEJBModule ejbm = (FFEJBModule) iter.next (); 275 if (cache.get (ejbm) == null) { 276 results[i] = EjbJarFactory.createEjbJar (ejbm); 277 cache.put (ejbm, results[i]); 278 } else { 279 results[i] = (EjbJar) cache.get(ejbm); 280 } 281 } 282 return results; 283 } 284 285 private static final class FFEJBModule implements EjbJarImplementation { 286 287 public static final String FILE_DD = "ejb-jar.xml"; 290 private FileObject configFilesFO; 291 private FileObject [] sourcesFOs; 292 private ClassPath classPath; 293 private String j2eeSpec; 294 private MetadataUnit metadataUnit; 295 private ClassPath metadataClassPath; 296 private File [] j2eePlatformClasspath; 297 299 FFEJBModule (FileObject configFilesFO, String j2eeSpec, FileObject sourcesFOs[], ClassPath classPath, File [] j2eePlatformClasspath) { 300 this.configFilesFO = configFilesFO; 301 this.j2eeSpec = j2eeSpec; 302 this.sourcesFOs = sourcesFOs; 304 this.classPath = classPath; 305 this.j2eePlatformClasspath = j2eePlatformClasspath; 306 } 307 308 boolean contais (FileObject fo) { 309 if (configFilesFO == fo || FileUtil.isParentOf (configFilesFO , fo)) 310 return true; 311 for (int i = 0; i < sourcesFOs.length; i++) { 312 if (sourcesFOs [i] == fo || FileUtil.isParentOf (sourcesFOs [i], fo)) 313 return true; 314 } 315 return false; 316 } 317 318 322 public ClassPath findClassPath (FileObject file, String type) { 323 return classPath; 324 } 325 326 public String getJ2eePlatformVersion () { 327 return j2eeSpec; 328 } 329 330 334 public String toString () { 335 StringBuffer sb = new StringBuffer ("EJB module in freeform project" + 336 "\n\tconfig files:" + configFilesFO.getPath () + 337 "\n\tj2ee version:" + j2eeSpec); 339 for (int i = 0; i < sourcesFOs.length; i++) { 340 sb.append ("\n\tsource root:" + sourcesFOs [i].getPath ()); 341 } 342 return sb.toString (); 343 } 344 345 public FileObject getDeploymentDescriptor () { 346 return getMetaInf ().getFileObject (FILE_DD); 347 } 348 349 public FileObject getMetaInf () { 350 return configFilesFO; 351 } 352 353 public FileObject[] getJavaSources() { 354 return sourcesFOs; 355 } 356 357 public MetadataUnit getMetadataUnit() { 358 synchronized (this) { 359 if (metadataUnit == null) { 360 metadataUnit = new MetadataUnitImpl(); 361 } 362 return metadataUnit; 363 } 364 } 365 366 private class MetadataUnitImpl implements MetadataUnit { 367 public ClassPath getClassPath() { 368 return classPath; 369 } 370 public FileObject getDeploymentDescriptor() { 371 return FFEJBModule.this.getDeploymentDescriptor(); 372 } 373 } 374 375 } 376 377 } 378 | Popular Tags |