01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.tools.ant.taskdefs.optional.native2ascii;
19:
20: import org.apache.tools.ant.BuildException;
21: import org.apache.tools.ant.ProjectComponent;
22: import org.apache.tools.ant.util.ClasspathUtils;
23: import org.apache.tools.ant.util.JavaEnvUtils;
24:
25: /**
26: * Creates the Native2AsciiAdapter based on the user choice and
27: * potentially the VM vendor.
28: *
29: * @since Ant 1.6.3
30: */
31: public class Native2AsciiAdapterFactory {
32:
33: /**
34: * Determines the default choice of adapter based on the VM
35: * vendor.
36: *
37: * @return the default choice of adapter based on the VM
38: * vendor
39: */
40: public static String getDefault() {
41: if (JavaEnvUtils.isKaffe()) {
42: return KaffeNative2Ascii.IMPLEMENTATION_NAME;
43: }
44: return SunNative2Ascii.IMPLEMENTATION_NAME;
45: }
46:
47: /**
48: * Creates the Native2AsciiAdapter based on the user choice and *
49: * potentially the VM vendor.
50: *
51: * @param choice the user choice (if any).
52: * @param log a ProjectComponent instance used to access Ant's
53: * logging system.
54: * @return The adapter to use.
55: * @throws BuildException if there was a problem.
56: */
57: public static Native2AsciiAdapter getAdapter(String choice,
58: ProjectComponent log) throws BuildException {
59: if ((JavaEnvUtils.isKaffe() && choice == null)
60: || KaffeNative2Ascii.IMPLEMENTATION_NAME.equals(choice)) {
61: return new KaffeNative2Ascii();
62: } else if (SunNative2Ascii.IMPLEMENTATION_NAME.equals(choice)) {
63: return new SunNative2Ascii();
64: } else if (choice != null) {
65: return resolveClassName(choice);
66: }
67:
68: // This default has been good enough until Ant 1.6.3, so stick
69: // with it
70: return new SunNative2Ascii();
71: }
72:
73: /**
74: * Tries to resolve the given classname into a native2ascii adapter.
75: * Throws a fit if it can't.
76: *
77: * @param className The fully qualified classname to be created.
78: * @throws BuildException This is the fit that is thrown if className
79: * isn't an instance of Native2AsciiAdapter.
80: */
81: private static Native2AsciiAdapter resolveClassName(String className)
82: throws BuildException {
83: return (Native2AsciiAdapter) ClasspathUtils.newInstance(
84: className, Native2AsciiAdapterFactory.class
85: .getClassLoader(), Native2AsciiAdapter.class);
86: }
87: }
|