You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2007/02/14 07:51:15 UTC
svn commit: r507421 - in
/incubator/tuscany/branches/sca-java-integration/sca/kernel: core/
core/src/main/java/org/apache/tuscany/core/services/deployment/
core/src/main/java/org/apache/tuscany/core/util/
core/src/test/java/org/apache/tuscany/core/serv...
Author: lresende
Date: Tue Feb 13 22:51:14 2007
New Revision: 507421
URL: http://svn.apache.org/viewvc?view=rev&rev=507421
Log:
Contribution repository
Added:
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOUtils.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/resources/repository/
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/resources/repository/sample-calculator.jar (with props)
incubator/tuscany/branches/sca-java-integration/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/deployer/ContributionRepository.java
Modified:
incubator/tuscany/branches/sca-java-integration/sca/kernel/core/pom.xml
Modified: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/pom.xml?view=diff&rev=507421&r1=507420&r2=507421
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/pom.xml (original)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/pom.xml Tue Feb 13 22:51:14 2007
@@ -60,9 +60,13 @@
<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-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.3</version>
+ </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Added: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java?view=auto&rev=507421
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryImpl.java Tue Feb 13 22:51:14 2007
@@ -0,0 +1,223 @@
+/*
+ * 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.tuscany.core.services.deployment;
+
+import java.io.File;
+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;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.tuscany.core.util.IOUtils;
+import org.apache.tuscany.spi.deployer.ContributionRepository;
+
+public class ContributionRepositoryImpl implements ContributionRepository {
+ protected final File rootFile;
+ protected final Map<URI, URL> reposirotyContent = new HashMap<URI, URL>();
+
+ /**
+ * Constructor with repository root URI
+ *
+ * @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();
+ }
+
+ 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 + ")");
+ }
+
+ File rootFile = new File(resolvedUri);
+ return rootFile;
+ }
+
+ /**
+ * Helper method to get a filename from a URL
+ *
+ * @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();
+ }
+
+
+ /*
+ * (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());
+
+ // assure that there isn't already a contribution on the resolved location
+ if (location.exists()) {
+ throw new IllegalArgumentException("Destination " + location.getAbsolutePath() + " already exists!");
+ }
+
+ IOUtils.write(contributionStream, location);
+
+ // add contribution to repositoryContent
+ URL contributionURL = location.toURL();
+ reposirotyContent.put(contribution, contributionURL);
+
+ 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");
+ }
+
+ return this.reposirotyContent.get(contribution);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.tuscany.spi.deployer.ContributionRepository#remove(java.net.URI)
+ */
+ public void remove(URI contribution){
+ URL contributionURL = this.find(contribution);
+ if(contributionURL != null){
+ //remove
+ try{
+ FileUtils.forceDelete(FileUtils.toFile(contributionURL));
+ this.reposirotyContent.remove(contribution);
+ }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;
+
+ }
+
+}
Added: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOUtils.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOUtils.java?view=auto&rev=507421
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOUtils.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/main/java/org/apache/tuscany/core/util/IOUtils.java Tue Feb 13 22:51:14 2007
@@ -0,0 +1,51 @@
+/*
+ * 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.tuscany.core.util;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class IOUtils extends org.apache.commons.io.IOUtils {
+
+ /**
+ * Write a specific source inputstream to a file on disk
+ * @param source contents of the file to be written to disk
+ * @param target file to be written
+ * @throws IOException
+ */
+ public static void write(InputStream source, File target) throws IOException {
+ BufferedOutputStream out = null;
+ BufferedInputStream in = null;
+
+ try {
+ out = new BufferedOutputStream(new FileOutputStream(target));
+ in = new BufferedInputStream(source);
+
+ copy(in, out);
+ }finally{
+ closeQuietly(out);
+ closeQuietly(in);
+ }
+ }
+}
Added: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java?view=auto&rev=507421
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/java/org/apache/tuscany/core/services/deployment/ContributionRepositoryTestCase.java Tue Feb 13 22:51:14 2007
@@ -0,0 +1,77 @@
+/*
+ * 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.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(rootRepositoryDir, true);
+
+ }
+
+ 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);
+
+ 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.remove(contribution);
+ URL contributionURL = repository.find(contribution);
+ assertNull(contributionURL);
+ }
+
+
+ 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());
+ }
+}
Added: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/resources/repository/sample-calculator.jar
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/resources/repository/sample-calculator.jar?view=auto&rev=507421
==============================================================================
Binary file - no diff available.
Propchange: incubator/tuscany/branches/sca-java-integration/sca/kernel/core/src/test/resources/repository/sample-calculator.jar
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: incubator/tuscany/branches/sca-java-integration/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/deployer/ContributionRepository.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/branches/sca-java-integration/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/deployer/ContributionRepository.java?view=auto&rev=507421
==============================================================================
--- incubator/tuscany/branches/sca-java-integration/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/deployer/ContributionRepository.java (added)
+++ incubator/tuscany/branches/sca-java-integration/sca/kernel/spi/src/main/java/org/apache/tuscany/spi/deployer/ContributionRepository.java Tue Feb 13 22:51:14 2007
@@ -0,0 +1,63 @@
+/*
+ * 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.tuscany.spi.deployer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+import java.util.List;
+
+public interface ContributionRepository {
+
+ /**
+ * Copies a contribution to the repository.
+ *
+ * @param contribution A URl pointing to the contribution being copied to
+ * the repository
+ * @param contributionStream InputStream with the content of the
+ * distribution
+ */
+ URL store(URI contribution, InputStream contributionStream) throws IOException;
+
+ /**
+ * Look up the contribution by URI
+ *
+ * @param contribution The URI of the contribution
+ * @return A URL pointing to the content of the contribution in the
+ * repository, it will be null if the contribution cannot be found
+ * in the repository
+ */
+ URL find(URI contribution);
+
+ /**
+ * Remove a contribution from the repository
+ *
+ * @param contribution The URI of the contribution to be removed
+ */
+ void remove(URI contribution);
+
+ /**
+ * Get list of URIs for all the contributions in the repository
+ *
+ * @return A list of contribution URIs
+ */
+ List<URI> list();
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org