KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > util > JPAClassPathHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.util;
21
22 import java.net.URL JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
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 /**
35  * A helper class for ensuring that the class path is correctly
36  * set for generating classes that require the Java Persistence API, such
37  * as entity classes.
38  */

39 public class JPAClassPathHelper {
40     
41     private final Set JavaDoc<ClassPath> boot;
42     private final Set JavaDoc<ClassPath> compile;
43     private final Set JavaDoc<ClassPath> source;
44     
45     /**
46      * Creates a new JPAClassPathHelper.
47      *
48      * @param boot the boot class paths. Must not be null.
49      * @param compile the compile class paths. Must not be null.
50      * @param source the source class paths. Must not be null.
51      */

52     public JPAClassPathHelper(Set JavaDoc<ClassPath> boot, Set JavaDoc<ClassPath> compile, Set JavaDoc<ClassPath> source){
53         Parameters.notNull("boot", boot);
54         Parameters.notNull("compile", compile);
55         Parameters.notNull("source", source);
56         this.boot = new HashSet JavaDoc<ClassPath>(boot);
57         this.compile = new HashSet JavaDoc<ClassPath>(compile);
58         this.source = new HashSet JavaDoc<ClassPath>(source);
59     }
60     
61     /**
62      * Creates a ClassPathInfo (based on our class paths) that can be used for generating entities.
63      * Ensures that the compile class path has the Java Persistence API present by checking
64      * whether JPA is already present in the compile class path and if it isn't, adds
65      * an appropriate JPA library to the compile class path. It is the client's responsibility
66      * to make sure that the IDE has a library that contains the Java Persistence API. If no
67      * appropriate library could be found, an IllegalStateException is thrown.
68      *
69      * @return the ClassPathInfo for generating entities.
70      * @throws IllegalStateException if there were no libraries in the IDE that
71      * contain the Java Persistence API.
72      */

73     public ClasspathInfo createClasspathInfo(){
74         
75         if (!ensureJPA()){
76             throw new IllegalStateException JavaDoc("Cannot find a Java Persistence API library"); // NOI18N
77
}
78         
79         return ClasspathInfo.create(
80                 createProxyClassPath(boot),
81                 createProxyClassPath(compile),
82                 createProxyClassPath(source)
83                 );
84     }
85     
86     /**
87      * Ensure that the compile class path has the Java Persistence API present. Checks
88      * whether JPA is already present in the compile class path and if not, tries to
89      * find an appropriate JPA library and add it to the compile class path.
90      *
91      * @return true if the compile class path contained or could be made to contain
92      * the Java Persistence API.
93      */

94     private boolean ensureJPA() {
95         for (ClassPath classPath : compile) {
96             if (classPath.findResource("javax/persistence/Entity.class") != null) { // NOI18N
97
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 JavaDoc<URL JavaDoc> urls = library.getContent("classpath"); // NOI18N
115
return ClassPathSupport.createClassPath(urls.toArray(new URL JavaDoc[urls.size()]));
116     }
117     
118     
119     private ClassPath createProxyClassPath(Set JavaDoc<ClassPath> classPaths) {
120         return ClassPathSupport.createProxyClassPath(classPaths.toArray(new ClassPath[classPaths.size()]));
121     }
122 }
123
Popular Tags