
de.smartics.maven.plugin.jboss.modules.util.classpath.ClassPathContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smartics-jboss-modules-maven-plugin Show documentation
Show all versions of smartics-jboss-modules-maven-plugin Show documentation
Generates an archive of modules based on information in a POM to be copied
to an JBoss 7 installation.
The newest version!
/*
* Copyright 2013-2018 smartics, Kronseder & Reiner GmbH
*
* 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 de.smartics.maven.plugin.jboss.modules.util.classpath;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import org.apache.commons.lang.ObjectUtils;
import de.smartics.maven.plugin.jboss.modules.util.Arg;
/**
* A constraint on resources to be loaded from the class path.
*/
public final class ClassPathContext
{
// ********************************* Fields *********************************
// --- constants ------------------------------------------------------------
// --- members --------------------------------------------------------------
/**
* The class loader to load the directory listings.
*/
private final ClassLoader classLoader;
/**
* Selects the archive root to load from.
*/
private final String archiveRoot;
// ****************************** Initializer *******************************
// ****************************** Constructors ******************************
/**
* Default constructor.
*
* @param classLoader the class loader to load the directory listings.
* @param archiveRoot the value for archiveRoot.
* @throws NullPointerException if {@code classLoader} is null
.
*/
public ClassPathContext(final ClassLoader classLoader,
final String archiveRoot) throws NullPointerException
{
Arg.checkNotNull("classLoader", classLoader);
this.classLoader = classLoader;
this.archiveRoot = archiveRoot;
}
// ****************************** Inner Classes *****************************
// ********************************* Methods ********************************
// --- init -----------------------------------------------------------------
// --- get&set --------------------------------------------------------------
// --- business -------------------------------------------------------------
/**
* Constructs the URL to the resource.
*
* @param resource the resource whose URL is requested.
* @return the URL to the resource or null
if the resource cannot
* be found on the class path.
*/
public URL getResource(final String resource)
{
if (archiveRoot == null)
{
return classLoader.getResource(resource);
}
// TODO: maybe we should simply construct the URL?
try
{
for (final Enumeration en = classLoader.getResources(resource); en
.hasMoreElements();)
{
final URL current = en.nextElement();
final String urlString = current.toExternalForm();
if (urlString.startsWith(archiveRoot))
{
return current;
}
}
}
catch (final IOException e)
{
// return null
}
return null;
}
/**
* Opens the stream to the resource.
*
* @param resource the resource whose stream is requested.
* @return the stream to the resource or null
if the resource
* cannot be found on the class path. If a stream is returned, the
* client is responsible to close that stream.
*/
public InputStream getResourceAsStream(final String resource)
{
final URL url = getResource(resource);
if (url != null)
{
try
{
return url.openStream();
}
catch (final IOException e)
{
// return null;
}
}
return null;
}
/**
* Constructs the URL to the resource.
*
* @param resource the resource whose URL is requested.
* @return the constructed URL.
* @throws IllegalArgumentException if the URL to the resource cannot be
* constructed.
*/
public URL createUrl(final String resource) throws IllegalArgumentException
{
try
{
final String urlString =
(archiveRoot != null ? archiveRoot : "") + resource;
return new URL(urlString);
}
catch (final MalformedURLException e)
{
throw new IllegalArgumentException("Cannot construct URL with resource '"
+ resource + "'.", e);
}
}
// --- object basics --------------------------------------------------------
/**
* Returns the hash code of the object.
*
* @return the hash code.
*/
@Override
public int hashCode()
{
return ObjectUtils.hashCode(archiveRoot);
}
/**
* Returns true
if the given object is semantically equal to the
* given object, false
otherwise.
*
* @param object the instance to compare to.
* @return true
if the given object is semantically equal to the
* given object, false
otherwise.
*/
@Override
public boolean equals(final Object object)
{
if (this == object)
{
return true;
}
else if (object == null || getClass() != object.getClass())
{
return false;
}
final ClassPathContext other = (ClassPathContext) object;
return ObjectUtils.equals(archiveRoot, other.archiveRoot);
}
@Override
public String toString()
{
return archiveRoot != null ? archiveRoot : "";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy