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:org.kuali.student.common.util.ManifestInspector.java

/**
 * Return a Manifest object//from  w w  w . ja  va2 s  . c  om
 */
protected Manifest getManifest(ServletContext servletContext) throws IOException {
    InputStream in = null;
    try {
        in = servletContext.getResourceAsStream(MANIFEST_LOCATION);
        if (in == null) {
            return null;
        } else {
            return new Manifest(in);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        closeQuietly(in);
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.rest.StaticNavigationHandler.java

/**
 * XXX hack: add manual parsing of the main faces-config.xml file navigation cases, to handle hot reload and work
 * around the JSF application cache./*  w  ww  .  j a v  a2  s .  c o  m*/
 * <p>
 * TODO: try to reset and rebuild the app navigation cases by reflection, if it works...
 *
 * @since 5.6
 */
protected void handleHotReloadResources(ServletContext context) {
    InputStream stream = null;
    if (context != null) {
        stream = context.getResourceAsStream("/WEB-INF/faces-config.xml");
    }
    if (stream != null) {
        parse(stream);
    }
}

From source file:csiro.pidsvc.core.Settings.java

private Settings(HttpServlet servlet) throws NullPointerException, IOException {
    // Retrieve manifest.
    if ((_servlet = servlet) != null) {
        ServletConfig config = _servlet.getServletConfig();
        if (config != null) {
            ServletContext application = config.getServletContext();
            _manifest = new Manifest(application.getResourceAsStream("/META-INF/MANIFEST.MF"));
        }/* w w w  . j a  v  a 2s . co m*/
    }

    // Retrieve settings.
    FileInputStream fis = null;
    try {
        InitialContext context = new InitialContext();
        String settingsFile = (String) context.lookup("java:comp/env/" + SETTINGS_OPT);
        fis = new FileInputStream(settingsFile);
        _properties = new PropertyResourceBundle(fis);
    } catch (NamingException ex) {
        _logger.debug("Using default pidsvc.properties file.");
        _properties = ResourceBundle.getBundle("pidsvc");
    } finally {
        if (fis != null)
            fis.close();
    }

    // Get additional system properties.
    _serverProperties.put("serverJavaVersion", System.getProperty("java.version"));
    _serverProperties.put("serverJavaVendor", System.getProperty("java.vendor"));
    _serverProperties.put("javaHome", System.getProperty("java.home"));
    _serverProperties.put("serverOsArch", System.getProperty("os.arch"));
    _serverProperties.put("serverOsName", System.getProperty("os.name"));
    _serverProperties.put("serverOsVersion", System.getProperty("os.version"));
}

From source file:org.nuxeo.runtime.deployment.NuxeoStarter.java

protected void findBundles(ServletContext servletContext) throws IOException {
    InputStream bundlesListStream = servletContext.getResourceAsStream("/WEB-INF/" + NUXEO_BUNDLES_LIST);
    if (bundlesListStream != null) {
        File lib = new File(servletContext.getRealPath("/WEB-INF/lib/"));
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(bundlesListStream))) {
            String bundleName;/*  www.  ja v a  2 s  .  c o m*/
            while ((bundleName = reader.readLine()) != null) {
                bundleFiles.add(new File(lib, bundleName));
            }
        }
    }
    if (bundleFiles.isEmpty()) { // Fallback on directory scan
        File root = new File(servletContext.getRealPath("/"));
        Set<String> ctxpaths = servletContext.getResourcePaths("/WEB-INF/lib/");
        if (ctxpaths != null) {
            for (String ctxpath : ctxpaths) {
                if (!ctxpath.endsWith(".jar")) {
                    continue;
                }
                bundleFiles.add(new File(root, ctxpath));
            }
        }
    }
}

From source file:org.seasar.mayaa.impl.source.ApplicationResourceSourceDescriptor.java

public boolean exists() {
    URL url = getURL();/* ww w.  j av  a 2 s . c  om*/
    if (url != null) {
        return true;
    }
    // GAE for Java _url?null???Stream???
    ServletContext context = (ServletContext) getApplicationScope().getUnderlyingContext();
    InputStream resourceAsStream = context.getResourceAsStream(_systemID);
    try {
        return resourceAsStream != null;
    } finally {
        try {
            if (resourceAsStream != null) {
                resourceAsStream.close();
            }
        } catch (IOException e) {
            return false;
        }
    }
}

From source file:com.haulmont.cuba.web.sys.singleapp.SingleAppWebServletListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    try {/*ww w.  ja va2  s.c  om*/
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        //need to put the following class to WebAppClassLoader, to share it between for web and core
        contextClassLoader.loadClass("com.haulmont.cuba.core.sys.remoting.LocalServiceDirectory");

        ServletContext servletContext = sce.getServletContext();
        String dependenciesFile;
        try {
            dependenciesFile = IOUtils.toString(servletContext.getResourceAsStream("/WEB-INF/web.dependencies"),
                    "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException("An error occurred while loading dependencies file", e);
        }

        String[] dependenciesNames = dependenciesFile.split("\\n");
        URL[] urls = Arrays.stream(dependenciesNames).map((String name) -> {
            try {
                return servletContext.getResource("/WEB-INF/lib/" + name);
            } catch (MalformedURLException e) {
                throw new RuntimeException("An error occurred while loading dependency " + name, e);
            }
        }).toArray(URL[]::new);
        URLClassLoader webClassLoader = new CubaSingleAppClassLoader(urls, contextClassLoader);

        Thread.currentThread().setContextClassLoader(webClassLoader);
        Class<?> appContextLoaderClass = webClassLoader.loadClass(getAppContextLoaderClassName());
        appContextLoader = appContextLoaderClass.newInstance();

        Method setJarsNamesMethod = ReflectionUtils.findMethod(appContextLoaderClass, "setJarNames",
                String.class);
        ReflectionUtils.invokeMethod(setJarsNamesMethod, appContextLoader, dependenciesFile);

        Method contextInitializedMethod = ReflectionUtils.findMethod(appContextLoaderClass,
                "contextInitialized", ServletContextEvent.class);
        ReflectionUtils.invokeMethod(contextInitializedMethod, appContextLoader, sce);

        Thread.currentThread().setContextClassLoader(contextClassLoader);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("An error occurred while starting single WAR application", e);
    }
}

From source file:com.opengamma.web.bundle.BundleManagerFactory.java

/**
 * Resolves the config file./*from  www. ja v a 2  s  .  com*/
 * @param servletContext 
 * 
 * @return the resolved file
 */
private InputStream getXMLStream(ServletContext servletContext) {
    String configXMlPath = _configXmlPath == null ? DEFAULT_CONFIG_XML_PATH : _configXmlPath;
    InputStream result = servletContext.getResourceAsStream(configXMlPath);
    if (result == null) {
        throw new IllegalStateException("Cannot find resource XML in " + configXMlPath);
    }
    return result;
}

From source file:org.rhq.enterprise.gui.startup.Configurator.java

/**
 * Load the specified properties file and return the properties.
 *
 * @param  context  the <code>ServletContext</code>
 * @param  filename the fully qualifed name of the properties file
 *
 * @return the contents of the properties file
 *
 * @throws Exception if a problem occurs while loading the file
 *//* ww w.  j  a  v  a2s. c o m*/
private Properties loadProperties(ServletContext context, String filename) throws Exception {
    Properties props = new Properties();
    InputStream is = context.getResourceAsStream(filename);
    if (is != null) {
        props.load(is);
        is.close();
    }

    return props;
}

From source file:com.haulmont.cuba.core.sys.singleapp.SingleAppCoreServletListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    try {/*from   w  w  w .  j a  v a  2 s  .c  om*/
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        //need to put the following class to WebAppClassLoader, to share it between for web and core
        contextClassLoader.loadClass("com.haulmont.cuba.core.sys.remoting.LocalServiceDirectory");

        ServletContext servletContext = sce.getServletContext();
        String dependenciesFile;
        try {
            dependenciesFile = IOUtils
                    .toString(servletContext.getResourceAsStream("/WEB-INF/core.dependencies"), "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException("An error occurred while loading dependencies file", e);
        }

        String[] dependenciesNames = dependenciesFile.split("\\n");
        URL[] urls = Arrays.stream(dependenciesNames).map((String name) -> {
            try {
                return servletContext.getResource("/WEB-INF/lib/" + name);
            } catch (MalformedURLException e) {
                throw new RuntimeException("An error occurred while loading dependency " + name, e);
            }
        }).toArray(URL[]::new);
        URLClassLoader coreClassLoader = new CubaSingleAppClassLoader(urls, contextClassLoader);

        Thread.currentThread().setContextClassLoader(coreClassLoader);
        Class<?> appContextLoaderClass = coreClassLoader.loadClass(getAppContextLoaderClassName());
        appContextLoader = appContextLoaderClass.newInstance();

        Method setJarsNamesMethod = ReflectionUtils.findMethod(appContextLoaderClass, "setJarNames",
                String.class);
        ReflectionUtils.invokeMethod(setJarsNamesMethod, appContextLoader, dependenciesFile);

        Method contextInitializedMethod = ReflectionUtils.findMethod(appContextLoaderClass,
                "contextInitialized", ServletContextEvent.class);
        ReflectionUtils.invokeMethod(contextInitializedMethod, appContextLoader, sce);

        Thread.currentThread().setContextClassLoader(contextClassLoader);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("An error occurred while starting single WAR application", e);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.SmokeTestController.java

private void readSmokeTestClassesFromFile(String p, ServletContext context) {
    //check that this is a file and not a directory.
    File f = new File(context.getRealPath(p));
    if (f.exists() && f.isFile()) {
        InputStream fileStream = context.getResourceAsStream(p);
        listOfSmokeTestClasses.addAll(getContentsFromFileStream(fileStream));
    } else {/*w  ww  .  j av  a  2s. com*/
        if (!f.exists()) {
            log.debug("File for path " + p + " does not exist");
        } else if (f.isDirectory()) {
            log.debug("Path " + p + " corresponds to a directory and not file. File was not read.");
        }
    }
}