KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > DefBase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import org.apache.tools.ant.AntClassLoader;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.types.Path;
25 import org.apache.tools.ant.types.Reference;
26 import org.apache.tools.ant.util.ClasspathUtils;
27
28 /**
29  * Base class for Definitions handling uri and class loading.
30  * (This was part of Definer)
31  *
32  * @since Ant 1.6
33  */

34 public abstract class DefBase extends AntlibDefinition {
35     private ClassLoader JavaDoc createdLoader;
36     private ClasspathUtils.Delegate cpDelegate;
37
38     /**
39      * Check if classpath attributes have been set.
40      * (to be called before getCpDelegate() is used.
41      * @return true if cpDelegate has been created.
42      */

43     protected boolean hasCpDelegate() {
44         return cpDelegate != null;
45     }
46
47     /**
48      * @param reverseLoader if true a delegated loader will take precedence over
49      * the parent
50      * @deprecated since 1.6.x.
51      * stop using this attribute
52      * @ant.attribute ignore="true"
53      */

54     public void setReverseLoader(boolean reverseLoader) {
55         getDelegate().setReverseLoader(reverseLoader);
56         log("The reverseloader attribute is DEPRECATED. It will be removed",
57             Project.MSG_WARN);
58     }
59
60     /**
61      * @return the classpath for this definition
62      */

63     public Path getClasspath() {
64         return getDelegate().getClasspath();
65     }
66
67     /**
68      * @return the reverse loader attribute of the classpath delegate.
69      */

70     public boolean isReverseLoader() {
71         return getDelegate().isReverseLoader();
72     }
73
74     /**
75      * Returns the loader id of the class path Delegate.
76      * @return the loader id
77      */

78     public String JavaDoc getLoaderId() {
79         return getDelegate().getClassLoadId();
80     }
81
82     /**
83      * Returns the class path id of the class path delegate.
84      * @return the class path id
85      */

86     public String JavaDoc getClasspathId() {
87         return getDelegate().getClassLoadId();
88     }
89
90     /**
91      * Set the classpath to be used when searching for component being defined.
92      *
93      * @param classpath an Ant Path object containing the classpath.
94      */

95     public void setClasspath(Path classpath) {
96         getDelegate().setClasspath(classpath);
97     }
98
99     /**
100      * Create the classpath to be used when searching for component being
101      * defined.
102      * @return the classpath of the this definition
103      */

104     public Path createClasspath() {
105         return getDelegate().createClasspath();
106     }
107
108     /**
109      * Set a reference to a classpath to use when loading the files.
110      * To actually share the same loader, set loaderref as well
111      * @param r the reference to the classpath
112      */

113     public void setClasspathRef(Reference r) {
114         getDelegate().setClasspathref(r);
115     }
116
117     /**
118      * Use the reference to locate the loader. If the loader is not
119      * found, the specified classpath will be used and registered
120      * with the specified name.
121      *
122      * This allows multiple taskdef/typedef to use the same class loader,
123      * so they can be used together, eliminating the need to
124      * put them in the CLASSPATH.
125      *
126      * @param r the reference to locate the loader.
127      * @since Ant 1.5
128      */

129     public void setLoaderRef(Reference r) {
130         getDelegate().setLoaderRef(r);
131     }
132
133     /**
134      * create a classloader for this definition
135      * @return the classloader from the cpDelegate
136      */

137     protected ClassLoader JavaDoc createLoader() {
138         if (getAntlibClassLoader() != null && cpDelegate == null) {
139             return getAntlibClassLoader();
140         }
141         if (createdLoader == null) {
142             createdLoader = getDelegate().getClassLoader();
143             // need to load Task via system classloader or the new
144
// task we want to define will never be a Task but always
145
// be wrapped into a TaskAdapter.
146
((AntClassLoader) createdLoader)
147                 .addSystemPackageRoot("org.apache.tools.ant");
148         }
149         return createdLoader;
150     }
151
152     /**
153      * @see org.apache.tools.ant.Task#init()
154      * @throws BuildException on error.
155      * @since Ant 1.6
156      */

157     public void init() throws BuildException {
158         super.init();
159     }
160
161     private ClasspathUtils.Delegate getDelegate() {
162         if (cpDelegate == null) {
163             cpDelegate = ClasspathUtils.getDelegate(this);
164         }
165         return cpDelegate;
166     }
167 }
168
Popular Tags