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:org.opennms.reporting.availability.render.HTMLReportRenderer.java

/** {@inheritDoc} */
@Override/*  www  .  j  ava2 s  . c om*/
public void render(final InputStream inputStream, final OutputStream outputStream, final Resource xsltResource)
        throws ReportRenderException {
    try {
        Logging.withPrefix(LOG4J_CATEGORY, new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                LOG.debug("Rendering InputStream with XSL File {} to OutputStream",
                        xsltResource.getDescription());

                FileInputStream xslt = null;
                Reader xsl = null;
                Reader xml = null;
                try {
                    xslt = new FileInputStream(xsltResource.getFile());
                    xsl = new InputStreamReader(xslt, "UTF-8");
                    xml = new InputStreamReader(inputStream, "UTF-8");
                    render(xml, outputStream, xsl);
                    return null;
                } finally {
                    IOUtils.closeQuietly(xml);
                    IOUtils.closeQuietly(xsl);
                    IOUtils.closeQuietly(xslt);
                }
            }
        });
    } catch (final Exception e) {
        if (e instanceof ReportRenderException)
            throw (ReportRenderException) e;
        throw new ReportRenderException(e);
    }
}

From source file:org.opennms.reporting.availability.render.HTMLReportRenderer.java

/** {@inheritDoc} */
@Override// w w w.  ja v a2  s  .  co  m
public void render(final String inputFileName, final String outputFileName, final Resource xsltResource)
        throws ReportRenderException {
    try {
        Logging.withPrefix(LOG4J_CATEGORY, new Callable<Void>() {
            @Override
            public Void call() throws Exception {

                LOG.debug("Rendering {} with XSL File {} to {} with base directory of {}", m_baseDir,
                        inputFileName, xsltResource.getDescription(), outputFileName);

                FileInputStream in = null, xslt = null;
                FileOutputStream out = null;
                Reader xsl = null, xml = null;
                try {

                    xslt = new FileInputStream(xsltResource.getFile());
                    xsl = new InputStreamReader(xslt, "UTF-8");
                    in = new FileInputStream(m_baseDir + "/" + inputFileName);
                    xml = new InputStreamReader(in, "UTF-8");

                    out = new FileOutputStream(new File(m_baseDir + "/" + outputFileName));

                    render(xml, out, xsl);
                    return null;
                } finally {
                    IOUtils.closeQuietly(xml);
                    IOUtils.closeQuietly(xsl);
                    IOUtils.closeQuietly(in);
                    IOUtils.closeQuietly(out);
                    IOUtils.closeQuietly(xslt);
                }
            }
        });
    } catch (final Exception e) {
        if (e instanceof ReportRenderException)
            throw (ReportRenderException) e;
        throw new ReportRenderException(e);
    }
}

From source file:org.opennms.reporting.availability.render.PDFReportRenderer.java

/** {@inheritDoc} */
@Override/*from  w  w w. j  a  v  a  2  s.c  o  m*/
public void render(final String inputFileName, final OutputStream outputStream, final Resource xsltResource)
        throws ReportRenderException {
    try {
        Logging.withPrefix(LOG4J_CATEGORY, new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                FileInputStream in = null, xslt = null;
                Reader xsl = null, xml = null;

                try {
                    LOG.debug("Rendering {} with XSL File {} to OutputStream", inputFileName,
                            xsltResource.getDescription());

                    in = new FileInputStream(xsltResource.getFile());
                    xsl = new InputStreamReader(in, "UTF-8");
                    xslt = new FileInputStream(inputFileName);
                    xml = new InputStreamReader(xslt, "UTF-8");

                    render(xml, outputStream, xsl);
                    return null;
                } finally {
                    IOUtils.closeQuietly(xml);
                    IOUtils.closeQuietly(xslt);
                    IOUtils.closeQuietly(xsl);
                    IOUtils.closeQuietly(in);
                }
            }
        });
    } catch (final Exception e) {
        if (e instanceof ReportRenderException)
            throw (ReportRenderException) e;
        throw new ReportRenderException(e);
    }
}

From source file:org.opennms.reporting.availability.render.PDFReportRenderer.java

/** {@inheritDoc} */
@Override//  w  ww. j  a va 2s.co m
public void render(final InputStream inputStream, final OutputStream outputStream, final Resource xsltResource)
        throws ReportRenderException {
    try {
        Logging.withPrefix(LOG4J_CATEGORY, new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                LOG.debug("Rendering InputStream with XSL File {} to OutputStream",
                        xsltResource.getDescription());

                FileInputStream xslt = null;
                Reader xsl = null, xml = null;
                try {
                    xslt = new FileInputStream(xsltResource.getFile());
                    xsl = new InputStreamReader(xslt, "UTF-8");
                    xml = new InputStreamReader(inputStream, "UTF-8");

                    render(xml, outputStream, xsl);
                    return null;
                } finally {
                    IOUtils.closeQuietly(xml);
                }
            }
        });
    } catch (final Exception e) {
        if (e instanceof ReportRenderException)
            throw (ReportRenderException) e;
        throw new ReportRenderException(e);
    }
}

