tidy up handling of caching local obr file


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@814601 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/AbstractOBRBundleRepository.java b/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/AbstractOBRBundleRepository.java
index 3a9e07d..6b5484c 100644
--- a/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/AbstractOBRBundleRepository.java
+++ b/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/AbstractOBRBundleRepository.java
@@ -29,10 +29,12 @@
 import java.net.URL;
 import java.net.URLConnection;
 
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
 import org.apache.felix.sigil.repository.AbstractBundleRepository;
+import org.xml.sax.SAXException;
 
 
 public abstract class AbstractOBRBundleRepository extends AbstractBundleRepository
@@ -61,64 +63,86 @@
     }
 
 
-    protected void readBundles( OBRListener listener ) throws Exception
+    protected void readBundles( OBRListener listener ) 
     {
-        syncOBRIndex();
+        File index = syncOBRIndex();
         OBRHandler handler = new OBRHandler( getObrURL(), getBundleCache(), listener );
-        SAXParser parser = factory.newSAXParser();
-        parser.parse( findLocalOBR(), handler );
+        try
+        {
+            SAXParser parser = factory.newSAXParser();
+            parser.parse( index, handler );
+        }
+        catch ( ParserConfigurationException e )
+        {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        catch ( SAXException e )
+        {
+            System.out.println( "Failed to parse " + index );
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        catch ( IOException e )
+        {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
     }
 
 
-    private File findLocalOBR()
+    private File syncOBRIndex()
     {
+        File file = null;
         if ( "file".equals( getObrURL().getProtocol() ) ) {
             try
             {
-               return new File( getObrURL().toURI() );
+               file = new File( getObrURL().toURI() );
             }
             catch ( URISyntaxException e )
             {
                 // should be impossible ?
                 throw new IllegalStateException( "Failed to convert file url to uri", e );
-            }
+            }            
         }
         else {
-            return getObrlCache();
+            file = getObrlCache();
+            if ( isUpdated() )
+            {
+                cacheIndex(file);
+            }
         }
+        
+        return file;
     }
 
 
-    private void syncOBRIndex()
+    private void cacheIndex(File file)
     {
-        if ( !"file".equals( getObrURL().getProtocol() ) && isUpdated() )
-        {
-            InputStream in = null;
-            OutputStream out = null;
+        InputStream in = null;
+        OutputStream out = null;
 
-            try
+        try
+        {
+            URLConnection c = getObrURL().openConnection();
+            c.connect();
+            in = c.getInputStream();
+            if ( !file.getParentFile().exists() && !file.getParentFile().mkdirs() )
             {
-                URLConnection c = getObrURL().openConnection();
-                c.connect();
-                in = c.getInputStream();
-                File file = getObrlCache();
-                if ( !file.getParentFile().exists() && !file.getParentFile().mkdirs() )
-                {
-                    throw new IOException( "Failed to create obr cache dir " + file.getParentFile() );
-                }
-                out = new FileOutputStream( file );
-                stream( in, out );
+                throw new IOException( "Failed to create obr cache dir " + file.getParentFile() );
             }
-            catch ( IOException e )
-            {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-                getObrlCache().setLastModified( 0 );
-            }
-            finally
-            {
-                close( in, out );
-            }
+            out = new FileOutputStream( file );
+            stream( in, out );
+        }
+        catch ( IOException e )
+        {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+            getObrlCache().setLastModified( 0 );
+        }
+        finally
+        {
+            close( in, out );
         }
     }
 
diff --git a/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/CachingOBRBundleRepository.java b/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/CachingOBRBundleRepository.java
index eb438ff..8fb7dea 100644
--- a/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/CachingOBRBundleRepository.java
+++ b/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/CachingOBRBundleRepository.java
@@ -71,24 +71,16 @@
         List<ISigilBundle> cached = bundles == null ? null : bundles.get();
         if ( cached == null )
         {
-            try
+            final LinkedList<ISigilBundle> read = new LinkedList<ISigilBundle>();
+            readBundles( new OBRListener()
             {
-                final LinkedList<ISigilBundle> read = new LinkedList<ISigilBundle>();
-                readBundles( new OBRListener()
+                public void handleBundle( ISigilBundle bundle )
                 {
-                    public void handleBundle( ISigilBundle bundle )
-                    {
-                        read.add( bundle );
-                    }
-                } );
-                cached = read;
-                bundles = new SoftReference<List<ISigilBundle>>( cached );
-            }
-            catch ( Exception e )
-            {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-            }
+                    read.add( bundle );
+                }
+            } );
+            cached = read;
+            bundles = new SoftReference<List<ISigilBundle>>( cached );
         }
 
         return cached;
diff --git a/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/NonCachingOBRBundleRepository.java b/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/NonCachingOBRBundleRepository.java
index a5bd2e4..8e34224 100644
--- a/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/NonCachingOBRBundleRepository.java
+++ b/sigil/common/obr/src/org/apache/felix/sigil/obr/impl/NonCachingOBRBundleRepository.java
@@ -55,27 +55,19 @@
     @Override
     public void accept( final IRepositoryVisitor visitor, int options )
     {
-        try
+        readBundles( new OBRListener()
         {
-            readBundles( new OBRListener()
+            boolean visit = true;
+
+
+            public void handleBundle( ISigilBundle bundle )
             {
-                boolean visit = true;
-
-
-                public void handleBundle( ISigilBundle bundle )
+                if ( visit )
                 {
-                    if ( visit )
-                    {
-                        visit = visitor.visit( bundle );
-                    }
+                    visit = visitor.visit( bundle );
                 }
-            } );
-        }
-        catch ( Exception e )
-        {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
+            }
+        } );
     }
 
 }