1 19 20 package org.netbeans.modules.j2ee.persistence.util; 21 22 import java.net.URL ; 23 import java.util.HashSet ; 24 import java.util.List ; 25 import java.util.Set ; 26 import org.netbeans.api.java.classpath.ClassPath; 27 import org.netbeans.api.java.source.ClasspathInfo; 28 import org.netbeans.api.project.libraries.Library; 29 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 30 import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibrarySupport; 31 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 32 import org.openide.util.Parameters; 33 34 39 public class JPAClassPathHelper { 40 41 private final Set <ClassPath> boot; 42 private final Set <ClassPath> compile; 43 private final Set <ClassPath> source; 44 45 52 public JPAClassPathHelper(Set <ClassPath> boot, Set <ClassPath> compile, Set <ClassPath> source){ 53 Parameters.notNull("boot", boot); 54 Parameters.notNull("compile", compile); 55 Parameters.notNull("source", source); 56 this.boot = new HashSet <ClassPath>(boot); 57 this.compile = new HashSet <ClassPath>(compile); 58 this.source = new HashSet <ClassPath>(source); 59 } 60 61 73 public ClasspathInfo createClasspathInfo(){ 74 75 if (!ensureJPA()){ 76 throw new IllegalStateException ("Cannot find a Java Persistence API library"); } 78 79 return ClasspathInfo.create( 80 createProxyClassPath(boot), 81 createProxyClassPath(compile), 82 createProxyClassPath(source) 83 ); 84 } 85 86 94 private boolean ensureJPA() { 95 for (ClassPath classPath : compile) { 96 if (classPath.findResource("javax/persistence/Entity.class") != null) { return true; 98 } 99 } 100 ClassPath jpaClassPath = findJPALibrary(); 101 if (jpaClassPath != null) { 102 compile.add(jpaClassPath); 103 return true; 104 } 105 106 return false; 107 } 108 109 private ClassPath findJPALibrary() { 110 Library library = PersistenceLibrarySupport.getFirstProviderLibrary(); 111 if (library == null) { 112 return null; 113 } 114 List <URL > urls = library.getContent("classpath"); return ClassPathSupport.createClassPath(urls.toArray(new URL [urls.size()])); 116 } 117 118 119 private ClassPath createProxyClassPath(Set <ClassPath> classPaths) { 120 return ClassPathSupport.createProxyClassPath(classPaths.toArray(new ClassPath[classPaths.size()])); 121 } 122 } 123 | Popular Tags |