void map(@NonNull HttpConfiguration httpConfiguration) throws IOException { org.springframework.core.io.Resource[] resources; try { resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources( applicationProperties.getResourcePath() + "/openapi/**.y*ml"); } catch (FileNotFoundException exp) { LOG.warn("No Open API resources found in path:{}/openapi", applicationProperties.getResourcePath()); return; } for (org.springframework.core.io.Resource resource : resources) { InputStream inputStream = new EnvironmentAwareResource(resource.getInputStream(), environment).getInputStream(); String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8)); if (!StringUtils.isBlank(result)) { Swagger swagger = openApiParser.parse(result); mapOpenApiDefinition(swagger, httpConfiguration); } } }
/** * Returns an instance which uses the the specified selector, as the name of the * definition file(s). In the case of a name with a Spring "classpath*:" prefix, * or with no prefix, which is treated the same, the current thread's context class * loader's {@code getResources} method will be called with this value to get * all resources having that name. These resources will then be combined to form a * definition. In the case where the name uses a Spring "classpath:" prefix, or * a standard URL prefix, then only one resource file will be loaded as the * definition. * @param selector the location of the resource(s) which will be read and * combined to form the definition for the BeanFactoryLocator instance. * Any such files must form a valid ApplicationContext definition. * @return the corresponding BeanFactoryLocator instance * @throws BeansException in case of factory loading failure */ public static BeanFactoryLocator getInstance(String selector) throws BeansException { String resourceLocation = selector; if (resourceLocation == null) { resourceLocation = DEFAULT_RESOURCE_LOCATION; } // For backwards compatibility, we prepend "classpath*:" to the selector name if there // is no other prefix (i.e. "classpath*:", "classpath:", or some URL prefix). if (!ResourcePatternUtils.isUrl(resourceLocation)) { resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation; } synchronized (instances) { if (logger.isTraceEnabled()) { logger.trace("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode() + ", instances=" + instances); } BeanFactoryLocator bfl = instances.get(resourceLocation); if (bfl == null) { bfl = new ContextSingletonBeanFactoryLocator(resourceLocation); instances.put(resourceLocation, bfl); } return bfl; } }
/** * Returns an instance which uses the the specified selector, as the name of the * definition file(s). In the case of a name with a Spring 'classpath*:' prefix, * or with no prefix, which is treated the same, the current thread context * ClassLoader's {@code getResources} method will be called with this value * to get all resources having that name. These resources will then be combined to * form a definition. In the case where the name uses a Spring 'classpath:' prefix, * or a standard URL prefix, then only one resource file will be loaded as the * definition. * @param selector the name of the resource(s) which will be read and * combined to form the definition for the BeanFactoryLocator instance. * Any such files must form a valid BeanFactory definition. * @return the corresponding BeanFactoryLocator instance * @throws BeansException in case of factory loading failure */ public static BeanFactoryLocator getInstance(String selector) throws BeansException { String resourceLocation = selector; if (resourceLocation == null) { resourceLocation = DEFAULT_RESOURCE_LOCATION; } // For backwards compatibility, we prepend 'classpath*:' to the selector name if there // is no other prefix (i.e. classpath*:, classpath:, or some URL prefix. if (!ResourcePatternUtils.isUrl(resourceLocation)) { resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation; } synchronized (instances) { if (logger.isTraceEnabled()) { logger.trace("SingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode() + ", instances=" + instances); } BeanFactoryLocator bfl = instances.get(resourceLocation); if (bfl == null) { bfl = new SingletonBeanFactoryLocator(resourceLocation); instances.put(resourceLocation, bfl); } return bfl; } }
/** * Subclasses can override this method to return the locations of their * config files. * <p>A plain path will be treated as class path location, e.g.: * "org/springframework/whatever/foo.xml". Note however that you may prefix * path locations with standard Spring resource prefixes. Therefore, a * config location path prefixed with "classpath:" with behave the same as a * plain path, but a config location such as * "file:/some/path/path/location/appContext.xml" will be treated as a * filesystem location. * <p>The default implementation builds config locations for the config paths * specified through {@link #getConfigPaths()}. * @return an array of config locations * @see #getConfigPaths() * @see org.springframework.core.io.ResourceLoader#getResource(String) */ protected final String[] getConfigLocations() { String[] paths = getConfigPaths(); String[] convertedPaths = new String[paths.length]; for (int i = 0; i < paths.length; i++) { String path = paths[i]; if (path.startsWith(SLASH)) { convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path; } else if (!ResourcePatternUtils.isUrl(path)) { convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(getClass()) + SLASH + path); } else { convertedPaths[i] = StringUtils.cleanPath(path); } } return convertedPaths; }
/** * Returns an instance which uses the specified selector, as the name of the * definition file(s). In the case of a name with a Spring "classpath*:" prefix, * or with no prefix, which is treated the same, the current thread's context class * loader's {@code getResources} method will be called with this value to get * all resources having that name. These resources will then be combined to form a * definition. In the case where the name uses a Spring "classpath:" prefix, or * a standard URL prefix, then only one resource file will be loaded as the * definition. * @param selector the location of the resource(s) which will be read and * combined to form the definition for the BeanFactoryLocator instance. * Any such files must form a valid ApplicationContext definition. * @return the corresponding BeanFactoryLocator instance * @throws BeansException in case of factory loading failure */ public static BeanFactoryLocator getInstance(String selector) throws BeansException { String resourceLocation = selector; if (resourceLocation == null) { resourceLocation = DEFAULT_RESOURCE_LOCATION; } // For backwards compatibility, we prepend "classpath*:" to the selector name if there // is no other prefix (i.e. "classpath*:", "classpath:", or some URL prefix). if (!ResourcePatternUtils.isUrl(resourceLocation)) { resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation; } synchronized (instances) { if (logger.isTraceEnabled()) { logger.trace("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode() + ", instances=" + instances); } BeanFactoryLocator bfl = instances.get(resourceLocation); if (bfl == null) { bfl = new ContextSingletonBeanFactoryLocator(resourceLocation); instances.put(resourceLocation, bfl); } return bfl; } }
/** * Returns an instance which uses the specified selector, as the name of the * definition file(s). In the case of a name with a Spring 'classpath*:' prefix, * or with no prefix, which is treated the same, the current thread context * ClassLoader's {@code getResources} method will be called with this value * to get all resources having that name. These resources will then be combined to * form a definition. In the case where the name uses a Spring 'classpath:' prefix, * or a standard URL prefix, then only one resource file will be loaded as the * definition. * @param selector the name of the resource(s) which will be read and * combined to form the definition for the BeanFactoryLocator instance. * Any such files must form a valid BeanFactory definition. * @return the corresponding BeanFactoryLocator instance * @throws BeansException in case of factory loading failure */ public static BeanFactoryLocator getInstance(String selector) throws BeansException { String resourceLocation = selector; if (resourceLocation == null) { resourceLocation = DEFAULT_RESOURCE_LOCATION; } // For backwards compatibility, we prepend 'classpath*:' to the selector name if there // is no other prefix (i.e. classpath*:, classpath:, or some URL prefix. if (!ResourcePatternUtils.isUrl(resourceLocation)) { resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation; } synchronized (instances) { if (logger.isTraceEnabled()) { logger.trace("SingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode() + ", instances=" + instances); } BeanFactoryLocator bfl = instances.get(resourceLocation); if (bfl == null) { bfl = new SingletonBeanFactoryLocator(resourceLocation); instances.put(resourceLocation, bfl); } return bfl; } }
@Bean public SqlSessionFactory sqlSessionFactory() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); try { Properties properties = new Properties(); properties.put("prefix", env.getProperty("datasource.prefix", "")); sqlSessionFactoryBean.setConfigurationProperties(properties); sqlSessionFactoryBean .setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:/META-INF/admin-mybatis-mappings/*.xml")); sqlSessionFactoryBean.afterPropertiesSet(); return sqlSessionFactoryBean.getObject(); } catch (Exception e) { throw new RuntimeException("Could not create sqlSessionFactory", e); } }
public DatabaseScriptContainer(String scriptLocation, IDatabasePlatform platform) { try { this.scriptLocation = scriptLocation; this.platform = platform; this.jdbcTemplate = new JdbcTemplate(platform.getDataSource()); replacementTokens = new HashMap<String, String>(); // Add any replacement tokens Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()) .getResources(String.format("classpath*:%s/*.sql", scriptLocation)); for (Resource r : resources) { DatabaseScript script = new DatabaseScript(r.getFilename()); script.setResource(r); if (script.getWhen() == DatabaseScript.WHEN_PREINSTALL) { preInstallScripts.add(script); } else if (script.getWhen() == DatabaseScript.WHEN_POSTINSTALL) { postInstallScripts.add(script); } } } catch (IOException e) { throw new IoException(e); } }
protected void executeImports(Resource resource) throws IOException { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(resource.getInputStream())); String line = reader.readLine(); while (line != null) { if (line.startsWith(IMPORT_PREFIX)) { String file = line.substring(IMPORT_PREFIX.length()).trim(); Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()) .getResources(String.format("classpath*:%s/%s", scriptLocation, file)); for (Resource resource2 : resources) { execute(resource2.getURL()); } } line = reader.readLine(); } } finally { IOUtils.closeQuietly(reader); } }
/** * Import Spring bean definitions from either XML or Groovy sources into the * current bean builder instance. * @param resourcePattern the resource pattern */ public void importBeans(String resourcePattern) throws IOException { Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(getResourceLoader()).getResources(resourcePattern); for (Resource resource : resources) { String filename = resource.getFilename(); if (filename.endsWith(".groovy")) { loadBeanDefinitions(resource); } else if (filename.endsWith(".xml")) { this.xmlBeanDefinitionReader.loadBeanDefinitions(resource); } } }
/** * Create a new LocalSessionFactoryBuilder for the given DataSource. * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using * (may be {@code null}) * @param resourceLoader the ResourceLoader to load application classes from */ @SuppressWarnings("deprecation") // to be able to build against Hibernate 4.3 public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader) { getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); if (dataSource != null) { getProperties().put(Environment.DATASOURCE, dataSource); } // APP_CLASSLOADER is deprecated as of Hibernate 4.3 but we need to remain compatible with 4.0+ getProperties().put(AvailableSettings.APP_CLASSLOADER, resourceLoader.getClassLoader()); this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); }
/** * Create a new LocalSessionFactoryBuilder for the given DataSource. * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using * (may be {@code null}) * @param resourceLoader the ResourceLoader to load application classes from */ public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader) { getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); if (dataSource != null) { getProperties().put(Environment.DATASOURCE, dataSource); } getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader())); this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); }
private Resource[] getResourcesWithContent() throws IOException { Resource[] candidates = ResourcePatternUtils .getResourcePatternResolver(new DefaultResourceLoader()) .getResources("file:target/output/**"); for (Resource candidate : candidates) { if (candidate.contentLength() == 0) { return new Resource[0]; } } return candidates; }
@Bean public SqlSessionFactory sqlSessionFactory() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); DataSource dataSource = dataSource(); sqlSessionFactoryBean.setDataSource(dataSource); String databaseType = initDatabaseType(dataSource); if (databaseType == null) { throw new FlowableException("couldn't deduct database type"); } try { Properties properties = new Properties(); properties.put("prefix", env.getProperty("datasource.prefix", "")); properties.put("blobType", "BLOB"); properties.put("boolValue", "TRUE"); properties.load(this.getClass().getClassLoader().getResourceAsStream("org/flowable/db/properties/" + databaseType + ".properties")); sqlSessionFactoryBean.setConfigurationProperties(properties); sqlSessionFactoryBean .setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:/META-INF/modeler-mybatis-mappings/*.xml")); sqlSessionFactoryBean.afterPropertiesSet(); return sqlSessionFactoryBean.getObject(); } catch (Exception e) { throw new FlowableException("Could not create sqlSessionFactory", e); } }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(applicationContext); String dsName = ConnectionSource.DEFAULT.equals(dataSourceName) ? "dataSource" : "dataSource_" + dataSourceName; Properties properties = getProperties(); if(applicationContext.containsBean(dsName)) { properties.put(Environment.DATASOURCE, applicationContext.getBean(dsName)); } properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, currentSessionContext.getName()); properties.put(AvailableSettings.CLASSLOADERS, applicationContext.getClassLoader()); }
/** * Create a new LocalSessionFactoryBuilder for the given DataSource. * * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using * (may be {@code null}) * @param resourceLoader the ResourceLoader to load application classes from * @param metadataSources the Hibernate MetadataSources service to use (e.g. reusing an existing one) * @since 4.3 */ public HibernateSpringSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) { super(metadataSources); getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); if (dataSource != null) { getProperties().put(AvailableSettings.DATASOURCE, dataSource); } // Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default) try { // Try Hibernate 5.2 AvailableSettings.class.getField("CONNECTION_HANDLING"); getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); } catch (NoSuchFieldException ex) { // Try Hibernate 5.1 try { AvailableSettings.class.getField("ACQUIRE_CONNECTIONS"); getProperties().put("hibernate.connection.release_mode", "ON_CLOSE"); } catch (NoSuchFieldException ex2) { // on Hibernate 5.0.x or lower - no need to change the default there } } getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader())); this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); }
/** * 设定 ResourceLoader * @param resourceLoader {@link ResourceLoader} */ public void setResourceLoader(ResourceLoader resourceLoader) { this.resourcePatternResolver = ResourcePatternUtils .getResourcePatternResolver(resourceLoader); this.metadataReaderFactory = new CachingMetadataReaderFactory( resourceLoader); }
public static SdcctApplication buildApplication() { ResourceLoader resourceLoader = new DefaultResourceLoader(); ResourcePatternResolver resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); SdcctApplication app = ((SdcctApplication) new SdcctApplicationBuilder(buildSources(resourcePatternResolver)).addCommandLineProperties(false) .bannerMode(Mode.OFF).headless(true).resourceLoader(resourceLoader).application()); app.setPropertySource(buildPropertySource(resourcePatternResolver)); app.setListeners( app.getListeners().stream().filter((appListener -> !appListener.getClass().equals(LoggingApplicationListener.class))).collect(Collectors.toList())); return app; }
public static void main(String[] args) throws URISyntaxException { // import 标签的部分解析代码测试 // 测试 String location = "classpath:framework.c3/lookpu-method.xml"; Environment environment = new StandardEnvironment(); System.out.println(location); System.out.println(environment.resolveRequiredPlaceholders(location)); // System.out.println("ResourcePatternUtils.isUrl(location)"+ResourcePatternUtils.isUrl(location)); System.out.println("ResourceUtils.toURI(location).isAbsolute()"+ResourceUtils.toURI(location).isAbsolute()); }
private static Object[] buildApplicationSources(ResourceLoader resourceLoader) { try { List<Object> srcs = Stream.of(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(APP_SOURCE_RESOURCE_LOC_PATTERN)).collect( Collectors.toList()); LOGGER.info(String.format("Resolved %d application source resource(s): %s", srcs.size(), StringUtils.join(srcs, "; "))); srcs.add(0, CrigttApplicationConfiguration.class); return srcs.toArray(); } catch (IOException e) { throw new ApplicationContextException(String.format("Unable to resolve application source resource(s): %s", APP_SOURCE_RESOURCE_LOC_PATTERN), e); } }
@Override public Object convertValue(Parameterized parameterized, Class targetType, String src) { if (!Resource.class.isAssignableFrom(targetType)) { return super.convertValue(parameterized, targetType, src); } boolean out = WritableResource.class.isAssignableFrom(targetType); if (src.equals(STD_STREAM_ARG)) { return (out ? new StandardOutputResource() : new StandardInputResource()); } if (!ResourcePatternUtils.isUrl(src) && !StringUtils.startsWithAny(src, ResourceUtils.FILE_URL_PREFIX, ResourceUtils.JAR_URL_PREFIX)) { src = ResourceUtils.FILE_URL_PREFIX + src; } try { ResourceSource resourceSrc = CliValidatorServiceImpl.this.resourceSrcResolver.resolve(src); if (resourceSrc == null) { throw new CliValidatorException(String.format("Unable to resolve resource: path=%s", src)); } return resourceSrc.getResource(); } catch (IOException e) { throw new CliValidatorException(String.format("Unable to parse resource command line option (names=[%s], value=%s).", StringUtils.join(parameterized.getWrappedParameter().names(), ", "), src), e); } }
/** * Create a new LocalSessionFactoryBuilder for the given DataSource. * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using * (may be {@code null}) * @param resourceLoader the ResourceLoader to load application classes from */ public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader) { getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); if (dataSource != null) { getProperties().put(Environment.DATASOURCE, dataSource); } getProperties().put(AvailableSettings.APP_CLASSLOADER, resourceLoader.getClassLoader()); this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); }
public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List<String> locations) { this.locations = locations; this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); }
@Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); }
List<Resource> loadResources(String pattern) throws IOException { return Arrays.asList(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern)); }
private Resource[] getResources(String packagePath) throws IOException { ResourcePatternResolver resourceResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); Resource[] resources = resourceResolver.getResources(packagePath); return resources; }
public void setResourceLoader(ResourceLoader resourceLoader) { this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader); }