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.ireland.jnetty.webapp.ServletContextImpl.java

/**
 * Maps from a URI to a real path./*from ww w  . j a v  a2s  .c  om*/
 * @see org.springframework.mock.web.MockServletContext
 */
@Override
public String getRealPath(String path) {
    Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
    try {
        return resource.getFile().getAbsolutePath();
    } catch (IOException ex) {
        log("Couldn't determine real path of resource " + resource, ex);
        return null;
    }
}

From source file:annis.administration.AbstractAdminstrationDao.java

@SuppressWarnings("unchecked")
private String readSqlFromResource(Resource resource, MapSqlParameterSource args) {
    // XXX: uses raw type, what are the parameters to Map in MapSqlParameterSource?
    Map<String, Object> parameters = args != null ? args.getValues() : new HashMap();

    try (BufferedReader reader = new BufferedReader(
            new InputStreamReader(new FileInputStream(resource.getFile()), "UTF-8"));) {
        StringBuilder sqlBuf = new StringBuilder();

        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            sqlBuf.append(line).append("\n");
        }/*from w ww  .ja  va 2s  . c o  m*/
        String sql = sqlBuf.toString();
        for (Map.Entry<String, Object> placeHolderEntry : parameters.entrySet()) {
            String key = placeHolderEntry.getKey();
            String value = placeHolderEntry.getValue().toString();
            log.debug("substitution for parameter '" + key + "' in SQL script: " + value);
            sql = sql.replaceAll(key, Matcher.quoteReplacement(value));
        }
        return sql;
    } catch (IOException e) {
        log.error("Couldn't read SQL script from resource file.", e);
        throw new FileAccessException("Couldn't read SQL script from resource file.", e);
    }
}

From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java

@Override
public Resource copyRecursive(Resource root, Resource target, List<String> includedPatterns,
        List<String> excludedPatterns) {
    try {/* w  w  w.j a v  a 2s.  com*/
        IOUtils.copy(root.getFile(), target.getFile(), includedPatterns, excludedPatterns);
    } catch (IOException ex) {
        throw new WMRuntimeException(ex);
    }
    return target;
}

From source file:org.jruby.rack.mock.MockServletContext.java

public Set<String> getResourcePaths(String path) {
    String actualPath = (path.endsWith("/") ? path : path + "/");
    Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
    try {//  ww w .  j  a v a  2s .c om
        File file = resource.getFile();
        String[] fileList = file.list();
        if (fileList == null || fileList.length == 0) {
            return null;
        }
        Set<String> resourcePaths = new LinkedHashSet<String>(fileList.length);
        for (String fileEntry : fileList) {
            String resultPath = actualPath + fileEntry;
            if (resource.createRelative(fileEntry).getFile().isDirectory()) {
                resultPath += "/";
            }
            resourcePaths.add(resultPath);
        }
        return resourcePaths;
    } catch (IOException ex) {
        logger.log("WARN: Couldn't get resource paths for " + resource, ex);
        return null;
    }
}

From source file:org.ireland.jnetty.webapp.ServletContextImpl.java

/**
 * Returns an enumeration of all the resources.
 * @see  org.springframework.mock.web.MockServletContext.getResourcePaths(String path)
 *///w  w w.  j a v  a  2 s  .  c om
@Override
public Set<String> getResourcePaths(String path) {
    String actualPath = (path.endsWith("/") ? path : path + "/");
    Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath));
    try {
        File file = resource.getFile();
        String[] fileList = file.list();
        if (ObjectUtils.isEmpty(fileList)) {
            return null;
        }
        Set<String> resourcePaths = new LinkedHashSet<String>(fileList.length);
        for (String fileEntry : fileList) {
            String resultPath = actualPath + fileEntry;
            if (resource.createRelative(fileEntry).getFile().isDirectory()) {
                resultPath += "/";
            }
            resourcePaths.add(resultPath);
        }
        return resourcePaths;
    } catch (IOException ex) {
        log.warn("Couldn't get resource paths for " + resource, ex);
        return null;
    }
}

From source file:podd.util.WebappInitializationUtil.java

