You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/02/14 08:28:47 UTC

svn commit: r507425 - in /incubator/tuscany/java/sca/kernel/core: ./ src/main/java/org/apache/tuscany/core/services/deployment/ src/test/java/org/apache/tuscany/core/services/deployment/

Author: rfeng
Date: Tue Feb 13 23:28:47 2007
New Revision: 507425

URL: http://svn.apache.org/viewvc?view=rev&rev=507425
Log:
Clean up the ContributionRepositoryImpl

Modified:
    incubator/tuscany/java/sca/kernel/core/pom.xml
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java

Modified: incubator/tuscany/java/sca/kernel/core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/pom.xml?view=diff&rev=507425&r1=507424&r2=507425
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/pom.xml (original)
+++ incubator/tuscany/java/sca/kernel/core/pom.xml Tue Feb 13 23:28:47 2007
@@ -1,21 +1,21 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
- * 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.    
+    * 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.    
 -->
 <project>
     <parent>
@@ -36,14 +36,14 @@
             <version>${pom.version}</version>
             <scope>compile</scope>
         </dependency>
-        
+
         <dependency>
             <groupId>org.apache.tuscany.sca.kernel</groupId>
             <artifactId>tuscany-host-api</artifactId>
             <version>${sca.version}</version>
             <scope>compile</scope>
         </dependency>
-        
+
         <dependency>
             <groupId>org.apache.tuscany</groupId>
             <artifactId>commonj-api_r1.1</artifactId>
@@ -62,26 +62,26 @@
         <dependency>
             <groupId>org.apache.geronimo.specs</groupId>
             <artifactId>geronimo-j2ee-connector_1.5_spec</artifactId>
-				<version>1.0.1</version>
+            <version>1.0.1</version>
         </dependency>
 
         <dependency>
             <groupId>commons-codec</groupId>
             <artifactId>commons-codec</artifactId>
-				<version>1.3</version>
+            <version>1.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>1.3</version>
         </dependency>
-        
-		<dependency>
-			<groupId>commons-io</groupId>
-			<artifactId>commons-io</artifactId>
-			<version>1.3</version>
-		</dependency>
 
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
-        
+
         <dependency>
             <groupId>org.easymock</groupId>
             <artifactId>easymockclassextension</artifactId>

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java?view=diff&rev=507425&r1=507424&r2=507425
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java Tue Feb 13 23:28:47 2007
@@ -23,7 +23,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -31,147 +30,50 @@
 import java.util.Map;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
 import org.apache.tuscany.core.util.IOUtils;
 import org.apache.tuscany.spi.deployer.ContributionRepository;
