001: package com.bm.ejb3metadata.finder;
002:
003: import java.io.File;
004: import java.net.URL;
005:
006: import com.bm.ejb3metadata.annotations.analyzer.AnnotationDeploymentAnalyzer;
007: import com.bm.ejb3metadata.annotations.exceptions.AnalyzerException;
008: import com.bm.ejb3metadata.annotations.exceptions.ResolverException;
009: import com.bm.ejb3metadata.annotations.helper.ResolverHelper;
010: import com.bm.ejb3metadata.annotations.metadata.EjbJarAnnotationMetadata;
011: import com.bm.ejb3metadata.utils.MetadataUtils;
012:
013: /**
014: * Scann a given archive or folder for annotated classes.
015: *
016: * @author Daniel Wiese
017: *
018: */
019: public class AnnonatedClassFinder {
020:
021: private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger
022: .getLogger(AnnonatedClassFinder.class);
023:
024: /**
025: * Annotation deployment analyzer.
026: */
027: private AnnotationDeploymentAnalyzer annotationDeploymentAnalyzer = null;
028:
029: private boolean isInitailized = false;
030:
031: /**
032: * Constructor.
033: */
034: public AnnonatedClassFinder() {
035:
036: }
037:
038: /**
039: * Searches all classes in a given directory (up to top lever and from there
040: * recursive) of in a given jar file.
041: *
042: * @param firstSearchClue -
043: * the location of this class will be used as a hint , where to
044: * start with the search
045: * @return - a EjbJarAnnotationMetadata objetc with list of classes (format:
046: * com/siemens/MyClass)
047: */
048: public EjbJarAnnotationMetadata getResult(Class firstSearchClue) {
049: if (!isInitailized) {
050: this .init(firstSearchClue);
051: }
052: return this .annotationDeploymentAnalyzer
053: .getEjbJarAnnotationMetadata();
054: }
055:
056: private void init(Class firstSearchClue) {
057: File rootDirOrArchive = this
058: .getJarFileOrRootDirectory(firstSearchClue);
059: this .annotationDeploymentAnalyzer = new AnnotationDeploymentAnalyzer(
060: rootDirOrArchive);
061: try {
062: annotationDeploymentAnalyzer.analyze();
063: ResolverHelper.resolve(annotationDeploymentAnalyzer
064: .getEjbJarAnnotationMetadata());
065: } catch (ResolverException e) {
066: logger.error("Error: ", e);
067: throw new RuntimeException(e);
068: } catch (AnalyzerException e) {
069: logger.error("Error: ", e);
070: throw new RuntimeException(e);
071: }
072: this .isInitailized = true;
073: }
074:
075: private File getJarFileOrRootDirectory(Class firstSearchClue) {
076: File back = null;
077: // Translate the package name into an absolute path
078: boolean isLoadInJar = false;
079: String name = firstSearchClue.getName().replace('.', '/');
080: URL loc = firstSearchClue.getResource("/" + name + ".class");
081:
082: File f = new File(loc.getFile());
083: // Class file is inside a jar file.
084: if (f.getPath().startsWith("file:")) {
085: String s = f.getPath();
086: int index = s.indexOf('!');
087: // It confirm it is a jar file
088: if (index != -1) {
089: isLoadInJar = true;
090: }
091: }
092:
093: if (!isLoadInJar) {
094: File rootDirectory = MetadataUtils.getRootPackageDir(f,
095: firstSearchClue.getName());
096: back = rootDirectory;
097: // generate interface-impl map
098: } else {
099: String jarName = MetadataUtils.isolateJarName(loc);
100: // windows bug with spaces
101: jarName = jarName.replaceAll("\\%20", " ");
102: back = new File(jarName);
103: }
104:
105: return back;
106: }
107:
108: }
|