/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.virtual.plugins.registry;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.VFSUtils;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
import org.jboss.virtual.spi.TempInfo;
import org.jboss.virtual.spi.VFSContextConstraints;
import org.jboss.virtual.spi.cache.VFSCache;
import org.jboss.virtual.spi.cache.VFSCacheFactory;
import org.jboss.virtual.spi.registry.VFSRegistry;

/**
 * Default vfs registry.
 *
 * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
 */
public class DefaultVFSRegistry extends VFSRegistry
{
   /**
    * Get vfs cache.
    *
    * @return the vfs cache
    */
   protected VFSCache getCache()
   {
      return VFSCacheFactory.getInstance();
   }

   public void addContext(VFSContext context)
   {
      if (context.getConstraints().contains(VFSContextConstraints.CACHEABLE))
      {
         getCache().putContext(context);
      }
   }

   public void removeContext(VFSContext context)
   {
      if (context.getConstraints().contains(VFSContextConstraints.CACHEABLE))
      {
         getCache().removeContext(context);
      }
   }

   public VirtualFile getFile(URI uri) throws IOException
   {
      if (uri == null)
         throw new IllegalArgumentException("Null uri");

      VFSContext context = getCache().findContext(uri);
      if (context != null)
      {
         String relativePath = VFSUtils.getRelativePath(context, uri);

         TempInfo ti = context.getFurthestParentTemp(relativePath);
         if (ti != null)
         {
            String path = ti.getPath();
            String subpath = relativePath.substring(path.length());
            VirtualFileHandler child = findHandler(ti.getHandler(), subpath, true);
            if (child != null)
                  return child.getVirtualFile();
         }

         VirtualFileHandler root = context.getRoot();
         VirtualFileHandler child = findHandler(root, relativePath, false);
         return child.getVirtualFile();
      }
      return null;
   }

   /**
    * Find the handler.
    *
    * @param root the root
    * @param path the path
    * @return child handler
    * @param allowNotFound do we allow not found
    * @throws IOException for any error
    */
   protected VirtualFileHandler findHandler(VirtualFileHandler root, String path, boolean allowNotFound) throws IOException
   {
      VirtualFileHandler child = root.getChild(path);
      if (child == null && allowNotFound == false)
      {
         List<VirtualFileHandler> children = root.getChildren(true);
         throw new IOException("Child not found " + path + " for " + root + ", available children: " + children);
      }
      return child;
   }

   public VirtualFile getFile(URL url) throws IOException
   {
      if (url == null)
         throw new IllegalArgumentException("Null url");

      try
      {
         return getFile(VFSUtils.toURI(url));
      }
      catch (URISyntaxException e)
      {
         IOException ioe = new IOException();
         ioe.initCause(e);
         throw ioe;
      }
   }
}

@@ -1226,16 +1226,39 @@
 l.List;%0D
+%0Aimport java.util.Set;%0D
 %0A%0D%0Aimpor
@@ -1278,26 +1278,23 @@
 irtual.V
-irtualFile
+FSUtils
 ;%0D%0Aimpor
@@ -1306,39 +1306,42 @@
 .jboss.virtual.V
-FSUtils
+irtualFile
 ;%0D%0Aimport org.jb
@@ -1356,26 +1356,24 @@
 ual.spi.
-VFSContext
+TempInfo
 ;%0D%0Aimpor
@@ -1397,33 +1397,25 @@
 al.spi.V
-irtualFileHandler
+FSContext
 ;%0D%0Aimpor
@@ -1434,32 +1434,45 @@
 virtual.spi.
-TempInfo
+VFSContextConstraints
 ;%0D%0Aimport or
@@ -1492,36 +1492,33 @@
 al.spi.V
-FSContextConstraints
+irtualFileHandler
 ;%0D%0Aimpor
@@ -2002,133 +2002,480 @@
 %0A   
-public void addContext(VFSContext context)%0D%0A   %7B%0D%0A      if (context.getConstraints().contains(VFSContextConstraints.CACHEABLE
+/**%0D%0A    * Is the vfs context cacheable.%0D%0A    *%0D%0A    * @param context the vfs context%0D%0A    * @return true if context is cacheable, false otherwise%0D%0A    */%0D%0A   protected boolean isCacheable(VFSContext context)%0D%0A   %7B%0D%0A      Set%3CVFSContextConstraints%3E constraints = context.getConstraints();%0D%0A      return constraints != null && constraints.contains(VFSContextConstraints.CACHEABLE);%0D%0A   %7D%0D%0A%0D%0A   public void addContext(VFSContext context)%0D%0A   %7B%0D%0A      if (isCacheable(context
 ))%0D%0A
@@ -2612,73 +2612,27 @@
 if (
-context.getConstraints().contains(VFSContextConstraints.CACHEABLE
+isCacheable(context
 ))%0D%0A

Refactor cachable check.