+import org.osoa.sca.annotations.Property;
 
 public class ContributionRepositoryImpl implements ContributionRepository {
     protected final File rootFile;
     protected final Map<URI, URL> reposirotyContent = new HashMap<URI, URL>();
 
     /**
-     * Constructor with repository root URI
+     * Constructor with repository root
      * 
      * @param root
      */
-    public ContributionRepositoryImpl(URI root) {
-        this.rootFile = resolveRoot(root);
-    }
-
-    /**
-     * Constructor with repository root directory
-     * 
-     * @param rootFile
-     */
-    public ContributionRepositoryImpl(File rootFile) {
-        if (rootFile == null)
-            throw new NullPointerException("root is null");
-
-        if (!rootFile.exists() || !rootFile.isDirectory() || !rootFile.canRead()) {
-            throw new IllegalStateException("FileSystemRepository must have a root that's a valid readable directory (not "
-                    + rootFile.getAbsolutePath() + ")");
-        }
-
-        this.rootFile = rootFile;
-    }
-
-    /**
-     * Constructor with repository root directory
-     * 
-     * @param rootFile
-     */
-    public ContributionRepositoryImpl(File rootFile, boolean forceCreation) {
-        if (rootFile == null)
-            throw new NullPointerException("root is null");
-
-        if (!rootFile.exists() && forceCreation) {
-            // force creation of the repository
-            rootFile.mkdirs();
-        }
-
+    public ContributionRepositoryImpl(@Property String root) throws IOException {
+        this.rootFile = new File(root);
+        FileUtils.forceMkdir(rootFile);
         if (!rootFile.exists() || !rootFile.isDirectory() || !rootFile.canRead()) {
-            throw new IllegalStateException("FileSystemRepository must have a root that's a valid readable directory (not "
-                    + rootFile.getAbsolutePath() + ")");
-        }
-
-        this.rootFile = rootFile;
-    }
-
-    /**
-     * Resolve root URI to a directory on the fileSystem
-     * 
-     * @param root
-     * @return
-     */
-    private static File resolveRoot(URI root) {
-        if (root == null)
-            throw new NullPointerException("root is null");
-
-        if (!root.toString().endsWith("/")) {
-            try {
-                root = new URI(root.toString() + "/");
-            } catch (URISyntaxException e) {
-                throw new RuntimeException("Invalid repository root (does not end with / ) and can't add myself", e);
-            }
-        }
-
-        URI resolvedUri = root;
-
-        if (!resolvedUri.getScheme().equals("file")) {
-            throw new IllegalStateException("FileSystemRepository must have a root that's a local directory (not " + resolvedUri + ")");
+            throw new IOException("The root is not a directory: " + root);
         }
-
-        File rootFile = new File(resolvedUri);
-        return rootFile;
     }
 
     /**
-     * Helper method to get a filename from a URL
+     * Resolve contribution location in the repository -> root repository /
+     * contribution file -> contribution group id / artifact id / version
      * 
      * @param contribution
      * @return
      */
-    private String getUrlFilename(URL contribution) {
-        String contributionFileName = contribution.getPath();
-        int indexSlash = contributionFileName.lastIndexOf("/");
-
-        return contributionFileName.substring(indexSlash + 1);
-    }
-
-    /**
-     * Resolve contribution location in the repository -> root repository / contribution file -> contribution group id / artifact id / version
-     * 
-     * @param contribution
-     * @return
-     */
-    private File resolveContributionLocation(URL contribution) {
-        String resolvedContributionLocation = rootFile.getPath() + File.separatorChar + getUrlFilename(contribution);
-
-        return new File(resolvedContributionLocation);
-    }
-
-    /**
-     * Check if an specific contribution is available on the repository
-     * 
-     * @param artifact
-     * @return
-     */
-    public boolean contains(URL contribution) {
-        File location = resolveContributionLocation(contribution);
-        return location.canRead() && location.isFile();
+    private File mapToFile(URI contribution) {
+        // FIXME: Map the contribution URI to a file?
+        return new File(rootFile, FilenameUtils.getName(contribution.toString()));
     }
 
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.tuscany.spi.deployer.contribution.ContributionRepository#copyToRepository(java.net.URL, java.io.InputStream)
-     */
     public URL store(URI contribution, InputStream contributionStream) throws IOException {
-        // is this a writable repository
-        if (!rootFile.canWrite()) {
-            throw new IllegalStateException("This repository is not writable: " + rootFile.getAbsolutePath() + ")");
-        }
-
         // where the file should be stored in the repository
-        File location = resolveContributionLocation(contribution.toURL());
+        File location = mapToFile(contribution);
 
-        // assure that there isn't already a contribution on the resolved location
-        if (location.exists()) {
-            throw new IllegalArgumentException("Destination " + location.getAbsolutePath() + " already exists!");
+        try {
+            IOUtils.write(contributionStream, location);
+        } finally {
+            IOUtils.closeQuietly(contributionStream);
         }
 
-        IOUtils.write(contributionStream, location);
-
         // add contribution to repositoryContent
         URL contributionURL = location.toURL();
         reposirotyContent.put(contribution, contributionURL);
@@ -179,45 +81,28 @@
         return contributionURL;
     }
 
-    /* (non-Javadoc)
-     * @see org.apache.tuscany.spi.deployer.ContributionRepository#find(java.net.URI)
-     */
     public URL find(URI contribution) {
-        if(contribution == null){
-            throw new IllegalArgumentException("Invalid contribution URI : null");
+        if (contribution == null) {
+            return null;
         }
-        
         return this.reposirotyContent.get(contribution);
     }
-    
-    /* (non-Javadoc)
-     * @see org.apache.tuscany.spi.deployer.ContributionRepository#remove(java.net.URI)
-     */
-    public void remove(URI contribution){
+
+    public void remove(URI contribution) {
         URL contributionURL = this.find(contribution);
-        if(contributionURL != null){
-            //remove
-            try{
+        if (contributionURL != null) {
+            // remove
+            try {
                 FileUtils.forceDelete(FileUtils.toFile(contributionURL));
                 this.reposirotyContent.remove(contribution);
-            }catch(IOException ioe){
-                //handle file could not be removed
+            } catch (IOException ioe) {
+                // handle file could not be removed
             }
         }
     }
 
-
-    /* (non-Javadoc)
-     * @see org.apache.tuscany.spi.deployer.ContributionRepository#list()
-     */
-    public List<URI> list(){
-        List<URI> reposirotyList = new ArrayList<URI>(this.reposirotyContent.size());
-        
-        for(URI contributionURI : this.reposirotyContent.keySet()){
-            reposirotyList.add(contributionURI);
-        }
-        return reposirotyList;
-        
+    public List<URI> list() {
+        return new ArrayList<URI>(reposirotyContent.keySet());
     }
 
 }

Modified: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java?view=diff&rev=507425&r1=507424&r2=507425
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java Tue Feb 13 23:28:47 2007
@@ -19,59 +19,49 @@
 
 package org.apache.tuscany.core.services.deployment;
 
-import java.io.File;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
 
 import junit.framework.TestCase;
 
-import org.apache.commons.io.FileUtils;
-
 public class ContributionRepositoryTestCase extends TestCase {
     protected ContributionRepositoryImpl repository;
-    protected File rootRepositoryDir;
 
     protected void setUp() throws Exception {
         super.setUp();
-        this.rootRepositoryDir = new File("target/repository");
-
-        //clean repository
-        FileUtils.deleteDirectory(rootRepositoryDir);
+        // create repository (this should re-create the root directory)
+        this.repository = new ContributionRepositoryImpl("target/repository");
 
-        //create repository (this should re-create the root directory)
-        this.repository = new ContributionRepositoryImpl(rootRepositoryDir, true);
-        
     }
 
-    public void testStore() throws Exception{
+    public void testStore() throws Exception {
         String resourceLocation = "/repository/sample-calculator.jar";
         URI contribution = getClass().getResource(resourceLocation).toURI();
         InputStream contributionStream = getClass().getResourceAsStream(resourceLocation);
-        repository.store(contribution,contributionStream);
-        
+        repository.store(contribution, contributionStream);
+
         URL contributionURL = repository.find(contribution);
         assertNotNull(contributionURL);
     }
-        
+
     public void testRemove() throws Exception {
         String resourceLocation = "/repository/sample-calculator.jar";
         URI contribution = getClass().getResource(resourceLocation).toURI();
         InputStream contributionStream = getClass().getResourceAsStream(resourceLocation);
-        repository.store(contribution,contributionStream);
-        
+        repository.store(contribution, contributionStream);
+
         repository.remove(contribution);
         URL contributionURL = repository.find(contribution);
         assertNull(contributionURL);
     }
-    
-    
-    public void testList() throws Exception{
+
+    public void testList() throws Exception {
         String resourceLocation = "/repository/sample-calculator.jar";
         URI contribution = getClass().getResource(resourceLocation).toURI();
         InputStream contributionStream = getClass().getResourceAsStream(resourceLocation);
-        repository.store(contribution,contributionStream);
-        
-        assertEquals(1, repository.list().size());        
+        repository.store(contribution, contributionStream);
+
+        assertEquals(1, repository.list().size());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org