本文整理汇总了Java中javax.servlet.ServletContext.getResourceAsStream方法的典型用法代码示例。如果您正苦于以下问题:Java ServletContext.getResourceAsStream方法的具体用法?Java ServletContext.getResourceAsStream怎么用?Java ServletContext.getResourceAsStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletContext
的用法示例。
在下文中一共展示了ServletContext.getResourceAsStream方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDeploymentDescriptor
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* JSR77 deploymentDescriptor attribute
*
* @return string deployment descriptor
*/
public String getDeploymentDescriptor() {
InputStream stream = null;
ServletContext servletContext = getServletContext();
if (servletContext != null) {
stream = servletContext.getResourceAsStream(
org.apache.catalina.startup.Constants.ApplicationWebXml);
}
if (stream == null) {
return "";
}
BufferedReader br = new BufferedReader(
new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();
String strRead = "";
try {
while (strRead != null) {
sb.append(strRead);
strRead = br.readLine();
}
} catch (IOException e) {
return "";
}
return sb.toString();
}
示例2: readFromResource
import javax.servlet.ServletContext; //导入方法依赖的package包/类
private static String readFromResource(ServletContext servletContext, String resourcePath) {
// TODO: make shure we close the file here?
String tmp = "X";
InputStream is = servletContext.getResourceAsStream(resourcePath);
if (is != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
tmp = br.readLine();
} catch (IOException e) {
tmp = "DEV";
}
}
return tmp;
}
示例3: getServletContextResourceStream
import javax.servlet.ServletContext; //导入方法依赖的package包/类
private static InputStream getServletContextResourceStream(ServletContext servletContext, String path) {
InputStream is = null;
path = WebUtils.normalize(path);
if (servletContext != null) {
is = servletContext.getResourceAsStream(path);
}
return is;
}
示例4: getRegionMetadata
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* Gets the RegionMetadata.
* Initially this will parse the region metadata files to create and cache
* an instance of this metadata object.
* All resources with the name "/META-INF/region-metadata.xml" will be searched.
* /WEB-INF/region-metadata.xml will also be searched.
*/
@SuppressWarnings("unchecked")
public static synchronized RegionMetadata getRegionMetadata(
FacesContext context)
{
Map<String, Object> appMap = context.getExternalContext().getApplicationMap();
RegionMetadata bean = (RegionMetadata) appMap.get(_KEY);
if (bean == null)
{
bean = new RegionMetadata();
appMap.put(_KEY, bean);
ClassLoader loader = Thread.currentThread().getContextClassLoader();
_readAllRegionMDFiles(loader, bean, __CONFIG_FILE_OTHER);
_readAllRegionMDFiles(loader, bean, _CONFIG_FILE);
Object sContext = context.getExternalContext().getContext();
if (sContext instanceof ServletContext)
{
ServletContext servletContext = (ServletContext) sContext;
InputStream in = servletContext.getResourceAsStream(_CONFIG_FILE_2);
if (in != null)
{
_readRegionMetadata(bean, in, _CONFIG_FILE_2);
try
{
in.close();
}
catch (IOException e)
{
_LOG.severe("ERR_CLOSING_FILE",_CONFIG_FILE_2);
_LOG.severe(e);
}
}
}
}
return bean;
}
示例5: setServletResourceName
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* Setter for the resource name that we can use to read a file from the
* servlet context
* @param servletResourceName The name to lookup
* @throws IOException On file read failure
* @throws ParserConfigurationException On XML setup failure
* @throws SAXException On XML parse failure
*/
public void setServletResourceName(String servletResourceName) throws IOException, ParserConfigurationException, SAXException
{
this.servletResourceName = servletResourceName;
ServletContext servletContext = WebContextFactory.get().getServletContext();
if (servletContext == null)
{
throw new IOException(Messages.getString("DwrXmlConfigurator.MissingServletContext"));
}
InputStream in = null;
try
{
in = servletContext.getResourceAsStream(servletResourceName);
if (in == null)
{
throw new IOException(Messages.getString("DwrXmlConfigurator.MissingConfigFile", servletResourceName));
}
log.debug("Configuring from servlet resource: " + servletResourceName);
setInputStream(in);
}
finally
{
LocalUtil.close(in);
}
}
示例6: DatabaseOperations
import javax.servlet.ServletContext; //导入方法依赖的package包/类
public DatabaseOperations(ServletContext context) {
InputStream input = context.getResourceAsStream("/WEB-INF/database.properties");
DatabaseProperties properties = new DatabaseProperties();
try {
properties.load(input);
} catch (IOException e) {
e.printStackTrace();
}
datasource = new DatabaseConnectionPool(properties);
}
示例7: fillLangs
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* Fills the specified data model with the current skink's
* (WebRoot/skins/${skinName}/lang/lang_xx_XX.properties) and core language
* (WebRoot/WEB-INF/classes/lang_xx_XX.properties) configurations.
*
* @param localeString
* the specified locale string
* @param currentSkinDirName
* the specified current skin directory name
* @param dataModel
* the specified data model
* @throws ServiceException
* service exception
*/
public void fillLangs(final String localeString, String currentSkinDirName, final Map<String, Object> dataModel)
throws ServiceException {
Stopwatchs.start("Fill Skin Langs");
if (null == currentSkinDirName) {
currentSkinDirName = "9IPHP";
}
try {
final String langName = currentSkinDirName + "." + localeString;
Map<String, String> langs = LANG_MAP.get(langName);
if (null == langs) {
LANG_MAP.clear(); // Collect unused skin languages
logger.debug("Loading skin [dirName={}, locale={}]", currentSkinDirName, localeString);
langs = new HashMap<>();
final String language = Locales.getLanguage(localeString);
final String country = Locales.getCountry(localeString);
ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
String path = "/skins/" + currentSkinDirName + "/lang/lang_" + language + '_' + country + ".properties";
final InputStream inputStream = servletContext.getResourceAsStream(path);
final Properties props = new Properties();
props.load(inputStream);
final Set<Object> keys = props.keySet();
for (final Object key : keys) {
langs.put((String) key, props.getProperty((String) key));
}
LANG_MAP.put(langName, langs);
logger.debug("Loaded skin[dirName={}, locale={}, keyCount={}]", currentSkinDirName, localeString,
langs.size());
}
dataModel.putAll(langs); // Fills the current skin's language
// configurations
// Fills the core language configurations
// final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
// final LangPropsService langPropsService =
// beanManager.getReference(LangPropsService.class);
dataModel.putAll(langPropsService.getAll(Latkes.getLocale()));
} catch (final IOException e) {
logger.error("Fills skin langs failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
示例8: tldScanResourcePaths
import javax.servlet.ServletContext; //导入方法依赖的package包/类
private void tldScanResourcePaths(String startPath) {
if (log.isTraceEnabled()) {
log.trace(sm.getString("tldConfig.webinfScan", startPath));
}
ServletContext ctxt = context.getServletContext();
Set<String> dirList = ctxt.getResourcePaths(startPath);
if (dirList != null) {
Iterator<String> it = dirList.iterator();
while (it.hasNext()) {
String path = it.next();
if (!path.endsWith(TLD_EXT)
&& (path.startsWith(WEB_INF_LIB)
|| path.startsWith("/WEB-INF/classes/"))) {
continue;
}
if (path.endsWith(TLD_EXT)) {
if (path.startsWith("/WEB-INF/tags/") &&
!path.endsWith("implicit.tld")) {
continue;
}
InputStream stream = ctxt.getResourceAsStream(path);
try {
XmlErrorHandler handler = tldScanStream(stream);
handler.logFindings(log, path);
} catch (IOException ioe) {
log.warn(sm.getString("tldConfig.webinfFail", path),
ioe);
} finally {
if (stream != null) {
try {
stream.close();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
}
}
} else {
tldScanResourcePaths(path);
}
}
}
}
示例9: getManifestConfiguration
import javax.servlet.ServletContext; //导入方法依赖的package包/类
private ConfigurationSource getManifestConfiguration(ServletContext servletContext) {
Map<String, String> defaultManifestMap = new ImmutableMap.Builder<String, String>()
.put(MANIFEST_TITLE, "Comms Router Web")
.put(MANIFEST_VERSION, "NOT_AVAILABLE")
.put(MANIFEST_BUILD, "NOT_AVAILABLE")
.put(MANIFEST_BUILD_TIME, "NOT_AVAILABLE")
.put(MANIFEST_BUILD_JDK, "NOT_AVAILABLE")
.build();
Properties defaultManifestProperties = new Properties();
defaultManifestProperties.putAll(defaultManifestMap);
InMemoryConfigurationSource defaultManifestSource =
new InMemoryConfigurationSource(defaultManifestProperties);
ConfigurationSource manifestSource = new EmptyConfigurationSource();
try {
final Properties manifestProperties = new Properties();
try (InputStream stream = servletContext.getResourceAsStream(MANIFEST_MF)) {
Manifest manifest = new Manifest(stream);
Attributes mainAttributes = manifest.getMainAttributes();
Map<String, Object> collect = mainAttributes.entrySet().stream()
.filter(entry -> !entry.getValue().toString().isEmpty())
.map(entry -> {
String key = entry.getKey().toString().replace("-", "");
return new SimpleEntry<>(key, entry.getValue());
})
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
manifestProperties.putAll(collect);
}
manifestSource = new InMemoryConfigurationSource(manifestProperties);
} catch (Exception e) {
LOGGER.warn("Could not load the MANIFEST.MF file!", e);
}
MergeConfigurationSource mergeConfigurationSource =
new MergeConfigurationSource(defaultManifestSource, manifestSource);
LOGGER.debug("manifestProperties: {}", mergeConfigurationSource);
return mergeConfigurationSource;
}
示例10: getWebXmlWelcomeFileList
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* Accessor for the list of components to strip to normalize a filename
* @param context Our route to reading web.xml
* @return A list of the welcome files from web.xml or null if none are found there
*/
protected static List getWebXmlWelcomeFileList(ServletContext context)
{
try
{
InputStream in = context.getResourceAsStream(PathConstants.RESOURCE_WEB_XML);
if (in == null)
{
log.warn("Missing " + PathConstants.RESOURCE_WEB_XML);
return null;
}
if (buildFactory == null)
{
buildFactory = DocumentBuilderFactory.newInstance();
buildFactory.setValidating(false);
}
DocumentBuilder builder = buildFactory.newDocumentBuilder();
builder.setEntityResolver(new EmptyEntityResolver());
builder.setErrorHandler(new LogErrorHandler());
InputSource is = new InputSource(in);
Document doc = builder.parse(is);
Element webapp = doc.getDocumentElement();
NodeList welcomeFileListNodes = webapp.getElementsByTagName("welcome-file-list");
if (welcomeFileListNodes.getLength() == 0)
{
log.debug("web.xml contains no <welcome-file-list> element");
return null;
}
List reply = new ArrayList();
for (int i = 0; i < welcomeFileListNodes.getLength(); i++)
{
Element welcomeFileListNode = (Element) welcomeFileListNodes.item(i);
NodeList welcomeFileNodes = welcomeFileListNode.getElementsByTagName("welcome-file");
for (int j = 0; j < welcomeFileNodes.getLength(); j++)
{
Element welcomeFileNode = (Element) welcomeFileNodes.item(j);
String welcomeFile = DomUtil.getText(welcomeFileNode);
reply.add(welcomeFile);
log.debug("Adding welcome-file: " + welcomeFile);
}
}
return reply;
}
catch (Exception ex)
{
log.warn("Failed to calculate welcome files from web.xml.", ex);
return null;
}
}
示例11: getFileTypeImage
import javax.servlet.ServletContext; //导入方法依赖的package包/类
private static String getFileTypeImage(FacesContext fc, ServletContext sc, String name, FileTypeImageSize size)
{
String image = null;
String defaultImage = null;
switch (size)
{
case Small:
defaultImage = DEFAULT_FILE_IMAGE16; break;
case Medium:
defaultImage = DEFAULT_FILE_IMAGE32; break;
case Large:
defaultImage = DEFAULT_FILE_IMAGE64; break;
}
int extIndex = name.lastIndexOf('.');
if (extIndex != -1 && name.length() > extIndex + 1)
{
String ext = name.substring(extIndex + 1).toLowerCase();
String key = ext + ' ' + size.toString();
// found file extension for appropriate size image
synchronized (s_fileExtensionMap)
{
image = s_fileExtensionMap.get(key);
if (image == null)
{
// not found create for first time
if (size != FileTypeImageSize.Large)
{
image = (size == FileTypeImageSize.Small ? IMAGE_PREFIX16 : IMAGE_PREFIX32) +
ext + IMAGE_POSTFIX_GIF;
}
else
{
image = IMAGE_PREFIX64 + ext + IMAGE_POSTFIX_PNG;
}
// does this image exist on the web-server?
if ((fc != null && fc.getExternalContext().getResourceAsStream(image) != null) ||
(sc != null && sc.getResourceAsStream(image) != null))
{
// found the image for this extension - save it for later
s_fileExtensionMap.put(key, image);
}
else if ((fc == null) && (sc == null))
{
// we have neither FacesContext nor ServerContext so return the default image but don't cache it
image = defaultImage;
}
else
{
// not found, save the default image for this extension instead
s_fileExtensionMap.put(key, defaultImage);
image = defaultImage;
}
}
}
}
return (image != null ? image : defaultImage);
}
示例12: tldScanResourcePaths
import javax.servlet.ServletContext; //导入方法依赖的package包/类
private void tldScanResourcePaths(String startPath) {
if (log.isTraceEnabled()) {
log.trace(sm.getString("tldConfig.webinfScan", startPath));
}
ServletContext ctxt = context.getServletContext();
Set<String> dirList = ctxt.getResourcePaths(startPath);
if (dirList != null) {
Iterator<String> it = dirList.iterator();
while (it.hasNext()) {
String path = it.next();
if (!path.endsWith(TLD_EXT) && (path.startsWith(WEB_INF_LIB) || path.startsWith("/WEB-INF/classes/"))) {
continue;
}
if (path.endsWith(TLD_EXT)) {
if (path.startsWith("/WEB-INF/tags/") && !path.endsWith("implicit.tld")) {
continue;
}
InputStream stream = ctxt.getResourceAsStream(path);
try {
XmlErrorHandler handler = tldScanStream(stream);
handler.logFindings(log, path);
} catch (IOException ioe) {
log.warn(sm.getString("tldConfig.webinfFail", path), ioe);
} finally {
if (stream != null) {
try {
stream.close();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
}
}
} else {
tldScanResourcePaths(path);
}
}
}
}