001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.verifier;
023:
024: /*
025: * Class org.jboss.verifier.BeanVerifier
026: * Copyright (C) 2000 Juha Lindfors
027: *
028: * This library is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU Lesser General Public
030: * License as published by the Free Software Foundation; either
031: * version 2 of the License, or (at your option) any later version
032: *
033: * This library is distributed in the hope that it will be useful,
034: * but WITHOUT ANY WARRANTY; without even the implied warranty of
035: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036: * Lesser General Public License for more details.
037: *
038: * You should have received a copy of the GNU Lesser General Public
039: * License along with this library; if not, write to the Free Software
040: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
041: *
042: * This package and its source code is available at www.jboss.org
043: * $Id: BeanVerifier.java 57209 2006-09-26 12:21:57Z dimitris@jboss.org $
044: */
045:
046: // standard imports
047: import org.jboss.logging.Logger;
048: import org.jboss.metadata.ApplicationMetaData;
049: import org.jboss.metadata.BeanMetaData;
050: import org.jboss.metadata.EntityMetaData;
051: import org.jboss.metadata.MessageDrivenMetaData;
052: import org.jboss.metadata.SessionMetaData;
053: import org.jboss.verifier.event.VerificationEvent;
054: import org.jboss.verifier.event.VerificationEventGeneratorSupport;
055: import org.jboss.verifier.event.VerificationListener;
056: import org.jboss.verifier.strategy.EJBVerifier11;
057: import org.jboss.verifier.strategy.EJBVerifier20;
058: import org.jboss.verifier.strategy.EJBVerifier21;
059: import org.jboss.verifier.strategy.VerificationContext;
060: import org.jboss.verifier.strategy.VerificationStrategy;
061:
062: import java.net.URL;
063: import java.util.Iterator;
064:
065: /**
066: * Attempts to verify the spec compliance of the beans in a given
067: * EJB-JAR file. Works against EJB spec 1.1 and 2.0. Built for use in
068: * JBoss project.
069: *
070: * @see org.jboss.verifier.strategy.VerificationStrategy
071: * @see org.jboss.verifier.factory.VerificationEventFactory
072: *
073: * @author <a href="mailto:juha.lindfors@jboss.org">Juha Lindfors</a>
074: * @author Thomas.Diesler@jboss.org
075: * @version $Revision: 57209 $
076: * @since JDK 1.3
077: */
078: public class BeanVerifier implements VerificationContext {
079: private ApplicationMetaData ejbMetaData;
080: private ClassLoader ejbClassLoader;
081: private URL ejbURL;
082:
083: private VerificationStrategy verifier;
084:
085: private boolean success = true;
086:
087: private static Logger log = Logger.getLogger(BeanVerifier.class);
088:
089: /*
090: * Support class which handles the event notification logic.
091: */
092: private VerificationEventGeneratorSupport events = new VerificationEventGeneratorSupport();
093:
094: /**
095: * Default constructor.
096: */
097: public BeanVerifier() {
098: }
099:
100: /**
101: * Checks the Enterprise Java Beans found in this Jar for EJB spec
102: * compliance (EJB Spec. 1.1). Ensures that the given interfaces
103: * and implementation classes implement required methods and follow
104: * the contract given in the spec.
105: *
106: * @param url URL to the bean jar file
107: */
108: public void verify(URL url, ApplicationMetaData metaData) {
109: verify(url, metaData, null);
110: }
111:
112: /**
113: * Checks the Enterprise Java Beans found in this Jar for EJB spec
114: * compliance (EJB Spec. 1.1). Ensures that the given interfaces
115: * and implementation classes implement required methods and follow
116: * the contract given in the spec.
117: *
118: * @param url URL to the bean jar file
119: * @param cl The ClassLoader to use
120: */
121: public void verify(URL url, ApplicationMetaData metaData,
122: ClassLoader cl) {
123: ejbURL = url;
124: ejbMetaData = metaData;
125: ejbClassLoader = cl;
126:
127: if (metaData.isEJB1x()) {
128: setVerifier(VERSION_1_1);
129: } else if (metaData.isEJB21()) {
130: setVerifier(VERSION_2_1);
131: } else {
132: setVerifier(VERSION_2_0);
133: }
134:
135: Iterator beans = ejbMetaData.getEnterpriseBeans();
136:
137: while (beans.hasNext()) {
138: BeanMetaData bean = (BeanMetaData) beans.next();
139:
140: if (bean.isEntity()) {
141: EntityMetaData entityBean = (EntityMetaData) bean;
142: if (metaData.isEJB2x() && entityBean.isCMP1x()) {
143: // Hook for verifying CMP 1.x Beans in a 2.x JAR: store
144: // current state and restore this state after verification
145: // of the EJB completes.
146: boolean storedSuccess = success;
147:
148: verifier.checkEntity(entityBean);
149:
150: if (success != storedSuccess) {
151: log
152: .warn("The CMP 1.x EJB '"
153: + entityBean.getEjbName()
154: + "' generated some verification warnings. The Deployer "
155: + "will ignore these warnings but you should check "
156: + "your EJB to be on the safe side of things.");
157: }
158:
159: success = storedSuccess;
160: } else {
161: verifier.checkEntity(entityBean);
162: }
163: } else if (bean.isSession()) {
164: verifier.checkSession((SessionMetaData) bean);
165: } else {
166: verifier.checkMessageBean((MessageDrivenMetaData) bean);
167: }
168: }
169: }
170:
171: /**
172: * Check if the Verifier was successful
173: *
174: * @return <code>true</code> if all Beans have been verified,
175: * <code>false</code> otherwise.
176: */
177: public boolean getSuccess() {
178: return success;
179: }
180:
181: /*
182: *************************************************************************
183: *
184: * IMPLEMENTS VERIFICATION EVENT GENERATOR INTERFACE
185: *
186: *************************************************************************
187: */
188: public void addVerificationListener(VerificationListener listener) {
189: events.addVerificationListener(listener);
190: }
191:
192: public void removeVerificationListener(VerificationListener listener) {
193: events.removeVerificationListener(listener);
194: }
195:
196: public void fireBeanChecked(VerificationEvent event) {
197: events.fireBeanChecked(event);
198: }
199:
200: public void fireSpecViolation(VerificationEvent event) {
201: // A Spec Violation has been found. Mark as unsuccessful.
202: success = false;
203: events.fireSpecViolation(event);
204: }
205:
206: /*
207: **************************************************************************
208: *
209: * IMPLEMENTS VERIFICATION CONTEXT INTERFACE
210: *
211: **************************************************************************
212: */
213: public ApplicationMetaData getApplicationMetaData() {
214: return ejbMetaData;
215: }
216:
217: public URL getJarLocation() {
218: return ejbURL;
219: }
220:
221: public ClassLoader getClassLoader() {
222: return ejbClassLoader;
223: }
224:
225: public String getEJBVersion() {
226: return VERSION_1_1;
227:
228: // [TODO] fix this to return a correct version
229: }
230:
231: /*
232: * Will set the correct strategy implementation according to the supplied
233: * version information. Might widen the scope to public, but protected
234: * will do for now.
235: */
236: protected void setVerifier(String version) {
237: if (VERSION_1_1.equals(version)) {
238: verifier = new EJBVerifier11(this );
239: } else if (VERSION_2_0.equals(version)) {
240: verifier = new EJBVerifier20(this );
241: } else if (VERSION_2_1.equals(version)) {
242: verifier = new EJBVerifier21(this );
243: } else {
244: throw new IllegalArgumentException(UNRECOGNIZED_VERSION
245: + ": " + version);
246: }
247: }
248:
249: /*
250: * accessor for reference to the verification strategy in use
251: */
252: protected VerificationStrategy getVerifier() {
253: return verifier;
254: }
255:
256: /*
257: * String constants
258: */
259: private final static String UNRECOGNIZED_VERSION = "Unknown version string";
260: }
261:
262: /*
263: vim:ts=3:sw=3:et
264: */
|