KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > config > rules > CheckClasses


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: CheckClasses.java 2082 2005-08-16 04:18:56Z dblevins $
44  */

45 package org.openejb.alt.config.rules;
46
47 import org.openejb.OpenEJBException;
48 import org.openejb.alt.config.Bean;
49 import org.openejb.alt.config.EjbSet;
50 import org.openejb.alt.config.ValidationFailure;
51 import org.openejb.alt.config.ValidationRule;
52 import org.openejb.util.SafeToolkit;
53
54 import javax.ejb.EJBLocalHome JavaDoc;
55 import javax.ejb.EJBLocalObject JavaDoc;
56
57
58
59 /**
60
61  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
62
63  */

64
65 public class CheckClasses implements ValidationRule {
66
67     private EjbSet set;
68     private ClassLoader JavaDoc classLoader;
69     public void validate( EjbSet set ) {
70         this.set = set;
71
72         Bean[] beans = set.getBeans();
73         Bean b = null;
74         try {
75             for ( int i=0; i < beans.length; i++ ) {
76                 b = beans[i];
77                 check_hasEjbClass( b );
78                 check_isEjbClass( b );
79                 if (b.getHome() != null){
80                     check_hasHomeClass( b );
81                     check_hasRemoteClass( b );
82                     check_isHomeInterface( b );
83                     check_isRemoteInterface( b );
84                 }
85                 if (b.getLocalHome() != null){
86                     check_hasLocalHomeClass( b );
87                     check_hasLocalClass( b );
88                     check_isLocalHomeInterface( b );
89                     check_isLocalInterface( b );
90                 }
91             }
92         } catch (RuntimeException JavaDoc e) {
93             throw new RuntimeException JavaDoc(b.getEjbName(),e);
94         }
95     }
96
97     private void check_hasLocalClass(Bean b) {
98         lookForClass(b, b.getLocal(), "<local>");
99     }
100
101     private void check_hasLocalHomeClass(Bean b) {
102         lookForClass(b, b.getLocalHome(), "<local-home>");
103     }
104
105
106     public void check_hasEjbClass( Bean b ) {
107
108         lookForClass(b, b.getEjbClass(), "<ejb-class>");
109
110     }
111
112
113
114     public void check_hasHomeClass( Bean b ) {
115
116         lookForClass(b, b.getHome(), "<home>");
117
118     }
119
120
121
122     public void check_hasRemoteClass( Bean b ) {
123
124         lookForClass(b, b.getRemote(), "<remote>");
125
126     }
127
128
129
130     public void check_isEjbClass( Bean b ) {
131
132         if ( b instanceof org.openejb.alt.config.SessionBean ) {
133
134             compareTypes(b, b.getEjbClass(), javax.ejb.SessionBean JavaDoc.class);
135
136         } else if (b instanceof org.openejb.alt.config.EntityBean ) {
137
138             compareTypes(b, b.getEjbClass(), javax.ejb.EntityBean JavaDoc.class);
139
140         }
141
142     }
143
144
145
146     private void check_isLocalInterface(Bean b) {
147         compareTypes(b, b.getLocal(), EJBLocalObject JavaDoc.class);
148     }
149
150     private void check_isLocalHomeInterface(Bean b) {
151         compareTypes(b, b.getLocalHome(), EJBLocalHome JavaDoc.class);
152     }
153
154
155     public void check_isHomeInterface( Bean b ) {
156
157         compareTypes(b, b.getHome(), javax.ejb.EJBHome JavaDoc.class);
158
159     }
160
161
162
163     public void check_isRemoteInterface( Bean b ) {
164
165         compareTypes(b, b.getRemote(), javax.ejb.EJBObject JavaDoc.class);
166
167     }
168
169
170
171     private void lookForClass(Bean b, String JavaDoc clazz, String JavaDoc type){
172     try {
173         loadClass(clazz);
174     } catch ( OpenEJBException e ) {
175             /*
176             # 0 - Class name
177             # 1 - Element (home, ejb-class, remote)
178             # 2 - Bean name
179             */

180
181             ValidationFailure failure = new ValidationFailure("missing.class");
182             failure.setDetails( clazz, type, b.getEjbName());
183             failure.setBean( b );
184
185             set.addFailure( failure );
186
187             //set.addFailure( new ValidationFailure("missing.class", clazz, type, b.getEjbName()) );
188
} catch ( NoClassDefFoundError JavaDoc e){
189             /*
190              # 0 - Class name
191              # 1 - Element (home, ejb-class, remote)
192              # 2 - Bean name
193              # 3 - Misslocated Class name
194              */

195             ValidationFailure failure = new ValidationFailure("misslocated.class");
196             failure.setDetails( clazz, type, b.getEjbName(), e.getMessage());
197             failure.setBean( b );
198
199             set.addFailure( failure );
200             throw e;
201         }
202         
203     }
204
205     private void compareTypes(Bean b, String JavaDoc clazz1, Class JavaDoc class2 ){
206         Class JavaDoc class1 = null;
207         try {
208             class1 = loadClass(clazz1);
209         } catch (OpenEJBException e) {
210             return;
211         }
212
213         if ( class1 != null && !class2.isAssignableFrom( class1 ) ) {
214             ValidationFailure failure = new ValidationFailure("wrong.class.type");
215             failure.setDetails( clazz1, class2.getName());
216             failure.setBean( b );
217
218             set.addFailure( failure );
219             //set.addFailure( new ValidationFailure("wrong.class.type", clazz1, class2.getName()) );
220
}
221     }
222
223     private Class JavaDoc loadClass(String JavaDoc clazz) throws OpenEJBException {
224         ClassLoader JavaDoc cl = set.getClassLoader();
225         try {
226             return cl.loadClass(clazz);
227         } catch (ClassNotFoundException JavaDoc cnfe) {
228             throw new OpenEJBException(SafeToolkit.messages.format("cl0007", clazz, set.getJarPath()));
229         }
230     }
231
232 }
233
234
235
236
237
238
Popular Tags