List of usage examples for org.springframework.core.io Resource createRelative
Resource createRelative(String relativePath) throws IOException;
From source file:com.civilizer.web.handler.ResourceHttpRequestHandler.java
protected Resource getResource(HttpServletRequest request) { String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); if (path == null) { throw new IllegalStateException("Required request attribute '" + HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE + "' is not set"); }//from ww w . j a v a 2s. c o m // // For resources having UTF-8 encoded path; // path = FsUtil.toUtf8Path(path); if (!StringUtils.hasText(path) || isInvalidPath(path)) { if (logger.isDebugEnabled()) { logger.debug("Ignoring invalid resource path [" + path + "]"); } return null; } for (Resource location : this.locations) { try { if (logger.isDebugEnabled()) { logger.debug("Trying relative path [" + path + "] against base location: " + location); } Resource resource = location.createRelative(path); if (resource.exists() && resource.isReadable()) { if (logger.isDebugEnabled()) { logger.debug("Found matching resource: " + resource); } return resource; } else if (logger.isTraceEnabled()) { logger.trace("Relative resource doesn't exist or isn't readable: " + resource); } } catch (IOException ex) { logger.debug("Failed to create relative resource - trying next resource location", ex); } } return null; }
From source file:se.trillian.goodies.spring.HostNameBasedPropertyPlaceHolderConfigurer.java
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { String hostName = getHostName(); try {// w w w . j a v a 2s . c om List<Resource> tempLocations = new ArrayList<Resource>(); for (Resource res : locations) { String basename = res.getFilename(); String extension = ""; int index = basename.lastIndexOf('.'); if (index != -1) { extension = basename.substring(index + 1); basename = basename.substring(0, index); } tempLocations.add(res.createRelative(basename + "-defaults." + extension)); for (Filter f : hostNameFilters) { String filteredHostName = hostName.replaceAll(f.getPattern(), f.getReplacement()); if (!filteredHostName.equals(hostName)) { tempLocations.add( res.createRelative(basename + "-defaults-" + filteredHostName + "." + extension)); } } tempLocations.add(res.createRelative(basename + "-defaults-" + hostName + "." + extension)); tempLocations.add(res.createRelative(basename + "." + extension)); for (Filter f : hostNameFilters) { String filteredHostName = hostName.replaceAll(f.getPattern(), f.getReplacement()); if (!filteredHostName.equals(hostName)) { tempLocations.add(res.createRelative(basename + "-" + filteredHostName + "." + extension)); } } tempLocations.add(res.createRelative(basename + "-" + hostName + "." + extension)); } List<Resource> newLocations = new ArrayList<Resource>(); for (Resource r : tempLocations) { // Make sure we add a location only once. if (!newLocations.contains(r)) { newLocations.add(r); } } super.setLocations(newLocations.toArray(new Resource[0])); } catch (IOException e) { throw new RuntimeException(e); } super.postProcessBeanFactory(beanFactory); }
From source file:com.wavemaker.commons.classloader.ThrowawayFileClassLoader.java
@Override protected URL findResource(String name) { URL ret = null;// w ww . jav a 2 s .c o m JarFile jarFile = null; try { for (Resource entry : this.classPath) { if (entry.getFilename().toLowerCase().endsWith(".jar")) { jarFile = new JarFile(entry.getFile()); ZipEntry ze = jarFile.getEntry(name); jarFile.close(); if (ze != null) { ret = new URL("jar:" + entry.getURL() + "!/" + name); break; } } else { Resource file = entry.createRelative(name); if (file.exists()) { ret = file.getURL(); break; } } } } catch (IOException e) { throw new WMRuntimeException(e); } return ret; }
From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java
@Override public Resource copyFile(Resource root, InputStream source, String filePath) { Assert.isInstanceOf(FileSystemResource.class, root, "Expected a FileSystemResource"); try {/*from w w w .jav a2s .co m*/ FileSystemResource targetFile = (FileSystemResource) root.createRelative(filePath); FileCopyUtils.copy(source, getOutputStream(targetFile)); return targetFile; } catch (IOException ex) { throw new WMRuntimeException(ex); } }
From source file:com.wavemaker.commons.classloader.ThrowawayFileClassLoader.java
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { if (this.classPath == null) { throw new ClassNotFoundException("invalid search root: " + this.classPath); } else if (name == null) { throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage()); }/* w w w .j av a2 s. c o m*/ String classNamePath = name.replace('.', '/') + ".class"; byte[] fileBytes = null; try { InputStream is = null; JarFile jarFile = null; for (Resource entry : this.classPath) { if (entry.getFilename().toLowerCase().endsWith(".jar")) { jarFile = new JarFile(entry.getFile()); ZipEntry ze = jarFile.getEntry(classNamePath); if (ze != null) { is = jarFile.getInputStream(ze); break; } else { jarFile.close(); } } else { Resource classFile = entry.createRelative(classNamePath); if (classFile.exists()) { is = classFile.getInputStream(); break; } } } if (is != null) { try { fileBytes = IOUtils.toByteArray(is); is.close(); } finally { if (jarFile != null) { jarFile.close(); } } } } catch (IOException e) { throw new ClassNotFoundException(e.getMessage(), e); } if (name.contains(".")) { String packageName = name.substring(0, name.lastIndexOf('.')); if (getPackage(packageName) == null) { definePackage(packageName, "", "", "", "", "", "", null); } } Class<?> ret; if (fileBytes == null) { ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader); } else { ret = defineClass(name, fileBytes, 0, fileBytes.length); } if (ret == null) { throw new ClassNotFoundException( "Couldn't find class " + name + " in expected classpath: " + this.classPath); } return ret; }
From source file:com.wavemaker.common.util.ThrowawayFileClassLoader.java
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { if (this.classPath == null) { throw new ClassNotFoundException("invalid search root: " + this.classPath); } else if (name == null) { throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage()); }// w ww.j a v a2s . c o m String classNamePath = name.replace('.', '/') + ".class"; byte[] fileBytes = null; try { InputStream is = null; JarFile jarFile = null; for (Resource entry : this.classPath) { if (entry.getFilename().toLowerCase().endsWith(".jar")) { jarFile = new JarFile(entry.getFile()); ZipEntry ze = jarFile.getEntry(classNamePath); if (ze != null) { is = jarFile.getInputStream(ze); break; } else { jarFile.close(); } } else { Resource classFile = entry.createRelative(classNamePath); if (classFile.exists()) { is = classFile.getInputStream(); break; } } } if (is != null) { try { fileBytes = IOUtils.toByteArray(is); is.close(); } finally { if (jarFile != null) { jarFile.close(); } } } } catch (IOException e) { throw new ClassNotFoundException(e.getMessage(), e); } if (name.contains(".")) { String packageName = name.substring(0, name.lastIndexOf('.')); if (getPackage(packageName) == null) { definePackage(packageName, "", "", "", "", "", "", null); } } Class<?> ret; if (fileBytes == null) { ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader); } else { ret = defineClass(name, fileBytes, 0, fileBytes.length); } if (ret == null) { throw new ClassNotFoundException( "Couldn't find class " + name + " in expected classpath: " + this.classPath); } return ret; }
From source file:com.haulmont.cuba.desktop.theme.impl.DesktopThemeLoaderImpl.java
private void includeThemeFile(DesktopThemeImpl theme, Element element, Resource resource) throws IOException { String fileName = element.attributeValue("file"); if (StringUtils.isEmpty(fileName)) { log.error("Missing 'file' attribute to include"); return;//w ww. j av a 2 s . co m } Resource relativeResource = resource.createRelative(fileName); if (relativeResource.exists()) { log.info("Including theme file " + relativeResource.getURL()); loadThemeFromXml(theme, relativeResource); } else { log.error("Resource " + fileName + " not found, ignore it"); } }
From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java
protected static Resource getDefaultWaveMakerHome() { Resource userHome = null; if (SystemUtils.IS_OS_WINDOWS) { String userProfileEnvVar = System.getenv("USERPROFILE"); if (StringUtils.hasText(userProfileEnvVar)) { userProfileEnvVar = userProfileEnvVar.endsWith("/") ? userProfileEnvVar : userProfileEnvVar + "/"; userHome = new FileSystemResource(System.getenv("USERPROFILE")); }// ww w. j av a 2s .c o m } if (userHome == null) { String userHomeProp = System.getProperty("user.home"); userHomeProp = userHomeProp.endsWith("/") ? userHomeProp : userHomeProp + "/"; userHome = new FileSystemResource(userHomeProp); } String osVersionStr = System.getProperty("os.version"); if (osVersionStr.contains(".")) { String sub = osVersionStr.substring(osVersionStr.indexOf(".") + 1); if (sub.contains(".")) { osVersionStr = osVersionStr.substring(0, osVersionStr.indexOf('.', osVersionStr.indexOf('.') + 1)); } } try { if (SystemUtils.IS_OS_WINDOWS) { userHome = new FileSystemResource( javax.swing.filechooser.FileSystemView.getFileSystemView().getDefaultDirectory()); } else if (SystemUtils.IS_OS_MAC) { userHome = userHome.createRelative("Documents/"); } if (!userHome.exists()) { throw new WMRuntimeException(MessageResource.PROJECT_USERHOMEDNE, userHome); } Resource wmHome = userHome.createRelative(WAVEMAKER_HOME); if (!wmHome.exists()) { wmHome.getFile().mkdir(); } return wmHome; } catch (IOException ex) { throw new WMRuntimeException(ex); } }
From source file:fr.acxio.tools.agia.tasks.FilesOperationTaskletTest.java
@Test public void testExecuteCopyEmptySource() throws Exception { FilesOperationTasklet aTasklet = new FilesOperationTasklet(); ResourcesFactory aSourceFactory = mock(ResourcesFactory.class); when(aSourceFactory.getResources(anyMapOf(Object.class, Object.class))).thenReturn(new Resource[] {}); ResourceFactory aDestinationFactory = mock(ResourceFactory.class); Resource aDestResource = mock(Resource.class); when(aDestResource.getFile()).thenReturn(new File("target/CP-input.csv")); when(aDestResource.exists()).thenReturn(false); Resource aRelativeResource = mock(Resource.class); when(aRelativeResource.getFile()).thenReturn(new File("target")); when(aDestResource.createRelative("/.")).thenReturn(aRelativeResource); when(aDestinationFactory.getResource(anyMapOf(Object.class, Object.class))).thenReturn(aDestResource); assertFalse(aDestResource.getFile().exists()); aTasklet.setSourceFactory(aSourceFactory); aTasklet.setDestinationFactory(aDestinationFactory); aTasklet.setOperation(Operation.COPY); aTasklet.afterPropertiesSet();/* w ww . j a v a 2 s. co m*/ assertEquals(RepeatStatus.FINISHED, aTasklet.execute(null, null)); assertFalse(aDestResource.getFile().exists()); }