/*
* ========================================================================
*
* Copyright 2005 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.maven2;
import java.io.File;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.cargo.container.LocalContainer;
import org.codehaus.cargo.container.RemoteContainer;
import org.codehaus.cargo.container.installer.ZipURLInstaller;
import org.codehaus.cargo.generic.ContainerFactory;
import org.codehaus.cargo.generic.ContainerType;
import org.codehaus.cargo.generic.DefaultContainerFactory;
import org.codehaus.cargo.util.monitor.FileMonitor;
import org.codehaus.cargo.util.monitor.Monitor;
/**
* Holds configuration data for the <container> tag used to configure
* the plugin in the pom.xml file.
*
* @version $Id: $
*/
public class Container
{
private String containerId;
private Class implementation;
private File home;
private File output;
private ZipUrlInstaller zipUrlInstaller;
private boolean append;
private File log;
private Configuration configuration;
private String type = ContainerType.LOCAL.getType();
public ContainerType getType()
{
return ContainerType.toType(this.type);
}
public String getContainerId()
{
return this.containerId;
}
public File getHome()
{
return this.home;
}
public File getOutput()
{
return this.output;
}
public ZipUrlInstaller getZipUrlInstaller()
{
return this.zipUrlInstaller;
}
public boolean shouldAppend()
{
return this.append;
}
public File getLog()
{
return this.log;
}
public Configuration getConfiguration()
{
return this.configuration;
}
public Class getImplementation()
{
return this.implementation;
}
public org.codehaus.cargo.container.Container createContainer() throws MojoExecutionException
{
ContainerFactory factory = new DefaultContainerFactory();
// If the user has registered a custom container class, register it against the
// default container factory.
if (getImplementation() != null)
{
factory.registerContainer(getContainerId(), getType(), getImplementation());
}
if (getConfiguration() == null)
{
throw new MojoExecutionException("Missing mandatory [configuration] element.");
}
org.codehaus.cargo.container.Container container = factory.createContainer(
getContainerId(), getType(), getConfiguration().createConfiguration(getContainerId()));
if (container instanceof LocalContainer)
{
setupHome(container);
}
setupMonitor(container);
return container;
}
/**
* Set up a home dir (possibly using a ZipURLInstaller).
*/
private void setupHome(org.codehaus.cargo.container.Container container)
{
if (getHome() != null)
{
((LocalContainer) container).setHomeDir(getHome());
}
else if (getZipUrlInstaller() != null)
{
ZipURLInstaller installer = getZipUrlInstaller().createInstaller();
if (getLog() != null)
{
installer.setMonitor(container.getMonitor());
}
installer.install();
((LocalContainer) container).setHomeDir(installer.getHomeDir());
}
}
/**
* Set up a log monitor for the container if logging is enabled.
*/
private void setupMonitor(org.codehaus.cargo.container.Container container)
{
if (getLog() != null)
{
// Ensure that the directories where the log will go are created
getLog().getParentFile().mkdirs();
Monitor monitor = new FileMonitor(getLog(), false);
container.setMonitor(monitor);
// TODO: Split this task into 2 (one for local containers and one for remote containers
// so that there's no need to check the container type).
if (container instanceof LocalContainer)
{
((LocalContainer) container).getConfiguration().setMonitor(monitor);
}
else
{
((RemoteContainer) container).getConfiguration().setMonitor(monitor);
}
}
}
}