1 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 ; 55 import javax.ejb.EJBLocalObject ; 56 57 58 59 64 65 public class CheckClasses implements ValidationRule { 66 67 private EjbSet set; 68 private ClassLoader 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 e) { 93 throw new RuntimeException (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 .class); 135 136 } else if (b instanceof org.openejb.alt.config.EntityBean ) { 137 138 compareTypes(b, b.getEjbClass(), javax.ejb.EntityBean .class); 139 140 } 141 142 } 143 144 145 146 private void check_isLocalInterface(Bean b) { 147 compareTypes(b, b.getLocal(), EJBLocalObject .class); 148 } 149 150 private void check_isLocalHomeInterface(Bean b) { 151 compareTypes(b, b.getLocalHome(), EJBLocalHome .class); 152 } 153 154 155 public void check_isHomeInterface( Bean b ) { 156 157 compareTypes(b, b.getHome(), javax.ejb.EJBHome .class); 158 159 } 160 161 162 163 public void check_isRemoteInterface( Bean b ) { 164 165 compareTypes(b, b.getRemote(), javax.ejb.EJBObject .class); 166 167 } 168 169 170 171 private void lookForClass(Bean b, String clazz, String type){ 172 try { 173 loadClass(clazz); 174 } catch ( OpenEJBException e ) { 175 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 } catch ( NoClassDefFoundError e){ 189 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 clazz1, Class class2 ){ 206 Class 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 } 221 } 222 223 private Class loadClass(String clazz) throws OpenEJBException { 224 ClassLoader cl = set.getClassLoader(); 225 try { 226 return cl.loadClass(clazz); 227 } catch (ClassNotFoundException cnfe) { 228 throw new OpenEJBException(SafeToolkit.messages.format("cl0007", clazz, set.getJarPath())); 229 } 230 } 231 232 } 233 234 235 236 237 238 | Popular Tags |