1 | /*
|
---|
2 | * Copyright 2005 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.native2ascii;
|
---|
18 |
|
---|
19 | import org.apache.tools.ant.BuildException;
|
---|
20 | import org.apache.tools.ant.ProjectComponent;
|
---|
21 | import org.apache.tools.ant.util.JavaEnvUtils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Creates the Native2AsciiAdapter based on the user choice and
|
---|
25 | * potentially the VM vendor.
|
---|
26 | *
|
---|
27 | * @since Ant 1.6.3
|
---|
28 | */
|
---|
29 | public class Native2AsciiAdapterFactory {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Determines the default choice of adapter based on the VM
|
---|
33 | * vendor.
|
---|
34 | *
|
---|
35 | * @return the default choice of adapter based on the VM
|
---|
36 | * vendor
|
---|
37 | */
|
---|
38 | public static String getDefault() {
|
---|
39 | if (JavaEnvUtils.isKaffe()) {
|
---|
40 | return KaffeNative2Ascii.IMPLEMENTATION_NAME;
|
---|
41 | }
|
---|
42 | return SunNative2Ascii.IMPLEMENTATION_NAME;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Creates the Native2AsciiAdapter based on the user choice and *
|
---|
47 | * potentially the VM vendor.
|
---|
48 | *
|
---|
49 | * @param choice the user choice (if any).
|
---|
50 | * @param log a ProjectComponent instance used to access Ant's
|
---|
51 | * logging system.
|
---|
52 | * @return The adapter to use.
|
---|
53 | */
|
---|
54 | public static Native2AsciiAdapter getAdapter(String choice,
|
---|
55 | ProjectComponent log)
|
---|
56 | throws BuildException {
|
---|
57 | if ((JavaEnvUtils.isKaffe() && choice == null)
|
---|
58 | || KaffeNative2Ascii.IMPLEMENTATION_NAME.equals(choice)) {
|
---|
59 | return new KaffeNative2Ascii();
|
---|
60 | } else if (SunNative2Ascii.IMPLEMENTATION_NAME.equals(choice)) {
|
---|
61 | return new SunNative2Ascii();
|
---|
62 | } else if (choice != null) {
|
---|
63 | return resolveClassName(choice);
|
---|
64 | }
|
---|
65 |
|
---|
66 | // This default has been good enough until Ant 1.6.3, so stick
|
---|
67 | // with it
|
---|
68 | return new SunNative2Ascii();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Tries to resolve the given classname into a native2ascii adapter.
|
---|
73 | * Throws a fit if it can't.
|
---|
74 | *
|
---|
75 | * @param className The fully qualified classname to be created.
|
---|
76 | * @throws BuildException This is the fit that is thrown if className
|
---|
77 | * isn't an instance of Native2AsciiAdapter.
|
---|
78 | */
|
---|
79 | private static Native2AsciiAdapter resolveClassName(String className)
|
---|
80 | throws BuildException {
|
---|
81 | try {
|
---|
82 | Class c = Class.forName(className);
|
---|
83 | Object o = c.newInstance();
|
---|
84 | return (Native2AsciiAdapter) o;
|
---|
85 | } catch (ClassNotFoundException cnfe) {
|
---|
86 | throw new BuildException("Can't load " + className, cnfe);
|
---|
87 | } catch (ClassCastException cce) {
|
---|
88 | throw new BuildException(className
|
---|
89 | + " is not a Native2Ascii adapter", cce);
|
---|
90 | } catch (Throwable t) {
|
---|
91 | // for all other possibilities
|
---|
92 | throw new BuildException(className + " caused an interesting "
|
---|
93 | + "exception.", t);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|