001 /* 002 $Id: RootLoaderRef.java 3836 2006-06-15 13:31:42Z blackdrag $ 003 004 Copyright 2003 (C) Jochen Theodorou. All Rights Reserved. 005 006 Redistribution and use of this software and associated documentation 007 ("Software"), with or without modification, are permitted provided 008 that the following conditions are met: 009 010 1. Redistributions of source code must retain copyright 011 statements and notices. Redistributions must also contain a 012 copy of this document. 013 014 2. Redistributions in binary form must reproduce the 015 above copyright notice, this list of conditions and the 016 following disclaimer in the documentation and/or other 017 materials provided with the distribution. 018 019 3. The name "groovy" must not be used to endorse or promote 020 products derived from this Software without prior written 021 permission of The Codehaus. For written permission, 022 please contact info@codehaus.org. 023 024 4. Products derived from this Software may not be called "groovy" 025 nor may "groovy" appear in their names without prior written 026 permission of The Codehaus. "groovy" is a registered 027 trademark of The Codehaus. 028 029 5. Due credit should be given to The Codehaus - 030 http://groovy.codehaus.org/ 031 032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS 033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 043 OF THE POSSIBILITY OF SUCH DAMAGE. 044 045 */ 046 047 package org.codehaus.groovy.ant; 048 049 import org.apache.tools.ant.AntClassLoader; 050 import org.apache.tools.ant.BuildException; 051 import org.apache.tools.ant.Project; 052 import org.apache.tools.ant.taskdefs.MatchingTask; 053 import org.apache.tools.ant.types.Path; 054 import org.apache.tools.ant.types.Reference; 055 import org.codehaus.groovy.tools.LoaderConfiguration; 056 import org.codehaus.groovy.tools.RootLoader; 057 058 059 /** 060 * Sets the RootLoader as reference. 061 * Reexecution of this task will set a new instance of RootLoader for 062 * the reference. 063 * 064 * arguments: 065 * <ul> 066 * <li>ref</li> 067 * <li>classpath</li> 068 * </ul> 069 * 070 * all arguments are requiered. 071 * 072 * As ant requieres an AntClassLoader as reference, this will create a RootLoader 073 * and set an AntClassLoader as child and stored in the reference. The AntClassLoader 074 * instance will not have a classpath nor will it have access to the classpath somehow, 075 * all loading is done by the RootLoader parent. To avoid problems with loading classes 076 * multiple times and using them at the same time, this task will filter out the ant jars 077 * and the commons-logging jars. This only works if the ant jars are starting with "ant-" and 078 * the logging jar starts with "commons-logging-". 079 * 080 * This was needed because if ant wants to access a task argument that uses for example a Path 081 * it look for a matching method which includes a matching class. But two classes of the same name 082 * with different classloaders are different, so ant would not be able to find the method. 083 * 084 * @see org.codehaus.groovy.tools.RootLoader 085 * @author Jochen Theodorou 086 * @version $Revision: 3836 $ 087 */ 088 public class RootLoaderRef extends MatchingTask { 089 private String name; 090 private Path taskClasspath; 091 092 /** 093 * sets the name of the reference which should store the Loader 094 */ 095 public void setRef(String n){ 096 name = n; 097 } 098 099 public void execute() throws BuildException { 100 if (taskClasspath==null || taskClasspath.size()==0) { 101 throw new BuildException("no classpath given"); 102 } 103 Project project = getProject(); 104 AntClassLoader loader = new AntClassLoader(makeRoot(),true); 105 project.addReference(name,loader); 106 } 107 108 private RootLoader makeRoot() { 109 String[] list = taskClasspath.list(); 110 LoaderConfiguration lc = new LoaderConfiguration(); 111 for (int i=0; i<list.length; i++) { 112 if (list[i].matches(".*ant-[^/]*jar$")) { 113 continue; 114 } 115 if (list[i].matches(".*commons-logging-[^/]*jar$")) { 116 continue; 117 } 118 if (list[i].matches(".*xerces-[^/]*jar$")) { 119 continue; 120 } 121 lc.addFile(list[i]); 122 } 123 return new RootLoader(lc); 124 } 125 126 /** 127 * Set the classpath to be used for this compilation. 128 * 129 * @param classpath an Ant Path object containing the compilation classpath. 130 */ 131 public void setClasspath(Path classpath) { 132 if (taskClasspath == null) { 133 taskClasspath = classpath; 134 } 135 else { 136 taskClasspath.append(classpath); 137 } 138 } 139 140 /** 141 * Adds a reference to a classpath defined elsewhere. 142 * @param r a reference to a classpath 143 */ 144 public void setClasspathRef(Reference r) { 145 createClasspath().setRefid(r); 146 } 147 148 /** 149 * Adds a path to the classpath. 150 * @return a class path to be configured 151 */ 152 public Path createClasspath() { 153 if (taskClasspath == null) { 154 taskClasspath = new Path(getProject()); 155 } 156 return taskClasspath.createPath(); 157 } 158 }