Example usage for javax.servlet ServletContext getResourceAsStream

List of usage examples for javax.servlet ServletContext getResourceAsStream

Introduction

In this page you can find the example usage for javax.servlet ServletContext getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String path);

Source Link

Document

Returns the resource located at the named path as an InputStream object.

Usage

From source file:iox.refused.util.TextUtil.java

public static String loadfile(ServletContext context, String filename) {
    InputStream is = context.getResourceAsStream(filename);
    if (is == null) {
        logger.log(Level.SEVERE, "<loadfile> unable to load " + filename);
        return "";
    }//from   ww  w.ja v  a2  s .  com
    String data;
    try {
        data = IOUtils.toString(is, CS);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "<loadfile> unable to load " + filename, e);
        data = "";
    }
    IOUtils.closeQuietly(is);
    return data;
}

From source file:org.cruxframework.crux.core.server.dispatch.ServicesCompileMap.java

/**
 * @param context/*from  w  w w .j  a  v  a 2 s. c o  m*/
 */
public static boolean initialize(ServletContext context) {
    Properties properties = new Properties();
    try {
        if (context != null) {
            properties.load(context.getResourceAsStream("/META-INF/crux-remote"));
        } else {
            properties.load(ServicesCompileMap.class.getResourceAsStream("/META-INF/crux-remote"));
        }
        Enumeration<?> serviceNames = (Enumeration<?>) properties.propertyNames();
        while (serviceNames.hasMoreElements()) {
            String serviceName = (String) serviceNames.nextElement();
            remoteServices.put(serviceName, properties.getProperty(serviceName));
        }
        return true;
    } catch (Exception e) {
        logger.info("Error initializing services with service maps strategy...");
    }
    return false;
}

From source file:org.cruxframework.crux.core.server.rest.core.registry.RestServicesCompileMap.java

/**
 * @param context//w  ww .j  a va 2  s  . com
 */
public static boolean initialize(ServletContext context) {
    Properties properties = new Properties();
    try {
        if (context != null) {
            properties.load(context.getResourceAsStream("/META-INF/crux-rest"));
        } else {
            properties.load(RestServicesCompileMap.class.getResourceAsStream("/META-INF/crux-rest"));
        }
        Enumeration<?> serviceNames = (Enumeration<?>) properties.propertyNames();
        while (serviceNames.hasMoreElements()) {
            String serviceName = (String) serviceNames.nextElement();
            remoteServices.put(serviceName, properties.getProperty(serviceName));
        }
        return true;
    } catch (Exception e) {
        logger.info("Error initializing REST services with service maps strategy...");
    }
    return false;
}

From source file:it.attocchi.web.config.SoftwareProperties.java

/**
 * Inizializza i Valori della BL e della PersistenceUnit
 *//*from www  .  ja  va  2 s.co m*/
public static void init(ServletContext servletContext) {

    if (jpaDbProps == null) {

        InputStream fileProperties = servletContext.getResourceAsStream(WEB_INF_CONFIG_PROPERTIES);
        if (fileProperties != null) {

            logger.warn("Loading " + WEB_INF_CONFIG_PROPERTIES);
            properties = PropertiesUtils.getProperties(fileProperties);

            if (properties != null) {

                // RapportoServerBL.connString =
                // properties.getProperty("connString");
                // RapportoServerBL.driverClass =
                // properties.getProperty("driverClass");
                // RapportoServerBL.userName =
                // properties.getProperty("userName");
                // RapportoServerBL.password =
                // properties.getProperty("password");

                connString = StringUtils.trim(properties.getProperty(PROPERTY_connString));
                driverClass = StringUtils.trim(properties.getProperty(PROPERTY_driverClass));
                userName = StringUtils.trim(properties.getProperty(PROPERTY_userName));
                password = StringUtils.trim(properties.getProperty(PROPERTY_password));

                jpaDbProps = new HashMap<String, String>();

                jpaDbProps.put("javax.persistence.jdbc.url", connString);
                jpaDbProps.put("javax.persistence.jdbc.driver", driverClass);
                jpaDbProps.put("javax.persistence.jdbc.user", userName);
                jpaDbProps.put("javax.persistence.jdbc.password", password);

                /*
                 * Se uno dei Parametri da File e' nullo annullo tutti gli
                 * oggetti
                 */
                if (StringUtils.isEmpty(connString) || StringUtils.isEmpty(driverClass)) {
                    properties = null;
                    jpaDbProps = null;

                    logger.warn("SoftwareConfig loaded but Empty Database Configuration");
                }
            }
        }
    } else {
        logger.warn("SoftwareConfig already inizialized");
    }
}

From source file:org.brekka.commons.maven.ModuleVersion.java

/**
 * @param groupId the id of the group/*from www  . j  a v  a  2  s.co  m*/
 * @param artifactId the id of the artifact
 * @param servletContext the servlet context of the application containing the module to resolve the version for
 * @param notFoundString the string to return should the module information not be resolvable
 */
public static ModuleVersion getVersion(String groupId, String artifactId, ServletContext servletContext,
        NotFoundVersion noFoundVersion) {
    String pomPropsPath = preparePath(groupId, artifactId);
    String version = null;
    try {
        version = getVersion(servletContext.getResourceAsStream(pomPropsPath));
    } catch (IOException e) {
        // Something went wrong
        if (log.isWarnEnabled()) {
            log.warn(String.format(
                    "Failed to determine version of Maven module with groupId '%s' and artifactId '%s' from classpath",
                    groupId, artifactId));
        }
    }
    if (version == null) {
        version = noFoundVersion.getVersion();
    }
    return new ModuleVersion(groupId, artifactId, version);
}

