001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: AnnotationDeploymentAnalyzer.java 2059 2007-11-22 17:22:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.analyzer;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.net.URL;
029: import java.net.URLConnection;
030: import java.util.Iterator;
031:
032: import org.ow2.easybeans.asm.ClassReader;
033: import org.ow2.easybeans.deployment.annotations.exceptions.AnalyzerException;
034: import org.ow2.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
035: import org.ow2.util.ee.deploy.api.archive.ArchiveException;
036: import org.ow2.util.ee.deploy.api.archive.IArchive;
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039:
040: /**
041: * This class finds the annotated class and fill metadata class.
042: * @author Florent Benoit
043: */
044: public class AnnotationDeploymentAnalyzer {
045:
046: /**
047: * Archive which will be analyzed.
048: */
049: private IArchive archive = null;
050:
051: /**
052: * Metadata representing the parsed ejb-jar file.
053: */
054: private EjbJarAnnotationMetadata ejbJarAnnotationMetadata = null;
055:
056: /**
057: * Logger.
058: */
059: private static Log logger = LogFactory
060: .getLog(AnnotationDeploymentAnalyzer.class);
061:
062: /**
063: * ASM visitor used to visit classes.
064: */
065: private ScanClassVisitor scanVisitor = null;
066:
067: /**
068: * Constructor.<br>
069: * Archive which will be used when analyzing.
070: * @param archive the archive to analyze.
071: */
072: public AnnotationDeploymentAnalyzer(final IArchive archive) {
073: this .archive = archive;
074: this .ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata();
075: scanVisitor = new ScanClassVisitor(ejbJarAnnotationMetadata);
076: }
077:
078: /**
079: * Analyzes the archive and fill metadata struct.
080: * @throws AnalyzerException if analyze of archive fails
081: */
082: public void analyze() throws AnalyzerException {
083:
084: Iterator<URL> iterator;
085: try {
086: iterator = archive.getResources();
087: // Scan all resources
088: while (iterator.hasNext()) {
089: URL url = iterator.next();
090: // only .class
091: if (url.getPath().toLowerCase().endsWith(".class")) {
092: InputStream is = null;
093: try {
094: URLConnection urlConnection = url
095: .openConnection();
096: urlConnection.setDefaultUseCaches(false);
097: is = urlConnection.getInputStream();
098: new ClassReader(is).accept(scanVisitor, 0);
099: } catch (Exception ioe) {
100: throw new AnalyzerException(
101: "Error while analyzing file entry '"
102: + url + "' in archive '"
103: + archive.getName() + "'", ioe);
104: } finally {
105: if (is != null) {
106: try {
107: is.close();
108: } catch (IOException e) {
109: throw new AnalyzerException(
110: "Error while closing input stream of the entry '"
111: + url
112: + "' in archive '"
113: + archive.getName()
114: + "'", e);
115: }
116: }
117: }
118: }
119: }
120:
121: } catch (ArchiveException e) {
122: throw new AnalyzerException(
123: "Error while analyzing archive '"
124: + archive.getName() + "'", e);
125: } finally {
126: archive.close();
127: }
128:
129: }
130:
131: /**
132: * @return struct (metadata) filled by this analyzer
133: */
134: public EjbJarAnnotationMetadata getEjbJarAnnotationMetadata() {
135: return ejbJarAnnotationMetadata;
136: }
137:
138: }
|