List of usage examples for org.springframework.core.io Resource getURL
URL getURL() throws IOException;
From source file:org.apache.servicecomb.core.definition.loader.SchemaLoader.java
public SchemaMeta registerSchema(String microserviceName, Resource resource) { try {/* w w w. j a va 2s .com*/ String schemaId = FilenameUtils.getBaseName(resource.getFilename()); String swaggerContent = IOUtils.toString(resource.getURL()); SchemaMeta schemaMeta = registerSchema(microserviceName, schemaId, swaggerContent); return schemaMeta; } catch (Throwable e) { throw new Error(e); } }
From source file:org.apache.servicecomb.foundation.common.utils.Log4jUtils.java
private static void outputFile(List<Resource> resList, Properties properties) throws IOException { //??class???outputFile??log??? //must create org.slf4j.impl.Log4jLoggerAdapter by LoggerExtFactory //in order to redefine Log4jLoggerAdapter before other class load Log4jLoggerAdapter Logger log = LoggerFactory.getLogger(Log4jUtils.class); String content = genFileContext(resList, properties); //????,??/*from w w w. ja va 2 s.c o m*/ //log.info("Merged log4j:\n{}", content); Resource res = resList.get(resList.size() - 1); // ?res.getFilejar??getFile File file = new File(res.getURL().getPath()); if (!file.getParentFile().canWrite()) { log.error("Can not output {},because can not write to directory of file {}", MERGED_FILE, res.getURL().getPath()); return; } File mergedfile = new File(res.getFile().getParentFile(), MERGED_FILE); FileUtils.writeStringToFile(mergedfile, content); log.info("Write merged log4j config file to {}", mergedfile.getAbsolutePath()); }
From source file:org.bonitasoft.engine.bdm.client.ResourcesLoader.java
private List<URL> getURLs(Resource[] resources) throws IOException { List<URL> classNames = new ArrayList<URL>(); for (Resource resource : resources) { classNames.add(resource.getURL()); }//from w w w. j av a 2 s. co m return classNames; }
From source file:org.bonitasoft.engine.business.data.generator.client.ResourcesLoader.java
private static List<URL> getURLs(Resource[] resources) throws IOException { List<URL> classNames = new ArrayList<>(); for (Resource resource : resources) { classNames.add(resource.getURL()); }//from w ww .j av a2 s. com return classNames; }
From source file:org.broadleafcommerce.common.extensibility.context.merge.ImportProcessor.java
public ResourceInputStream[] extract(ResourceInputStream[] sources) throws MergeException { if (sources == null) { return null; }/*from w w w . j a v a 2 s . co m*/ try { DynamicResourceIterator resourceList = new DynamicResourceIterator(); resourceList.addAll(Arrays.asList(sources)); while (resourceList.hasNext()) { ResourceInputStream myStream = resourceList.nextResource(); Document doc = builder.parse(myStream); NodeList nodeList = (NodeList) xPath.evaluate(IMPORT_PATH, doc, XPathConstants.NODESET); int length = nodeList.getLength(); for (int j = 0; j < length; j++) { Element element = (Element) nodeList.item(j); Resource resource = loader.getResource(element.getAttribute("resource")); ResourceInputStream ris = new ResourceInputStream(resource.getInputStream(), resource.getURL().toString()); resourceList.addEmbeddedResource(ris); element.getParentNode().removeChild(element); } if (length > 0) { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer xmlTransformer = tFactory.newTransformer(); xmlTransformer.setOutputProperty(OutputKeys.VERSION, "1.0"); xmlTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); xmlTransformer.setOutputProperty(OutputKeys.METHOD, "xml"); xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos)); StreamResult result = new StreamResult(writer); xmlTransformer.transform(source, result); byte[] itemArray = baos.toByteArray(); resourceList.set(resourceList.getPosition() - 1, new ResourceInputStream( new ByteArrayInputStream(itemArray), null, myStream.getNames())); } else { myStream.reset(); } } return resourceList.toArray(new ResourceInputStream[resourceList.size()]); } catch (Exception e) { throw new MergeException(e); } }
From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java
public static boolean isGrailsResource(Resource r) { try {// www. j a v a 2 s .c o m return isGrailsPath(r.getURL().getFile()); } catch (IOException e) { return false; } }
From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java
public static Resource getViewsDir(Resource resource) { if (resource == null) return null; try {//from w w w. j a va 2s . c om Resource appDir = getAppDir(resource); return new UrlResource(appDir.getURL().toString() + "/views"); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Error reading URL whilst resolving views dir from [" + resource + "]: " + e.getMessage(), e); } return null; } }
From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java
public static Resource getAppDir(Resource resource) { if (resource == null) return null; try {//ww w.j a v a2s . c o m String url = resource.getURL().toString(); int i = url.lastIndexOf(GRAILS_APP_DIR); if (i > -1) { url = url.substring(0, i + 10); return new UrlResource(url); } return null; } catch (MalformedURLException e) { return null; } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Error reading URL whilst resolving app dir from [" + resource + "]: " + e.getMessage(), e); } return null; } }
From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java
/** * Takes a Grails resource (one located inside the grails-app dir) and gets its relative path inside the WEB-INF directory * when deployed./*from w w w . j ava 2s . c o m*/ * * @param resource The Grails resource, which is a file inside the grails-app dir * @return The relative URL of the file inside the WEB-INF dir at deployment time or null if it cannot be established */ public static String getRelativeInsideWebInf(Resource resource) { if (resource == null) return null; try { String url = resource.getURL().toString(); int i = url.indexOf(WEB_INF); if (i > -1) { return url.substring(i); } Matcher m = PLUGIN_PATTERN.matcher(url); if (m.find()) { return WEB_INF + m.group(1); } i = url.lastIndexOf(GRAILS_APP_DIR); if (i > -1) { return WEB_INF + "/" + url.substring(i); } } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Error reading URL whilst resolving relative path within WEB-INF from [" + resource + "]: " + e.getMessage(), e); } return null; } return null; }
From source file:org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator.java
private void doPostResourceConfiguration(GrailsApplication app, RuntimeSpringConfiguration springConfig) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String resourceName = null;/* ww w.j av a 2s. c o m*/ try { Resource springResources; if (app.isWarDeployed()) { resourceName = GrailsRuntimeConfigurator.SPRING_RESOURCES_XML; springResources = parent.getResource(resourceName); } else { resourceName = DEVELOPMENT_SPRING_RESOURCES_XML; ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(); springResources = patternResolver.getResource(resourceName); } if (springResources != null && springResources.exists()) { if (LOG.isDebugEnabled()) LOG.debug( "[RuntimeConfiguration] Configuring additional beans from " + springResources.getURL()); DefaultListableBeanFactory xmlBf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xmlBf).loadBeanDefinitions(springResources); xmlBf.setBeanClassLoader(classLoader); String[] beanNames = xmlBf.getBeanDefinitionNames(); if (LOG.isDebugEnabled()) LOG.debug("[RuntimeConfiguration] Found [" + beanNames.length + "] beans to configure"); for (String beanName : beanNames) { BeanDefinition bd = xmlBf.getBeanDefinition(beanName); final String beanClassName = bd.getBeanClassName(); Class<?> beanClass = beanClassName == null ? null : ClassUtils.forName(beanClassName, classLoader); springConfig.addBeanDefinition(beanName, bd); String[] aliases = xmlBf.getAliases(beanName); for (String alias : aliases) { springConfig.addAlias(alias, beanName); } if (beanClass != null) { if (BeanFactoryPostProcessor.class.isAssignableFrom(beanClass)) { ((ConfigurableApplicationContext) springConfig.getUnrefreshedApplicationContext()) .addBeanFactoryPostProcessor( (BeanFactoryPostProcessor) xmlBf.getBean(beanName)); } } } } else if (LOG.isDebugEnabled()) { LOG.debug("[RuntimeConfiguration] " + resourceName + " not found. Skipping configuration."); } } catch (Exception ex) { LOG.error("[RuntimeConfiguration] Unable to perform post initialization config: " + resourceName, ex); } GrailsRuntimeConfigurator.loadSpringGroovyResources(springConfig, app); }