Example usage for org.springframework.core.io Resource getFile

List of usage examples for org.springframework.core.io Resource getFile

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getFile.

Prototype

File getFile() throws IOException;

Source Link

Document

Return a File handle for this resource.

Usage

From source file:com.edgenius.core.util.FileUtil.java

/**
 * /*from w  w w  .ja  va  2s  . c o m*/
 * @param location
 * @return
 * @throws IOException 
 */
public static InputStream getFileInputStream(String location) throws IOException {
    //Don't user DefaultResourceLoader directly, as test it try to find host "c" if using method resource.getInputStream()
    // while location is "file://c:/var/test" etc.

    DefaultResourceLoader loader = new DefaultResourceLoader();
    Resource res = loader.getResource(location);
    if (ResourceUtils.isJarURL(res.getURL())) {
        //it is in jar file, we just assume it won't be changed in runtime, so below method is safe.
        try {
            return res.getInputStream();
        } catch (IOException e) {
            throw (e);
        }
    } else {
        //in Tomcat, the classLoader cache the input stream even using thread scope classloader, but it is still failed
        //if the reload in same thread. For example, DataRoot class save and reload in same thread when install.
        //So, we assume if the file is not inside jar file, we will always reload the file into a new InputStream from file system.

        //if it is not jar resource, then try to refresh the input stream by file system
        return new FileInputStream(res.getFile());
    }
}

From source file:org.jasig.ssp.util.importer.job.tasklet.BatchFinalizer.java

private File createDirectory(Resource directory) throws Exception {
    File dir = directory.getFile();
    if (!dir.exists())
        if (!dir.mkdirs())
            throw new Exception("Archive directory not created mkdirs failed. Attempted location: {}"
                    + dir.getAbsolutePath());
    return dir;//from  ww  w  . j  a va2s. c om
}

From source file:fr.acxio.tools.agia.io.AbstractFileOperations.java

protected void moveFile(Resource sOriginFile, Resource sDestinationFile) throws IOException {
    File aOrigineFile = sOriginFile.getFile();
    if (aOrigineFile.isFile()) {
        if (!isDirectory(sDestinationFile)) {
            FileUtils.moveFile(aOrigineFile, sDestinationFile.getFile());
        } else {/* w w  w  . ja  va  2  s . com*/
            FileUtils.moveFileToDirectory(aOrigineFile, sDestinationFile.getFile(), true);
        }
    } else {
        FileUtils.moveDirectoryToDirectory(aOrigineFile, sDestinationFile.getFile(), true);
    }
}

From source file:com.predic8.membrane.core.config.spring.TrackingFileSystemXmlApplicationContext.java

@Override
public Resource getResource(String location) {
    final Resource r = super.getResource(location);
    try {/* w  w w . j  a v a2  s.  co  m*/
        files.add(r.getFile());
    } catch (IOException e) {
        log.debug(e);
    }
    return new Resource() {
        Resource r2 = r;

        public boolean exists() {
            return r2.exists();
        }

        public InputStream getInputStream() throws IOException {
            return r2.getInputStream();
        }

        public boolean isReadable() {
            return r2.isReadable();
        }

        public boolean isOpen() {
            return r2.isOpen();
        }

        public URL getURL() throws IOException {
            return r2.getURL();
        }

        public URI getURI() throws IOException {
            return r2.getURI();
        }

        public File getFile() throws IOException {
            return r2.getFile();
        }

        public long lastModified() throws IOException {
            return r2.lastModified();
        }

        public Resource createRelative(String relativePath) throws IOException {
            Resource r = r2.createRelative(relativePath);
            files.add(r.getFile());
            return r;
        }

        public String getFilename() {
            return r2.getFilename();
        }

        public String getDescription() {
            return r2.getDescription();
        }

        public long contentLength() throws IOException {
            return r2.contentLength();
        }

        @Override
        public String toString() {
            return r2.toString();
        }
    };
}

From source file:fr.acxio.tools.agia.io.AbstractFileOperations.java

protected void copyFile(Resource sOriginFile, Resource sDestinationFile) throws IOException {
    File aOrigineFile = sOriginFile.getFile();
    if (aOrigineFile.isFile()) {
        if (!isDirectory(sDestinationFile)) {
            FileUtils.copyFile(aOrigineFile, sDestinationFile.getFile(), preserveAttributes);
        } else {//from  w  w w. j a v  a  2s.c  om
            FileUtils.copyFileToDirectory(aOrigineFile, sDestinationFile.getFile(), preserveAttributes);
        }
    } else {
        if (recursive) {
            FileUtils.copyDirectory(aOrigineFile, sDestinationFile.getFile(), preserveAttributes);
        } else {
            FileUtils.copyDirectory(aOrigineFile, sDestinationFile.getFile(), FileFileFilter.FILE,
                    preserveAttributes);
        }
    }
}

From source file:de.alpharogroup.duplicate.files.panels.progressbar.ProgressbarPanel.java

/**
 * Inits the components./*w w  w. j a va 2s .c  o m*/
 */