From source file:org.opennms.reporting.availability.render.PDFReportRenderer.java

/** {@inheritDoc} */
@Override/*  w w w. j  a  v  a 2 s . c o  m*/
public void render(final String inputFileName, final String outputFileName, final Resource xsltResource)
        throws ReportRenderException {
    try {
        Logging.withPrefix(LOG4J_CATEGORY, new Callable<Void>() {
            @Override
            public Void call() throws Exception {

                LOG.debug("Rendering {} with XSL File {} to {} with base directory of {}", m_baseDir,
                        inputFileName, xsltResource.getDescription(), outputFileName);

                FileInputStream in = null, xslt = null;
                FileOutputStream out = null;
                Reader xsl = null, xml = null;
                try {

                    xslt = new FileInputStream(xsltResource.getFile());
                    xsl = new InputStreamReader(xslt, "UTF-8");
                    in = new FileInputStream(m_baseDir + "/" + inputFileName);
                    xml = new InputStreamReader(in, "UTF-8");

                    out = new FileOutputStream(new File(m_baseDir + "/" + outputFileName));

                    render(xml, out, xsl);
                    return null;
                } finally {
                    IOUtils.closeQuietly(out);
                    IOUtils.closeQuietly(xml);
                    IOUtils.closeQuietly(in);
                    IOUtils.closeQuietly(xsl);
                    IOUtils.closeQuietly(xslt);
                }
            }
        });
    } catch (final Exception e) {
        if (e instanceof ReportRenderException)
            throw (ReportRenderException) e;
        throw new ReportRenderException(e);
    }
}

From source file:org.opens.tanaguru.scenarioloader.ScenarioLoaderFactoryImpl.java

public void setJsScriptMap(Map<String, String> jsScriptMap) {
    if (this.jsScriptMap == null) {
        this.jsScriptMap = new HashMap<String, String>();
    }/* w  w w . j av a 2s.c om*/
    for (Map.Entry<String, String> entry : jsScriptMap.entrySet()) {
        Resource resource = new ClassPathResource(entry.getValue());
        try {
            this.jsScriptMap.put(entry.getKey(), FileUtils.readFileToString(resource.getFile()));
        } catch (IOException ex) {
            Logger.getLogger(this.getClass()).warn(ex);
        }
    }
}

From source file:org.pssframework.dao.SqlSessionFactoryFactoryBean.java

private SqlSessionFactory createSqlSessionFactory() throws IOException {
    Reader reader = new InputStreamReader(getConfigLocation().getInputStream());
    try {/*from w w w .j  a v  a2 s.  co m*/
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        Configuration conf = sqlSessionFactory.getConfiguration();
        if (dataSource != null) {
            DataSource dataSourceToUse = this.dataSource;
            if (this.useTransactionAwareDataSource
                    && !(this.dataSource instanceof TransactionAwareDataSourceProxy)) {
                dataSourceToUse = new TransactionAwareDataSourceProxy(this.dataSource);
            }

            conf.setEnvironment(
                    new Environment("development", new ManagedTransactionFactory(), dataSourceToUse));
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(conf);
        }

        if (mapperLocations != null) {
            Map<String, XNode> sqlFragments = new HashMap<String, XNode>();
            for (Resource r : mapperLocations) {
                logger.info("Loading iBatis3 mapper xml from file[" + r.getFile().getAbsolutePath() + "]");

                Reader mapperReader = new InputStreamReader(r.getInputStream());
                try {
                    XMLMapperBuilder mapperBuilder = new XMLMapperBuilder(mapperReader, conf,
                            r.getFile().getAbsolutePath(), sqlFragments);
                    mapperBuilder.parse();
                } finally {
                    mapperReader.close();
                }
            }
        }
        return sqlSessionFactory;
    } finally {
        reader.close();
    }
}

From source file:org.red5.server.persistence.FilePersistence.java

/**
  * Setter for file path./*from w  w  w  . j av a 2s  .c o  m*/
  *
  * @param path  New path
  */
public void setPath(String path) {
    Resource rootFile = resources.getResource(path);
    try {
        rootDir = rootFile.getFile().getAbsolutePath();
        this.path = path;
    } catch (IOException err) {
        log.error("I/O exception thrown when setting file path to " + path);
        throw (new RuntimeException(err));
    }
}

From source file:org.red5.server.persistence.FilePersistence.java

