1 29 30 package com.caucho.ejb.cfg; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.bytecode.JMethod; 34 import com.caucho.config.ConfigException; 35 import com.caucho.ejb.gen.BeanAssembler; 36 import com.caucho.ejb.gen.ViewClass; 37 import com.caucho.java.gen.CallChain; 38 import com.caucho.log.Log; 39 import com.caucho.util.L10N; 40 41 import java.rmi.RemoteException ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.logging.Logger ; 45 46 49 public class EjbView { 50 private static final Logger log = Log.open(EjbView.class); 51 private static final L10N L = new L10N(EjbView.class); 52 53 private EjbBean _bean; 54 55 private JClass _apiClass; 56 57 private String _prefix; 58 59 private HashMap <String ,EjbMethod> _methodMap = 60 new HashMap <String ,EjbMethod>(); 61 62 65 public EjbView(EjbBean bean, JClass apiClass, String prefix) 66 throws ConfigException 67 { 68 _bean = bean; 69 _apiClass = apiClass; 70 _prefix = prefix; 71 } 72 73 76 protected EjbBean getBean() 77 { 78 return _bean; 79 } 80 81 84 protected JClass getApiClass() 85 { 86 return _apiClass; 87 } 88 89 92 protected JClass getImplClass() 93 { 94 return _bean.getEJBClassWrapper(); 95 } 96 97 100 protected String getPrefix() 101 { 102 return _prefix; 103 } 104 105 108 protected boolean isLocal() 109 { 110 return "Local".equals(_prefix); 111 } 112 113 116 public ArrayList <EjbMethod> getMethods() 117 { 118 ArrayList <EjbMethod> methods = new ArrayList <EjbMethod>(); 119 120 methods.addAll(_methodMap.values()); 121 122 return methods; 123 } 124 125 128 protected void introspect() 129 throws ConfigException 130 { 131 JMethod []implMethods = EjbBean.getMethods(getImplClass()); 133 134 for (int i = 0; i < implMethods.length; i++) { 135 JMethod method = implMethods[i]; 136 137 EjbMethod ejbMethod = null; 138 139 String name = method.getName(); 140 141 if (JClass.OBJECT.getMethod(name, method.getParameterTypes()) != null && 142 ! name.equals("toString")) { 143 } 144 else if (name.startsWith("ejb")) 145 ejbMethod = introspectEJBMethod(method); 146 else 147 ejbMethod = introspectBusinessMethod(method); 148 149 if (ejbMethod != null) { 150 _methodMap.put(getFullMethodName(ejbMethod.getApiMethod()), ejbMethod); 151 } 152 } 153 154 JMethod []apiMethods = EjbBean.getMethods(_apiClass); 156 157 for (int i = 0; i < apiMethods.length; i++) { 158 JMethod method = apiMethods[i]; 159 160 if (method.getDeclaringClass().getName().startsWith("javax.ejb")) 161 continue; 162 else if (JClass.OBJECT.getMethod(method.getName(), 163 method.getParameterTypes()) != null) { 164 continue; 165 } 166 167 EjbMethod ejbMethod = _methodMap.get(getFullMethodName(method)); 168 169 if (ejbMethod != null) 170 continue; 171 172 ejbMethod = introspectApiMethod(method); 173 174 if (ejbMethod != null) { 175 _methodMap.put(getFullMethodName(ejbMethod.getApiMethod()), ejbMethod); 176 177 validateApiMethod(ejbMethod.getApiMethod()); 178 } 179 } 180 } 181 182 185 protected EjbMethod introspectEJBMethod(JMethod method) 186 throws ConfigException 187 { 188 return null; 189 } 190 191 194 protected EjbMethod introspectBusinessMethod(JMethod implMethod) 195 throws ConfigException 196 { 197 JMethod apiMethod = EjbBean.getMethod(_apiClass, 198 implMethod.getName(), 199 implMethod.getParameterTypes()); 200 201 if (apiMethod == null) 202 return null; 203 204 return createBusinessMethod(apiMethod, implMethod); 205 } 206 207 210 protected EjbMethod createBusinessMethod(JMethod apiMethod, 211 JMethod implMethod) 212 throws ConfigException 213 { 214 validateImplMethod(implMethod); 215 216 return new EjbMethod(this, apiMethod, implMethod); 217 } 218 219 222 protected void validateImplMethod(JMethod implMethod) 223 throws ConfigException 224 { 225 if (! implMethod.isPublic()) { 226 throw error(L.l("{0}: `{1}' must be public. Business method implementations must be public.", 227 implMethod.getDeclaringClass().getName(), 228 getFullMethodName(implMethod))); 229 } 230 231 if (implMethod.isStatic()) { 232 throw error(L.l("{0}: `{1}' must not be static. Business method implementations must not be static.", 233 implMethod.getDeclaringClass().getName(), 234 getFullMethodName(implMethod))); 235 } 236 237 if (implMethod.isAbstract()) { 238 throw error(L.l("{0}: `{1}' must not be abstract. Business methods must be implemented.", 239 implMethod.getDeclaringClass().getName(), 240 getFullMethodName(implMethod))); 241 } 242 } 243 244 248 protected EjbMethod introspectApiMethod(JMethod apiMethod) 249 throws ConfigException 250 { 251 258 if (apiMethod.getName().startsWith("ejb")) { 259 throw error(L.l("{0}: '{1}' must not start with 'ejb'. The EJB spec reserves all methods starting with ejb.", 260 apiMethod.getDeclaringClass().getName(), 261 getFullMethodName(apiMethod))); 262 } 263 else 264 throw errorMissingMethod(getImplClass(), apiMethod.getName(), apiMethod); 265 } 266 267 270 protected void validateApiMethod(JMethod apiMethod) 271 throws ConfigException 272 { 273 if ("Remote".equals(_prefix)) 274 validateException(apiMethod, RemoteException .class); 275 } 276 277 protected ConfigException errorMissingMethod(JClass expectedClass, 278 String expectedName, 279 JMethod matchMethod) 280 { 281 return error(L.l("{0}: missing `{1}' method needed to match {2}.{3}", 282 expectedClass.getName(), 283 getFullMethodName(expectedName, 284 matchMethod.getParameterTypes()), 285 getShortClassName(matchMethod.getDeclaringClass()), 286 getFullMethodName(matchMethod))); 287 } 288 289 292 protected void assembleView(BeanAssembler assembler, 293 String fullClassName) 294 throws ConfigException 295 { 296 } 297 298 301 protected void assembleMethods(BeanAssembler assembler, 302 ViewClass viewClass, 303 String fullClassName) 304 throws ConfigException 305 { 306 ArrayList <EjbMethod> methods = getMethods(); 307 308 for (int i = 0; i < methods.size(); i++) { 309 EjbMethod method = methods.get(i); 310 311 method.assembleBean(assembler, fullClassName); 312 313 viewClass.addMethod(method.assemble(viewClass, fullClassName)); 314 } 315 } 316 317 320 protected EjbMethodPattern findMethodPattern(JMethod apiMethod, 321 String prefix) 322 { 323 return _bean.getMethodPattern(apiMethod, prefix); 324 } 325 326 329 protected CallChain getTransactionChain(CallChain callChain, 330 JMethod apiMethod, 331 String prefix) 332 { 333 return _bean.getTransactionChain(callChain, apiMethod, prefix); 334 } 335 336 339 protected CallChain getSecurityChain(CallChain callChain, 340 JMethod apiMethod, 341 String prefix) 342 { 343 return _bean.getSecurityChain(callChain, apiMethod, prefix); 344 } 345 346 349 protected void validateException(JMethod method, Class exn) 350 throws ConfigException 351 { 352 _bean.validateException(method, exn); 353 } 354 355 358 protected void validateException(JMethod method, JClass exn) 359 throws ConfigException 360 { 361 _bean.validateException(method, exn); 362 } 363 364 367 protected void validateExceptions(JMethod method, JClass []exn) 368 throws ConfigException 369 { 370 _bean.validateExceptions(method, exn); 371 } 372 373 376 static String getClassName(Class cl) 377 { 378 if (cl != null) 379 return cl.getName(); 380 else 381 return null; 382 } 383 384 static String getFullMethodName(JMethod method) 385 { 386 return EjbBean.getFullMethodName(method); 387 } 388 389 static String getFullMethodName(String methodName, JClass []paramTypes) 390 { 391 return EjbBean.getFullMethodName(methodName, paramTypes); 392 } 393 394 static String getShortClassName(JClass cl) 395 { 396 return EjbBean.getShortClassName(cl); 397 } 398 399 402 public ConfigException error(String msg) 403 { 404 return new ConfigException(msg); 405 } 406 } 407 | Popular Tags |