config.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 803 行 · 第 1/2 页
JAVA
803 行
return doc; } private Schema findCompactSchema(String location) throws IOException, ConfigException { try { if (location == null) return null; Thread thread = Thread.currentThread(); ClassLoader loader = thread.getContextClassLoader(); if (loader == null) loader = ClassLoader.getSystemClassLoader(); URL url = loader.getResource(location); if (url == null) return null; Path path = Vfs.lookup(URLDecoder.decode(url.toString())); // VerifierFactory factory = VerifierFactory.newInstance("http://caucho.com/ns/compact-relax-ng/1.0"); CompactVerifierFactoryImpl factory; factory = new CompactVerifierFactoryImpl(); return factory.compileSchema(path); } catch (IOException e) { throw e; } catch (Exception e) { throw ConfigException.create(e); } } /** * Returns true if the class can be instantiated. */ public static void checkCanInstantiate(Class beanClass) throws ConfigException { if (beanClass == null) throw new ConfigException(L.l("null classes can't be instantiated.")); else if (beanClass.isInterface()) throw new ConfigException(L.l("'{0}' must be a concrete class. Interfaces cannot be instantiated.", beanClass.getName())); else if (! Modifier.isPublic(beanClass.getModifiers())) throw new ConfigException(L.l("Custom bean class '{0}' is not public. Bean classes must be public, concrete, and have a zero-argument constructor.", beanClass.getName())); else if (Modifier.isAbstract(beanClass.getModifiers())) throw new ConfigException(L.l("Custom bean class '{0}' is abstract. Bean classes must be public, concrete, and have a zero-argument constructor.", beanClass.getName())); Constructor []constructors = beanClass.getDeclaredConstructors(); Constructor constructor = null; for (int i = 0; i < constructors.length; i++) { if (constructors[i].getParameterTypes().length == 0) { constructor = constructors[i]; break; } } if (constructor == null) throw new ConfigException(L.l("Custom bean class '{0}' doesn't have a zero-arg constructor. Bean classes must be have a zero-argument constructor.", beanClass.getName())); if (! Modifier.isPublic(constructor.getModifiers())) { throw new ConfigException(L.l("The zero-argument constructor for '{0}' isn't public. Bean classes must have a public zero-argument constructor.", beanClass.getName())); } } /** * Returns true if the class can be instantiated. */ public static void validate(Class cl, Class api) throws ConfigException { checkCanInstantiate(cl); if (! api.isAssignableFrom(cl)) { throw new ConfigException(L.l("{0} must implement {1}.", cl.getName(), api.getName())); } } /** * Returns true if the class can be instantiated using zero args constructor * or constructor that accepts an instance of class passed in type argument */ public static void checkCanInstantiate(Class beanClass, Class type) throws ConfigException { if (beanClass == null) throw new ConfigException(L.l("null classes can't be instantiated.")); else if (beanClass.isInterface()) throw new ConfigException(L.l( "'{0}' must be a concrete class. Interfaces cannot be instantiated.", beanClass.getName())); else if (! Modifier.isPublic(beanClass.getModifiers())) throw new ConfigException(L.l( "Custom bean class '{0}' is not public. Bean classes must be public, concrete, and have a zero-argument constructor.", beanClass.getName())); else if (Modifier.isAbstract(beanClass.getModifiers())) throw new ConfigException(L.l( "Custom bean class '{0}' is abstract. Bean classes must be public, concrete, and have a zero-argument constructor.", beanClass.getName())); Constructor [] constructors = beanClass.getDeclaredConstructors(); Constructor zeroArgsConstructor = null; Constructor singleArgConstructor = null; for (int i = 0; i < constructors.length; i++) { if (constructors [i].getParameterTypes().length == 0) { zeroArgsConstructor = constructors [i]; if (singleArgConstructor != null) break; } else if (type != null && constructors [i].getParameterTypes().length == 1 && type.isAssignableFrom(constructors[i].getParameterTypes()[0])) { singleArgConstructor = constructors [i]; if (zeroArgsConstructor != null) break; } } if (zeroArgsConstructor == null && singleArgConstructor == null) if (type != null) throw new ConfigException(L.l( "Custom bean class '{0}' doesn't have a zero-arg constructor, or a constructor accepting parameter of type '{1}'. Bean class '{0}' must have a zero-argument constructor, or a constructor accepting parameter of type '{1}'", beanClass.getName(), type.getName())); else throw new ConfigException(L.l( "Custom bean class '{0}' doesn't have a zero-arg constructor. Bean classes must have a zero-argument constructor.", beanClass.getName())); if (singleArgConstructor != null) { if (! Modifier.isPublic(singleArgConstructor.getModifiers()) && (zeroArgsConstructor == null || ! Modifier.isPublic(zeroArgsConstructor.getModifiers()))) { throw new ConfigException(L.l( "The constructor for bean '{0}' accepting parameter of type '{1}' is not public. Constructor accepting parameter of type '{1}' must be public.", beanClass.getName(), type.getName())); } } else if (zeroArgsConstructor != null) { if (! Modifier.isPublic(zeroArgsConstructor.getModifiers())) throw new ConfigException(L.l( "The zero-argument constructor for '{0}' isn't public. Bean classes must have a public zero-argument constructor.", beanClass.getName())); } } public static void validate(Class cl, Class api, Class type) throws ConfigException { checkCanInstantiate(cl, type); if (! api.isAssignableFrom(cl)) { throw new ConfigException(L.l("{0} must implement {1}.", cl.getName(), api.getName())); } } /** * Sets an attribute with a value. * * @param obj the bean to be set * @param attr the attribute name * @param value the attribute value */ public static void setAttribute(Object obj, String attr, Object value) throws Exception { ConfigType type = TypeFactory.getType(obj.getClass()); QName attrName = new QName(attr); Attribute attrStrategy = type.getAttribute(attrName); if (attrStrategy == null) throw new ConfigException(L.l("{0}: '{1}' is an unknown attribute.", obj.getClass().getName(), attrName.getName())); value = attrStrategy.getConfigType().valueOf(value); attrStrategy.setValue(obj, attrName, value); } /** * Sets an attribute with a value. * * @param obj the bean to be set * @param attr the attribute name * @param value the attribute value */ public static void setStringAttribute(Object obj, String attr, String value) throws Exception { ConfigContext builder = new ConfigContext(); QAttr qAttr = new QAttr(attr); qAttr.setValue(value); builder.configureAttribute(obj, qAttr); } public static void init(Object bean) throws ConfigException { try { ConfigType type = TypeFactory.getType(bean.getClass()); type.init(bean); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw ConfigException.create(e); } } public static void inject(Object bean) throws ConfigException { try { ConfigType type = TypeFactory.getType(bean.getClass()); type.inject(bean); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw ConfigException.create(e); } } public static Object replaceObject(Object bean) throws Exception { ConfigType type = TypeFactory.getType(bean.getClass()); return type.replaceObject(bean); } /** * Returns the variable resolver. */ public static ELContext getEnvironment() { ConfigContext builder = ConfigContext.getCurrentBuilder(); if (builder != null) { return builder.getELContext(); } else return EL.getEnvironment(); } /** * Returns the variable resolver. */ public static ConfigELContext getELContext() { ConfigContext builder = ConfigContext.getCurrentBuilder(); if (builder != null) { return builder.getELContext(); } else return null; } /** * Sets an EL configuration variable. */ public static Object getCurrentVar(String var) { return WebBeansContainer.create().findByName(var); } /** * Evaluates an EL string in the context. */ public static String evalString(String str) throws ELException { return EL.evalString(str, getEnvironment()); } /** * Evaluates an EL string in the context. */ public static String evalString(String str, HashMap<String,Object> varMap) throws ELException { return EL.evalString(str, getEnvironment(varMap)); } /** * Evaluates an EL boolean in the context. */ public static boolean evalBoolean(String str) throws ELException { return EL.evalBoolean(str, getEnvironment()); } public static ELContext getEnvironment(HashMap<String,Object> varMap) { if (varMap != null) return new EnvironmentContext(varMap); else return new EnvironmentContext(); } public static ConfigException error(Field field, String msg) { return new ConfigException(location(field) + msg); } public static ConfigException error(Method method, String msg) { return new ConfigException(location(method) + msg); } public static RuntimeException createLine(String systemId, int line, Throwable e) { while (e.getCause() != null && (e instanceof InstantiationException || e instanceof InvocationTargetException || e.getClass().equals(ConfigRuntimeException.class))) { e = e.getCause(); } if (e instanceof LineConfigException) throw (LineConfigException) e; String lines = getSourceLines(systemId, line); String loc = systemId + ":" + line + ": "; if (e instanceof DisplayableException) { return new LineConfigException(loc + e.getMessage() + "\n" + lines, e); } else return new LineConfigException(loc + e + "\n" + lines, e); } public static String location(Field field) { String className = field.getDeclaringClass().getName(); return className + "." + field.getName() + ": "; } public static String location(Method method) { String className = method.getDeclaringClass().getName(); return className + "." + method.getName() + ": "; } private static String getSourceLines(String systemId, int errorLine) { if (systemId == null) return ""; ReadStream is = null; try { is = Vfs.lookup().lookup(systemId).openRead(); int line = 0; StringBuilder sb = new StringBuilder("\n\n"); String text; while ((text = is.readLine()) != null) { line++; if (errorLine - 2 <= line && line <= errorLine + 2) { sb.append(line); sb.append(": "); sb.append(text); sb.append("\n"); } } return sb.toString(); } catch (IOException e) { log.log(Level.FINEST, e.toString(), e); return ""; } finally { if (is != null) is.close(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?