List of usage examples for org.springframework.util ResourceUtils toURI
public static URI toURI(String location) throws URISyntaxException
From source file:com.trigonic.utils.spring.beans.ImportHelper.java
private static boolean isAbsoluteLocation(String location) { boolean absoluteLocation = false; try {//from w w w .jav a 2 s.com absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute(); } catch (URISyntaxException ex) { // cannot convert to an URI, considering the location relative unless it has the "classpath*:" prefix } return absoluteLocation; }
From source file:org.shept.util.JarUtils.java
/** * Copy resources from a classPath, typically within a jar file * to a specified destination, typically a resource directory in * the projects webApp directory (images, sounds, e.t.c. ) * /*www . ja v a 2s .c om*/ * Copies resources only if the destination file does not exist and * the specified resource is available. * * The ClassPathResource will be scanned for all resources in the path specified by the resource. * For example a path like: * new ClassPathResource("resource/images/pager/", SheptBaseController.class); * takes all the resources in the path 'resource/images/pager' (but not in sub-path) * from the specified clazz 'SheptBaseController' * * @param cpr ClassPathResource specifying the source location on the classPath (maybe within a jar) * @param webAppDestPath Full path String to the fileSystem destination directory * @throws IOException when copying on copy error * @throws URISyntaxException */ public static void copyResources(ClassPathResource cpr, String webAppDestPath) throws IOException, URISyntaxException { String dstPath = webAppDestPath; // + "/" + jarPathInternal(cpr.getURL()); File dir = new File(dstPath); dir.mkdirs(); URL url = cpr.getURL(); // jarUrl is the URL of the containing lib, e.g. shept.org in this case URL jarUrl = ResourceUtils.extractJarFileURL(url); String urlFile = url.getFile(); String resPath = ""; int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR); if (separatorIndex != -1) { // just copy the the location path inside the jar without leading separators !/ resPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length()); } else { return; // no resource within jar to copy } File f = new File(ResourceUtils.toURI(jarUrl)); JarFile jf = new JarFile(f); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String path = entry.getName(); if (path.startsWith(resPath) && entry.getSize() > 0) { String fileName = path.substring(path.lastIndexOf("/")); File dstFile = new File(dstPath, fileName); // (StringUtils.applyRelativePath(dstPath, fileName)); Resource fileRes = cpr.createRelative(fileName); if (!dstFile.exists() && fileRes.exists()) { FileOutputStream fos = new FileOutputStream(dstFile); FileCopyUtils.copy(fileRes.getInputStream(), fos); logger.info("Successfully copied file " + fileName + " from " + cpr.getPath() + " to " + dstFile.getPath()); } } } if (jf != null) { jf.close(); } }
From source file:com.enonic.cms.core.plugin.container.OsgiContainer.java
private void copyFrameworkJar(final File targetFile) throws Exception { URL location = FrameworkProperties.class.getProtectionDomain().getCodeSource().getLocation(); final String locationFile = location.getFile(); LOG.info("Location of framework.jar : " + locationFile); if (locationFile.endsWith(".jar!/")) // for IBM Websphere 8.5 Liberty Profile {// w w w . j ava 2s .c om String absolutePath = locationFile.substring(0, locationFile.length() - 2); location = new URL(absolutePath); } else if (ResourceUtils.URL_PROTOCOL_VFS.equals(location.getProtocol())) // JBOSS 7.1.1 VFS { final URI uri = ResourceUtils.toURI(location); final UrlResource urlResource = new UrlResource(uri); final File file = urlResource.getFile(); String absolutePath = file.getAbsolutePath(); if (!absolutePath.endsWith(urlResource.getFilename())) { // removing /contents folder from path and adding unpacked jar to path. absolutePath = absolutePath.substring(0, absolutePath.length() - VFS_CONTENTS_FOLDER.length()) + urlResource.getFilename(); } final StringBuilder stringBuilder = new StringBuilder("file:/"); if (!SystemUtils.IS_OS_WINDOWS) // windows already has one slash in path like /c:/Program Files/.... { stringBuilder.append('/'); } stringBuilder.append(absolutePath); location = new URL(stringBuilder.toString()); } LOG.info("Copying " + location.toString() + " to " + targetFile.toString()); Files.copy(Resources.newInputStreamSupplier(location), targetFile); }
From source file:org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.java
/** * Parse an "import" element and load the bean definitions * from the given resource into the bean factory. *//*from w ww. j a va 2 s.c o m*/ protected void importBeanDefinitionResource(Element ele) { String location = ele.getAttribute(RESOURCE_ATTRIBUTE); if (!StringUtils.hasText(location)) { getReaderContext().error("Resource location must not be empty", ele); return; } // Resolve system properties: e.g. "${user.dir}" location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location); Set<Resource> actualResources = new LinkedHashSet<>(4); // Discover whether the location is an absolute or relative URI boolean absoluteLocation = false; try { absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute(); } catch (URISyntaxException ex) { // cannot convert to an URI, considering the location relative // unless it is the well-known Spring prefix "classpath*:" } // Absolute or relative? if (absoluteLocation) { try { int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources); if (logger.isDebugEnabled()) { logger.debug( "Imported " + importCount + " bean definitions from URL location [" + location + "]"); } } catch (BeanDefinitionStoreException ex) { getReaderContext().error("Failed to import bean definitions from URL location [" + location + "]", ele, ex); } } else { // No URL -> considering resource location as relative to the current file. try { int importCount; Resource relativeResource = getReaderContext().getResource().createRelative(location); if (relativeResource.exists()) { importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource); actualResources.add(relativeResource); } else { String baseLocation = getReaderContext().getResource().getURL().toString(); importCount = getReaderContext().getReader().loadBeanDefinitions( StringUtils.applyRelativePath(baseLocation, location), actualResources); } if (logger.isDebugEnabled()) { logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]"); } } catch (IOException ex) { getReaderContext().error("Failed to resolve current resource location", ele, ex); } catch (BeanDefinitionStoreException ex) { getReaderContext().error( "Failed to import bean definitions from relative location [" + location + "]", ele, ex); } } Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]); getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele)); }
From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java
/** * Resolve the given jar file URL into a JarFile object. */// w w w . j a v a 2 s . co m protected JarFile getJarFile(String jarFileUrl) throws IOException { if (jarFileUrl.startsWith(ResourceUtils.FILE_URL_PREFIX)) { try { return new JarFile(ResourceUtils.toURI(jarFileUrl).getSchemeSpecificPart()); } catch (URISyntaxException ex) { // Fallback for URLs that are not valid URIs (should hardly ever happen). return new JarFile(jarFileUrl.substring(ResourceUtils.FILE_URL_PREFIX.length())); } } else { return new JarFile(jarFileUrl); } }