KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.deployment.util;
24
25 import com.sun.enterprise.deployment.annotation.introspection.ClassFile;
26 import com.sun.enterprise.deployment.annotation.introspection.ConstantPoolInfo;
27 import com.sun.enterprise.deployment.annotation.introspection.CustomAnnotationScanner;
28 import com.sun.enterprise.deployment.annotation.introspection.EjbComponentAnnotationScanner;
29 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
30 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
31
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.nio.channels.Channels JavaDoc;
39 import java.nio.channels.ReadableByteChannel JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.jar.JarEntry JavaDoc;
42 import java.util.jar.JarFile JavaDoc;
43
44 /**
45  * This is a utility class for detecting ejb component annotations. This class
46  * can be refactored to support other type of annotation dectection in the
47  * future.
48  *
49  * @author Qingqing Ouyang
50  */

51 public class EjbComponentAnnotationDetector {
52
53     private ClassFile classFile = new ClassFile();
54
55     public EjbComponentAnnotationDetector() {
56         CustomAnnotationScanner scanner = new EjbComponentAnnotationScanner();
57         ConstantPoolInfo poolInfo = new ConstantPoolInfo(scanner);
58         classFile.setConstantPoolInfo(poolInfo);
59     }
60
61     public boolean hasAnnotationInArchive(AbstractArchive archive) throws IOException JavaDoc {
62         File file = new File(archive.getArchiveUri());
63         if (!file.exists()) {
64             throw new FileNotFoundException JavaDoc(archive.getArchiveUri());
65         }
66
67         //@@@ note that the two methods could probably be combined into one.
68
//The challenge is to find the size of each entry without having to
69
//read the entry twice, once for the inputstream, the other for size.
70
if (file.isDirectory()) {
71             return hasAnnotationInDirectory(archive);
72         } else {
73             return hasAnnotationInJar(archive);
74         }
75     }
76
77     /**
78      *@return true if the jar file contains any ejb component annotations
79      */

80     private boolean hasAnnotationInJar(AbstractArchive archive) throws IOException JavaDoc {
81         JarFile JavaDoc jf = null;
82         try {
83             jf = new JarFile JavaDoc(new File(archive.getArchiveUri()));
84             Enumeration JavaDoc<JarEntry JavaDoc> entriesEnum = jf.entries();
85             while(entriesEnum.hasMoreElements()) {
86                 JarEntry JavaDoc je = entriesEnum.nextElement();
87                 if (je.getName().endsWith(".class")) {
88                     // check if it contains top level annotations...
89
ReadableByteChannel JavaDoc channel = Channels.newChannel(jf.getInputStream(je));
90                     if (channel!=null) {
91                         if (classFile.containsAnnotation(channel, je.getSize())) {
92                             return true;
93                         }
94                      }
95                 }
96             }
97         } finally {
98             if (jf != null) {
99                 jf.close();
100             }
101         }
102         return false;
103     }
104
105     /**
106      *@return true if the directory contains any ejb component annotations
107      */

108     private boolean hasAnnotationInDirectory(AbstractArchive archive) throws IOException JavaDoc {
109         Enumeration JavaDoc entriesEnum = archive.entries();
110         while(entriesEnum.hasMoreElements()) {
111             String JavaDoc entry = (String JavaDoc) entriesEnum.nextElement();
112             if (entry.endsWith(".class")) {
113                 File file = null;
114                 int ind = entry.lastIndexOf("/");
115                 if (ind != -1) {
116                     String JavaDoc entryName = entry.substring(ind + 1);
117                     String JavaDoc parent = archive.getArchiveUri() + File.separatorChar +
118                                     entry.substring(0, ind);
119                     file = new File(parent.replace('/', File.separatorChar), entryName);
120                 } else {
121                     file = new File(archive.getArchiveUri(), entry);
122                 }
123                 // check if it contains top level annotations...
124
InputStream JavaDoc is = null;
125                 try {
126                     is =
127                         new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
128                     ReadableByteChannel JavaDoc channel = Channels.newChannel(is);
129                     if (channel!=null) {
130                         if (classFile.containsAnnotation(channel, file.length())) {
131                             return true;
132                         }
133                      }
134                 } finally {
135                     if (is != null) {
136                         is.close();
137                     }
138                 }
139             }
140         }
141         return false;
142     }
143 }
144
Popular Tags