001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.loader;
018:
019: import java.io.File;
020: import java.util.StringTokenizer;
021:
022: import org.apache.catalina.LifecycleException;
023:
024: /**
025: * Simple webapp classloader that allows a customized classpath to be added
026: * through configuration in context xml. Any additional classpath entry will be
027: * added to the default webapp classpath, making easy to emulate a standard
028: * webapp without the need for assembly all the webapp dependencies as jars in
029: * WEB-INF/lib.
030: *
031: * <code>
032: * <Context docBase="\webapps\mydocbase">
033: * <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
034: * virtualClasspath="\dir\classes;\somedir\somejar.jar"/>
035: * </Context>
036: * </code>
037: *
038: *
039: * <strong>This is not meant to be used for production.
040: * Its meant to ease development with IDE's without the
041: * need for fully republishing jars in WEB-INF/lib</strong>
042: *
043: *
044: *
045: * @author Fabrizio Giustina
046: * @version $Id: $
047: */
048: public class VirtualWebappLoader extends WebappLoader {
049:
050: /**
051: * <code>;</code> separated list of additional path elements.
052: */
053: private String virtualClasspath;
054:
055: /**
056: * Construct a new WebappLoader with no defined parent class loader (so that
057: * the actual parent will be the system class loader).
058: */
059: public VirtualWebappLoader() {
060: super ();
061: }
062:
063: /**
064: * Construct a new WebappLoader with the specified class loader to be
065: * defined as the parent of the ClassLoader we ultimately create.
066: *
067: * @param parent The parent class loader
068: */
069: public VirtualWebappLoader(ClassLoader parent) {
070: super (parent);
071: }
072:
073: /**
074: * <code>virtualClasspath</code> attribute that will be automatically set
075: * from the <code>Context</code> <code>virtualClasspath</code> attribute
076: * from the context xml file.
077: * @param path <code>;</code> separated list of path elements.
078: */
079: public void setVirtualClasspath(String path) {
080: virtualClasspath = path;
081: }
082:
083: @Override
084: public void start() throws LifecycleException {
085:
086: // just add any jar/directory set in virtual classpath to the
087: // repositories list before calling start on the standard WebappLoader
088: StringTokenizer tkn = new StringTokenizer(virtualClasspath, ";");
089: while (tkn.hasMoreTokens()) {
090: File file = new File(tkn.nextToken());
091: if (!file.exists()) {
092: continue;
093: }
094: if (file.isDirectory()) {
095: addRepository("file:/" + file.getAbsolutePath() + "/");
096: } else {
097: addRepository("file:/" + file.getAbsolutePath());
098: }
099: }
100:
101: super.start();
102: }
103:
104: }
|