[Join-cvs] join1/src/main/org/figure8/join/services/repository VFSRepository.java, 1.1, 1.2
Brought to you by:
lbroudoux
|
From: Laurent B. <lbr...@us...> - 2007-04-08 19:04:02
|
Update of /cvsroot/join/join1/src/main/org/figure8/join/services/repository In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv30460/services/repository Modified Files: VFSRepository.java Log Message: Fixing Checkstyle errors Index: VFSRepository.java =================================================================== RCS file: /cvsroot/join/join1/src/main/org/figure8/join/services/repository/VFSRepository.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** VFSRepository.java 7 Jul 2006 21:04:13 -0000 1.1 --- VFSRepository.java 8 Apr 2007 19:03:55 -0000 1.2 *************** *** 1,288 **** ! /** ! * Copyright 2005-2006 the original author or authors. ! * ! * Licensed under the Gnu General Pubic 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.opensource.org/licenses/gpl-license.php ! * ! * This program 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 General Public License for more details. ! */ ! package org.figure8.join.services.repository; ! ! import org.figure8.join.businessobjects.artifact.Artifact; ! import org.figure8.join.util.LogUtil; ! ! import org.apache.commons.vfs.VFS; ! import org.apache.commons.vfs.FileType; ! import org.apache.commons.vfs.FileObject; ! import org.apache.commons.vfs.FileSelector; ! import org.apache.commons.vfs.FileSelectInfo; ! import org.apache.commons.vfs.FileSystemManager; ! import org.apache.commons.vfs.FileSystemException; ! import org.apache.commons.logging.Log; ! ! import java.io.File; ! import java.io.InputStream; ! import java.io.OutputStream; ! import java.io.FileInputStream; ! /** ! * @author <a href="mailto:lau...@fr...">Laurent Broudoux</a> ! * @version $Revision$ ! */ ! public class VFSRepository extends AbstractStructuredRepository{ ! ! // Static ------------------------------------------------------------------- ! ! /** Get a commons logger. */ ! private static final Log log = LogUtil.getLog(VFSRepository.class); ! ! ! // Attributes --------------------------------------------------------------- ! ! /** String representation of the repository base url */ ! private String baseUrl = null; ! ! ! // Constructors ------------------------------------------------------------- ! ! /** Creates a new instance of VFSRepository. */ ! public VFSRepository(){ ! } ! ! ! // Public ------------------------------------------------------------------- ! ! /** ! * Get the base url (string representation) of the physical repository to access ! * @return The base url of the repository ! */ ! public String getBaseUrl(){ ! return baseUrl; ! } ! /** ! * Set the base url (string representation) of the physical repository to access ! * @param baseUrl The base url of the repository ! */ ! public void setBaseUrl(String baseUrl){ ! if (!baseUrl.endsWith("/")) ! baseUrl += "/"; ! this.baseUrl = baseUrl; ! } ! ! ! // Implementation of Repository --------------------------------------------- ! ! /** ! * Retrieve the input stream corresponding to a given Artifact. This ! * method can be used for artifact content downloading or retrieval. ! * @param artifact The domain object representing artifact to retrieve ! * @return An {@code InputStream} on artifact content ! * @throws ConnectionException if physical repository cannot be connected ! * @throws TransferException if something wrong occur after connection, during transfer ! */ ! public InputStream getArtifact(Artifact artifact) throws ConnectionException, TransferException{ ! log.info("Retrieving stream of artifact with id: " + artifact.getUniqueId()); ! // Get structure path for artifact. ! String structurePath = getStructurePath(artifact); ! if (!structurePath.endsWith("/")) ! structurePath += "/"; ! ! // Build a path for target file. ! String target = baseUrl + structurePath; ! // We don't know file name (it may have an extension or not), so list them ... ! FileObject[] files = null; ! try{ ! FileSystemManager fsManager = VFS.getManager(); ! FileObject parentDir = fsManager.resolveFile(structurePath); ! parentDir.findFiles(getArtifactFileSelector(artifact)); ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while resolving the file into repository: " + target); ! throw new ConnectionException("Exception while resolving target file into repository", fse); ! } ! // Repository should contain only one valid file. ! if (files == null || files.length == 0){ ! log.error("There's no file coresponding to artifact " + artifact.getUniqueId()); ! throw new ConnectionException("Repository is in an unsafe state", ! new IllegalStateException("No file available for artifact")); ! } ! FileObject artifactFile = files[0]; ! try{ ! // Check it is a file (not a directory). ! if (!artifactFile.getType().equals(FileType.FILE)){ ! log.error("ArtifactFile is not a valid file: " + artifactFile.getName().toString()); ! throw new ConnectionException("Repository is in an unsafe state", ! new IllegalStateException("No valid file for artifact")); ! } ! // Check it is readable. ! if (!artifactFile.isReadable()){ ! log.error("ArtifactFile is not a readable file: " + artifactFile.getName().toString()); ! throw new ConnectionException("Repository is in an unsafe state", ! new IllegalStateException("File for artifact not readable")); ! } ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while accessing the properties of file: " + artifactFile.getName().toString()); ! throw new ConnectionException("Exception while accessing the properties of artifact file", fse); ! } ! try{ ! // Return input stream on artifact file content. ! return artifactFile.getContent().getInputStream(); ! } ! catch (Exception e){ ! // Log and wrap this into a TransferException. ! log.error("Exception while creating input stream onto file " + artifactFile.getName().toString()); ! throw new TransferException("Exception while creating input stream on artifact", e); ! } ! } ! ! /** ! * Store the content of a given Artifact within repository datastore. ! * @param artifact The domain object representing artifact to store ! * @param content File representing artifact content (may be a directory) ! * @throws ConnectionException if physical repository cannot be connected ! * @throws TransferException if something wrong occur after connection, during transfer ! */ ! public void storeArtifact(Artifact artifact, File content) throws ConnectionException, TransferException{ ! log.info("Storing file of artifact with id: " + artifact.getUniqueId()); ! // Get structure path for artifact. ! String structurePath = getStructurePath(artifact); ! if (!structurePath.endsWith("/")) ! structurePath += "/"; ! ! // Build a path for target file. ! String target = baseUrl + structurePath; ! if (content.getName().lastIndexOf(".") != -1){ ! // If there's extension, concat to artifact id. ! target += artifact.getUniqueId() + content.getName().substring(content.getName().lastIndexOf(".")); ! log.debug("Content has an extension. Target path is " + target); ! } ! else{ ! // If no extension, file name = identifier. ! target += artifact.getUniqueId(); ! log.debug("Content has no extension. Target path is " + target); ! } ! ! // Resolve destination file. ! FileObject dest = null; ! try{ ! FileSystemManager fsManager = VFS.getManager(); ! dest = fsManager.resolveFile(target); ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while resolving the file into repository: " + target); ! throw new ConnectionException("Exception while resolving target file into repository", fse); ! } ! ! // Create an input and output streams for writing file. ! OutputStream os = null; ! FileInputStream contentFis = null; ! try{ ! os = dest.getContent().getOutputStream(); ! contentFis = new FileInputStream(content); ! } ! catch (Exception e){ ! e.printStackTrace(); ! // Log and wrap exception into a connection exception. ! log.error("Exception while opening copy streams into repository for: " + target); ! throw new ConnectionException("Exception while opening copy streams into repository", e); ! } ! writeStream(contentFis, os); ! } ! ! /** ! * Store the content of a given Artifact within repository datastore using an input stream. ! * @param artifact The domain object representing artifact to store ! * @param is InputStream on artifact content ! * @throws ConnectionException if physical repository cannot be connected ! * @throws TransferException if something wrong occur after connection, during transfer ! */ ! public void storeArtifact(Artifact artifact, InputStream is) throws ConnectionException, TransferException{ ! log.info("Storing stream of artifact with id: " + artifact.getUniqueId()); ! // Get structure path for artifact. ! String structurePath = getStructurePath(artifact); ! if (!structurePath.endsWith("/")) ! structurePath += "/"; ! ! // Build a path for target file. ! String target = baseUrl + structurePath + artifact.getUniqueId(); ! log.debug("Content is an InputStream. Target file path is " + target); ! ! // Resolve destination file. ! FileObject dest = null; ! try{ ! FileSystemManager fsManager = VFS.getManager(); ! dest = fsManager.resolveFile(target); ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while resolving the file into repository: " + target); ! throw new ConnectionException("Exception while resolving target file into repository", fse); ! } ! ! // Create an input and output streams for writing file. ! OutputStream os = null; ! try {os = dest.getContent().getOutputStream();} ! catch (Exception e){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while opening copy streams into repository for: " + target); ! throw new ConnectionException("Exception while opening copy streams into repository", e); ! } ! writeStream(is, os); ! } ! ! ! // Private ------------------------------------------------------------------ ! ! /** ! * Write an input stream content to a target output stream. This method closes ! * the input and output streams at the end of write operation. ! * @param is The input stream to use for getting data ! * @param os The output stream to use for writing data ! * @throws TransferException if an IOException occurs during writing output ! */ ! private void writeStream(InputStream is, OutputStream os) throws TransferException{ ! try{ ! // Read is and write on fos unsing a buffer of bytes. ! int bytes = 0; ! byte[] buffer = new byte[8192]; ! while ((bytes = is.read(buffer, 0, 8192)) != -1) ! os.write(buffer, 0, bytes); ! } ! catch (Exception e){ ! // Log and wrap IOException into a transfer exception. ! log.error("IOException while writing into stream: " + os); ! throw new TransferException("IOException while writing into target stream", e); ! } ! finally{ ! try {is.close();} ! catch (Exception e) {/* Do nothing here... */} ! try {os.close();} ! catch (Exception e) {/* Do nothing here... */} ! } ! } ! ! /** ! * Get a FileSelector implementation for filtering files ! * corresponding to artifact. ! * @return A FileSelector for artifact ! */ ! private FileSelector getArtifactFileSelector(final Artifact artifact){ ! return new FileSelector(){ ! public boolean includeFile(FileSelectInfo info){ ! return info.getFile().getName().getBaseName().startsWith(artifact.getUniqueId()); ! } ! public boolean traverseDescendents(FileSelectInfo info){ ! return false; ! } ! }; ! } ! } --- 1,289 ---- ! /** ! * Copyright 2005-2006 the original author or authors. ! * ! * Licensed under the Gnu General Pubic 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.opensource.org/licenses/gpl-license.php ! * ! * This program 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 General Public License for more details. ! */ ! package org.figure8.join.services.repository; ! ! import org.figure8.join.businessobjects.artifact.Artifact; ! import org.figure8.join.util.LogUtil; ! ! import org.apache.commons.vfs.VFS; ! import org.apache.commons.vfs.FileType; ! import org.apache.commons.vfs.FileObject; ! import org.apache.commons.vfs.FileSelector; ! import org.apache.commons.vfs.FileSelectInfo; ! import org.apache.commons.vfs.FileSystemManager; ! import org.apache.commons.vfs.FileSystemException; ! import org.apache.commons.logging.Log; ! ! import java.io.File; ! import java.io.InputStream; ! import java.io.OutputStream; ! import java.io.FileInputStream; ! /** ! * This is an implementation of {@link Repository} using Jakarta VFS Commons. ! * @author <a href="mailto:lau...@fr...">Laurent Broudoux</a> ! * @version $Revision$ ! */ ! public class VFSRepository extends AbstractStructuredRepository{ ! ! // Static ------------------------------------------------------------------- ! ! /** Get a commons logger. */ ! private static final Log log = LogUtil.getLog(VFSRepository.class); ! ! ! // Attributes --------------------------------------------------------------- ! ! /** String representation of the repository base url */ ! private String baseUrl = null; ! ! ! // Constructors ------------------------------------------------------------- ! ! /** Creates a new instance of VFSRepository. */ ! public VFSRepository(){ ! } ! ! ! // Public ------------------------------------------------------------------- ! ! /** ! * Get the base url (string representation) of the physical repository to access ! * @return The base url of the repository ! */ ! public String getBaseUrl(){ ! return baseUrl; ! } ! /** ! * Set the base url (string representation) of the physical repository to access ! * @param baseUrl The base url of the repository ! */ ! public void setBaseUrl(String baseUrl){ ! if (!baseUrl.endsWith("/")) ! baseUrl += "/"; ! this.baseUrl = baseUrl; ! } ! ! ! // Implementation of Repository --------------------------------------------- ! ! /** ! * Retrieve the input stream corresponding to a given Artifact. This ! * method can be used for artifact content downloading or retrieval. ! * @param artifact The domain object representing artifact to retrieve ! * @return An {@code InputStream} on artifact content ! * @throws ConnectionException if physical repository cannot be connected ! * @throws TransferException if something wrong occur after connection, during transfer ! */ ! public InputStream getArtifact(Artifact artifact) throws ConnectionException, TransferException{ ! log.info("Retrieving stream of artifact with id: " + artifact.getUniqueId()); ! // Get structure path for artifact. ! String structurePath = getStructurePath(artifact); ! if (!structurePath.endsWith("/")) ! structurePath += "/"; ! ! // Build a path for target file. ! String target = baseUrl + structurePath; ! // We don't know file name (it may have an extension or not), so list them ... ! FileObject[] files = null; ! try{ ! FileSystemManager fsManager = VFS.getManager(); ! FileObject parentDir = fsManager.resolveFile(structurePath); ! parentDir.findFiles(getArtifactFileSelector(artifact)); ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while resolving the file into repository: " + target); ! throw new ConnectionException("Exception while resolving target file into repository", fse); ! } ! // Repository should contain only one valid file. ! if (files == null || files.length == 0){ ! log.error("There's no file coresponding to artifact " + artifact.getUniqueId()); ! throw new ConnectionException("Repository is in an unsafe state", ! new IllegalStateException("No file available for artifact")); ! } ! FileObject artifactFile = files[0]; ! try{ ! // Check it is a file (not a directory). ! if (!artifactFile.getType().equals(FileType.FILE)){ ! log.error("ArtifactFile is not a valid file: " + artifactFile.getName().toString()); ! throw new ConnectionException("Repository is in an unsafe state", ! new IllegalStateException("No valid file for artifact")); ! } ! // Check it is readable. ! if (!artifactFile.isReadable()){ ! log.error("ArtifactFile is not a readable file: " + artifactFile.getName().toString()); ! throw new ConnectionException("Repository is in an unsafe state", ! new IllegalStateException("File for artifact not readable")); ! } ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while accessing the properties of file: " + artifactFile.getName().toString()); ! throw new ConnectionException("Exception while accessing the properties of artifact file", fse); ! } ! try{ ! // Return input stream on artifact file content. ! return artifactFile.getContent().getInputStream(); ! } ! catch (Exception e){ ! // Log and wrap this into a TransferException. ! log.error("Exception while creating input stream onto file " + artifactFile.getName().toString()); ! throw new TransferException("Exception while creating input stream on artifact", e); ! } ! } ! ! /** ! * Store the content of a given Artifact within repository datastore. ! * @param artifact The domain object representing artifact to store ! * @param content File representing artifact content (may be a directory) ! * @throws ConnectionException if physical repository cannot be connected ! * @throws TransferException if something wrong occur after connection, during transfer ! */ ! public void storeArtifact(Artifact artifact, File content) throws ConnectionException, TransferException{ ! log.info("Storing file of artifact with id: " + artifact.getUniqueId()); ! // Get structure path for artifact. ! String structurePath = getStructurePath(artifact); ! if (!structurePath.endsWith("/")) ! structurePath += "/"; ! ! // Build a path for target file. ! String target = baseUrl + structurePath; ! if (content.getName().lastIndexOf(".") != -1){ ! // If there's extension, concat to artifact id. ! target += artifact.getUniqueId() + content.getName().substring(content.getName().lastIndexOf(".")); ! log.debug("Content has an extension. Target path is " + target); ! } ! else{ ! // If no extension, file name = identifier. ! target += artifact.getUniqueId(); ! log.debug("Content has no extension. Target path is " + target); ! } ! ! // Resolve destination file. ! FileObject dest = null; ! try{ ! FileSystemManager fsManager = VFS.getManager(); ! dest = fsManager.resolveFile(target); ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while resolving the file into repository: " + target); ! throw new ConnectionException("Exception while resolving target file into repository", fse); ! } ! ! // Create an input and output streams for writing file. ! OutputStream os = null; ! FileInputStream contentFis = null; ! try{ ! os = dest.getContent().getOutputStream(); ! contentFis = new FileInputStream(content); ! } ! catch (Exception e){ ! e.printStackTrace(); ! // Log and wrap exception into a connection exception. ! log.error("Exception while opening copy streams into repository for: " + target); ! throw new ConnectionException("Exception while opening copy streams into repository", e); ! } ! writeStream(contentFis, os); ! } ! ! /** ! * Store the content of a given Artifact within repository datastore using an input stream. ! * @param artifact The domain object representing artifact to store ! * @param is InputStream on artifact content ! * @throws ConnectionException if physical repository cannot be connected ! * @throws TransferException if something wrong occur after connection, during transfer ! */ ! public void storeArtifact(Artifact artifact, InputStream is) throws ConnectionException, TransferException{ ! log.info("Storing stream of artifact with id: " + artifact.getUniqueId()); ! // Get structure path for artifact. ! String structurePath = getStructurePath(artifact); ! if (!structurePath.endsWith("/")) ! structurePath += "/"; ! ! // Build a path for target file. ! String target = baseUrl + structurePath + artifact.getUniqueId(); ! log.debug("Content is an InputStream. Target file path is " + target); ! ! // Resolve destination file. ! FileObject dest = null; ! try{ ! FileSystemManager fsManager = VFS.getManager(); ! dest = fsManager.resolveFile(target); ! } ! catch (FileSystemException fse){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while resolving the file into repository: " + target); ! throw new ConnectionException("Exception while resolving target file into repository", fse); ! } ! ! // Create an input and output streams for writing file. ! OutputStream os = null; ! try {os = dest.getContent().getOutputStream();} ! catch (Exception e){ ! // Log and wrap exception into a connection exception. ! log.error("Exception while opening copy streams into repository for: " + target); ! throw new ConnectionException("Exception while opening copy streams into repository", e); ! } ! writeStream(is, os); ! } ! ! ! // Private ------------------------------------------------------------------ ! ! /** ! * Write an input stream content to a target output stream. This method closes ! * the input and output streams at the end of write operation. ! * @param is The input stream to use for getting data ! * @param os The output stream to use for writing data ! * @throws TransferException if an IOException occurs during writing output ! */ ! private void writeStream(InputStream is, OutputStream os) throws TransferException{ ! try{ ! // Read is and write on fos unsing a buffer of bytes. ! int bytes = 0; ! byte[] buffer = new byte[8192]; ! while ((bytes = is.read(buffer, 0, 8192)) != -1) ! os.write(buffer, 0, bytes); ! } ! catch (Exception e){ ! // Log and wrap IOException into a transfer exception. ! log.error("IOException while writing into stream: " + os); ! throw new TransferException("IOException while writing into target stream", e); ! } ! finally{ ! try {is.close();} ! catch (Exception e) {/* Do nothing here... */} ! try {os.close();} ! catch (Exception e) {/* Do nothing here... */} ! } ! } ! ! /** ! * Get a FileSelector implementation for filtering files ! * corresponding to artifact. ! * @return A FileSelector for artifact ! */ ! private FileSelector getArtifactFileSelector(final Artifact artifact){ ! return new FileSelector(){ ! public boolean includeFile(FileSelectInfo info){ ! return info.getFile().getName().getBaseName().startsWith(artifact.getUniqueId()); ! } ! public boolean traverseDescendents(FileSelectInfo info){ ! return false; ! } ! }; ! } ! } |