001: /**
002: * EasyBeans
003: * Copyright (C) 2007 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:WarAnnotationDeploymentAnalyzer.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.util.List;
029:
030: import org.ow2.easybeans.asm.ClassReader;
031: import org.ow2.easybeans.deployment.annotations.exceptions.AnalyzerException;
032: import org.ow2.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
033: import org.ow2.util.ee.deploy.api.archive.IArchive;
034: import org.ow2.util.log.Log;
035: import org.ow2.util.log.LogFactory;
036:
037: /**
038: * This class fill metadata for Servlet classes.
039: * @author Florent Benoit
040: */
041: public class WarAnnotationDeploymentAnalyzer {
042:
043: /**
044: * Logger.
045: */
046: private static Log logger = LogFactory
047: .getLog(WarAnnotationDeploymentAnalyzer.class);
048:
049: /**
050: * Archive which will be analyzed.
051: */
052: private IArchive archive = null;
053:
054: /**
055: * ClassLoader used to read classes.
056: */
057: private ClassLoader classLoader = null;
058:
059: /**
060: * Metadata representing the parsed war file.
061: */
062: private EjbJarAnnotationMetadata ejbJarAnnotationMetadata = null;
063:
064: /**
065: * ASM visitor used to visit classes.
066: */
067: private ScanClassVisitor scanVisitor = null;
068:
069: /**
070: * Constructor.<br>
071: * Archive which will be used when analyzing.
072: * @param archive the archive to analyze.
073: * @param classLoader the classloader used to read the classes.
074: */
075: public WarAnnotationDeploymentAnalyzer(final IArchive archive,
076: final ClassLoader classLoader) {
077: this .archive = archive;
078: this .ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata();
079: this .classLoader = classLoader;
080: this .scanVisitor = new ScanClassVisitor(
081: ejbJarAnnotationMetadata);
082: }
083:
084: /**
085: * Analyzes the given set of classes and fill metadata struct.
086: * @param classNames the class names to analyze
087: * @throws AnalyzerException if analyze of archive fails
088: */
089: public void analyze(final List<String> classNames)
090: throws AnalyzerException {
091:
092: //TODO: Analyze super classes
093:
094: // Analyze the set of given classes
095: for (String className : classNames) {
096: logger.debug(
097: "Analyzing ''{0}'' class of the archive ''{1}''",
098: className, archive);
099: InputStream is = classLoader.getResourceAsStream(className
100: .replace('.', '/')
101: + ".class");
102: if (is == null) {
103: logger.error("Unable to analyze the classname '"
104: + className + "' of archive '" + archive
105: + "' as it was not found in the classloader '"
106: + classLoader + "'");
107: continue;
108: }
109:
110: try {
111: new ClassReader(is).accept(scanVisitor, 0);
112: } catch (Exception ioe) {
113: throw new AnalyzerException(
114: "Error while analyzing the classname '"
115: + className + "' of archive '"
116: + archive
117: + "' found in the classloader '"
118: + classLoader + "'", ioe);
119: } finally {
120: if (is != null) {
121: try {
122: is.close();
123: } catch (IOException e) {
124: throw new AnalyzerException(
125: "Error while closing input stream of the entry '"
126: + className + "' in archive '"
127: + archive.getName() + "'", e);
128: }
129: }
130: }
131:
132: }
133:
134: }
135:
136: /**
137: * @return struct (metadata) filled by this analyzer
138: */
139: public EjbJarAnnotationMetadata getEjbJarAnnotationMetadata() {
140: return ejbJarAnnotationMetadata;
141: }
142: }
|