tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jsdelf...@apache.org
Subject svn commit: r662176 - /incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/
Date Sun, 01 Jun 2008 08:38:08 GMT
Author: jsdelfino
Date: Sun Jun  1 01:38:08 2008
New Revision: 662176

URL: http://svn.apache.org/viewvc?rev=662176&view=rev
Log:
Starting to implement some caching of contribution metadata in domain manager app.

Modified:
    incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/CompositeGeneratorServiceImpl.java
    incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/ContributionCollectionImpl.java
    incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployableCompositeCollectionImpl.java
    incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployedCompositeCollectionImpl.java
    incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DomainManagerUtil.java

Modified: incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/CompositeGeneratorServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/CompositeGeneratorServiceImpl.java?rev=662176&r1=662175&r2=662176&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/CompositeGeneratorServiceImpl.java
(original)
+++ incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/CompositeGeneratorServiceImpl.java
Sun Jun  1 01:38:08 2008
@@ -21,6 +21,7 @@
 
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.compositeQName;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.contributionURI;
+import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.lastModified;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.locationURL;
 
 import java.io.IOException;
@@ -28,6 +29,8 @@
 import java.net.URI;
 import java.net.URL;
 import java.net.URLDecoder;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.servlet.Servlet;
@@ -58,6 +61,7 @@
 import org.apache.tuscany.sca.contribution.service.ContributionWriteException;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.core.UtilityExtensionPoint;
+import org.apache.tuscany.sca.domain.manager.impl.CompositeGeneratorServiceImpl.Cache.ContributionCache;
 import org.apache.tuscany.sca.implementation.data.collection.Item;
 import org.apache.tuscany.sca.implementation.data.collection.LocalItemCollection;
 import org.apache.tuscany.sca.implementation.data.collection.NotFoundException;
@@ -94,6 +98,19 @@
     private XMLOutputFactory outputFactory;
     
     /**
+     * Cache contribution models. 
+     */
+    static class Cache {
+        static class ContributionCache {
+            private Contribution contribution;
+            private long contributionLastModified;
+        }
+        private Map<URL, ContributionCache> contributions = new HashMap<URL, ContributionCache>();
+    }
+    
+    private Cache cache = new Cache();
+    
+    /**
      * Initialize the component.
      */
     @Init
@@ -199,15 +216,37 @@
         try {
             URI uri = URI.create(contributionURI);
             URL location = locationURL(contributionLocation);
+
+            // Get contribution from cache
+            ContributionCache contributionCache = cache.contributions.get(location);
+            long lastModified = lastModified(location);
+            if (contributionCache != null) {
+                if (contributionCache.contributionLastModified == lastModified) {
+                    return contributionCache.contribution;
+                }
+                
+                // Reset contribution cache
+                cache.contributions.remove(location);
+            }
+            
             Contribution contribution = (Contribution)contributionProcessor.read(null, uri,
location);
             
             contributionProcessor.resolve(contribution, new DefaultModelResolver());
+
+            // Cache contribution
+            contributionCache = new ContributionCache();
+            contributionCache.contribution = contribution;
+            contributionCache.contributionLastModified = lastModified;
+            cache.contributions.put(location, contributionCache);
+            
             return contribution;
 
         } catch (ContributionReadException e) {
             throw e;
         } catch (MalformedURLException e) {
             throw new ContributionReadException(e);
+        } catch (IOException e) {
+            throw new ContributionReadException(e);
         } catch (ContributionResolveException e) {
             throw new ContributionReadException(e);
         }

Modified: incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/ContributionCollectionImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/ContributionCollectionImpl.java?rev=662176&r1=662175&r2=662176&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/ContributionCollectionImpl.java
(original)
+++ incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/ContributionCollectionImpl.java
Sun Jun  1 01:38:08 2008
@@ -22,6 +22,7 @@
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.DEPLOYMENT_CONTRIBUTION_URI;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.compositeSimpleTitle;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.compositeSourceLink;
+import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.lastModified;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.locationURL;
 
 import java.io.ByteArrayInputStream;
@@ -33,7 +34,9 @@
 import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.xml.namespace.QName;
