Author: markt
Date: Sun Sep  9 18:15:01 2012
New Revision: 1382552

URL: http://svn.apache.org/viewvc?rev=1382552&view=rev
Log:
Initial implementation of new resources API

Added:
    tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/   
(with props)
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
   (with props)
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/EmptyResource.java
   (with props)
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
   (with props)
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResource.java
   (with props)
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResourceSet.java
   (with props)
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/StandardRoot.java
   (with props)

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/
------------------------------------------------------------------------------
    bugtraq:append = false

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/
------------------------------------------------------------------------------
    bugtraq:label = Bugzilla ID (optional)

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/
------------------------------------------------------------------------------
--- bugtraq:message (added)
+++ bugtraq:message Sun Sep  9 18:15:01 2012
@@ -0,0 +1 @@
+Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=%BUGID%

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/
------------------------------------------------------------------------------
    bugtraq:url = https://issues.apache.org/bugzilla/show_bug.cgi?id=%BUGID%

Added: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java?rev=1382552&view=auto
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
 (added)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
 Sun Sep  9 18:15:01 2012
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Set;
+
+import org.apache.catalina.WebResource;
+import org.apache.catalina.WebResourceRoot;
+import org.apache.catalina.WebResourceSet;
+import org.apache.catalina.util.ResourceSet;
+
+public class DirResourceSet implements WebResourceSet {
+
+    private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+    private final WebResourceRoot root;
+    private final File base;
+    private final String webAppMount;
+
+    public DirResourceSet(WebResourceRoot root, File base, String webAppMount,
+            String internalPath) {
+        this.root = root;
+        base = new File(base, internalPath);
+        if (base.isDirectory() == false) {
+            throw new IllegalArgumentException(
+                    "TODO-i18n: base/internalPath is not a directory");
+        }
+        this.base = base;
+        this.webAppMount = webAppMount;
+    }
+
+    @Override
+    public WebResource getResource(String path) {
+        if (path.startsWith(webAppMount)) {
+            File f = new File(base, path.substring(webAppMount.length()));
+            return new FileResource(root, f, path);
+        } else {
+            return new EmptyResource(root, path);
+        }
+    }
+
+    @Override
+    public String[] list(String path) {
+        if (path.startsWith(webAppMount)) {
+            File f = new File(base, path.substring(webAppMount.length()));
+            String[] result = f.list();
+            if (result == null) {
+                return EMPTY_STRING_ARRAY;
+            } else {
+                return result;
+            }
+        } else {
+            return EMPTY_STRING_ARRAY;
+        }
+    }
+
+    @Override
+    public Set<String> listWebAppPaths(String path) {
+        ResourceSet<String> result = new ResourceSet<>();
+        if (path.startsWith(webAppMount)) {
+            File f = new File(base, path.substring(webAppMount.length()));
+            String[] list = f.list();
+            if (list != null) {
+                for (String entry : list) {
+                    result.add(path + "/" + entry);
+                }
+            }
+        }
+        result.setLocked(true);
+        return result;
+    }
+
+    @Override
+    public boolean mkdir(String path) {
+        if (path.startsWith(webAppMount)) {
+            File f = new File(base, path.substring(webAppMount.length()));
+            return f.mkdir();
+        } else {
+            return false;
+        }
+    }
+
+    @Override
+    public boolean write(String path, InputStream is) {
+        // TODO Auto-generated method stub
+        return false;
+    }
+}

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/EmptyResource.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/EmptyResource.java?rev=1382552&view=auto
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/EmptyResource.java
 (added)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/EmptyResource.java
 Sun Sep  9 18:15:01 2012
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.catalina.WebResource;
+import org.apache.catalina.WebResourceRoot;
+
+public class EmptyResource implements WebResource {
+
+    private final WebResourceRoot root;
+    private final String webAppPath;
+
+    public EmptyResource(WebResourceRoot root, String webAppPath) {
+        this.root = root;
+        this.webAppPath = webAppPath;
+    }
+
+    @Override
+    public long getLastModified() {
+        return 0;
+    }
+
+    @Override
+    public String getLastModifiedHttp() {
+        return null;
+    }
+
+    @Override
+    public boolean exists() {
+        return false;
+    }
+
+    @Override
+    public boolean isDirectory() {
+        return false;
+    }
+
+    @Override
+    public boolean isFile() {
+        return false;
+    }
+
+    @Override
+    public boolean delete() {
+        return false;
+    }
+
+    @Override
+    public String getName() {
+        int index = webAppPath.lastIndexOf('/');
+        if (index == -1) {
+            return webAppPath;
+        } else {
+            return webAppPath.substring(index + 1);
+        }
+    }
+
+    @Override
+    public long getContentLength() {
+        return 0;
+    }
+
+    @Override
+    public String getCanonicalPath() {
+        return null;
+    }
+
+    @Override
+    public boolean canRead() {
+        return false;
+    }
+
+    @Override
+    public String getWebappPath() {
+        return webAppPath;
+    }
+
+    @Override
+    public String getETag() {
+        return null;
+    }
+
+    @Override
+    public void setMimeType(String mimeType) {
+        // NOOP
+    }
+
+    @Override
+    public String getMimeType() {
+        return null;
+    }
+
+    @Override
+    public InputStream getInputStream() {
+        return null;
+    }
+
+    @Override
+    public byte[] getContent() {
+        return null;
+    }
+
+    @Override
+    public long getCreation() {
+        return 0;
+    }
+
+    @Override
+    public URL getURL() {
+        return null;
+    }
+
+    @Override
+    public WebResourceRoot getWebResourceRoot() {
+        return root;
+    }
+}

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/EmptyResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java?rev=1382552&view=auto
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
 (added)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
 Sun Sep  9 18:15:01 2012
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.catalina.WebResource;
+import org.apache.catalina.WebResourceRoot;
+
+public class FileResource implements WebResource {
+
+    private final WebResourceRoot root;
+    private final String webAppPath;
+    private final File resource;
+
+    public FileResource(WebResourceRoot root, File resource, String 
webAppPath) {
+        this.root = root;
+        this.webAppPath = webAppPath;
+        this.resource = resource;
+    }
+
+    @Override
+    public long getLastModified() {
+        return resource.lastModified();
+    }
+
+    @Override
+    public String getLastModifiedHttp() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public boolean exists() {
+        return resource.exists();
+    }
+
+    @Override
+    public boolean isDirectory() {
+        return resource.isDirectory();
+    }
+
+    @Override
+    public boolean isFile() {
+        return resource.isFile();
+    }
+
+    @Override
+    public boolean delete() {
+        return resource.delete();
+    }
+
+    @Override
+    public String getName() {
+        return resource.getName();
+    }
+
+    @Override
+    public long getContentLength() {
+        return resource.length();
+    }
+
+    @Override
+    public String getCanonicalPath() {
+        try {
+            return resource.getCanonicalPath();
+        } catch (IOException ioe) {
+            // TODO Log this?
+            return null;
+        }
+    }
+
+    @Override
+    public boolean canRead() {
+        return resource.canRead();
+    }
+
+    @Override
+    public String getWebappPath() {
+        return webAppPath;
+    }
+
+    @Override
+    public String getETag() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void setMimeType(String mimeType) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public String getMimeType() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public InputStream getInputStream() {
+        if (resource.exists()) {
+            try {
+                return new FileInputStream(resource);
+            } catch (FileNotFoundException fnfe) {
+                // Race condition - not an error
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+
+    @Override
+    public byte[] getContent() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public long getCreation() {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    @Override
+    public URL getURL() {
+        try {
+            return resource.toURI().toURL();
+        } catch (MalformedURLException e) {
+            // TODO Log this?
+            return null;
+        }
+    }
+
+    @Override
+    public WebResourceRoot getWebResourceRoot() {
+        return root;
+    }
+}

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResource.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResource.java?rev=1382552&view=auto
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResource.java
 (added)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResource.java
 Sun Sep  9 18:15:01 2012
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import org.apache.catalina.WebResource;
+import org.apache.catalina.WebResourceRoot;
+
+public class JarResource implements WebResource {
+
+    private final WebResourceRoot root;
+    private final JarFile base;
+    private final String baseUrl;
+    private final JarEntry resource;
+    private final String webAppPath;
+
+    public JarResource(WebResourceRoot root, JarFile base, String baseUrl,
+            JarEntry jarEntry, String webAppPath) {
+        this.root = root;
+        this.base = base;
+        this.baseUrl = "jar:" + baseUrl;
+        this.resource = jarEntry;
+        this.webAppPath = webAppPath;
+    }
+
+    @Override
+    public long getLastModified() {
+        return resource.getTime();
+    }
+
+    @Override
+    public String getLastModifiedHttp() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public boolean exists() {
+        return true;
+    }
+
+    @Override
+    public boolean isDirectory() {
+        return resource.isDirectory();
+    }
+
+    @Override
+    public boolean isFile() {
+        return !resource.isDirectory();
+    }
+
+    @Override
+    public boolean delete() {
+        return false;
+    }
+
+    @Override
+    public String getName() {
+        String path = resource.getName();
+        int index = path.lastIndexOf('/');
+        if (index == -1) {
+            return path;
+        } else {
+            return path.substring(index + 1);
+        }
+    }
+
+    @Override
+    public long getContentLength() {
+        return resource.getSize();
+    }
+
+    @Override
+    public String getCanonicalPath() {
+        return null;
+    }
+
+    @Override
+    public boolean canRead() {
+        return true;
+    }
+
+    @Override
+    public String getWebappPath() {
+        return webAppPath;
+    }
+
+    @Override
+    public String getETag() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void setMimeType(String mimeType) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public String getMimeType() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public InputStream getInputStream() {
+        try {
+            return base.getInputStream(resource);
+        } catch (IOException e) {
+            // TODO log this?
+            return null;
+        }
+    }
+
+    @Override
+    public byte[] getContent() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public long getCreation() {
+        return resource.getTime();
+    }
+
+    @Override
+    public URL getURL() {
+        try {
+            return new URL(baseUrl + "!/" + resource.getName());
+        } catch (MalformedURLException e) {
+            // TODO Log this?
+            return null;
+        }
+    }
+
+    @Override
+    public WebResourceRoot getWebResourceRoot() {
+        return root;
+    }
+}

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResourceSet.java?rev=1382552&view=auto
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResourceSet.java
 (added)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResourceSet.java
 Sun Sep  9 18:15:01 2012
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.util.Enumeration;
+import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import org.apache.catalina.WebResource;
+import org.apache.catalina.WebResourceRoot;
+import org.apache.catalina.WebResourceSet;
+import org.apache.catalina.util.ResourceSet;
+
+public class JarResourceSet implements WebResourceSet {
+
+    private final WebResourceRoot root;
+    private final JarFile base;
+    private final String baseUrl;
+    private final String webAppMount;
+    private final String internalPath;
+
+    public JarResourceSet(WebResourceRoot root, File base, String webAppMount,
+            String internalPath) throws IllegalArgumentException {
+        this.root = root;
+        try {
+            this.base = new JarFile(base);
+        } catch (IOException ioe) {
+            throw new IllegalArgumentException(ioe);
+        }
+        try {
+            this.baseUrl = base.toURI().toURL().toString();
+        } catch (MalformedURLException e) {
+            throw new IllegalArgumentException(e);
+        }
+        this.webAppMount = webAppMount;
+        this.internalPath = internalPath;
+    }
+
+    @Override
+    public WebResource getResource(String path) {
+        if (path.startsWith(webAppMount)) {
+            StringBuilder pathInJar = new StringBuilder(internalPath);
+            pathInJar.append(path.substring(webAppMount.length()));
+            JarEntry jarEntry = base.getJarEntry(pathInJar.toString());
+            if (jarEntry == null) {
+                return new EmptyResource(root, path);
+            } else {
+                return new JarResource(root, base, baseUrl, jarEntry, path);
+            }
+        } else {
+            return new EmptyResource(root, path);
+        }
+    }
+
+    @Override
+    public String[] list(String path) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public Set<String> listWebAppPaths(String path) {
+        ResourceSet<String> result = new ResourceSet<>();
+        Enumeration<JarEntry> entries = base.entries();
+        while (entries.hasMoreElements()) {
+            JarEntry entry = entries.nextElement();
+            String name = entry.getName();
+            if (name.startsWith(path)) {
+                result.add(name);
+            }
+        }
+        result.setLocked(true);
+        return result;
+    }
+
+    @Override
+    public boolean mkdir(String path) {
+        return false;
+    }
+
+    @Override
+    public boolean write(String path, InputStream is) {
+        return false;
+    }
+}

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/JarResourceSet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/StandardRoot.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/StandardRoot.java?rev=1382552&view=auto
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/StandardRoot.java
 (added)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/StandardRoot.java
 Sun Sep  9 18:15:01 2012
@@ -0,0 +1,274 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.File;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Host;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.LifecycleState;
+import org.apache.catalina.WebResource;
+import org.apache.catalina.WebResourceRoot;
+import org.apache.catalina.WebResourceSet;
+import org.apache.catalina.util.LifecycleMBeanBase;
+
+public class StandardRoot extends LifecycleMBeanBase
+        implements WebResourceRoot {
+
+    private final Context context;
+    private ArrayList<WebResourceSet> preResources = new ArrayList<>();
+    private WebResourceSet main;
+    private ArrayList<WebResourceSet> jarResources = new ArrayList<>();
+    private ArrayList<WebResourceSet> postResources = new ArrayList<>();
+
+    // Constructs to make iteration over all WebResourceSets simpler
+    private ArrayList<WebResourceSet> mainResources = new ArrayList<>();
+    private ArrayList<ArrayList<WebResourceSet>> allResources =
+            new ArrayList<>();
+    {
+        allResources.add(preResources);
+        allResources.add(mainResources);
+        allResources.add(jarResources);
+        allResources.add(postResources);
+    }
+
+
+    public StandardRoot(Context context) {
+        this.context = context;
+    }
+
+    @Override
+    public String[] list(String path) {
+        checkState();
+
+        // Set because we don't want duplicates
+        HashSet<String> result = new HashSet<>();
+        for (ArrayList<WebResourceSet> list : allResources) {
+            for (WebResourceSet webResourceSet : list) {
+                String[] entries = webResourceSet.list(path);
+                for (String entry : entries) {
+                    result.add(entry);
+                }
+            }
+        }
+        return result.toArray(new String[result.size()]);
+    }
+
+
+    @Override
+    public Set<String> listWebAppPaths(String path) {
+        checkState();
+
+        // Set because we don't want duplicates
+        HashSet<String> result = new HashSet<>();
+        for (ArrayList<WebResourceSet> list : allResources) {
+            for (WebResourceSet webResourceSet : list) {
+                result.addAll(webResourceSet.listWebAppPaths(path));
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public boolean mkdir(String path) {
+        checkState();
+        // TODO Check pre-Resources for overrides
+        return main.mkdir(path);
+    }
+
+    @Override
+    public boolean write(String path, InputStream is) {
+        checkState();
+        // TODO Check pre-Resources for overrides
+        return main.write(path, is);
+    }
+
+    @Override
+    public WebResource getResource(String path) {
+        checkState();
+
+        WebResource result = null;
+        for (ArrayList<WebResourceSet> list : allResources) {
+            for (WebResourceSet webResourceSet : list) {
+                result = webResourceSet.getResource(path);
+                if (result.exists()) {
+                    return result;
+                }
+            }
+        }
+
+        // Default is empty resource in main resources
+        return main.getResource(path);
+    }
+
+    @Override
+    public WebResource[] listResources(String path) {
+        checkState();
+
+        String[] resources = list(path);
+        WebResource[] result = new WebResource[resources.length];
+        for (int i = 0; i < resources.length; i++) {
+            result[i] = getResource(path + "/" + resources[i]);
+        }
+        return result;
+    }
+
+
+    @Override
+    public void createWebResourceSet(ResourceSetType type, URL url,
+            String webAppPath, String internalPath) {
+        createWebResourceSet(type, toFile(url), webAppPath, internalPath);
+    }
+
+    @Override
+    public void createWebResourceSet(ResourceSetType type, File file,
+            String webAppPath, String internalPath) {
+
+        ArrayList<WebResourceSet> resourceList;
+        WebResourceSet resourceSet;
+
+        switch (type) {
+            case PRE:
+                resourceList = preResources;
+                break;
+            case RESOURCE_JAR:
+                resourceList = jarResources;
+                break;
+            case POST:
+                resourceList = postResources;
+                break;
+            default:
+                throw new IllegalArgumentException("TODO i18n - Unknown type");
+        }
+
+        if (file.isFile()) {
+            if (file.getName().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
+                resourceSet = new JarResourceSet(this, file, webAppPath,
+                        internalPath);
+            } else {
+                throw new UnsupportedOperationException("No FileResourceSet 
class");
+            }
+        } else if (file.isDirectory()) {
+            resourceSet =
+                    new DirResourceSet(this, file, webAppPath, internalPath);
+        } else {
+            throw new IllegalArgumentException("TODO i18n - invalid file");
+        }
+
+        resourceList.add(resourceSet);
+    }
+
+    @Override
+    public void setAllowLinking(boolean allowLinking) {
+        // TODO Implement this feature
+    }
+
+    @Override
+    public String getContextName() {
+        return context.getName();
+    }
+
+    private void checkState() {
+        if (!getState().isAvailable()) {
+            throw new IllegalStateException(
+                    "TODO - i18n: Can't access resources before they are 
started");
+        }
+    }
+
+    private File toFile(URL url) {
+        File f = null;
+
+        if ("jar".equals(url.getProtocol())) {
+            String jarUrl = url.toString();
+            String fileUrl = jarUrl.substring(4, jarUrl.length() - 2);
+            try {
+                f = new File(new URL(fileUrl).toURI());
+            } catch (MalformedURLException | URISyntaxException e) {
+                throw new IllegalArgumentException(e);
+            }
+        } else {
+            try {
+                f = new File(url.toURI());
+            } catch (URISyntaxException e) {
+                throw new IllegalArgumentException(e);
+            }
+        }
+        return f;
+    }
+
+    // ----------------------------------------------------------- JMX 
Lifecycle
+    @Override
+    protected String getDomainInternal() {
+        return context.getDomain();
+    }
+
+    @Override
+    protected String getObjectNameKeyProperties() {
+        StringBuilder keyProperties = new 
StringBuilder("type=WebResourceRoot");
+        keyProperties.append(context.getMBeanKeyProperties());
+
+        return keyProperties.toString();
+    }
+
+    // --------------------------------------------------------------- 
Lifecycle
+
+    // TODO Review the lifecycle, particularly around context 
start/stop/restart
+    //      Need to consider where resource sets may be defined / identified (
+    //      e.g. context.xml for pre/post, Jar scanning for resource JARs).
+    //      Clarify what gets set/reset when in the Javadoc for this class.
+    @Override
+    protected void startInternal() throws LifecycleException {
+        String docBase = context.getDocBase();
+
+        File f = new File(docBase);
+        if (!f.isAbsolute()) {
+            f = new File(((Host)context.getParent()).getAppBaseFile(), 
f.getName());
+        }
+        if (f.isDirectory()) {
+            main = new DirResourceSet(this, f, "", "");
+        } else if(f.isFile() && docBase.endsWith(".war")) {
+            main = new JarResourceSet(this, f, "", "");
+        } else {
+            throw new IllegalArgumentException(
+                    "TODO - i18n: Invalid main resoucres");
+        }
+
+        mainResources.clear();
+        mainResources.add(main);
+
+        setState(LifecycleState.STARTING);
+    }
+
+    @Override
+    protected void stopInternal() throws LifecycleException {
+        preResources.clear();
+        mainResources.clear();
+        jarResources.clear();
+        postResources.clear();
+
+        setState(LifecycleState.STOPPING);
+    }
+}

Propchange: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/StandardRoot.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to