blob: 0a533793ec939b7e901279477fc35c7573f9b979 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.myfaces.buildtools.maven2.plugin.javascript.jmt.compress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.Iterator;
import org.apache.maven.artifact.Artifact;
/**
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
public class IsolatedClassLoader
extends URLClassLoader
{
public IsolatedClassLoader( Artifact artifact )
{
super( getArtifactURLs( artifact ) );
}
public IsolatedClassLoader( Collection artifacts )
{
super( getArtifactURLs( artifacts ) );
}
private static URL[] getArtifactURLs( Collection artifacts )
{
URL[] urls = new URL[artifacts.size()];
try
{
int i = 0;
for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); )
{
Artifact artifact = (Artifact) iterator.next();
urls[i++] = artifact.getFile().toURL();
}
}
catch ( MalformedURLException e )
{
return null;
}
return urls;
}
private static URL[] getArtifactURLs( Artifact artifact )
{
URL url;
try
{
url = artifact.getFile().toURL();
return new URL[] { url };
}
catch ( MalformedURLException e )
{
return null;
}
}
public Class loadClass( String name )
throws ClassNotFoundException
{
Class c = findLoadedClass( name );
if ( c == null )
{
try
{
return findClass( name );
}
catch ( ClassNotFoundException e )
{
return super.loadClass( name );
}
}
return super.loadClass( name );
}
}