@@ -57,6 +60,7 @@
 import org.apache.tuscany.sca.contribution.service.ContributionReadException;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.core.UtilityExtensionPoint;
+import org.apache.tuscany.sca.domain.manager.impl.ContributionCollectionImpl.Cache.ContributionCache;
 import org.apache.tuscany.sca.implementation.data.collection.Entry;
 import org.apache.tuscany.sca.implementation.data.collection.Item;
 import org.apache.tuscany.sca.implementation.data.collection.ItemCollection;
@@ -109,6 +113,22 @@
     private DocumentBuilder documentBuilder;
     
     /**
+     * Cache workspace and contribution models. 
+     */
+    static class Cache {
+        private Workspace workspace;
+        private long workspaceLastModified;
+        
+        static class ContributionCache {
+            private Contribution contribution;
+            private long contributionLastModified;
+        }
+        private Map<URL, ContributionCache> contributions = new HashMap<URL, ContributionCache>();
+    }
+    
+    private Cache cache = new Cache();
+    
+    /**
      * Initialize the component.
      */
     @Init
@@ -392,16 +412,35 @@
         Workspace workspace;
         File file = new File(rootDirectory + "/" + workspaceFile);
         if (file.exists()) {
-            try {
-                FileInputStream is = new FileInputStream(file);
-                XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
-                reader.nextTag();
-                workspace = (Workspace)staxProcessor.read(reader);
-            } catch (Exception e) {
-                throw new ServiceRuntimeException(e);
+            
+            // Get workspace from cache
+            if (cache.workspace != null && file.lastModified() == cache.workspaceLastModified)
{
+                workspace = cache.workspace;
+                
+            } else {
+                
+                try {
+                    FileInputStream is = new FileInputStream(file);
+                    XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
+                    reader.nextTag();
+                    workspace = (Workspace)staxProcessor.read(reader);
+                } catch (Exception e) {
+                    throw new ServiceRuntimeException(e);
+                }
+
+                // Cache workspace
+                cache.workspaceLastModified = file.lastModified();
+                cache.workspace = workspace;
             }
+            
         } else {
+            
+            // Create new workspace
             workspace = workspaceFactory.createWorkspace();
+
+            // Cache workspace
+            cache.workspaceLastModified = 0;
+            cache.workspace = workspace;
         }
         
         // Make sure that the workspace contains the cloud contribution
@@ -420,6 +459,7 @@
             contribution.setLocation(cloudDirectory.toURI().toString());
             workspace.getContributions().add(contribution);
         }
+        
         return workspace;
     }
     
@@ -444,10 +484,16 @@
             format.setIndent(2);
             
             // Write to workspace.xml
-            FileOutputStream os = new FileOutputStream(new File(rootDirectory + "/" + workspaceFile));
+            File file = new File(rootDirectory + "/" + workspaceFile);
+            FileOutputStream os = new FileOutputStream(file);
             XMLSerializer serializer = new XMLSerializer(os, format);
             serializer.serialize(document);
             os.close();
+            
+            // Cache workspace
+            cache.workspace = workspace;
+            cache.workspaceLastModified = file.lastModified();
+            
         } catch (Exception e) {
             throw new ServiceRuntimeException(e);
         }
@@ -461,27 +507,51 @@
      * @return
      */
     private Workspace readContributions(Workspace workspace) {
-        Workspace dependencyWorkspace = workspaceFactory.createWorkspace();
+        
+        Workspace contributions = workspaceFactory.createWorkspace();
         try {
             for (Contribution c: workspace.getContributions()) {
                 URI uri = URI.create(c.getURI());
-                URL url = locationURL(c.getLocation());
+                URL location = locationURL(c.getLocation());
+                
+                // Get contribution from cache
+                ContributionCache contributionCache = cache.contributions.get(location);
+                long lastModified = lastModified(location);
+                if (contributionCache != null) {
+                    if (contributionCache.contributionLastModified == lastModified) {
+                        Contribution contribution = contributionCache.contribution;
+                        contribution.setUnresolved(false);
+                        contributions.getContributions().add(contribution);
+                        continue;
+                    }
+                    
+                    // Reset contribution cache
+                    cache.contributions.remove(location);
+                }
+                
                 try {
-                    Contribution contribution = (Contribution)contributionProcessor.read(null,
uri, url);
+                    Contribution contribution = (Contribution)contributionProcessor.read(null,
uri, location);
                     contribution.setUnresolved(false);
-                    dependencyWorkspace.getContributions().add(contribution);
+                    contributions.getContributions().add(contribution);
+                    
+                    // Cache contribution
+                    contributionCache = new ContributionCache();
+                    contributionCache.contribution = contribution;
+                    contributionCache.contributionLastModified = lastModified;
+                    cache.contributions.put(location, contributionCache);
+                    
                 } catch (ContributionReadException e) {
                     Contribution contribution = contributionFactory.createContribution();
                     contribution.setURI(c.getURI());
                     contribution.setLocation(c.getLocation());
                     contribution.setUnresolved(true);
-                    dependencyWorkspace.getContributions().add(contribution);
+                    contributions.getContributions().add(contribution);
                 }
             }
         } catch (Exception e) {
             throw new ServiceRuntimeException(e);
         }
-        return dependencyWorkspace;
+        return contributions;
     }
     
 }

Modified: incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployableCompositeCollectionImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployableCompositeCollectionImpl.java?rev=662176&r1=662175&r2=662176&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployableCompositeCollectionImpl.java
(original)
+++ incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployableCompositeCollectionImpl.java
Sun Jun  1 01:38:08 2008
@@ -25,13 +25,16 @@
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.compositeSourceLink;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.compositeTitle;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.contributionURI;
+import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.lastModified;
 import static org.apache.tuscany.sca.domain.manager.impl.DomainManagerUtil.locationURL;
 
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.xml.namespace.QName;
@@ -57,6 +60,7 @@
 import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.core.UtilityExtensionPoint;
+import org.apache.tuscany.sca.domain.manager.impl.DeployableCompositeCollectionImpl.Cache.ContributionCache;
 import org.apache.tuscany.sca.implementation.data.collection.Entry;
 import org.apache.tuscany.sca.implementation.data.collection.Item;
 import org.apache.tuscany.sca.implementation.data.collection.ItemCollection;
@@ -98,6 +102,19 @@
     private Monitor monitor;
     
     /**
+     * Cache contribution models. 
+     */
+    static class Cache {
+        static class ContributionCache {
+            private Contribution contribution;
+            private long contributionLastModified;
+        }
+        private Map<URL, ContributionCache> contributions = new HashMap<URL, ContributionCache>();
+    }
+    
+    private Cache cache = new Cache();
+    
+    /**
      * Initialize the component.
      */
     @Init
@@ -252,9 +269,29 @@
         try {
             URI uri = URI.create(contributionURI);
             URL location = locationURL(contributionLocation);
+            
+            // Get contribution from cache
+            ContributionCache contributionCache = cache.contributions.get(location);
+            long lastModified = lastModified(location);
+            if (contributionCache != null) {
+                if (contributionCache.contributionLastModified == lastModified) {
+                    return contributionCache.contribution;
+                }
+                
+                // Reset contribution cache
+                cache.contributions.remove(location);
+            }
+            
             Contribution contribution = (Contribution)contributionProcessor.read(null, uri,
location);
             
             contributionProcessor.resolve(contribution, new DefaultModelResolver());
+            
+            // Cache contribution
+            contributionCache = new ContributionCache();
+            contributionCache.contribution = contribution;
+            contributionCache.contributionLastModified = lastModified;
+            cache.contributions.put(location, contributionCache);
+            
             return contribution;
 
         } catch (ContributionReadException e) {

Modified: incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployedCompositeCollectionImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployedCompositeCollectionImpl.java?rev=662176&r1=662175&r2=662176&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployedCompositeCollectionImpl.java
(original)
+++ incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DeployedCompositeCollectionImpl.java
Sun Jun  1 01:38:08 2008
@@ -114,6 +114,16 @@
     private DocumentBuilder documentBuilder;
     
     /**
+     * Cache domain composite model. 
+     */
+    static class Cache {
+        private Composite compositeCollection;
+        private long compositeCollectionLastModified;
+    }
+    
+    private Cache cache = new Cache();
+    
+    /**
      * Initialize the component.
      */
     @Init
@@ -352,14 +362,27 @@
         Composite compositeCollection;
         File file = new File(rootDirectory + "/" + compositeFile);
         if (file.exists()) {
-            XMLInputFactory inputFactory = modelFactories.getFactory(XMLInputFactory.class);
-            try {
-                FileInputStream is = new FileInputStream(file);
-                XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
-                compositeCollection = compositeProcessor.read(reader);
-            } catch (Exception e) {
-                throw new ServiceRuntimeException(e);
+
+            // Get composite collection from cache
+            if (cache.compositeCollection != null && file.lastModified() == cache.compositeCollectionLastModified)
{
+                compositeCollection = cache.compositeCollection;
+                
+            } else {
+                    
+                XMLInputFactory inputFactory = modelFactories.getFactory(XMLInputFactory.class);
+                try {
+                    FileInputStream is = new FileInputStream(file);
+                    XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
+                    compositeCollection = compositeProcessor.read(reader);
+                } catch (Exception e) {
+                    throw new ServiceRuntimeException(e);
+                }
+                
+                // Cache composite collection
+                cache.compositeCollectionLastModified = file.lastModified();
+                cache.compositeCollection = compositeCollection;
             }
+            
         } else {
             compositeCollection = assemblyFactory.createComposite();
             String name;
@@ -370,6 +393,10 @@
                 name = compositeFile;
             }
             compositeCollection.setName(new QName(Constants.SCA10_TUSCANY_NS, name));
+
+            // Cache composite collection
+            cache.compositeCollectionLastModified = 0;
+            cache.compositeCollection = compositeCollection;
         }
         return compositeCollection;
     }
@@ -395,9 +422,15 @@
             format.setIndent(2);
             
             // Write to domain.composite
-            FileOutputStream os = new FileOutputStream(new File(rootDirectory + "/" + compositeFile));
+            File file = new File(rootDirectory + "/" + compositeFile);
+            FileOutputStream os = new FileOutputStream(file);
             XMLSerializer serializer = new XMLSerializer(os, format);
             serializer.serialize(document);
+
+            // Cache composite collection
+            cache.compositeCollection = compositeCollection;
+            cache.compositeCollectionLastModified = file.lastModified();
+            
         } catch (Exception e) {
             throw new ServiceRuntimeException(e);
         }

Modified: incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DomainManagerUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DomainManagerUtil.java?rev=662176&r1=662175&r2=662176&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DomainManagerUtil.java
(original)
+++ incubator/tuscany/java/sca/modules/domain-manager/src/main/java/org/apache/tuscany/sca/domain/manager/impl/DomainManagerUtil.java
Sun Jun  1 01:38:08 2008
@@ -20,9 +20,11 @@
 package org.apache.tuscany.sca.domain.manager.impl;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
+import java.net.URLConnection;
 
 import javax.xml.namespace.QName;
 
@@ -239,4 +241,44 @@
         }
     }
 
+    /**
+     * Returns the last modified time of the content at the given URL.
+     * 
+     * @param url
+     * @return
+     * @throws IOException
+     */
+    static long lastModified(URL url) throws IOException {
+        
+        if (url.getProtocol() == null || "file".equals(url.getProtocol())) {
+            return lastModified(new File(url.getPath()));
+        } else {
+            URLConnection connection = url.openConnection();
+            long lastModified = connection.getLastModified();
+            return lastModified;
+        }
+    }
+
+    /**
+     * Returns the last modified time of the given file or directory.
+     *  
+     * @param file
+     * @return
+     */
+    static long lastModified(File file) {
+        if (file.isDirectory()) {
+            long lastModified = file.lastModified();
+            
+            for (File child: file.listFiles()) {
+                long m = lastModified(child);
+                if (m > lastModified) {
+                    lastModified = m;
+                }
+            }
+            return lastModified;
+            
+        } else {
+            return file.lastModified();
+        }
+    }
 }



Mime
View raw message