Index: extensions/maven2/src/main/java/org/codehaus/cargo/maven2/AbstractCargoMojo.java =================================================================== --- extensions/maven2/src/main/java/org/codehaus/cargo/maven2/AbstractCargoMojo.java (revision 1234) +++ extensions/maven2/src/main/java/org/codehaus/cargo/maven2/AbstractCargoMojo.java (working copy) @@ -233,6 +233,8 @@ if (this.cargoProject == null) { this.cargoProject = new CargoProject(this.project, getLog()); + + cargoProject.setArtifactDownloader(new ArtifactDownloader(artifactFactory, artifactResolver, repositories, localRepository)); } } Index: extensions/maven2/src/main/java/org/codehaus/cargo/maven2/ArtifactInstaller.java =================================================================== --- extensions/maven2/src/main/java/org/codehaus/cargo/maven2/ArtifactInstaller.java (revision 0) +++ extensions/maven2/src/main/java/org/codehaus/cargo/maven2/ArtifactInstaller.java (revision 0) @@ -0,0 +1,96 @@ +package org.codehaus.cargo.maven2; + +import java.io.File; + +import org.codehaus.cargo.container.installer.AbstractInstaller; +import org.codehaus.cargo.container.installer.Installer; + +public class ArtifactInstaller +{ + + private String groupId; + + private String version; + + private String artifactId; + + private String classifier; + + private String type = "zip"; + + public String getArtifactId() + { + return artifactId; + } + + public void setArtifactId(String artifactId) + { + this.artifactId = artifactId; + } + + public String getClassifier() + { + return classifier; + } + + public void setClassifier(String classifier) + { + this.classifier = classifier; + } + + public String getGroupId() + { + return groupId; + } + + public void setGroupId(String groupId) + { + this.groupId = groupId; + } + + public String getVersion() + { + return version; + } + + public void setVersion(String version) + { + this.version = version; + } + + public String getType() + { + return type; + } + + public void setType(String type) + { + this.type = type; + } + + public Installer createInstaller(final ArtifactDownloader downloader) + { + + return new AbstractInstaller() + { + + protected File download() + { + downloader.download(groupId, artifactId, version, type, + classifier); + return downloader.getDestFile(groupId, artifactId, version, + type, classifier); + } + + protected String getSourceFileName() + { + String name = artifactId + "-" + version; + if (classifier != null) { + name += "-" + classifier; + } + return name; + } + + }; + } +} Index: extensions/maven2/src/main/java/org/codehaus/cargo/maven2/ArtifactDownloader.java =================================================================== --- extensions/maven2/src/main/java/org/codehaus/cargo/maven2/ArtifactDownloader.java (revision 0) +++ extensions/maven2/src/main/java/org/codehaus/cargo/maven2/ArtifactDownloader.java (revision 0) @@ -0,0 +1,87 @@ +package org.codehaus.cargo.maven2; + +import java.io.File; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.codehaus.cargo.container.ContainerException; + +/** + * An ArtifactDownloader encapsulates the resolving of an artifact from a local + * or remote Maven repository. + * + */ +public class ArtifactDownloader +{ + + private ArtifactRepository localRepository; + + private ArtifactResolver resolver; + + private ArtifactFactory factory; + + private List remoteRepositories; + + public ArtifactDownloader(final ArtifactFactory factory, + final ArtifactResolver resolver, final List remoteRepositories, + final ArtifactRepository localRepository) + { + this.factory = factory; + this.resolver = resolver; + this.remoteRepositories = remoteRepositories; + this.localRepository = localRepository; + } + + /** + * Resolves a dependencies, and return the artifact file + * + * @param groupId + * @param artifactId + * @param version + * @param type + * @param classifier + * @return + */ + public File download(String groupId, String artifactId, String version, + String type, String classifier) + { + try + { + Artifact artifact = factory.createArtifactWithClassifier(groupId, + artifactId, version, type, classifier); + resolver.resolve(artifact, remoteRepositories, localRepository); + return artifact.getFile(); + } catch (ArtifactResolutionException e) + { + throw new ContainerException("Could not resolve artifact", e); + } catch (ArtifactNotFoundException e) + { + throw new ContainerException("Could not find artifact", e); + } + } + + /** + * Returns the location where the artifact is or will be downloaded + * + * @param groupId + * @param artifactId + * @param version + * @param type + * @param classifier + * @return + */ + public File getDestFile(String groupId, String artifactId, String version, + String type, String classifier) + { + Artifact artifact = factory.createArtifactWithClassifier(groupId, + artifactId, version, type, classifier); + String path = localRepository.pathOf(artifact); + return new File(localRepository.getBasedir(), path); + } + +} Index: extensions/maven2/src/main/java/org/codehaus/cargo/maven2/Container.java =================================================================== --- extensions/maven2/src/main/java/org/codehaus/cargo/maven2/Container.java (revision 1234) +++ extensions/maven2/src/main/java/org/codehaus/cargo/maven2/Container.java (working copy) @@ -28,17 +28,17 @@ import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.cargo.container.ContainerType; +import org.codehaus.cargo.container.EmbeddedLocalContainer; +import org.codehaus.cargo.container.InstalledLocalContainer; import org.codehaus.cargo.container.LocalContainer; import org.codehaus.cargo.container.RemoteContainer; -import org.codehaus.cargo.container.EmbeddedLocalContainer; -import org.codehaus.cargo.container.InstalledLocalContainer; import org.codehaus.cargo.container.configuration.Configuration; -import org.codehaus.cargo.container.installer.ZipURLInstaller; +import org.codehaus.cargo.container.installer.Installer; import org.codehaus.cargo.generic.ContainerFactory; import org.codehaus.cargo.generic.DefaultContainerFactory; +import org.codehaus.cargo.maven2.util.CargoProject; +import org.codehaus.cargo.util.log.LogLevel; import org.codehaus.cargo.util.log.Logger; -import org.codehaus.cargo.util.log.LogLevel; -import org.codehaus.cargo.maven2.util.CargoProject; /** * Holds configuration data for the <container> tag used to configure @@ -59,6 +59,8 @@ private String output; private ZipUrlInstaller zipUrlInstaller; + + private ArtifactInstaller artifactInstaller; private boolean append; @@ -136,6 +138,11 @@ { return this.zipUrlInstaller; } + + public ArtifactInstaller getArtifactInstaller() + { + return this.artifactInstaller; + } public boolean shouldAppend() { @@ -208,7 +215,7 @@ } else if (container.getType() == ContainerType.INSTALLED) { - setupHome((InstalledLocalContainer) container); + setupHome((InstalledLocalContainer) container, project); setupOutput((InstalledLocalContainer) container, project); setupExtraClasspath((InstalledLocalContainer) container, project); setupSystemProperties((InstalledLocalContainer) container); @@ -311,21 +318,32 @@ /** * Set up a home dir (possibly using a ZipURLInstaller). */ - private void setupHome(InstalledLocalContainer container) + private void setupHome(InstalledLocalContainer container, CargoProject project) { if (getHome() != null) { container.setHome(getHome()); } - else if (getZipUrlInstaller() != null) - { - ZipURLInstaller installer = getZipUrlInstaller().createInstaller(); - if (getLog() != null) - { - installer.setLogger(container.getLogger()); - } - installer.install(); - container.setHome(installer.getHome()); + else { + Installer installer = null; + if (getZipUrlInstaller() != null) + { + installer = getZipUrlInstaller().createInstaller(); + } + else if (getArtifactInstaller() != null) + { + installer = getArtifactInstaller().createInstaller(project.getArtifactDownloader()); + + } + if (installer != null) + { + if (getLog() != null) + { + installer.setLogger(container.getLogger()); + } + installer.install(); + container.setHome(installer.getHome()); + } } } Index: extensions/maven2/src/main/java/org/codehaus/cargo/maven2/util/CargoProject.java =================================================================== --- extensions/maven2/src/main/java/org/codehaus/cargo/maven2/util/CargoProject.java (revision 1234) +++ extensions/maven2/src/main/java/org/codehaus/cargo/maven2/util/CargoProject.java (working copy) @@ -23,6 +23,7 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; +import org.codehaus.cargo.maven2.ArtifactDownloader; /** * Holder class to transport all required information to the configuration classes. @@ -40,6 +41,7 @@ private String finalName; private Set artifacts; private ClassLoader embeddedClassLoader; + private ArtifactDownloader artifactDownloader; public CargoProject(String packaging, String groupId, String artifactId, String buildDirectory, String finalName, Set artifacts, Log log) @@ -117,4 +119,14 @@ { return this.embeddedClassLoader; } + + public ArtifactDownloader getArtifactDownloader() + { + return artifactDownloader; + } + + public void setArtifactDownloader(ArtifactDownloader artifactDownloader) + { + this.artifactDownloader = artifactDownloader; + } } Index: core/api/container/src/main/java/org/codehaus/cargo/container/installer/AbstractInstaller.java =================================================================== --- core/api/container/src/main/java/org/codehaus/cargo/container/installer/AbstractInstaller.java (revision 0) +++ core/api/container/src/main/java/org/codehaus/cargo/container/installer/AbstractInstaller.java (revision 0) @@ -0,0 +1,306 @@ +/* + * ======================================================================== + * + * Copyright 2004-2006 Vincent Massol. + * + * Licensed 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.codehaus.cargo.container.installer; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; + +import org.apache.tools.ant.taskdefs.Expand; +import org.codehaus.cargo.container.ContainerException; +import org.codehaus.cargo.util.AntTaskFactory; +import org.codehaus.cargo.util.AntUtils; +import org.codehaus.cargo.util.DefaultFileHandler; +import org.codehaus.cargo.util.FileHandler; +import org.codehaus.cargo.util.log.LoggedObject; + +/** + * Installs an archived container file to a location on your local disk. + * + * @version $Id: ZipURLInstaller.java 1166 2006-08-01 09:53:28Z vmassol $ + */ +public abstract class AbstractInstaller extends LoggedObject implements + Installer +{ + /** + * Destination directory where the zipped container install will be + * downloaded and installed. + */ + private String installDir; + + /** + * Ant utility class. + */ + private AntUtils antUtils; + + /** + * File utility class. + */ + private FileHandler fileHandler; + + /** + */ + public AbstractInstaller() + { + this(null); + } + + /** + * @param installDir + * directory where we will unpack the zip container file + */ + public AbstractInstaller(String installDir) + { + this.installDir = installDir; + this.fileHandler = new DefaultFileHandler(); + this.antUtils = new AntUtils(); + } + + /** + * @param installDir + * the destination directory where the zipped container install + * will be downloaded and installed. + */ + public void setInstallDir(String installDir) + { + this.installDir = installDir; + } + + /** + * Convenience method used for testing in isolation. Test cases can use it + * for introducing a custom {@link AntTaskFactory} that returns a custom + * test-made Ant task. + * + * @param antTaskFactory + * the test-provided {@link AntTaskFactory} + */ + protected void setAntTaskFactory(AntTaskFactory antTaskFactory) + { + this.antUtils = new AntUtils(antTaskFactory); + } + + /** + * @return the AntUtils + */ + protected AntUtils getAntUtils() + { + return antUtils; + } + + /** + * @return the file utility class to use for performing all file I/O. + */ + public FileHandler getFileHandler() + { + return this.fileHandler; + } + + /** + * @param fileHandler + * the file utility class to use for performing all file I/O. + */ + public void setFileHandler(FileHandler fileHandler) + { + this.fileHandler = fileHandler; + } + + /** + * @see Installer#install() + */ + public void install() + { + getLogger().info( + "Using container installation dir [" + getDestinationDir() + + "]", getClass().getName()); + + if (!isAlreadyInstalled()) + { + createDestinationDirectory(); + File file = download(); + unpack(file); + registerInstallation(); + } + } + + /** + * Create timestamp file to register that the installation has been + * successful. This allows to prevent installing again next time. If the + * remote URL changes, then the container will be reinstalled. + */ + public void registerInstallation() + { + try + { + File timestampFile = new File(getDestinationDir(), + getInstallDirName() + "/.cargo"); + BufferedWriter bw = new BufferedWriter( + new FileWriter(timestampFile)); + bw.write("Do not remove this file"); + bw.close(); + } + catch (Exception e) + { + // Failed to write timestamp. Too bad. The application will be + // installed again next + // time. + } + } + + /** + * @return true if the container has already been installed, false otherwise + */ + public boolean isAlreadyInstalled() + { + boolean isInstalled = false; + String timestampFile = getFileHandler().append(getDestinationDir(), + getInstallDirName() + "/.cargo"); + if (getFileHandler().exists(timestampFile)) + { + isInstalled = true; + } + + getLogger().debug( + "Container [" + + getSourceFileName() + + "] is " + + (isInstalled ? "already installed" + : "not yet installed as timestamp file [" + + timestampFile + "] was not found"), + this.getClass().getName()); + + return isInstalled; + } + + /** + * {@inheritDoc} + * + * @see Installer#getHome() + */ + public String getHome() + { + String home; + + if (!isAlreadyInstalled()) + { + throw new ContainerException( + "Failed to get container installation home as the " + + "container has not yet been installed. Please call install() first."); + } + + String targetDir = getFileHandler().append(getDestinationDir(), + getInstallDirName()); + String[] files = getFileHandler().getChildren(targetDir); + int nbDirectories = 0; + String foundDirectory = null; + for (int i = 0; i < files.length; i++) + { + if (getFileHandler().isDirectory(files[i])) + { + nbDirectories++; + foundDirectory = files[i]; + } + } + + // If the unpacking has revealed only 1 directory, then it's the home + // dir. Otherwise, it + // means the packing of the zip files did not have a root dir. + if (nbDirectories != 1) + { + home = targetDir; + } + else + { + home = foundDirectory; + } + + return home; + } + + /** + * Unpacks the zip file containing the container files. + * + * @param sourceFile + * the zip file to unpack + */ + private void unpack(File sourceFile) + { + Expand expandTask = (Expand) this.antUtils.createAntTask("unzip"); + expandTask.setSrc(sourceFile); + expandTask.setDest(new File(getDestinationDir(), getInstallDirName())); + expandTask.execute(); + } + + /** + * Downloads and returns the zip file containing the container files. + * + * @return the downloaded file + */ + protected abstract File download(); + + /** + * @return the name of the source zip file (without the path) + */ + protected abstract String getSourceFileName(); + + /** + * @return the directory where we will unpack the zip container file + */ + protected String getInstallDirName() + { + String name = getSourceFileName(); + int dotPos = name.lastIndexOf("."); + if (dotPos > -1) + { + name = name.substring(0, dotPos); + } + + return name; + } + + /** + * Create the directory (if need be) where we will unpack the zip container + * file. + */ + private void createDestinationDirectory() + { + String destinationDir = getDestinationDir(); + if (!getFileHandler().exists(destinationDir)) + { + getFileHandler().mkdirs(destinationDir); + } + } + + /** + * @return the directory where we will puth both the zip container file and + * its unpacking + */ + protected String getDestinationDir() + { + String dir = this.installDir; + + if (dir == null) + { + dir = getFileHandler().getTmpPath("installs"); + } + + return dir; + } + +} Index: core/api/container/src/main/java/org/codehaus/cargo/container/installer/ZipURLInstaller.java =================================================================== --- core/api/container/src/main/java/org/codehaus/cargo/container/installer/ZipURLInstaller.java (revision 1234) +++ core/api/container/src/main/java/org/codehaus/cargo/container/installer/ZipURLInstaller.java (working copy) @@ -19,53 +19,30 @@ */ package org.codehaus.cargo.container.installer; -import org.apache.tools.ant.taskdefs.Expand; -import org.apache.tools.ant.taskdefs.Get; -import org.codehaus.cargo.container.ContainerException; -import org.codehaus.cargo.util.log.LoggedObject; -import org.codehaus.cargo.util.AntUtils; -import org.codehaus.cargo.util.AntTaskFactory; -import org.codehaus.cargo.util.DefaultFileHandler; -import org.codehaus.cargo.util.FileHandler; - -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.net.URL; +import org.apache.tools.ant.taskdefs.Get; +import org.codehaus.cargo.container.ContainerException; + /** * Installs a zipped container file from a URL to a location on your local disk. * * @version $Id$ */ -public class ZipURLInstaller extends LoggedObject implements Installer +public class ZipURLInstaller extends AbstractInstaller { /** * URL where the zipped container is located. */ private URL remoteLocation; - /** - * Destination directory where the zipped container install will be downloaded and installed. - */ - private String installDir; - - /** + /** * Proxy settings to use when downloading distributions. */ private Proxy proxy; /** - * Ant utility class. - */ - private AntUtils antUtils; - - /** - * File utility class. - */ - private FileHandler fileHandler; - - /** * @param remoteLocation URL where the zipped container is located */ public ZipURLInstaller(URL remoteLocation) @@ -79,163 +56,17 @@ */ public ZipURLInstaller(URL remoteLocation, String installDir) { + super(installDir); this.remoteLocation = remoteLocation; - this.installDir = installDir; - this.fileHandler = new DefaultFileHandler(); - this.antUtils = new AntUtils(); } /** - * @param installDir the destination directory where the zipped container install will be - * downloaded and installed. - */ - public void setInstallDir(String installDir) - { - this.installDir = installDir; - } - - /** - * Convenience method used for testing in isolation. Test cases can use it for introducing - * a custom {@link AntTaskFactory} that returns a custom test-made Ant task. + * Downloads the zip file containing the container files. * - * @param antTaskFactory the test-provided {@link AntTaskFactory} + * @return the downloaded file */ - protected void setAntTaskFactory(AntTaskFactory antTaskFactory) + protected File download() { - this.antUtils = new AntUtils(antTaskFactory); - } - - /** - * @return the file utility class to use for performing all file I/O. - */ - public FileHandler getFileHandler() - { - return this.fileHandler; - } - - /** - * @param fileHandler the file utility class to use for performing all file I/O. - */ - public void setFileHandler(FileHandler fileHandler) - { - this.fileHandler = fileHandler; - } - - /** - * @see Installer#install() - */ - public void install() - { - getLogger().info("Using container installation dir [" + getDestinationDir() - + "]", getClass().getName()); - - if (!isAlreadyInstalled()) - { - createDestinationDirectory(); - download(); - unpack(); - registerInstallation(); - } - } - - /** - * Create timestamp file to register that the installation has been successful. This allows - * to prevent installing again next time. If the remote URL changes, then the container will - * be reinstalled. - */ - public void registerInstallation() - { - try - { - File timestampFile = new File(getDestinationDir(), getInstallDirName() + "/.cargo"); - BufferedWriter bw = new BufferedWriter(new FileWriter(timestampFile)); - bw.write("Do not remove this file"); - bw.close(); - } - catch (Exception e) - { - // Failed to write timestamp. Too bad. The application will be installed again next - // time. - } - } - - /** - * @return true if the container has already been installed, false otherwise - */ - public boolean isAlreadyInstalled() - { - boolean isInstalled = false; - String timestampFile = - getFileHandler().append(getDestinationDir(), getInstallDirName() + "/.cargo"); - if (getFileHandler().exists(timestampFile)) - { - isInstalled = true; - } - - getLogger().debug("Container [" + getSourceFileName() + "] is " - + (isInstalled ? "already installed" : "not yet installed as timestamp file [" - + timestampFile + "] was not found"), this.getClass().getName()); - - return isInstalled; - } - - /** - * {@inheritDoc} - * @see Installer#getHome() - */ - public String getHome() - { - String home; - - if (!isAlreadyInstalled()) - { - throw new ContainerException("Failed to get container installation home as the " - + "container has not yet been installed. Please call install() first."); - } - - String targetDir = getFileHandler().append(getDestinationDir(), getInstallDirName()); - String[] files = getFileHandler().getChildren(targetDir); - int nbDirectories = 0; - String foundDirectory = null; - for (int i = 0; i < files.length; i++) - { - if (getFileHandler().isDirectory(files[i])) - { - nbDirectories++; - foundDirectory = files[i]; - } - } - - // If the unpacking has revealed only 1 directory, then it's the home dir. Otherwise, it - // means the packing of the zip files did not have a root dir. - if (nbDirectories != 1) - { - home = targetDir; - } - else - { - home = foundDirectory; - } - - return home; - } - - /** - * Unpacks the zip file containing the container files. - */ - private void unpack() - { - Expand expandTask = (Expand) this.antUtils.createAntTask("unzip"); - expandTask.setSrc(new File(getDestinationDir(), getSourceFileName())); - expandTask.setDest(new File(getDestinationDir(), getInstallDirName())); - expandTask.execute(); - } - - /** - * Downloads the zip file containing the container files. - */ - protected void download() - { // Try once with the proxy settings on (if set up by the user) and if it doesn't work, try // again with no proxy settings... try @@ -244,7 +75,7 @@ { this.proxy.configure(); } - doDownload(); + return doDownload(); } catch (Exception e) { @@ -253,7 +84,7 @@ try { this.proxy.clear(); - doDownload(); + return doDownload(); } catch (Exception ee) { @@ -270,14 +101,18 @@ /** * Perform the actual HTTP download. + * @return the downloaded file */ - private void doDownload() + private File doDownload() { - Get getTask = (Get) this.antUtils.createAntTask("get"); + Get getTask = (Get) getAntUtils().createAntTask("get"); getTask.setUseTimestamp(true); getTask.setSrc(this.remoteLocation); - getTask.setDest(new File(getDestinationDir(), getSourceFileName())); + File destFile = new File(getDestinationDir(), getSourceFileName()); + getTask.setDest(destFile); getTask.execute(); + + return destFile; } /** @@ -296,48 +131,6 @@ } /** - * @return the directory where we will unpack the zip container file - */ - protected String getInstallDirName() - { - String name = getSourceFileName(); - int dotPos = name.lastIndexOf(".zip"); - if (dotPos > -1) - { - name = name.substring(0, dotPos); - } - - return name; - } - - /** - * Create the directory (if need be) where we will unpack the zip container file. - */ - private void createDestinationDirectory() - { - String destinationDir = getDestinationDir(); - if (!getFileHandler().exists(destinationDir)) - { - getFileHandler().mkdirs(destinationDir); - } - } - - /** - * @return the directory where we will puth both the zip container file and its unpacking - */ - protected String getDestinationDir() - { - String dir = this.installDir; - - if (dir == null) - { - dir = getFileHandler().getTmpPath("installs"); - } - - return dir; - } - - /** * Sets proxy details. * * @param proxy the proxy configuration to set @@ -346,4 +139,5 @@ { this.proxy = proxy; } + }