private void initComponents() {

    prgrBrTask = new javax.swing.JProgressBar();
    btnCancel = new javax.swing.JButton();
    lblInfo = new javax.swing.JLabel();
    ApplicationContext ctx = SpringApplicationContext.getInstance().getApplicationContext();
    Resource resource = ctx.getResource("classpath:images/nico.gif");

    File imageFile = null;
    try {
        imageFile = resource.getFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedImage image = null;
    try {
        image = javax.imageio.ImageIO.read(imageFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    pnlIconPanel = new ImagePanel(image);

    btnCancel.setText("Cancel");
    lblInfo.setText("Info");

}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testValidProcessControlFile() throws IOException {
    Resource ctlFileResource = new ClassPathResource(controlFileName);
    File ctlFile = ctlFileResource.getFile();

    ValidationController vc = Mockito.spy(validationController);

    vc.processControlFile(ctlFile);//from   ww  w  . ja  v  a 2s.  com

    Mockito.verify(vc).processValidators(Mockito.any(BatchJob.class));
}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testProcessZip() throws IOException, NoSuchFieldException, IllegalAccessException {
    ZipFileHandler handler = Mockito.mock(ZipFileHandler.class);

    ErrorReport er = Mockito.mock(ErrorReport.class);
    Field errorReport = validationController.getClass().getDeclaredField("errorReport");
    errorReport.setAccessible(true);/*w  w w  .j a  v  a2  s.c  o  m*/
    errorReport.set(null, er);

    Resource zipFileResource = new ClassPathResource(zipFileName);
    File zipFile = zipFileResource.getFile();

    ValidationController vc = Mockito.spy(validationController);
    Mockito.doNothing().when(vc).processControlFile((File) Mockito.any());
    Mockito.doReturn(zipFile).when(handler).handle(zipFile, er);

    vc.setZipFileHandler(handler);
    vc.processZip(zipFile);
    Mockito.verify(handler, Mockito.atLeastOnce()).handle(zipFile, er);
}

From source file:com.laxser.blitz.lama.core.LamaDaoProcessor.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (logger.isInfoEnabled()) {
        logger.info("[jade] starting ...");
    }//  ww  w .  j av a2 s .c om
    final List<ResourceRef> resources;
    try {
        resources = BlitzScanner.getInstance().getJarOrClassesFolderResources();
    } catch (IOException e) {
        throw new ApplicationContextException("error on getJarResources/getClassesFolderResources", e);
    }
    List<String> urls = new LinkedList<String>();
    for (ResourceRef resourceInfo : resources) {
        if (resourceInfo.hasModifier("dao") || resourceInfo.hasModifier("DAO")) {
            try {
                Resource resource = resourceInfo.getResource();
                File resourceFile = resource.getFile();
                if (resourceFile.isFile()) {
                    urls.add("jar:file:" + resourceFile.toURI().getPath() + ResourceUtils.JAR_URL_SEPARATOR);
                } else if (resourceFile.isDirectory()) {
                    urls.add(resourceFile.toURI().toString());
                }
            } catch (IOException e) {
                throw new ApplicationContextException("error on resource.getFile", e);
            }
        }
    }

    if (logger.isInfoEnabled()) {
        logger.info("[jade] found " + urls.size() + " jade urls: " + urls);
    }
    if (urls.size() > 0) {
        LamaDaoComponentProvider provider = new LamaDaoComponentProvider(true);
        if (filters != null) {
            for (TypeFilter excludeFilter : filters) {
                provider.addExcludeFilter(excludeFilter);
            }
        }

        final DataAccessProvider dataAccessProvider = createJdbcTemplateDataAccessProvider();

        Set<String> daoClassNames = new HashSet<String>();

        for (String url : urls) {
            if (logger.isInfoEnabled()) {
                logger.info("[jade] call 'jade/find'");
            }
            Set<BeanDefinition> dfs = provider.findCandidateComponents(url);
            if (logger.isInfoEnabled()) {
                logger.info("[jade] found " + dfs.size()//
                        + " beanDefinition from '" + url + "'");
            }
            for (BeanDefinition beanDefinition : dfs) {
                String daoClassName = beanDefinition.getBeanClassName();

                if (daoClassNames.contains(daoClassName)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("[jade] ignored replicated jade dao class: " + daoClassName + "  [" + url
                                + "]");
                    }
                    continue;
                }
                daoClassNames.add(daoClassName);

                MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
                propertyValues.addPropertyValue("dataAccessProvider", dataAccessProvider);
                propertyValues.addPropertyValue("daoClass", daoClassName);
                ScannedGenericBeanDefinition scannedBeanDefinition = (ScannedGenericBeanDefinition) beanDefinition;
                scannedBeanDefinition.setPropertyValues(propertyValues);
                scannedBeanDefinition.setBeanClass(LamaDaoFactoryBean.class);

                DefaultListableBeanFactory defaultBeanFactory = (DefaultListableBeanFactory) beanFactory;
                defaultBeanFactory.registerBeanDefinition(daoClassName, beanDefinition);

                if (logger.isDebugEnabled()) {
                    logger.debug("[jade] register jade dao bean: " + daoClassName);
                }
            }
        }
    }
    if (logger.isInfoEnabled()) {
        logger.info("[jade] exits");
    }
}

From source file:org.slc.sli.ingestion.tool.ValidationControllerTest.java

@Test
public void testInvalidProcessControlFile() throws IOException, SecurityException, NoSuchFieldException,
        IllegalArgumentException, IllegalAccessException {
    Resource ctlFileResource = new ClassPathResource(invalidControlFile);
    File ctlFile = ctlFileResource.getFile();

    Logger log = Mockito.mock(Logger.class);
    Field logField = validationController.getClass().getDeclaredField("logger");
    logField.setAccessible(true);/*from   w w  w .j  a v a  2s  . c  om*/
    logField.set(null, log);

    validationController.processControlFile(ctlFile);

    Mockito.verify(log).error(Mockito.anyString());
}