/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.embedded.core.deployable;
import java.io.File;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Logger;
import org.jboss.embedded.api.Deployable;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
/**
* {@link Deployable} view backed by a ShrinkWrap
* {@link Archive}. Immutable and Thread-safe.
*
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
* @version $Revision: $
*/
public final class DeployableArchive implements Deployable
{
//-------------------------------------------------------------------------------------||
// Class Members ----------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* Logger
*/
private static final Logger log = Logger.getLogger(DeployableArchive.class.getName());
/**
* System property denoting the location of the temp dir
*/
private static final String SYS_PROP_TMP_DIR = "java.io.tmpdir";
//-------------------------------------------------------------------------------------||
// Instance Members -------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* The serialized view of this archive which
* will act as the delegate
*/
private final Deployable serializedView;
/**
* Archive from which this Deployable was created; we use
* this in determining value equality
*/
private final Archive<?> createdFrom;
//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* Creates a new {@link Deployable} backed by the specified
* {@link Archive}
*
* @param archive
* @throws IllegalArgumentException If the archive is not specified
*/
public DeployableArchive(final Archive<?> archive) throws IllegalArgumentException
{
if (archive == null)
{
throw new IllegalArgumentException("archive must be specified");
}
/*
* Make a temp file
*/
// Get location for the new temp dir, and validate
final String tempDirLocation = AccessController.doPrivileged(GetTempDirAction.INSTANCE);
final File tmpDir = new File(tempDirLocation);
if (!tmpDir.exists())
{
throw new IllegalStateException("Could not obtain valid temp directory: " + tmpDir.getAbsolutePath());
}
if (!tmpDir.isDirectory())
{
throw new IllegalStateException("Temp location must be a directory: " + tmpDir.getAbsolutePath());
}
// Make the new temp file
final String name = archive.getName();
final File tmpFile = new File(tmpDir, name);
tmpFile.deleteOnExit();
// Write the ZIP to the temp file
archive.as(ZipExporter.class).exportZip(tmpFile, true);
// Create a Deployable from the File
final Deployable deployableFile = new DeployableFile(tmpFile);
// Set
this.createdFrom = archive;
this.serializedView = deployableFile;
}
//-------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* {@inheritDoc}
* @see org.jboss.embedded.api.Deployable#getTarget()
*/
@Override
public URL getTarget()
{
return serializedView.getTarget();
}
//-------------------------------------------------------------------------------------||
// Inner Classes ----------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* Privileged action to obtain the temp directory
*/
private static enum GetTempDirAction implements PrivilegedAction<String> {
INSTANCE;
@Override
public String run()
{
return System.getProperty(SYS_PROP_TMP_DIR);
}
}
//-------------------------------------------------------------------------------------||
// OVerridden Implementations ---------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* {@inheritDoc}
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((createdFrom == null) ? 0 : createdFrom.hashCode());
return result;
}
/**
* {@inheritDoc}
* Value equality is based upon the underlying archive;
* if the referents are equal, so are we.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final DeployableArchive other = (DeployableArchive) obj;
if (createdFrom == null)
{
// Should never be null
throw new IllegalStateException("Created from archive must never be null");
}
// If the underlying archives created from are equal, so are we
else if (createdFrom == other.createdFrom)
{
return true;
}
return false;
}
/**
* {@inheritDoc}
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return this.getClass().getSimpleName() + " [archive=" + createdFrom.toString(true) + "]";
}
}