private void createDefaultDatastore(Resource resource) {
    if (null == resource)
        return;//from w w w  .  j av  a  2  s .  c  o  m
    try {
        File propertyFile = resource.getFile();
        final FileReader fileReader = new FileReader(propertyFile);
        Properties properties = new Properties();
        properties.load(fileReader);

        // try to load the datastore and if it doesn't exist create a new one
        String ip = properties.get("ip").toString();
        int port = Integer.parseInt(properties.get("port").toString());
        String login = properties.get("login").toString();
        String defaultDirectory = properties.get("defaultDirectory").toString();
        Datastore datastore = datastoreRegistryDao.loadByIP(ip);
        if (null == datastore) {
            datastore = new Datastore(ip, port, login, defaultDirectory);
        } else {
            datastore.setPort(port);
            datastore.setLoginId(login);
            datastore.setDefaultDirectory(defaultDirectory);
        }
        datastore.setAccessMethod(properties.get("accessMethod").toString());
        LOGGER.info("Creating default datastore: " + datastore.getLoginId() + "@" + datastore.getIp() + ":"
                + datastore.getPort());
        datastoreRegistryDao.saveOrUpdate(datastore);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:podd.util.WebappInitialisationUtil.java

private void createDefaultDatastore(Resource resource) {
    if (null == resource)
        return;//from w  w w.j  a va  2 s  .com
    try {
        File propertyFile = resource.getFile();
        final FileReader fileReader = new FileReader(propertyFile);
        Properties properties = new Properties();
        properties.load(fileReader);

        // try to load the datastore and if it doesn't exist create a new one
        String ip = properties.get("ip").toString();
        int port = Integer.parseInt(properties.get("port").toString());
        String login = properties.get("login").toString();
        String defaultDirectory = properties.get("defaultDirectory").toString();
        Datastore datastore = datastoreRegistryDao.loadByIP(ip);
        if (null == datastore) {
            datastore = new Datastore(ip, port, login, defaultDirectory);
        } else {
            datastore.setPort(port);
            datastore.setLoginId(login);
            datastore.setDefaultDirectory(defaultDirectory);
        }
        datastore.setAccessMethod(properties.get("accessMethod").toString());
        logger.info("Creating default datastore: " + datastore.getLoginId() + "@" + datastore.getIp() + ":"
                + datastore.getPort());
        datastoreRegistryDao.saveOrUpdate(datastore);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jpoweredcart.common.mock.servlet.MockServletContext.java

public String getRealPath(String path) {
    Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
    try {//  w w  w  .  j a  va2 s  .  co  m
        return resource.getFile().getAbsolutePath();
    } catch (IOException ex) {
        logger.warn("Couldn't determine real path of resource " + resource, ex);
        return null;
    }
}

From source file:com.wavemaker.tools.project.LocalStudioFileSystem.java

@Override
public List<Resource> listChildren(Resource resource, ResourceFilter filter) {
    List<Resource> children = new ArrayList<Resource>();
    File[] files;/*w w  w . j  a v a2s .c  o m*/
    try {
        files = resource.getFile().listFiles();
    } catch (IOException e) {
        throw new WMRuntimeException(e);
    }
    if (files == null) {
        return children;
    }
    for (File file : files) {
        Resource fileResource = createResource(file.getAbsolutePath() + "/");
        if (filter.accept(fileResource)) {
            children.add(fileResource);
        }
    }
    return children;

}

From source file:com.wavemaker.tools.project.upgrade.six_dot_four.RemoveMySQLHBMCatalogUpgradeTask.java

@Override
public void doUpgrade(Project project, UpgradeInfo upgradeInfo) {
    Folder servicesFolder = project.getRootFolder().getFolder("services");
    // Don't bother if we do not have services
    try {//from www  .  j  a v  a 2s.  c  o  m
        if (servicesFolder.exists()) {
            Resource servicesDir = project.getProjectRoot().createRelative("services/");
            ArrayList<String> mySQLServices = new ArrayList<String>();
            try {
                // Find MySQL Services
                IOFileFilter propFilter = FileFilterUtils.andFileFilter(FileFilterUtils.fileFileFilter(),
                        FileFilterUtils.suffixFileFilter("properties"));
                List<File> propFiles = new UpgradeFileFinder(propFilter).findFiles(servicesDir.getFile());
                Iterator<File> propIt = propFiles.iterator();
                while (propIt.hasNext()) {
                    File propFile = propIt.next();
                    String propContent = FileUtils.readFileToString(propFile);
                    if (propContent.contains(mySQLStr)) {
                        String propFileName = propFile.getName();
                        int index = propFileName.lastIndexOf(".");
                        if (index > 0 && index <= propFileName.length()) {
                            mySQLServices.add(propFileName.substring(0, index));
                        }
                    }
                }
                // Find HBM files in MySQLServices
                Iterator<String> servIt = mySQLServices.iterator();
                while (servIt.hasNext()) {
                    IOFileFilter hbmFilter = FileFilterUtils.andFileFilter(FileFilterUtils.fileFileFilter(),
                            FileFilterUtils.suffixFileFilter("hbm.xml"));
                    String path = servIt.next();
                    Resource mySQLServiceDir = servicesDir
                            .createRelative(path.endsWith("/") ? path : path + "/");
                    List<File> hbmFiles = new UpgradeFileFinder(hbmFilter).findFiles(mySQLServiceDir.getFile());
                    Iterator<File> hbmIt = hbmFiles.iterator();
                    while (hbmIt.hasNext()) {
                        File hbmFile = hbmIt.next();
                        String hbmContent = FileUtils.readFileToString(hbmFile);
                        hbmContent.replaceFirst(catalogStr, replaceStr);
                        FileUtils.writeStringToFile(hbmFile, hbmContent);
                        System.out
                                .println("Project upgrade: Catalog removed from " + hbmFile.getAbsolutePath());
                    }
                }
                if (!mySQLServices.isEmpty()) {
                    upgradeInfo.addMessage("\nMySQL Catalog upgrade task completed. See wm.log for details");
                } else {
                    System.out.println("Project Upgrade: No MySQL Services found");
                }
            } catch (IOException ioe) {
                upgradeInfo
                        .addMessage("\nError removing Catalog from MySQL Services. Check wm.log for details.");
                ioe.printStackTrace();
            }
        } else {
            System.out.println("Project Upgrade: No Services found");
        }
    } catch (IOException e) {
        throw new WMRuntimeException(e);
    }
}