1 package org.hibernate.ejb.packaging; 2 3 import java.io.File ; 4 import java.net.URL ; 5 import java.util.HashSet ; 6 import java.util.Set ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.hibernate.AssertionFailure; 11 12 15 public abstract class JarVisitor { 16 protected static Log log = LogFactory.getLog( JarVisitor.class ); 17 protected String unqualifiedJarName; 18 protected String jarFileName; 19 protected Set <String > classNames = new HashSet <String >(); 20 protected Set <String > packageNames = new HashSet <String >(); 21 protected Set <String > hbmFileNames = new HashSet <String >(); 22 protected boolean detectClasses; 23 protected boolean detectHbm; 24 private boolean done = false; 25 26 public static JarVisitor getVisitor(URL 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 throw new AssertionFailure( "Unsupported protocol while reading jar/par: " + url.toString() ); 36 } 37 } 38 39 public static JarVisitor getVisitor(String fileName, boolean detectClasses, boolean detectHbm) { 40 File file = new File ( 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 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 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 getName() { 75 return unqualifiedJarName; 76 } 77 78 public Set <String > getClassNames() { 79 if ( !done ) doProcessElements(); 80 done = true; 81 return classNames; 82 } 83 84 public Set <String > getPackageNames() { 85 if ( !done ) doProcessElements(); 86 done = true; 87 return packageNames; 88 } 89 90 public Set <String > getHbmFiles() { 91 if ( !done ) doProcessElements(); 92 done = true; 93 return hbmFileNames; 94 } 95 96 protected abstract void doProcessElements(); 97 98 protected void addElement(String 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 |