/**
 * Load resource with given name and attaches to persistable object
 * @param name             Resource name
 * @param object           Object to attach to
 * @return                 Persistable object
 *//*w  w  w .j  a v a  2  s .com*/
private IPersistable doLoad(String name, IPersistable object) {
    IPersistable result = object;
    Resource data = resources.getResource(name);
    if (data == null || !data.exists()) {
        // No such file
        return null;
    }

    FileInputStream input;
    String filename;
    try {
        File fp = data.getFile();
        if (fp.length() == 0) {
            // File is empty
            log.error("The file at " + data.getFilename() + " is empty.");
            return null;
        }

        filename = fp.getAbsolutePath();
        input = new FileInputStream(filename);
    } catch (FileNotFoundException e) {
        log.error("The file at " + data.getFilename() + " does not exist.");
        return null;
    } catch (IOException e) {
        log.error("Could not load file from " + data.getFilename() + '.', e);
        return null;
    }

    try {
        ByteBuffer buf = ByteBuffer.allocate(input.available());
        try {
            ServletUtils.copy(input, buf.asOutputStream());
            buf.flip();
            Input in = new Input(buf);
            Deserializer deserializer = new Deserializer();
            String className = (String) deserializer.deserialize(in);
            if (result == null) {
                // We need to create the object first
                try {
                    Class theClass = Class.forName(className);
                    Constructor constructor = null;
                    try {
                        // Try to create object by calling constructor with Input stream as
                        // parameter.
                        for (Class interfaceClass : in.getClass().getInterfaces()) {
                            constructor = theClass.getConstructor(new Class[] { interfaceClass });
                            if (constructor != null) {
                                break;
                            }
                        }
                        if (constructor == null) {
                            throw new NoSuchMethodException();
                        }

                        result = (IPersistable) constructor.newInstance(in);
                    } catch (NoSuchMethodException err) {
                        // No valid constructor found, use empty
                        // constructor.
                        result = (IPersistable) theClass.newInstance();
                        result.deserialize(in);
                    } catch (InvocationTargetException err) {
                        // Error while invoking found constructor, use empty
                        // constructor.
                        result = (IPersistable) theClass.newInstance();
                        result.deserialize(in);
                    }
                } catch (ClassNotFoundException cnfe) {
                    log.error("Unknown class " + className);
                    return null;
                } catch (IllegalAccessException iae) {
                    log.error("Illegal access.", iae);
                    return null;
                } catch (InstantiationException ie) {
                    log.error("Could not instantiate class " + className);
                    return null;
                }

                // Set object's properties
                result.setName(getObjectName(name));
                result.setPath(getObjectPath(name, result.getName()));
            } else {
                // Initialize existing object
                String resultClass = result.getClass().getName();
                if (!resultClass.equals(className)) {
                    log.error("The classes differ: " + resultClass + " != " + className);
                    return null;
                }

                result.deserialize(in);
            }
        } finally {
            buf.release();
            buf = null;
        }
        if (result.getStore() != this) {
            result.setStore(this);
        }
        super.save(result);
        if (log.isDebugEnabled()) {
            log.debug("Loaded persistent object " + result + " from " + filename);
        }
    } catch (IOException e) {
        log.error("Could not load file at " + filename);
        return null;
    }

    return result;
}

From source file:org.red5.server.persistence.FilePersistence.java

/**
 * Save persistable object//from   ww  w.ja v  a 2 s .  co m
 * @param object           Persistable object
 * @return                 <code>true</code> on success, <code>false</code> otherwise
 */
private boolean saveObject(IPersistable object) {
    String path = getObjectFilepath(object, true);
    Resource resPath = resources.getResource(path);
    File file;
    try {
        file = resPath.getFile();
    } catch (IOException err) {
        log.error("Could not create resource file for path " + path, err);
        return false;
    }

    if (!file.isDirectory() && !file.mkdirs()) {
        log.error("Could not create directory " + file.getAbsolutePath());
        return false;
    }

    String filename = getObjectFilename(object);
    Resource resFile = resources.getResource(filename);
    try {
        ByteBuffer buf = ByteBuffer.allocate(1024);
        try {
            buf.setAutoExpand(true);
            Output out = new Output(buf);
            out.writeString(object.getClass().getName());
            object.serialize(out);
            buf.flip();

            FileOutputStream output = new FileOutputStream(resFile.getFile().getAbsolutePath());
            ServletUtils.copy(buf.asInputStream(), output);
            output.close();
        } finally {
            buf.release();
            buf = null;
        }
        if (log.isDebugEnabled()) {
            log.debug("Stored persistent object " + object + " at " + filename);
        }
        return true;
    } catch (IOException e) {
        log.error("Could not create / write file " + filename);
        return false;
    }
}