org.apache.catalina.loader.VirtualWebappLoader Maven / Gradle / Ivy
/*
* 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.catalina.loader;
import java.io.File;
import java.util.StringTokenizer;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.util.LifecycleBase;
/**
* Simple webapp classloader that allows a customized classpath to be added
* through configuration in context xml. Any additional classpath entry will be
* added to the default webapp classpath, making easy to emulate a standard
* webapp without the need for assembly all the webapp dependencies as jars in
* WEB-INF/lib.
*
*
* <Context docBase="\webapps\mydocbase">
* <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
* virtualClasspath="\dir\classes;\somedir\somejar.jar"/>
* </Context>
*
*
*
* This is not meant to be used for production.
* Its meant to ease development with IDE's without the
* need for fully republishing jars in WEB-INF/lib
*
*
*
* @author Fabrizio Giustina
* @version $Id: VirtualWebappLoader.java 936825 2010-04-22 13:11:24Z rjung $
*/
public class VirtualWebappLoader extends WebappLoader {
/**
* ;
separated list of additional path elements.
*/
private String virtualClasspath;
/**
* Construct a new WebappLoader with no defined parent class loader (so that
* the actual parent will be the system class loader).
*/
public VirtualWebappLoader() {
super();
}
/**
* Construct a new WebappLoader with the specified class loader to be
* defined as the parent of the ClassLoader we ultimately create.
*
* @param parent The parent class loader
*/
public VirtualWebappLoader(ClassLoader parent) {
super(parent);
}
/**
* virtualClasspath
attribute that will be automatically set
* from the Context
virtualClasspath
attribute
* from the context xml file.
* @param path ;
separated list of path elements.
*/
public void setVirtualClasspath(String path) {
virtualClasspath = path;
}
/**
* @return Returns searchVirtualFirst.
*/
public boolean getSearchVirtualFirst() {
return getSearchExternalFirst();
}
/**
* @param searchVirtualFirst Whether the virtual class path should be searched before the webapp
*/
public void setSearchVirtualFirst(boolean searchVirtualFirst) {
setSearchExternalFirst(searchVirtualFirst);
}
/**
* Implement the requirements
* of {@link LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
@Override
protected void startInternal() throws LifecycleException {
// just add any jar/directory set in virtual classpath to the
// repositories list before calling start on the standard WebappLoader
StringTokenizer tkn = new StringTokenizer(virtualClasspath, ";");
while (tkn.hasMoreTokens()) {
File file = new File(tkn.nextToken());
if (!file.exists()) {
continue;
}
addRepository(file.toURI().toString());
}
super.startInternal();
}
}