KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > annotations > analyzer > AnnotationDeploymentAnalyzer


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AnnotationDeploymentAnalyzer.java 925 2006-07-25 12:37:03Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.annotations.analyzer;
27
28 import java.net.URL JavaDoc;
29 import java.net.URLConnection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.objectweb.asm.ClassReader;
33 import org.objectweb.easybeans.api.EZBArchive;
34 import org.objectweb.easybeans.api.EZBArchiveException;
35 import org.objectweb.easybeans.deployment.annotations.exceptions.AnalyzerException;
36 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
37 import org.objectweb.easybeans.log.JLog;
38 import org.objectweb.easybeans.log.JLogFactory;
39
40 /**
41  * This class finds the annotated class and fill metadata class.
42  * @author Florent Benoit
43  */

44 public class AnnotationDeploymentAnalyzer {
45
46     /**
47      * Archive which will be analyzed.
48      */

49     private EZBArchive archive = null;
50
51     /**
52      * Metadata representing the parsed ejb-jar file.
53      */

54     private EjbJarAnnotationMetadata ejbJarAnnotationMetadata = null;
55
56     /**
57      * Logger.
58      */

59     private static JLog logger = JLogFactory.getLog(AnnotationDeploymentAnalyzer.class);
60
61     /**
62      * ASM visitor used to visit classes.
63      */

64     private ScanClassVisitor scanVisitor = null;
65
66     /**
67      * Constructor.<br>
68      * Archive which will be used when analyzing.
69      * @param archive the archive to analyze.
70      */

71     public AnnotationDeploymentAnalyzer(final EZBArchive archive) {
72         this.archive = archive;
73         this.ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata();
74         scanVisitor = new ScanClassVisitor(ejbJarAnnotationMetadata);
75     }
76
77     /**
78      * Analyzes the archive and fill metadata struct.
79      * @throws AnalyzerException if analyze of archive fails
80      */

81     public void analyze() throws AnalyzerException {
82
83         Iterator JavaDoc<URL JavaDoc> iterator;
84         try {
85             iterator = archive.getResources();
86             // Scan all resources
87
while (iterator.hasNext()) {
88                 URL JavaDoc url = iterator.next();
89                 // only .class
90
if (url.getPath().toLowerCase().endsWith(".class")) {
91                     try {
92                         URLConnection JavaDoc urlConnection = url.openConnection();
93                         urlConnection.setDefaultUseCaches(false);
94                         new ClassReader(urlConnection.getInputStream()).accept(scanVisitor, false);
95                     } catch (Exception JavaDoc ioe) {
96                         throw new AnalyzerException("Error while analyzing file entry '" + url + "' in archive '"
97                                 + archive.getName() + "'", ioe);
98                     }
99                 }
100             }
101
102         } catch (EZBArchiveException e) {
103             throw new AnalyzerException("Error while analyzing archive '" + archive.getName() + "'", e);
104         } finally {
105             archive.close();
106         }
107
108     }
109
110
111     /**
112      * @return struct (metadata) filled by this analyzer
113      */

114     public EjbJarAnnotationMetadata getEjbJarAnnotationMetadata() {
115         return ejbJarAnnotationMetadata;
116     }
117
118 }
119
Popular Tags