private void loadCustomVfs(Properties props) throws ClassNotFoundException { String value = props.getProperty("vfsImpl"); if (value != null) { String[] clazzes = value.split(","); for (String clazz : clazzes) { if (!clazz.isEmpty()) { @SuppressWarnings("unchecked") Class<? extends VFS> vfsImpl = (Class<? extends VFS>) Resources.classForName(clazz); configuration.setVfsImpl(vfsImpl); } } } }
if ("package".equals(child.getName())) { String mapperPackage = child.getStringAttribute("name"); ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>(); resolverUtil.find(new ResolverUtil.IsA(Object.class), mapperPackage); Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses(); mapper.addAll(mapperSet); } else { resources.add(url); } else if (resource == null && url == null && mapperClass != null) { Class<?> mapperInterface = Resources.classForName(mapperClass); mapper.add(mapperInterface); } else {
public static void executeScript(Connection connection, String path) { ScriptRunner scriptRunner = newScriptRunner(connection); try { scriptRunner.runScript(Resources.getResourceAsReader(path)); connection.commit(); } catch (Exception e) { throw new IllegalStateException("Fail to restore: " + path, e); } }
private void loadXmlResource() { // Spring may not know the real resource name so we check a flag // to prevent loading again a resource twice // this flag is set at XMLMapperBuilder#bindMapperForNamespace if (!configuration.isResourceLoaded("namespace:" + type.getName())) { String xmlResource = type.getName().replace('.', '/') + ".xml"; // #1347 InputStream inputStream = type.getResourceAsStream("/" + xmlResource); if (inputStream == null) { // Search XML mapper that is not in the module but in the classpath. try { inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource); } catch (IOException e2) { // ignore, resource is not required } } if (inputStream != null) { XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName()); xmlParser.parse(); } } }
private void propertiesElement(XNode context) throws Exception { if (context != null) { Properties defaults = context.getChildrenAsProperties(); String resource = context.getStringAttribute("resource"); String url = context.getStringAttribute("url"); if (resource != null && url != null) { throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other."); } if (resource != null) { defaults.putAll(Resources.getResourceAsProperties(resource)); } else if (url != null) { defaults.putAll(Resources.getUrlAsProperties(url)); } Properties vars = configuration.getVariables(); if (vars != null) { defaults.putAll(vars); } parser.setVariables(defaults); configuration.setVariables(defaults); } }
/** * Get a resource as a URL using the current class path * * @param resource - the resource to locate * @return the resource or null */ public URL getResourceAsURL(String resource) { return getResourceAsURL(resource, getClassLoaders(null)); }
public void setVfsImpl(Class<? extends VFS> vfsImpl) { if (vfsImpl != null) { this.vfsImpl = vfsImpl; VFS.addImplClass(this.vfsImpl); } }
static VirtualFile getChild(URL url) throws IOException { Object o = invoke(getChild, VFS, url); return o == null ? null : new VirtualFile(o); } }
/** * Loads a class * * @param className - the class to fetch * @return The loaded class * @throws ClassNotFoundException If the class cannot be found (duh!) */ public static Class<?> classForName(String className) throws ClassNotFoundException { return classLoaderWrapper.classForName(className); }
/** * Verifies that the provided object reference is null. If it is null, then this VFS is marked * as invalid for the current environment. * * @param object The object reference to check for null. */ protected static <T> T checkNotNull(T object) { if (object == null) { setInvalid(); } return object; }
/** * Returns true if the resource located at the given URL is a JAR file. * * @param url The URL of the resource to test. */ protected boolean isJar(URL url) { return isJar(url, new byte[JAR_MAGIC.length]); }
/** * @since 3.2.2 */ public void addMappers(String packageName, Class<?> superType) { ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>(); resolverUtil.find(new ResolverUtil.IsA(superType), packageName); Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses(); for (Class<?> mapperClass : mapperSet) { addMapper(mapperClass); } }
private void mapperElement(XNode parent) throws Exception { /* * 定义集合 用来分类放置mybatis的Mapper与XML 按顺序依次遍历 */ if (parent != null) { //指定在classpath中的mapper文件 Set<String> resources = new HashSet<>(); //指向一个mapper接口 Set<Class<?>> mapperClasses = new HashSet<>(); setResource(parent, resources, mapperClasses); // 依次遍历 首先 resource 然后 mapper for (String resource : resources) { ErrorContext.instance().resource(resource); InputStream inputStream = Resources.getResourceAsStream(resource); //TODO XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } for (Class<?> mapper : mapperClasses) { //TODO configuration.addMapper(mapper); } } }
private Class<?> resolveClass(String className) { try { // #699 className could be null if (className != null) { return Resources.classForName(className); } } catch (ClassNotFoundException e) { // ignore } return null; }
/** * Get a resource from the classpath, starting with a specific class loader * * @param resource - the resource to find * @param classLoader - the first classloader to try * @return the stream or null */ public URL getResourceAsURL(String resource, ClassLoader classLoader) { return getResourceAsURL(resource, getClassLoaders(classLoader)); }
static VirtualFile getChild(URL url) throws IOException { Object o = invoke(getChild, VFS, url); return o == null ? null : new VirtualFile(o); } }
/** * Verifies that the provided object reference is null. If it is null, then this VFS is marked * as invalid for the current environment. * * @param object The object reference to check for null. */ protected static <T> T checkNotNull(T object) { if (object == null) { setInvalid(); } return object; }
/** * @since 3.2.2 */ public void addMappers(String packageName, Class<?> superType) { ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>(); resolverUtil.find(new ResolverUtil.IsA(superType), packageName); Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses(); for (Class<?> mapperClass : mapperSet) { addMapper(mapperClass); } }
private void typeAliasesElement(XNode parent) { if (parent != null) { for (XNode child : parent.getChildren()) { if ("package".equals(child.getName())) { String typeAliasPackage = child.getStringAttribute("name"); configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage); } else { String alias = child.getStringAttribute("alias"); String type = child.getStringAttribute("type"); try { Class<?> clazz = Resources.classForName(type); if (alias == null) { typeAliasRegistry.registerAlias(clazz); } else { typeAliasRegistry.registerAlias(alias, clazz); } } catch (ClassNotFoundException e) { throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e); } } } } }
field.setAccessible(true); Map mapConfig = (Map) field.get(configuration.getMapperRegistry()); mapConfig.remove(Resources.classForName(namespace)); loadedResourcesSet.remove(resource.toString()); configuration.getCacheNames().remove(namespace);