From source file:org.rhq.helpers.rtfilter.util.ServletUtility.java

/**
 * Try to read the contextRoot from WEB-INF/jboss-web.xml
 *
 * @param  servletContext/*from  www  .  j av a2  s .c om*/
 *
 * @return
 */
private static String getContextRootFromJbossWebXml(ServletContext servletContext) {
    String ctxName = null;
    try {
        String path = SEPARATOR + "WEB-INF" + SEPARATOR + "jboss-web.xml";
        InputStream jbossWeb = servletContext.getResourceAsStream(path);
        if (jbossWeb != null) {
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = db.parse(jbossWeb);
            Element root = doc.getDocumentElement(); // <jboss-web>
            NodeList children = root.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node node = children.item(i);
                if (node instanceof Element) {
                    String name = node.getNodeName();
                    if (name.equals("context-root")) {
                        Node textNode = node.getFirstChild();
                        if (textNode != null) {
                            ctxName = textNode.getNodeValue();
                            LOG.debug("CtxName from jboss-web.xml: " + ctxName);
                        }

                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        LOG.debug("Can not get contextRoot from jboss-web.xml: " + e);
    }

    return ctxName;
}

From source file:org.megatome.frame2.validator.CommonsValidatorWrapper.java

public static void load(ServletContext context) throws CommonsValidatorException {

    // validatorResources = new ValidatorResources();

    String filePath = cvFilePath + Globals.FORWARD_SLASH + cvMappingsFile;
    InputStream in = context.getResourceAsStream(filePath);
    if (in == null) {
        // validatorResources = null;
        throw new CommonsValidatorException("invalid filePath: " + filePath); //$NON-NLS-1$
    }/*from w w w.j  av  a 2 s.  c o m*/
    try {
        validatorResources = new ValidatorResources(in);
    } catch (IOException e) {
        validatorResources = null;
        throw new CommonsValidatorException(e);
    } catch (SAXException e) {
        validatorResources = null;
        throw new CommonsValidatorException(e);
    }

    /*
     * List<String> fileNames = new ArrayList<String>();
     * fileNames.add(cvRulesFile); fileNames.add(cvMappingsFile);
     * 
     * for (int i = 0; i < fileNames.size(); i++) { String filePath =
     * cvFilePath + Globals.FORWARD_SLASH + fileNames.get(i); InputStream in =
     * context.getResourceAsStream(filePath); if (in == null) {
     * validatorResources = null; throw new
     * CommonsValidatorException("invalid filePath: " + filePath);
     * //$NON-NLS-1$ } try {
     * ValidatorResourcesInitializer.initialize(validatorResources, in); }
     * catch (IOException e) { validatorResources = null; throw new
     * CommonsValidatorException(e); } }
     */

}

From source file:fr.norad.servlet.sample.html.template.ContextUtils.java

public static void servletContextResourceToFile(ServletContext context, String contextPath, String output)
        throws IOException {
    @SuppressWarnings("unchecked")
    Set<String> resources = context.getResourcePaths(contextPath);
    if (resources == null) {
        new File(output + contextPath).mkdirs();
        return;// w  ww  . ja  v  a2 s . c om
    }
    for (String resource : resources) {
        InputStream resourceAsStream = context.getResourceAsStream(resource);
        if (resourceAsStream == null) {
            // this should be a directory
            servletContextResourceToFile(context, resource, output);
        } else { // this is a file
            try {
                File full = new File(output + resource);
                full.getParentFile().mkdirs();
                FileUtils.copyInputStreamToFile(resourceAsStream, full);
            } finally {
                resourceAsStream.close();
            }
        }
    }
}

From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java

/**
 * Processes the filenames listed in the specified .txt file and executes either
 * BSH or SQL//from   ww w . j a  va 2  s . c o  m
 *
 * @param context  the web context to use for finding the file resource
 * @param db       the database connection to use for executing the script against
 * @param baseName The resource filename
 * @throws SQLException Description of the Exception
 */
private static void upgradeTXT(ServletContext context, Connection db, String baseName) throws Exception {
    String yearString = baseName.substring(0, 4) + "/";
    ArrayList<String> files = new ArrayList<String>();
    StringUtils.loadText(
            context.getResourceAsStream("/WEB-INF/database/upgrade/" + yearString + "upgrade_" + baseName),
            files, true);
    LOG.info("Scripts to process: " + files.size());
    for (String thisFile : files) {
        if (thisFile.endsWith(".bsh")) {
            // Try to run a specified bean shell script if found
            upgradeBSH(context, db, thisFile);
        } else if (thisFile.endsWith(".sql")) {
            // Try to run the specified sql file
            upgradeSQL(context, db, thisFile);
        }
    }
}

From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java

/**
 * Iterates through and executes available database upgrade scripts
 *
 * @param db//w  w  w  .  ja v a2  s . co m
 * @param context
 * @return
 * @throws Exception
 */
public static ArrayList<String> performUpgrade(Connection db, ServletContext context) throws Exception {
    ArrayList<String> installLog = new ArrayList<String>();
    // Load the database versions to check...
    ArrayList<String> versionList = UpgradeUtils
            .retrieveDatabaseVersions(context.getResourceAsStream("/WEB-INF/database/database_versions.txt"));
    for (String version : versionList) {
        if (version.length() == 10) {
            install(context, db, version, installLog);
        }
    }
    return installLog;
}