ejbbean.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,224 行 · 第 1/4 页
JAVA
2,224 行
Class []oldParam = oldMethod.getParameterTypes(); Class []param = method.getParameterTypes(); if (oldParam.length != param.length) return false; for (int j = 0; j < param.length; j++) { if (! param[j].equals(oldParam[j])) return false; } return true; } /** * Returns the matching transaction attribute. */ public TransactionAttributeType getTransactionAttribute(ApiMethod method, String intf) { if (! isContainerTransaction()) return null; TransactionAttributeType transaction = TransactionAttributeType.REQUIRED; EjbMethodPattern ejbMethod = getMethodPattern(null, null); if (ejbMethod != null) transaction = ejbMethod.getTransactionType(); ejbMethod = getMethodPattern(method, null); if (ejbMethod != null) transaction = ejbMethod.getTransactionType(); ejbMethod = getMethodPattern(method, intf); if (ejbMethod != null) transaction = ejbMethod.getTransactionType(); return transaction; } /** * Finds the method in the class. * * @param cl owning class * @param method source method * * @return the matching method or null if non matches. */ public ApiMethod getMethod(String methodName, Class []paramTypes) { return getMethod(getEJBClassWrapper(), methodName, paramTypes); } /** * Finds the method in the class. * * @param cl owning class * @param method source method * * @return the matching method or null if non matches. */ public static ApiMethod getMethod(ApiClass cl, ApiMethod sourceMethod) { return getMethod(cl, sourceMethod.getName(), sourceMethod.getParameterTypes()); } /** * Finds the method in the class. * * @param apiList owning class * @param name method name to match * @param params method parameters to match * * @return the matching method or null if non matches. */ public static ApiMethod getMethod(ArrayList<ApiClass> apiList, String name, Class []param) { for (int i = 0; i < apiList.size(); i++) { ApiMethod method = getMethod(apiList.get(i), name, param); if (method != null) return method; } return null; } /** * Finds the method in the class. * * @param cl owning class * @param name method name to match * @param params method parameters to match * * @return the matching method or null if non matches. */ public static ApiMethod getMethod(ApiClass cl, String name, Class []param) { return cl.getMethod(name, param); } public boolean isCMP() { return false; } public boolean isCMP1() { return false; } public boolean isEJB3() { return ! (isCMP() || isCMP1()); } public static boolean isMatch(ApiMethod methodA, ApiMethod methodB) { if (methodA == methodB) return true; else if (methodA == null || methodB == null) return false; else return isMatch(methodA, methodB.getName(), methodB.getParameterTypes()); } public static boolean isMatch(ApiMethod method, String name, Class []param) { if (! method.getName().equals(name)) return false; Class []mparam = method.getParameterTypes(); if (mparam.length != param.length) return false; for (int j = 0; j < param.length; j++) { if (! mparam[j].equals(param[j])) return false; } return true; } /** * Finds the method in the class. * * @param cl owning class * @param name method name to match * @param params method parameters to match * * @return the matching method or null if non matches. */ public static ApiMethod findMethod(MethodSignature sig, ApiClass cl, String intf) { if (cl == null) return null; for (ApiMethod method : cl.getMethods()) { if (sig.isMatch(method, intf)) return method; } return null; } /** * Returns all the method in the class. */ public static ArrayList<ApiMethod> getMethods(ArrayList<ApiClass> apiList) { ArrayList<ApiMethod> methodList = new ArrayList<ApiMethod>(); for (ApiClass api : apiList) { for (ApiMethod method : api.getMethods()) { if (! methodList.contains(method)) methodList.add(method); } } return methodList; } /** * Finds the method in the class. * * @param cl owning class * @param method source method * * @return the matching method or null if non matches. */ public static ApiMethod findMethod(ArrayList<ApiMethod> methods, ApiMethod method) { loop: for (int i = 0; i < methods.size(); i++) { ApiMethod oldMethod = methods.get(i); if (oldMethod.equals(method)) return oldMethod; } return null; } /** * Returns a printable version of a class. */ public static String getClassName(Class cl) { if (cl == null) return "null"; else if (cl.isArray()) return getClassName(cl.getComponentType()) + "[]"; else if (cl.getName().startsWith("java")) { int p = cl.getName().lastIndexOf('.'); return cl.getName().substring(p + 1); } else return cl.getName(); } /** * Returns a printable version of a class. */ public static String getShortClassName(Class cl) { if (cl.isArray()) return getShortClassName(cl.getComponentType()) + "[]"; else return cl.getSimpleName(); } /** * Tests is a method is declared in a class. */ public boolean classHasMethod(ApiMethod method, ApiClass cl) { try { ApiMethod match = cl.getMethod(method.getName(), method.getParameterTypes()); return match != null; } catch (Exception e) { return false; } } public void validateException(ApiMethod method, Class e) throws ConfigException { validateExceptions(method, new Class[] { e }); } /** * Check that the method throws the expected exceptions. * * @param method the method to test * @param exn the expected exceptions */ public void validateExceptions(ApiMethod method, Class []exn) throws ConfigException { Class []methodExceptions = method.getExceptionTypes(); loop: for (int i = 0; i < exn.length; i++) { if (RuntimeException.class.isAssignableFrom(exn[i])) continue; for (int j = 0; j < methodExceptions.length; j++) { if (methodExceptions[j].isAssignableFrom(exn[i])) continue loop; } throw new ConfigException(L.l("{2}: '{0}' must throw {1}.", getFullMethodName(method), exn[i].getName(), method.getDeclaringClass().getName())); } } public void validateExceptions(ApiMethod caller, ApiMethod callee) throws ConfigException { Class []exn = callee.getExceptionTypes(); Class missing = findMissingException(caller, exn); if (missing != null) { throw error(L.l("{0}: '{1}' must throw {2}.", caller.getDeclaringClass().getName(), getFullMethodName(caller), getShortClassName(missing), caller.getDeclaringClass().getName()) + L.l(" {0} must throw all {1}.{2} exceptions.", caller.getName(), getShortClassName(callee.getDeclaringClass()), callee.getName())); } } /** * Finds any exception in the exception array that the method isn't * throwing. * * @param method the method which should throw a superset of exceptions. * @param exn an array of exceptions the method should throw. * * @return the first missing exception */ Class findMissingException(ApiMethod method, Class []exn) throws ConfigException { Class []methodExceptions = method.getExceptionTypes(); for (int i = 0; i < exn.length; i++) { if (! hasException(method, exn[i]) && ! RuntimeException.class.isAssignableFrom(exn[i])) return exn[i]; } return null; } public boolean hasException(ApiMethod method, Class exn) throws ConfigException { Class []methodExceptions = method.getExceptionTypes(); for (int j = 0; j < methodExceptions.length; j++) { if (methodExceptions[j].isAssignableFrom(exn)) return true; } return false; } protected ApiMethod findFirstCreateMethod(ApiClass cl) throws ConfigException { for (ApiMethod method : cl.getMethods()) { if (method.getName().startsWith("create")) return method; } return null; } protected void introspectBean(ApiClass type, String defaultName) throws ConfigException { try { setEJBClassWrapper(type); String name = getEJBName(); if (name == null || name.equals("")) name = defaultName; if (name == null || name.equals("")) { String className = type.getName(); int p = className.lastIndexOf('.'); if (p > 0) name = className.substring(p + 1); else name = className; } setEJBName(name); Local local = type.getAnnotation(Local.class); if (local != null) { Object []values = local.value(); for (int i = 0; i < values.length; i++) { if (values[i] instanceof Class) { Class localClass = (Class) values[i]; setLocalWrapper(new ApiClass(localClass)); } else if (values[i] instanceof Class) { setLocal((Class) values[i]); } } } Remote remote = type.getAnnotation(Remote.class); if (remote != null) { Object []values = remote.value(); for (int i = 0; i < values.length; i++) { if (values[i] instanceof Class) { Class remoteClass = (Class) values[i]; setRemoteWrapper(new ApiClass(remoteClass)); } else if (values[i] instanceof Class) { setRemote((Class) values[i]); } } // ejb/0f08: single interface if (values.length == 0) { // XXX: getGenericInterfaces Class []ifs = type.getJavaClass().getInterfaces(); if (ifs.length == 1) setRemoteWrapper(new ApiClass(ifs[0])); } } TransactionAttribute xa = type.getAnnotation(TransactionAttribute.class); if (xa != null) { MethodSignature sig = new MethodSignature(); sig.setMethodName("*"); EjbMethodPattern pattern = createMethod(sig); setPatternTransaction(pattern, xa); } configureMethods(type); /* for (int i = 0; i < _initList.size(); i++) addInitProgram(_initList.get(i).getBuilderProgram()); */ } catch (ConfigException e) { throw e; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw ConfigException.createLine(_location, e); } } private void configureMethods(ApiClass type) throws ConfigException { for (ApiMethod method : type.getMethods()) { TransactionAttribute xa = (TransactionAttribute) method.getAnnotation(TransactionAttribute.class); if (xa != null) { EjbMethodPattern pattern = createMethod(getSignature(method)); setPatternTransaction(pattern, xa); } Annotation aroundInvoke = method.getAnnotation(AroundInvoke.class); // ejb/0fb8 if (aroundInvoke != null) { _aroundInvokeMethodName = method.getName(); } Annotation timeout = method.getAnnotation(Timeout.class); // ejb/0fj0 if (timeout != null) { _timeoutMethodName = method.getName(); } } } private void setPatternTransaction(EjbMethodPattern pattern, TransactionAttribute xa) throws ConfigException { TransactionAttributeType xaType = xa.value(); pattern.setTransaction(xaType); } private MethodSignature getSignature(ApiMethod method) throws ConfigException { MethodSignature sig = new MethodSignature(); sig.setMethodName(method.getName()); Class []paramTypes = method.getParameterTypes(); for (int i = 0; i < paramTypes.length; i++) { sig.addParam(paramTypes[i].getName()); } return sig; } /** * Returns a full method name with arguments. */ public static String getFullMethodName(ApiMethod method) { return getFullMethodName(method.getName(), method.getParameterTypes()); } /** * Returns a full method name with arguments. */ public static String getFullMethodName(String methodName, Class []params) { String name = methodName + "("; for (int i = 0; i < params.length; i++) { if (i != 0) name += ", "; name += params[i].getSimpleName(); } return name + ")"; } /** * Returns an error. */ public ConfigException error(String msg) { if (_isInit && _filename != null) return new LineConfigException(_filename, _line, msg); else if (_isInit && ! "".equals(_location)) return new LineConfigException(_location + msg); else return new ConfigException(msg); } /** * Returns an error. */ public RuntimeException error(Exception e) { if (_filename != null) return LineConfigException.create(_filename, _line, e); else if (_location != null) return ConfigException.createLine(_location, e); else return ConfigException.create(e); } public String toString() { return getClass().getSimpleName() + "[" + _ejbName + "]"; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?