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. import org.apache.tools.ant.AntClassLoader;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Task;
  21. import org.apache.tools.ant.taskdefs.optional.jsp.JspNameMangler;
  22. import org.apache.tools.ant.taskdefs.optional.jsp.Jasper41Mangler;
  23. /**
  24. * Creates the necessary compiler adapter, given basic criteria.
  25. *
  26. */
  27. public class JspCompilerAdapterFactory {
  28. /** This is a singleton -- can't create instances!! */
  29. private JspCompilerAdapterFactory() {
  30. }
  31. /**
  32. * Based on the parameter passed in, this method creates the necessary
  33. * factory desired.
  34. *
  35. * The current mapping for compiler names are as follows:
  36. * <ul><li>jasper = jasper compiler (the default)
  37. * <li><i>a fully quallified classname</i> = the name of a jsp compiler
  38. * adapter
  39. * </ul>
  40. *
  41. * @param compilerType either the name of the desired compiler, or the
  42. * full classname of the compiler's adapter.
  43. * @param task a task to log through.
  44. * @throws BuildException if the compiler type could not be resolved into
  45. * a compiler adapter.
  46. */
  47. public static JspCompilerAdapter getCompiler(String compilerType, Task task)
  48. throws BuildException {
  49. return getCompiler(compilerType, task,
  50. task.getProject().createClassLoader(null));
  51. }
  52. /**
  53. * Based on the parameter passed in, this method creates the necessary
  54. * factory desired.
  55. *
  56. * The current mapping for compiler names are as follows:
  57. * <ul><li>jasper = jasper compiler (the default)
  58. * <li><i>a fully quallified classname</i> = the name of a jsp compiler
  59. * adapter
  60. * </ul>
  61. *
  62. * @param compilerType either the name of the desired compiler, or the
  63. * full classname of the compiler's adapter.
  64. * @param task a task to log through.
  65. * @param loader AntClassLoader with which the compiler should be loaded
  66. * @throws BuildException if the compiler type could not be resolved into
  67. * a compiler adapter.
  68. */
  69. public static JspCompilerAdapter getCompiler(String compilerType, Task task,
  70. AntClassLoader loader)
  71. throws BuildException {
  72. if (compilerType.equalsIgnoreCase("jasper")) {
  73. //tomcat4.0 gets the old mangler
  74. return new JasperC(new JspNameMangler());
  75. }
  76. if (compilerType.equalsIgnoreCase("jasper41")) {
  77. //tomcat4.1 gets the new one
  78. return new JasperC(new Jasper41Mangler());
  79. }
  80. return resolveClassName(compilerType, loader);
  81. }
  82. /**
  83. * Tries to resolve the given classname into a compiler adapter.
  84. * Throws a fit if it can't.
  85. *
  86. * @param className The fully qualified classname to be created.
  87. * @param classloader Classloader with which to load the class
  88. * @throws BuildException This is the fit that is thrown if className
  89. * isn't an instance of JspCompilerAdapter.
  90. */
  91. private static JspCompilerAdapter resolveClassName(String className,
  92. AntClassLoader classloader)
  93. throws BuildException {
  94. try {
  95. Class c = classloader.findClass(className);
  96. Object o = c.newInstance();
  97. return (JspCompilerAdapter) o;
  98. } catch (ClassNotFoundException cnfe) {
  99. throw new BuildException(className + " can\'t be found.", cnfe);
  100. } catch (ClassCastException cce) {
  101. throw new BuildException(className + " isn\'t the classname of "
  102. + "a compiler adapter.", cce);
  103. } catch (Throwable t) {
  104. // for all other possibilities
  105. throw new BuildException(className + " caused an interesting "
  106. + "exception.", t);
  107. }
  108. }
  109. }