KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > util > AnnotationDetector


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.util;
25
26 import com.sun.enterprise.deployment.annotation.introspection.ClassFile;
27 import com.sun.enterprise.deployment.annotation.introspection.ConstantPoolInfo;
28 import com.sun.enterprise.deployment.annotation.introspection.CustomAnnotationScanner;
29 import com.sun.enterprise.deployment.annotation.introspection.EjbComponentAnnotationScanner;
30 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
31 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
32
33 import java.io.BufferedInputStream JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.nio.channels.Channels JavaDoc;
40 import java.nio.channels.ReadableByteChannel JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.jar.JarEntry JavaDoc;
43 import java.util.jar.JarFile JavaDoc;
44
45 /**
46  * Abstract superclass for specific types of annotation detectors.
47  *
48  * @author Qingqing Ouyang
49  * @author tjquinn
50  */

51 public abstract class AnnotationDetector {
52     
53     private ClassFile classFile = new ClassFile();
54
55     public AnnotationDetector() {
56         CustomAnnotationScanner scanner = createAnnotationScanner();
57         ConstantPoolInfo poolInfo = new ConstantPoolInfo(scanner);
58         classFile.setConstantPoolInfo(poolInfo);
59     }
60
61     /**
62      * Provides a new annotation scanner, tailored to the particular type of
63      * annotation detector the concrete subclass implements.
64      * @return an implementation of CustomAnnotationScanner to be used by the detector
65      */

66     protected abstract CustomAnnotationScanner createAnnotationScanner();
67     
68     public boolean hasAnnotationInArchive(AbstractArchive archive) throws IOException JavaDoc {
69         File file = new File(archive.getArchiveUri());
70         if (!file.exists()) {
71             throw new FileNotFoundException JavaDoc(archive.getArchiveUri());
72         }
73
74         //@@@ note that the two methods could probably be combined into one.
75
//The challenge is to find the size of each entry without having to
76
//read the entry twice, once for the inputstream, the other for size.
77
if (file.isDirectory()) {
78             return hasAnnotationInDirectory(archive);
79         } else {
80             return hasAnnotationInJar(archive);
81         }
82     }
83
84     /**
85      *@return true if the jar file contains any of the appropriate type of annotations
86      */

87     private boolean hasAnnotationInJar(AbstractArchive archive) throws IOException JavaDoc {
88         JarFile JavaDoc jf = null;
89         try {
90             jf = new JarFile JavaDoc(new File(archive.getArchiveUri()));
91             Enumeration JavaDoc<JarEntry JavaDoc> entriesEnum = jf.entries();
92             while(entriesEnum.hasMoreElements()) {
93                 JarEntry JavaDoc je = entriesEnum.nextElement();
94                 if (je.getName().endsWith(".class")) {
95                     if (containsAnnotation(jf, je)) {
96                         return true;
97                     }
98                 }
99             }
100         } finally {
101             if (jf != null) {
102                 jf.close();
103             }
104         }
105         return false;
106     }
107
108     /**
109      *@return true if the directory contains any of the appropriate type of annotations
110      */

111     private boolean hasAnnotationInDirectory(AbstractArchive archive) throws IOException JavaDoc {
112         Enumeration JavaDoc entriesEnum = archive.entries();
113         while(entriesEnum.hasMoreElements()) {
114             String JavaDoc entry = (String JavaDoc) entriesEnum.nextElement();
115             if (entry.endsWith(".class")) {
116                 File file = null;
117                 int ind = entry.lastIndexOf("/");
118                 if (ind != -1) {
119                     String JavaDoc entryName = entry.substring(ind + 1);
120                     String JavaDoc parent = archive.getArchiveUri() + File.separatorChar +
121                                     entry.substring(0, ind);
122                     file = new File(parent.replace('/', File.separatorChar), entryName);
123                 } else {
124                     file = new File(archive.getArchiveUri(), entry);
125                 }
126                 if (containsAnnotation(file)) {
127                     return true;
128                 }
129             }
130         }
131         return false;
132     }
133     
134     public boolean containsAnnotation(JarFile JavaDoc jarFile, JarEntry JavaDoc entry) throws IOException JavaDoc {
135         boolean result = false;
136         // check if it contains top level annotations...
137
ReadableByteChannel JavaDoc channel = null;
138         try {
139             channel = Channels.newChannel(jarFile.getInputStream(entry));
140             if (channel!=null) {
141                 result = classFile.containsAnnotation(channel, entry.getSize());
142              }
143              return result;
144         } finally {
145             if (channel != null) {
146                 channel.close();
147             }
148         }
149     }
150     
151     public boolean containsAnnotation(File file) throws FileNotFoundException JavaDoc, IOException JavaDoc {
152         boolean result = false;
153         // check if it contains top level annotations...
154
InputStream JavaDoc is = null;
155         try {
156             is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
157             ReadableByteChannel JavaDoc channel = Channels.newChannel(is);
158             if (channel!=null) {
159                 result = classFile.containsAnnotation(channel, file.length());
160             }
161             return result;
162         } finally {
163             if (is != null) {
164                 is.close();
165             }
166         }
167     }
168 }
169
Popular Tags