/*
* 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.MalformedURLException;
import java.net.URL;
import org.jboss.embedded.api.Deployable;
/**
* {@link Deployable} view backed by a {@link File}.
* Immutable and Thread-safe.
*
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
* @version $Revision: $
*/
public final class DeployableFile implements Deployable
{
//-------------------------------------------------------------------------------------||
// Instance Members -------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* The backing file; used in determining
* value equality
*/
private final File file;
/**
* The delegate which will be representing this File as
* a {@link Deployable}
*/
private final Deployable delegate;
//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* Creates a new {@link Deployable} backed by the specified
* {@link File}.
*
* @param file File or exploded directory to be represented as {@link Deployable}
* @throws IllegalArgumentException If the file is not specified or does not exist
*/
public DeployableFile(final File file) throws IllegalArgumentException
{
// Precondition checks
if (file == null)
{
throw new IllegalArgumentException("file must be specified");
}
if (!file.exists())
{
throw new IllegalArgumentException("file does not exist: " + file.getAbsolutePath());
}
// Get a URL to the File
final URL url;
try
{
url = file.toURI().toURL();
}
catch (final MalformedURLException murle)
{
throw new IllegalArgumentException("URL of file is in improper form", murle);
}
// Create a deployable URL delegate
final Deployable delegate = new DeployableUrl(url);
// Set
this.file = file;
this.delegate = delegate;
}
//-------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* {@inheritDoc}
* @see org.jboss.embedded.api.Deployable#getTarget()
*/
@Override
public URL getTarget()
{
return delegate.getTarget();
}
//-------------------------------------------------------------------------------------||
// Overridden Implementations ---------------------------------------------------------||
//-------------------------------------------------------------------------------------||
/**
* {@inheritDoc}
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((file == null) ? 0 : file.hashCode());
return result;
}
/**
* {@inheritDoc}
* Value equality is based upon the value equality of the underlying File
* @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 DeployableFile other = (DeployableFile) obj;
if (file == null)
{
if (other.file != null)
return false;
}
else if (!file.equals(other.file))
return false;
return true;
}
/**
* {@inheritDoc}
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return this.getClass().getSimpleName() + " [file=" + file + "]";
}
}