1. /*
  2. * Copyright 2001-2002,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.rmic;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Task;
  20. /**
  21. * Creates the necessary rmic adapter, given basic criteria.
  22. *
  23. * @since 1.4
  24. */
  25. public class RmicAdapterFactory {
  26. /** This is a singleton -- can't create instances!! */
  27. private RmicAdapterFactory() {
  28. }
  29. /**
  30. * Based on the parameter passed in, this method creates the necessary
  31. * factory desired.
  32. *
  33. * <p>The current mapping for rmic names are as follows:</p>
  34. * <ul><li>sun = SUN's rmic
  35. * <li>kaffe = Kaffe's rmic
  36. * <li><i>a fully quallified classname</i> = the name of a rmic
  37. * adapter
  38. * </ul>
  39. *
  40. * @param rmicType either the name of the desired rmic, or the
  41. * full classname of the rmic's adapter.
  42. * @param task a task to log through.
  43. * @throws BuildException if the rmic type could not be resolved into
  44. * a rmic adapter.
  45. */
  46. public static RmicAdapter getRmic(String rmicType, Task task)
  47. throws BuildException {
  48. if (rmicType.equalsIgnoreCase("sun")) {
  49. return new SunRmic();
  50. } else if (rmicType.equalsIgnoreCase("kaffe")) {
  51. return new KaffeRmic();
  52. } else if (rmicType.equalsIgnoreCase("weblogic")) {
  53. return new WLRmic();
  54. }
  55. return resolveClassName(rmicType);
  56. }
  57. /**
  58. * Tries to resolve the given classname into a rmic adapter.
  59. * Throws a fit if it can't.
  60. *
  61. * @param className The fully qualified classname to be created.
  62. * @throws BuildException This is the fit that is thrown if className
  63. * isn't an instance of RmicAdapter.
  64. */
  65. private static RmicAdapter resolveClassName(String className)
  66. throws BuildException {
  67. try {
  68. Class c = Class.forName(className);
  69. Object o = c.newInstance();
  70. return (RmicAdapter) o;
  71. } catch (ClassNotFoundException cnfe) {
  72. throw new BuildException(className + " can\'t be found.", cnfe);
  73. } catch (ClassCastException cce) {
  74. throw new BuildException(className + " isn\'t the classname of "
  75. + "a rmic adapter.", cce);
  76. } catch (Throwable t) {
  77. // for all other possibilities
  78. throw new BuildException(className + " caused an interesting "
  79. + "exception.", t);
  80. }
  81. }
  82. }