remm 01/01/13 13:17:06 Modified: catalina/src/share/org/apache/naming/resources FileDirContext.java LocalStrings.properties Log: - Implement the last three missing calls in the FileDirContext. - Fix a problem where a file was locked after doing a lookup. It now uses an extended version of the resource class which only opens the stream to the file when the stream is actually requested. Revision Changes Path 1.4 +124 -26 jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/FileDirContext.java Index: FileDirContext.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- FileDirContext.java 2001/01/13 05:15:55 1.3 +++ FileDirContext.java 2001/01/13 21:17:05 1.4 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v 1.3 2001/01/13 05:15:55 remm Exp $ - * $Revision: 1.3 $ - * $Date: 2001/01/13 05:15:55 $ + * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v 1.4 2001/01/13 21:17:05 remm Exp $ + * $Revision: 1.4 $ + * $Date: 2001/01/13 21:17:05 $ * * ==================================================================== * @@ -82,6 +82,7 @@ import javax.naming.CompositeName; import javax.naming.NameParser; import javax.naming.OperationNotSupportedException; +import javax.naming.NameAlreadyBoundException; import javax.naming.directory.DirContext; import javax.naming.directory.Attributes; import javax.naming.directory.Attribute; @@ -97,7 +98,7 @@ * Filesystem Directory Context implementation helper class. * * @author Remy Maucherat - * @version $Revision: 1.3 $ $Date: 2001/01/13 05:15:55 $ + * @version $Revision: 1.4 $ $Date: 2001/01/13 21:17:05 $ */ public class FileDirContext extends BaseDirContext { @@ -230,13 +231,7 @@ tempContext.setDocBase(file.getPath()); result = tempContext; } else { - try { - InputStream is = new FileInputStream(file); - result = new Resource(is); - } catch (FileNotFoundException e) { - throw new NamingException - (sm.getString("resources.notFound", name)); - } + result = new FileResource(file); } return result; @@ -262,12 +257,14 @@ throws NamingException { File file = file(name); - + if (file == null) throw new NamingException (sm.getString("resources.notFound", name)); - file.delete(); + if (!file.delete()) + throw new NamingException + (sm.getString("resources.unbindFailed", name)); } @@ -524,8 +521,16 @@ */ public void bind(String name, Object obj, Attributes attrs) throws NamingException { - // No custom attributes - // FIXME + + // Note: No custom attributes allowed + + File file = new File(base, name); + if (file.exists()) + throw new NameAlreadyBoundException + (sm.getString("resources.alreadyBound", name)); + + rebind(name, obj, attrs); + } @@ -549,8 +554,56 @@ */ public void rebind(String name, Object obj, Attributes attrs) throws NamingException { - // No custom attributes - // FIXME + + // Note: No custom attributes allowed + // Check obj type + + File file = new File(base, name); + + InputStream is = null; + if (obj instanceof Resource) { + try { + is = ((Resource) obj).streamContent(); + } catch (IOException e) { + } + } else if (obj instanceof InputStream) { + is = (InputStream) obj; + } else if (obj instanceof DirContext) { + if (file.exists()) { + if (!file.delete()) + throw new NamingException + (sm.getString("resources.bindFailed", name)); + } + if (!file.mkdir()) + throw new NamingException + (sm.getString("resources.bindFailed", name)); + } + if (is == null) + throw new NamingException + (sm.getString("resources.bindFailed", name)); + + // Open os + + try { + FileOutputStream os = new FileOutputStream(file); + byte buffer[] = new byte[BUFFER_SIZE]; + int len = -1; + try { + while (true) { + len = is.read(buffer); + if (len == -1) + break; + os.write(buffer, 0, len); + } + os.close(); + } catch (IOException ex) { + os.close(); + } + } catch (IOException e) { + throw new NamingException + (sm.getString("resources.bindFailed", e)); + } + } @@ -573,8 +626,16 @@ */ public DirContext createSubcontext(String name, Attributes attrs) throws NamingException { - // FIXME - return null; + + File file = new File(base, name); + if (file.exists()) + throw new NameAlreadyBoundException + (sm.getString("resources.alreadyBound", name)); + if (!file.mkdir()) + throw new NamingException + (sm.getString("resources.bindFailed", name)); + return (DirContext) lookup(name); + } @@ -796,13 +857,14 @@ } catch (IOException e) { } absPath = absPath.substring(absoluteBase.length()); + if ((canPath == null) || (absPath == null)) + return null; if (absPath.equals("")) absPath = "/"; canPath = canPath.substring(absoluteBase.length()); if (canPath.equals("")) canPath = "/"; - if ((canPath == null) || (absPath == null) - || (!canPath.equals(absPath))) + if (!canPath.equals(absPath)) return null; } } else { @@ -834,11 +896,7 @@ tempContext.setDocBase(file.getPath()); object = tempContext; } else { - try { - object = new Resource(new FileInputStream(currentFile)); - } catch (FileNotFoundException e) { - continue; - } + object = new FileResource(currentFile); } entry = new NamingEntry(names[i], object, NamingEntry.ENTRY); entries.addElement(entry); @@ -847,6 +905,46 @@ return entries; + } + + + // ----------------------------------------------- FileResource Inner Class + + + /** + * This specialized resource implementation avoids opening the IputStream + * to the file right away (which would put a lock on the file). + */ + protected class FileResource extends Resource { + + + // -------------------------------------------------------- Constructor + + + public FileResource(File file) { + this.file = file; + } + + + // --------------------------------------------------- Member Variables + + + protected File file; + + + /** + * Content accessor. + * + * @return InputStream + */ + public InputStream streamContent() + throws IOException { + if ((inputStream == null) && (binaryContent == null)) + inputStream = new FileInputStream(file); + return super.streamContent(); + } + + } 1.3 +3 -0 jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/LocalStrings.properties Index: LocalStrings.properties =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/LocalStrings.properties,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- LocalStrings.properties 2001/01/13 05:17:04 1.2 +++ LocalStrings.properties 2001/01/13 21:17:06 1.3 @@ -7,6 +7,9 @@ resources.null=Document base cannot be null resources.notFound=Resource {0} not found resources.path=Context relative path {0} must start with '/' +resources.alreadyBound=Name {0} is already bound in this Context +resources.bindFailed=Bind failed: {0} +resources.unbindFailed=Unbind failed: {0} standardResources.alreadyStarted=Resources has already been started standardResources.directory=File base {0} is not a directory standardResources.exists=File base {0} does not exist --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]