1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22 package org.jboss.verifier;
23
24 /*
25 * Class org.jboss.verifier.BeanVerifier
26 * Copyright (C) 2000 Juha Lindfors
27 *
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Lesser General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version
32 *
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Lesser General Public License for more details.
37 *
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library; if not, write to the Free Software
40 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 *
42 * This package and its source code is available at www.jboss.org
43 * $Id: BeanVerifier.java 37459 2005-10-30 00:04:02Z starksm $
44 */
45
46
47 // standard imports
48 import java.util.Iterator;
49 import java.net.URL;
50
51 // non-standard class dependencies
52 import org.jboss.verifier.strategy.EJBVerifier21;
53 import org.jboss.verifier.strategy.VerificationContext;
54 import org.jboss.verifier.strategy.VerificationStrategy;
55 import org.jboss.verifier.strategy.EJBVerifier11;
56 import org.jboss.verifier.strategy.EJBVerifier20;
57
58 import org.jboss.verifier.event.VerificationEvent;
59 import org.jboss.verifier.event.VerificationListener;
60 import org.jboss.verifier.event.VerificationEventGeneratorSupport;
61
62 import org.jboss.metadata.ApplicationMetaData;
63 import org.jboss.metadata.BeanMetaData;
64 import org.jboss.metadata.EntityMetaData;
65 import org.jboss.metadata.SessionMetaData;
66 import org.jboss.metadata.MessageDrivenMetaData;
67
68 import org.jboss.logging.Logger;
69
70 /**
71 * Attempts to verify the spec compliance of the beans in a given
72 * EJB-JAR file. Works against EJB spec 1.1 and 2.0. Built for use in
73 * JBoss project.
74 *
75 * @see org.jboss.verifier.strategy.VerificationStrategy
76 * @see org.jboss.verifier.factory.VerificationEventFactory
77 *
78 * @author <a href="mailto:juha.lindfors@jboss.org">Juha Lindfors</a>
79 * @version $Revision: 37459 $
80 * @since JDK 1.3
81 */
82 public class BeanVerifier
83 implements VerificationContext
84 {
85 private ApplicationMetaData ejbMetaData = null;
86 private URL ejbURL = null;
87 private ClassLoader ejbClassLoader = null;
88
89 private VerificationStrategy verifier = null;
90
91 private boolean success = true;
92
93 private static Logger log = Logger.getLogger( BeanVerifier.class );
94
95 /*
96 * Support class which handles the event notification logic.
97 */
98 private VerificationEventGeneratorSupport events =
99 new VerificationEventGeneratorSupport();
100
101 /**
102 * Default constructor.
103 */
104 public BeanVerifier()
105 {}
106
107 /**
108 * Checks the Enterprise Java Beans found in this Jar for EJB spec
109 * compliance (EJB Spec. 1.1). Ensures that the given interfaces
110 * and implementation classes implement required methods and follow
111 * the contract given in the spec.
112 *
113 * @param url URL to the bean jar file
114 */
115 public void verify(URL url, ApplicationMetaData metaData)
116 {
117 verify(url, metaData, null);
118 }
119
120 /**
121 * Checks the Enterprise Java Beans found in this Jar for EJB spec
122 * compliance (EJB Spec. 1.1). Ensures that the given interfaces
123 * and implementation classes implement required methods and follow
124 * the contract given in the spec.
125 *
126 * @param url URL to the bean jar file
127 * @param cl The ClassLoader to use
128 */
129 public void verify(URL url, ApplicationMetaData metaData, ClassLoader cl)
130 {
131 ejbURL = url;
132 ejbMetaData = metaData;
133 ejbClassLoader = cl;
134
135 if(metaData.isEJB1x())
136 {
137 setVerifier(VERSION_1_1);
138 }
139 else if(metaData.isEJB21())
140 {
141 setVerifier(VERSION_2_1);
142 }
143 else
144 {
145 setVerifier(VERSION_2_0);
146 }
147
148 Iterator beans = ejbMetaData.getEnterpriseBeans();
149
150 while (beans.hasNext())
151 {
152 BeanMetaData bean = (BeanMetaData)beans.next();
153
154 if( bean.isEntity() )
155 {
156 EntityMetaData entityBean = (EntityMetaData)bean;
157 if( metaData.isEJB2x() && entityBean.isCMP1x() )
158 {
159 // Hook for verifying CMP 1.x Beans in a 2.x JAR: store
160 // current state and restore this state after verification
161 // of the EJB completes.
162 boolean storedSuccess = success;
163
164 verifier.checkEntity( entityBean );
165
166 if( success != storedSuccess )
167 {
168 log.warn( "The CMP 1.x EJB '" + entityBean.getEjbName() +
169 "' generated some verification warnings. The Deployer " +
170 "will ignore these warnings but you should check " +
171 "your EJB to be on the safe side of things." );
172 }
173
174 success = storedSuccess;
175 }
176 else
177 {
178 verifier.checkEntity( entityBean );
179 }
180 }
181 else if( bean.isSession() )
182 {
183 verifier.checkSession( (SessionMetaData)bean );
184 }
185 else
186 {
187 verifier.checkMessageBean( (MessageDrivenMetaData)bean );
188 }
189 }
190 }
191
192 /**
193 * Check if the Verifier was successful
194 *
195 * @return <code>true</code> if all Beans have been verified,
196 * <code>false</code> otherwise.
197 */
198 public boolean getSuccess()
199 {
200 return success;
201 }
202
203 /*
204 *************************************************************************
205 *
206 * IMPLEMENTS VERIFICATION EVENT GENERATOR INTERFACE
207 *
208 *************************************************************************
209 */
210 public void addVerificationListener(VerificationListener listener)
211 {
212 events.addVerificationListener(listener);
213 }
214
215 public void removeVerificationListener(VerificationListener listener)
216 {
217 events.removeVerificationListener(listener);
218 }
219
220 public void fireBeanChecked(VerificationEvent event)
221 {
222 events.fireBeanChecked(event);
223 }
224
225 public void fireSpecViolation( VerificationEvent event )
226 {
227 // A Spec Violation has been found. Mark as unsuccessful.
228 success = false;
229 events.fireSpecViolation(event);
230 }
231
232 /*
233 **************************************************************************
234 *
235 * IMPLEMENTS VERIFICATION CONTEXT INTERFACE
236 *
237 **************************************************************************
238 */
239 public ApplicationMetaData getApplicationMetaData()
240 {
241 return ejbMetaData;
242 }
243
244 public URL getJarLocation()
245 {
246 return ejbURL;
247 }
248
249 public ClassLoader getClassLoader()
250 {
251 return ejbClassLoader;
252 }
253
254 public String getEJBVersion()
255 {
256 return VERSION_1_1;
257
258 // [TODO] fix this to return a correct version
259 }
260
261 /*
262 * Will set the correct strategy implementation according to the supplied
263 * version information. Might widen the scope to public, but protected
264 * will do for now.
265 */
266 protected void setVerifier( String version )
267 {
268 if( VERSION_1_1.equals(version) )
269 {
270 verifier = new EJBVerifier11(this);
271 }
272 else if( VERSION_2_0.equals(version) )
273 {
274 verifier = new EJBVerifier20(this);
275 }
276 else if (VERSION_2_1.equals(version))
277 {
278 verifier=new EJBVerifier21(this);
279 }
280 else
281 {
282 throw new IllegalArgumentException( UNRECOGNIZED_VERSION +
283 ": " + version);
284 }
285 }
286
287 /*
288 * accessor for reference to the verification strategy in use
289 */
290 protected VerificationStrategy getVerifier()
291 {
292 return verifier;
293 }
294
295 /*
296 * String constants
297 */
298 private final static String UNRECOGNIZED_VERSION =
299 "Unknown version string";
300 }
301 /*
302 vim:ts=3:sw=3:et
303 */