KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > packaging > JarVisitor


1 package org.hibernate.ejb.packaging;
2
3 import java.io.File JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Set JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.hibernate.AssertionFailure;
11
12 /**
13  * @author Emmanuel Bernard
14  */

15 public abstract class JarVisitor {
16     protected static Log log = LogFactory.getLog( JarVisitor.class );
17     protected String JavaDoc unqualifiedJarName;
18     protected String JavaDoc jarFileName;
19     protected Set JavaDoc<String JavaDoc> classNames = new HashSet JavaDoc<String JavaDoc>();
20     protected Set JavaDoc<String JavaDoc> packageNames = new HashSet JavaDoc<String JavaDoc>();
21     protected Set JavaDoc<String JavaDoc> hbmFileNames = new HashSet JavaDoc<String JavaDoc>();
22     protected boolean detectClasses;
23     protected boolean detectHbm;
24     private boolean done = false;
25
26     public static JarVisitor getVisitor(URL JavaDoc url, boolean detectClasses, boolean detectHbm) {
27         if ( "jar".equals( url.getProtocol() ) ) {
28             return new ZippedJarVisitor( url, detectClasses, detectHbm );
29         }
30         else if ( "file".equals( url.getProtocol() ) ) {
31             return new ExplodedJarVisitor( url, detectClasses, detectHbm );
32         }
33         else {
34             //TODO: http
35
throw new AssertionFailure( "Unsupported protocol while reading jar/par: " + url.toString() );
36         }
37     }
38
39     public static JarVisitor getVisitor(String JavaDoc fileName, boolean detectClasses, boolean detectHbm) {
40         File JavaDoc file = new File JavaDoc( fileName );
41         if ( file.isFile() ) {
42             return new ZippedJarVisitor( fileName, detectClasses, detectHbm );
43         }
44         else {
45             return new ExplodedJarVisitor( fileName, detectClasses, detectHbm );
46         }
47     }
48
49     public JarVisitor(String JavaDoc jarFileName, boolean detectClasses, boolean detectHbm) {
50         this.detectClasses = detectClasses;
51         this.detectHbm = detectHbm;
52         this.jarFileName = jarFileName;
53         unqualify();
54     }
55
56     protected void unqualify() {
57         //FIXME weak algorithm
58
int slash = jarFileName.lastIndexOf( "/" );
59         if ( slash != -1 ) {
60             unqualifiedJarName = jarFileName.substring(
61                     jarFileName.lastIndexOf( "/" ) + 1,
62                     jarFileName.length() - ".xar".length()
63             );
64         }
65         else {
66             unqualifiedJarName = jarFileName;
67         }
68         log.debug( "Searching mapped entities in jar/par: " + jarFileName );
69     }
70
71     protected JarVisitor() {
72     }
73
74     public String JavaDoc getName() {
75         return unqualifiedJarName;
76     }
77
78     public Set JavaDoc<String JavaDoc> getClassNames() {
79         if ( !done ) doProcessElements();
80         done = true;
81         return classNames;
82     }
83
84     public Set JavaDoc<String JavaDoc> getPackageNames() {
85         if ( !done ) doProcessElements();
86         done = true;
87         return packageNames;
88     }
89
90     public Set JavaDoc<String JavaDoc> getHbmFiles() {
91         if ( !done ) doProcessElements();
92         done = true;
93         return hbmFileNames;
94     }
95
96     protected abstract void doProcessElements();
97
98     protected void addElement(String JavaDoc entryName) {
99         if ( detectClasses && entryName.endsWith( "package-info.class" ) ) {
100             packageNames.add( entryName.substring( 0, entryName.length() - ".package-info.class".length() ).replace( '/', '.' ) );
101         }
102         else if ( detectClasses && entryName.endsWith( ".class" ) ) {
103             classNames.add( entryName.substring( 0, entryName.length() - ".class".length() ).replace( '/', '.' ) );
104         }
105         else if ( detectHbm && entryName.endsWith( ".hbm.xml" ) ) {
106             hbmFileNames.add( entryName );
107         }
108     }
109 }
110
Popular Tags