1 | /*
|
---|
2 | * Copyright 2001-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.jsp.compilers;
|
---|
18 |
|
---|
19 | import org.apache.tools.ant.AntClassLoader;
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Task;
|
---|
22 | import org.apache.tools.ant.taskdefs.optional.jsp.JspNameMangler;
|
---|
23 | import org.apache.tools.ant.taskdefs.optional.jsp.Jasper41Mangler;
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Creates the necessary compiler adapter, given basic criteria.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class JspCompilerAdapterFactory {
|
---|
31 |
|
---|
32 | /** This is a singleton -- can't create instances!! */
|
---|
33 | private JspCompilerAdapterFactory() {
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Based on the parameter passed in, this method creates the necessary
|
---|
38 | * factory desired.
|
---|
39 | *
|
---|
40 | * The current mapping for compiler names are as follows:
|
---|
41 | * <ul><li>jasper = jasper compiler (the default)
|
---|
42 | * <li><i>a fully quallified classname</i> = the name of a jsp compiler
|
---|
43 | * adapter
|
---|
44 | * </ul>
|
---|
45 | *
|
---|
46 | * @param compilerType either the name of the desired compiler, or the
|
---|
47 | * full classname of the compiler's adapter.
|
---|
48 | * @param task a task to log through.
|
---|
49 | * @throws BuildException if the compiler type could not be resolved into
|
---|
50 | * a compiler adapter.
|
---|
51 | */
|
---|
52 | public static JspCompilerAdapter getCompiler(String compilerType, Task task)
|
---|
53 | throws BuildException {
|
---|
54 | return getCompiler(compilerType, task,
|
---|
55 | task.getProject().createClassLoader(null));
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Based on the parameter passed in, this method creates the necessary
|
---|
60 | * factory desired.
|
---|
61 | *
|
---|
62 | * The current mapping for compiler names are as follows:
|
---|
63 | * <ul><li>jasper = jasper compiler (the default)
|
---|
64 | * <li><i>a fully quallified classname</i> = the name of a jsp compiler
|
---|
65 | * adapter
|
---|
66 | * </ul>
|
---|
67 | *
|
---|
68 | * @param compilerType either the name of the desired compiler, or the
|
---|
69 | * full classname of the compiler's adapter.
|
---|
70 | * @param task a task to log through.
|
---|
71 | * @param loader AntClassLoader with which the compiler should be loaded
|
---|
72 | * @throws BuildException if the compiler type could not be resolved into
|
---|
73 | * a compiler adapter.
|
---|
74 | */
|
---|
75 | public static JspCompilerAdapter getCompiler(String compilerType, Task task,
|
---|
76 | AntClassLoader loader)
|
---|
77 | throws BuildException {
|
---|
78 |
|
---|
79 | if (compilerType.equalsIgnoreCase("jasper")) {
|
---|
80 | //tomcat4.0 gets the old mangler
|
---|
81 | return new JasperC(new JspNameMangler());
|
---|
82 | }
|
---|
83 | if (compilerType.equalsIgnoreCase("jasper41")) {
|
---|
84 | //tomcat4.1 gets the new one
|
---|
85 | return new JasperC(new Jasper41Mangler());
|
---|
86 | }
|
---|
87 | return resolveClassName(compilerType, loader);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Tries to resolve the given classname into a compiler adapter.
|
---|
92 | * Throws a fit if it can't.
|
---|
93 | *
|
---|
94 | * @param className The fully qualified classname to be created.
|
---|
95 | * @param classloader Classloader with which to load the class
|
---|
96 | * @throws BuildException This is the fit that is thrown if className
|
---|
97 | * isn't an instance of JspCompilerAdapter.
|
---|
98 | */
|
---|
99 | private static JspCompilerAdapter resolveClassName(String className,
|
---|
100 | AntClassLoader classloader)
|
---|
101 | throws BuildException {
|
---|
102 | try {
|
---|
103 | Class c = classloader.findClass(className);
|
---|
104 | Object o = c.newInstance();
|
---|
105 | return (JspCompilerAdapter) o;
|
---|
106 | } catch (ClassNotFoundException cnfe) {
|
---|
107 | throw new BuildException(className + " can\'t be found.", cnfe);
|
---|
108 | } catch (ClassCastException cce) {
|
---|
109 | throw new BuildException(className + " isn\'t the classname of "
|
---|
110 | + "a compiler adapter.", cce);
|
---|
111 | } catch (Throwable t) {
|
---|
112 | // for all other possibilities
|
---|
113 | throw new BuildException(className + " caused an interesting "
|
---|
114 | + "exception.", t);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | }
|
---|