use of org.apache.naming.resources.Resource in project tomcat70 by apache.
the class WebappLoader method setRepositories.
/**
* Configure the repositories for our class loader, based on the
* associated Context.
* @throws IOException
*/
private void setRepositories() throws IOException {
if (!(container instanceof Context))
return;
ServletContext servletContext = ((Context) container).getServletContext();
if (servletContext == null)
return;
loaderRepositories = new ArrayList<String>();
// Loading the work directory
File workDir = (File) servletContext.getAttribute(ServletContext.TEMPDIR);
if (workDir == null) {
log.info("No work dir for " + servletContext);
}
if (log.isDebugEnabled() && workDir != null)
log.debug(sm.getString("webappLoader.deploy", workDir.getAbsolutePath()));
classLoader.setWorkDir(workDir);
DirContext resources = container.getResources();
// Setting up the class repository (/WEB-INF/classes), if it exists
String classesPath = "/WEB-INF/classes";
DirContext classes = null;
try {
Object object = resources.lookup(classesPath);
if (object instanceof DirContext) {
classes = (DirContext) object;
}
} catch (NamingException e) {
// Silent catch: it's valid that no /WEB-INF/classes collection
// exists
}
if (classes != null) {
File classRepository = null;
String absoluteClassesPath = servletContext.getRealPath(classesPath);
if (absoluteClassesPath != null) {
classRepository = new File(absoluteClassesPath);
} else {
classRepository = new File(workDir, classesPath);
if (!classRepository.mkdirs() && !classRepository.isDirectory()) {
throw new IOException(sm.getString("webappLoader.mkdirFailure"));
}
if (!copyDir(classes, classRepository)) {
throw new IOException(sm.getString("webappLoader.copyFailure"));
}
}
if (log.isDebugEnabled())
log.debug(sm.getString("webappLoader.classDeploy", classesPath, classRepository.getAbsolutePath()));
// Adding the repository to the class loader
classLoader.addRepository(classesPath + "/", classRepository);
loaderRepositories.add(classesPath + "/");
}
// Setting up the JAR repository (/WEB-INF/lib), if it exists
String libPath = "/WEB-INF/lib";
classLoader.setJarPath(libPath);
DirContext libDir = null;
// Looking up directory /WEB-INF/lib in the context
try {
Object object = resources.lookup(libPath);
if (object instanceof DirContext)
libDir = (DirContext) object;
} catch (NamingException e) {
// Silent catch: it's valid that no /WEB-INF/lib collection
// exists
}
if (libDir != null) {
boolean copyJars = false;
String absoluteLibPath = servletContext.getRealPath(libPath);
File destDir = null;
if (absoluteLibPath != null) {
destDir = new File(absoluteLibPath);
} else {
copyJars = true;
destDir = new File(workDir, libPath);
if (!destDir.mkdirs() && !destDir.isDirectory()) {
throw new IOException(sm.getString("webappLoader.mkdirFailure"));
}
}
// Looking up directory /WEB-INF/lib in the context
NamingEnumeration<NameClassPair> enumeration = null;
try {
enumeration = libDir.list("");
} catch (NamingException e) {
IOException ioe = new IOException(sm.getString("webappLoader.namingFailure", libPath));
ioe.initCause(e);
throw ioe;
}
while (enumeration.hasMoreElements()) {
NameClassPair ncPair = enumeration.nextElement();
String filename = libPath + "/" + ncPair.getName();
if (!filename.endsWith(".jar"))
continue;
// Copy JAR in the work directory, always (the JAR file
// would get locked otherwise, which would make it
// impossible to update it or remove it at runtime)
File destFile = new File(destDir, ncPair.getName());
if (log.isDebugEnabled())
log.debug(sm.getString("webappLoader.jarDeploy", filename, destFile.getAbsolutePath()));
// Bug 45403 - Explicitly call lookup() on the name to check
// that the resource is readable. We cannot use resources
// returned by listBindings(), because that lists all of them,
// but does not perform the necessary checks on each.
Object obj = null;
try {
obj = libDir.lookup(ncPair.getName());
} catch (NamingException e) {
IOException ioe = new IOException(sm.getString("webappLoader.namingFailure", filename));
ioe.initCause(e);
throw ioe;
}
if (!(obj instanceof Resource))
continue;
Resource jarResource = (Resource) obj;
if (copyJars) {
if (!copy(jarResource.streamContent(), new FileOutputStream(destFile))) {
throw new IOException(sm.getString("webappLoader.copyFailure"));
}
}
try {
JarFile jarFile = JreCompat.getInstance().jarFileNewInstance(destFile);
classLoader.addJar(filename, jarFile, destFile);
} catch (Exception ex) {
// Catch the exception if there is an empty jar file
// Should ignore and continue loading other jar files
// in the dir
}
loaderRepositories.add(filename);
}
}
}
use of org.apache.naming.resources.Resource in project tomcat70 by apache.
the class WebappLoader method copyDir.
/**
* Copy directory.
*/
private boolean copyDir(DirContext srcDir, File destDir) {
try {
NamingEnumeration<NameClassPair> enumeration = srcDir.list("");
while (enumeration.hasMoreElements()) {
NameClassPair ncPair = enumeration.nextElement();
String name = ncPair.getName();
Object object = srcDir.lookup(name);
File currentFile = new File(destDir, name);
if (object instanceof Resource) {
InputStream is = ((Resource) object).streamContent();
OutputStream os = new FileOutputStream(currentFile);
if (!copy(is, os))
return false;
} else if (object instanceof InputStream) {
OutputStream os = new FileOutputStream(currentFile);
if (!copy((InputStream) object, os))
return false;
} else if (object instanceof DirContext) {
if (!currentFile.isDirectory() && !currentFile.mkdir())
return false;
if (!copyDir((DirContext) object, currentFile))
return false;
}
}
} catch (NamingException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
use of org.apache.naming.resources.Resource in project tomcat70 by apache.
the class WebdavStatus method copyResource.
/**
* Copy a collection.
*
* @param dirContext Resources implementation to be used
* @param errorList Hashtable containing the list of errors which occurred
* during the copy operation
* @param source Path of the resource to be copied
* @param dest Destination path
*/
private boolean copyResource(DirContext dirContext, Hashtable<String, Integer> errorList, String source, String dest) {
if (debug > 1)
log("Copy: " + source + " To: " + dest);
Object object = null;
try {
object = dirContext.lookup(source);
} catch (NamingException e) {
// Ignore
}
if (object instanceof DirContext) {
try {
dirContext.createSubcontext(dest);
} catch (NamingException e) {
errorList.put(dest, Integer.valueOf(WebdavStatus.SC_CONFLICT));
return false;
}
try {
NamingEnumeration<NameClassPair> enumeration = dirContext.list(source);
while (enumeration.hasMoreElements()) {
NameClassPair ncPair = enumeration.nextElement();
String childDest = dest;
if (!childDest.equals("/"))
childDest += "/";
childDest += ncPair.getName();
String childSrc = source;
if (!childSrc.equals("/"))
childSrc += "/";
childSrc += ncPair.getName();
copyResource(dirContext, errorList, childSrc, childDest);
}
} catch (NamingException e) {
errorList.put(dest, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
}
} else {
if (object instanceof Resource) {
try {
dirContext.bind(dest, object);
} catch (NamingException e) {
if (e.getCause() instanceof FileNotFoundException) {
// We know the source exists so it must be the
// destination dir that can't be found
errorList.put(source, Integer.valueOf(WebdavStatus.SC_CONFLICT));
} else {
errorList.put(source, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
}
return false;
}
} else {
errorList.put(source, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
return false;
}
}
return true;
}
use of org.apache.naming.resources.Resource in project tomcat70 by apache.
the class DefaultServlet method doPut.
/**
* Process a PUT request for the specified resource.
*
* @param req The servlet request we are processing
* @param resp The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet-specified error occurs
*/
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (readOnly) {
resp.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
String path = getRelativePath(req);
boolean exists = true;
try {
resources.lookup(path);
} catch (NamingException e) {
exists = false;
}
boolean result = true;
// Temp. content file used to support partial PUT
File contentFile = null;
Range range = parseContentRange(req, resp);
InputStream resourceInputStream = null;
// Assume just one range is specified for now
if (range != null) {
contentFile = executePartialPut(req, range, path);
resourceInputStream = new FileInputStream(contentFile);
} else {
resourceInputStream = req.getInputStream();
}
try {
Resource newResource = new Resource(resourceInputStream);
// FIXME: Add attributes
if (exists) {
resources.rebind(path, newResource);
} else {
resources.bind(path, newResource);
}
} catch (NamingException e) {
result = false;
}
if (result) {
if (exists) {
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
} else {
resp.setStatus(HttpServletResponse.SC_CREATED);
}
} else {
resp.sendError(HttpServletResponse.SC_CONFLICT);
}
}
use of org.apache.naming.resources.Resource in project tomcat70 by apache.
the class DefaultServlet method findXsltInputStream.
/**
* Return a Source for the xsl template (if possible)
*/
protected Source findXsltInputStream(DirContext directory) throws IOException {
if (localXsltFile != null) {
try {
Object obj = directory.lookup(localXsltFile);
if ((obj != null) && (obj instanceof Resource)) {
InputStream is = ((Resource) obj).streamContent();
if (is != null) {
if (Globals.IS_SECURITY_ENABLED) {
return secureXslt(is);
} else {
return new StreamSource(is);
}
}
}
} catch (NamingException e) {
if (debug > 10)
log("localXsltFile '" + localXsltFile + "' not found", e);
}
}
if (contextXsltFile != null) {
InputStream is = getServletContext().getResourceAsStream(contextXsltFile);
if (is != null) {
if (Globals.IS_SECURITY_ENABLED) {
return secureXslt(is);
} else {
return new StreamSource(is);
}
}
if (debug > 10)
log("contextXsltFile '" + contextXsltFile + "' not found");
}
/* Open and read in file in one fell swoop to reduce chance
* chance of leaving handle open.
*/
if (globalXsltFile != null) {
File f = validateGlobalXsltFile();
if (f != null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
/* danger! */
fis.read(b);
return new StreamSource(new ByteArrayInputStream(b));
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException ioe) {
// Ignore
}
}
}
}
}
return null;
}
Aggregations