injectintrospector.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 866 行 · 第 1/2 页
JAVA
866 行
else if (method.isAnnotationPresent(WebServiceRef.class)) { WebServiceRef webService = method.getAnnotation(WebServiceRef.class); gen = generateWebService(location, type, jndiName, webService); } */ else if (hasBindingAnnotation(method)) introspectWebBean(injectList, method); if (gen != null) injectList.add(new MethodGeneratorProgram(method, gen)); } /* private static ValueGenerator generateWebService(String location, Class type, String jndiName, WebServiceRef ref) throws ConfigException { String mappedName = ref.mappedName(); if (! "".equals(ref.name())) jndiName = ref.name(); return null; } */ private static ValueGenerator generatePersistenceContext(String location, Class type, String jndiName, PersistenceContext pContext) throws ConfigException { AmberContainer.create().start(); PersistenceContextType pType = pContext.type(); if (PersistenceContextType.EXTENDED.equals(pType)) return generateExtendedPersistenceContext(location, type, jndiName, pContext); if (! type.isAssignableFrom(EntityManager.class)) { throw new ConfigException(location + L.l("@PersistenceContext field type '{0}' must be assignable from EntityManager", type.getName())); } String unitName = pContext.unitName(); if (! "".equals(pContext.name())) jndiName = pContext.name(); WebBeansContainer webBeans = WebBeansContainer.create(); ComponentImpl component; if ("".equals(unitName)) { component = webBeans.bind(location, EntityManager.class); if (component == null) throw new ConfigException(location + L.l("@PersistenceContext cannot find any persistence contexts. No JPA persistence-units have been deployed")); } else { component = webBeans.bind(location, EntityManager.class, unitName); if (component == null) throw new ConfigException(location + L.l("'{0}' is an unknown @PersistenceContext.", unitName)); } bindJndi(location, jndiName, component); return new ComponentValueGenerator(location, component); } private static ValueGenerator generateExtendedPersistenceContext(String location, Class type, String jndiName, PersistenceContext pContext) throws ConfigException { AmberContainer.create().start(); if (! type.isAssignableFrom(EntityManager.class)) { throw new ConfigException(location + L.l("@PersistenceContext field type '{0}' must be assignable from EntityManager", type.getName())); } PersistenceContextGenerator gen; gen = new PersistenceContextGenerator(location, pContext); bindJndi(location, jndiName, gen); return gen; } private static ValueGenerator generatePersistenceUnit(String location, Class type, String jndiName, PersistenceUnit pUnit) throws ConfigException { if (! type.isAssignableFrom(EntityManagerFactory.class)) { throw new ConfigException(location + L.l("@PersistenceUnit field type '{0}' must be assignable from EntityManagerFactory", type.getName())); } String unitName = pUnit.unitName(); if (! "".equals(pUnit.name())) jndiName = pUnit.name(); WebBeansContainer webBeans = WebBeansContainer.create(); ComponentImpl component; if ("".equals(unitName)) { component = webBeans.bind(location, EntityManagerFactory.class); if (component == null) throw new ConfigException(location + L.l("@PersistenceUnit cannot find any persistence units. No JPA persistence-units have been deployed")); } else { component = webBeans.bind(location, EntityManagerFactory.class, unitName); if (component == null) throw new ConfigException(location + L.l("@PersistenceUnit(unitName='{0}') is an unknown persistence unit. No matching JPA persistence-units have been deployed", unitName)); } bindJndi(location, jndiName, component); return new ComponentValueGenerator(location, component); } private static ValueGenerator generateEjb(String location, Class fieldType, String jndiName, EJB ejb) throws ConfigException { Class type = ejb.beanInterface(); String mappedName = ejb.mappedName(); String beanName = ejb.beanName(); if (! "".equals(ejb.name())) jndiName = ejb.name(); return generateJndiComponent(location, fieldType, type, jndiName, beanName, mappedName); } private static ValueGenerator generateResource(String location, Class fieldType, String jndiName, Resource resource) throws ConfigException { String mappedName = resource.mappedName(); Class type = resource.type(); if (! "".equals(resource.name())) jndiName = resource.name(); return generateJndiComponent(location, fieldType, type, jndiName, mappedName, ""); } private static void introspectWebBean(ArrayList<ConfigProgram> injectList, Field field) throws ConfigException { WebBeansContainer webBeans = WebBeansContainer.create(); boolean isOptional = false; webBeans.createProgram(injectList, field, isOptional); } private static void introspectWebBean(ArrayList<ConfigProgram> injectList, Method method) throws ConfigException { WebBeansContainer webBeans = WebBeansContainer.create(); webBeans.createProgram(injectList, method); } /** * Creates the value. */ private static ValueGenerator generateJndiComponent(String location, Class fieldType, Class type, String jndiName, String mappedName, String beanName) { if (! fieldType.isAssignableFrom(type)) type = fieldType; if (type.isPrimitive()) type = _primitiveTypeMap.get(type); Object value = Jndi.lookup(jndiName); // XXX: can use lookup-link and store the proxy if (value != null) return new SingletonGenerator(value); WebBeansContainer webBeans = WebBeansContainer.create(); ComponentImpl component = null; if (mappedName == null || "".equals(mappedName)) mappedName = jndiName; component = webBeans.bind(location, type, mappedName); if (component != null) { bindJndi(location, jndiName, component); return new ComponentValueGenerator(location, component); } if (component == null && beanName != null && ! "".equals(beanName)) { component = webBeans.bind(location, type, beanName); if (component != null) { bindJndi(location, jndiName, component); return new ComponentValueGenerator(location, component); } } if (component == null && jndiName != null && ! "".equals(jndiName)) { component = webBeans.bind(location, type, jndiName); if (component != null) { bindJndi(location, jndiName, component); return new ComponentValueGenerator(location, component); } } if (component == null) component = webBeans.bind(location, type); if (component != null) { bindJndi(location, jndiName, component); return new ComponentValueGenerator(location, component); } else throw new ConfigException(location + L.l("{0} with mappedName={1}, beanName={2}, and jndiName={3} does not match anything", type.getName(), mappedName, beanName, jndiName)); /* if (_component != null && _jndiName != null && ! "".equals(_jndiName)) { try { Jndi.bindDeepShort(_jndiName, _component); } catch (NamingException e) { throw new ConfigException(e); } } } if (component != null) return component.get(); else return getJndiValue(_type); */ } private static void bindJndi(String location, String name, Object value) { try { if (! "".equals(name)) Jndi.bindDeepShort(name, value); } catch (NamingException e) { throw new ConfigException(location + e.getMessage(), e); } } private static Field findField(Class type, String name) { for (Field field : type.getDeclaredFields()) { if (field.getName().equals(name)) return field; } return null; } private static Method findMethod(Class type, String name) { for (Method method : type.getDeclaredMethods()) { if (method.getParameterTypes().length != 1) continue; String methodName = method.getName(); if (! methodName.startsWith("set")) continue; methodName = Introspector.decapitalize(methodName.substring(3)); if (name.equals(methodName)) return method; } return null; } private static boolean hasBindingAnnotation(Field field) { for (Annotation ann : field.getAnnotations()) { Class annType = ann.annotationType(); if (annType.equals(In.class)) return true; if (annType.isAnnotationPresent(BindingType.class)) return true; } return false; } private static boolean isBindingOptional(Field field) { for (Annotation ann : field.getAnnotations()) { Class annType = ann.annotationType(); if (annType.equals(In.class)) { In in = (In) ann; return in.optional(); } if (annType.isAnnotationPresent(BindingType.class)) return false; } return false; } private static boolean hasBindingAnnotation(Method method) { for (Annotation ann : method.getAnnotations()) { Class annType = ann.annotationType(); if (annType.equals(Produces.class)) return false; else if (annType.equals(Destroys.class)) return false; else if (annType.equals(In.class)) return true; } boolean hasBinding = false; for (Annotation []annList : method.getParameterAnnotations()) { if (annList == null) continue; for (Annotation ann : annList) { Class annType = ann.annotationType(); if (annType.equals(Observes.class)) return false; if (annType.equals(Disposes.class)) return false; else if (annType.isAnnotationPresent(BindingType.class)) hasBinding = true; } } return hasBinding; } private static String toFullName(String jndiName) { int colon = jndiName.indexOf(':'); int slash = jndiName.indexOf('/'); if (colon < 0 || slash > 0 && slash < colon) jndiName = "java:comp/env/" + jndiName; return jndiName; } private static ConfigException error(Field field, String msg) { return new ConfigException(location(field) + msg); } private static ConfigException error(Method method, String msg) { return new ConfigException(location(method) + msg); } private static String location(Field field) { String className = field.getDeclaringClass().getName(); return className + "." + field.getName() + ": "; } private static String location(Method method) { String className = method.getDeclaringClass().getName(); return className + "." + method.getName() + ": "; } static { _primitiveTypeMap.put(boolean.class, Boolean.class); _primitiveTypeMap.put(byte.class, Byte.class); _primitiveTypeMap.put(char.class, Character.class); _primitiveTypeMap.put(short.class, Short.class); _primitiveTypeMap.put(int.class, Integer.class); _primitiveTypeMap.put(long.class, Long.class); _primitiveTypeMap.put(float.class, Float.class); _primitiveTypeMap.put(double.class, Double.class); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?