annotate agent/src/share/classes/sun/jvm/hotspot/jdi/ConnectorImpl.java @ 5936:970cbbba54b0

7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6 Summary: On Mac OS X, align system property "os.arch" with Apple legacy JDKs. Also, improve os.name string matching by using contains() method instead of .startsWith(). Reviewed-by: dcubed, phh, ohair, katleman Contributed-by: james.melvin@oracle.com
author jmelvin
date Fri, 16 Mar 2012 15:13:22 -0400
parents 37be97a58393
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
5936
970cbbba54b0 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 2471
diff changeset
2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.jdi;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import com.sun.jdi.connect.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import com.sun.jdi.InternalException;
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import java.lang.ref.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import java.lang.reflect.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 abstract class ConnectorImpl implements Connector {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 Map defaultArguments = new LinkedHashMap();
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 // Used by BooleanArgument
a61af66fc99e Initial load
duke
parents:
diff changeset
39 static String trueString = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
40 static String falseString;
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42
a61af66fc99e Initial load
duke
parents:
diff changeset
43 /** This is not public in VirtualMachineManagerImpl
a61af66fc99e Initial load
duke
parents:
diff changeset
44 ThreadGroup mainGroupForJDI() {
a61af66fc99e Initial load
duke
parents:
diff changeset
45 return ((VirtualMachineManagerImpl)manager).mainGroupForJDI();
a61af66fc99e Initial load
duke
parents:
diff changeset
46 }
a61af66fc99e Initial load
duke
parents:
diff changeset
47 ***/
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // multiple debuggee support for SA/JDI
a61af66fc99e Initial load
duke
parents:
diff changeset
50 private static List freeVMClasses; // List<SoftReference<Class>>
a61af66fc99e Initial load
duke
parents:
diff changeset
51 private static ClassLoader myLoader;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // debug mode for SA/JDI connectors
a61af66fc99e Initial load
duke
parents:
diff changeset
53 static final protected boolean DEBUG;
a61af66fc99e Initial load
duke
parents:
diff changeset
54 static {
a61af66fc99e Initial load
duke
parents:
diff changeset
55 myLoader = ConnectorImpl.class.getClassLoader();
a61af66fc99e Initial load
duke
parents:
diff changeset
56 freeVMClasses = new ArrayList(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
57 DEBUG = System.getProperty("sun.jvm.hotspot.jdi.ConnectorImpl.DEBUG") != null;
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // add a new free VirtualMachineImpl class
a61af66fc99e Initial load
duke
parents:
diff changeset
61 private static synchronized void addFreeVMImplClass(Class clazz) {
a61af66fc99e Initial load
duke
parents:
diff changeset
62 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
63 System.out.println("adding free VirtualMachineImpl class");
a61af66fc99e Initial load
duke
parents:
diff changeset
64 }
a61af66fc99e Initial load
duke
parents:
diff changeset
65 freeVMClasses.add(new SoftReference(clazz));
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // returns null if we don't have anything free
a61af66fc99e Initial load
duke
parents:
diff changeset
69 private static synchronized Class getFreeVMImplClass() {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 while (!freeVMClasses.isEmpty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 SoftReference ref = (SoftReference) freeVMClasses.remove(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
72 Object o = ref.get();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 if (o != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 System.out.println("re-using loaded VirtualMachineImpl");
a61af66fc99e Initial load
duke
parents:
diff changeset
76 }
a61af66fc99e Initial load
duke
parents:
diff changeset
77 return (Class) o;
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79 }
a61af66fc99e Initial load
duke
parents:
diff changeset
80 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 private static Class getVMImplClassFrom(ClassLoader cl)
a61af66fc99e Initial load
duke
parents:
diff changeset
84 throws ClassNotFoundException {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 return Class.forName("sun.jvm.hotspot.jdi.VirtualMachineImpl", true, cl);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 /* SA has not been designed to support multiple debuggee VMs
a61af66fc99e Initial load
duke
parents:
diff changeset
89 * at-a-time. But, JDI supports multiple debuggee VMs. We
a61af66fc99e Initial load
duke
parents:
diff changeset
90 * support multiple debuggee VMs in SA/JDI, by creating a new
a61af66fc99e Initial load
duke
parents:
diff changeset
91 * class loader instance (refer to comment in SAJDIClassLoader
a61af66fc99e Initial load
duke
parents:
diff changeset
92 * for details). But, to avoid excessive class loading (and
a61af66fc99e Initial load
duke
parents:
diff changeset
93 * thereby resulting in larger footprint), we re-use 'dispose'd
a61af66fc99e Initial load
duke
parents:
diff changeset
94 * VirtualMachineImpl classes.
a61af66fc99e Initial load
duke
parents:
diff changeset
95 */
a61af66fc99e Initial load
duke
parents:
diff changeset
96 protected static Class loadVirtualMachineImplClass()
a61af66fc99e Initial load
duke
parents:
diff changeset
97 throws ClassNotFoundException {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 Class vmImplClass = getFreeVMImplClass();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 if (vmImplClass == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 ClassLoader cl = new SAJDIClassLoader(myLoader);
a61af66fc99e Initial load
duke
parents:
diff changeset
101 vmImplClass = getVMImplClassFrom(cl);
a61af66fc99e Initial load
duke
parents:
diff changeset
102 }
a61af66fc99e Initial load
duke
parents:
diff changeset
103 return vmImplClass;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 /* We look for System property sun.jvm.hotspot.jdi.<vm version>.
a61af66fc99e Initial load
duke
parents:
diff changeset
107 * This property should have the value of JDK HOME directory for
a61af66fc99e Initial load
duke
parents:
diff changeset
108 * the given <vm version>.
a61af66fc99e Initial load
duke
parents:
diff changeset
109 */
a61af66fc99e Initial load
duke
parents:
diff changeset
110 private static String getSAClassPathForVM(String vmVersion) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 final String prefix = "sun.jvm.hotspot.jdi.";
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // look for exact match of VM version
a61af66fc99e Initial load
duke
parents:
diff changeset
113 String jvmHome = System.getProperty(prefix + vmVersion);
a61af66fc99e Initial load
duke
parents:
diff changeset
114 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 System.out.println("looking for System property " + prefix + vmVersion);
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 if (jvmHome == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // omit chars after first '-' in VM version and try
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // for example, in '1.5.0-b55' we take '1.5.0'
a61af66fc99e Initial load
duke
parents:
diff changeset
121 int index = vmVersion.indexOf('-');
a61af66fc99e Initial load
duke
parents:
diff changeset
122 if (index != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 vmVersion = vmVersion.substring(0, index);
a61af66fc99e Initial load
duke
parents:
diff changeset
124 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 System.out.println("looking for System property " + prefix + vmVersion);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
a61af66fc99e Initial load
duke
parents:
diff changeset
127 jvmHome = System.getProperty(prefix + vmVersion);
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 if (jvmHome == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // System property is not set
a61af66fc99e Initial load
duke
parents:
diff changeset
132 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 System.out.println("can't locate JDK home for " + vmVersion);
a61af66fc99e Initial load
duke
parents:
diff changeset
134 }
a61af66fc99e Initial load
duke
parents:
diff changeset
135 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138
a61af66fc99e Initial load
duke
parents:
diff changeset
139 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
140 System.out.println("JDK home for " + vmVersion + " is " + jvmHome);
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }
a61af66fc99e Initial load
duke
parents:
diff changeset
142
a61af66fc99e Initial load
duke
parents:
diff changeset
143 // sa-jdi is in $JDK_HOME/lib directory
a61af66fc99e Initial load
duke
parents:
diff changeset
144 StringBuffer buf = new StringBuffer();
a61af66fc99e Initial load
duke
parents:
diff changeset
145 buf.append(jvmHome);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 buf.append(File.separatorChar);
a61af66fc99e Initial load
duke
parents:
diff changeset
147 buf.append("lib");
a61af66fc99e Initial load
duke
parents:
diff changeset
148 buf.append(File.separatorChar);
a61af66fc99e Initial load
duke
parents:
diff changeset
149 buf.append("sa-jdi.jar");
a61af66fc99e Initial load
duke
parents:
diff changeset
150 return buf.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 /* This method loads VirtualMachineImpl class by a ClassLoader
a61af66fc99e Initial load
duke
parents:
diff changeset
154 * configured with sa-jdi.jar path of given 'vmVersion'. This is
a61af66fc99e Initial load
duke
parents:
diff changeset
155 * used for cross VM version debugging. Refer to comments in
a61af66fc99e Initial load
duke
parents:
diff changeset
156 * SAJDIClassLoader as well.
a61af66fc99e Initial load
duke
parents:
diff changeset
157 */
a61af66fc99e Initial load
duke
parents:
diff changeset
158 protected static Class loadVirtualMachineImplClass(String vmVersion)
a61af66fc99e Initial load
duke
parents:
diff changeset
159 throws ClassNotFoundException {
a61af66fc99e Initial load
duke
parents:
diff changeset
160 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
161 System.out.println("attemping to load sa-jdi.jar for version " + vmVersion);
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163 String classPath = getSAClassPathForVM(vmVersion);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 if (classPath != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 ClassLoader cl = new SAJDIClassLoader(myLoader, classPath);
a61af66fc99e Initial load
duke
parents:
diff changeset
166 return getVMImplClassFrom(cl);
a61af66fc99e Initial load
duke
parents:
diff changeset
167 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
168 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
169 }
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 /* Is the given throwable an instanceof VMVersionMismatchException?
a61af66fc99e Initial load
duke
parents:
diff changeset
173 * Note that we can't do instanceof check because the exception
a61af66fc99e Initial load
duke
parents:
diff changeset
174 * class might have been loaded by a different class loader.
a61af66fc99e Initial load
duke
parents:
diff changeset
175 */
a61af66fc99e Initial load
duke
parents:
diff changeset
176 private static boolean isVMVersionMismatch(Throwable throwable) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 String className = throwable.getClass().getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
178 return className.equals("sun.jvm.hotspot.runtime.VMVersionMismatchException");
a61af66fc99e Initial load
duke
parents:
diff changeset
179 }
a61af66fc99e Initial load
duke
parents:
diff changeset
180
a61af66fc99e Initial load
duke
parents:
diff changeset
181 /* gets target VM version from the given VMVersionMismatchException.
a61af66fc99e Initial load
duke
parents:
diff changeset
182 * Note that we need to reflectively call the method because of we may
a61af66fc99e Initial load
duke
parents:
diff changeset
183 * have got this from different classloader's namespace */
a61af66fc99e Initial load
duke
parents:
diff changeset
184 private static String getVMVersion(Throwable throwable)
a61af66fc99e Initial load
duke
parents:
diff changeset
185 throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 // assert isVMVersionMismatch(throwable), "not a VMVersionMismatch"
a61af66fc99e Initial load
duke
parents:
diff changeset
187 Class expClass = throwable.getClass();
a61af66fc99e Initial load
duke
parents:
diff changeset
188 Method targetVersionMethod = expClass.getMethod("getTargetVersion", new Class[0]);
2471
37be97a58393 7010849: 5/5 Extraneous javac source/target options when building sa-jdi
andrew
parents: 1552
diff changeset
189 return (String) targetVersionMethod.invoke(throwable);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
190 }
a61af66fc99e Initial load
duke
parents:
diff changeset
191
a61af66fc99e Initial load
duke
parents:
diff changeset
192 /** If the causal chain has a sun.jvm.hotspot.runtime.VMVersionMismatchException,
a61af66fc99e Initial load
duke
parents:
diff changeset
193 attempt to load VirtualMachineImpl class for target VM version. */
a61af66fc99e Initial load
duke
parents:
diff changeset
194 protected static Class handleVMVersionMismatch(InvocationTargetException ite) {
a61af66fc99e Initial load
duke
parents:
diff changeset
195 Throwable cause = ite.getCause();
a61af66fc99e Initial load
duke
parents:
diff changeset
196 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 System.out.println("checking for version mismatch...");
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199 while (cause != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 if (isVMVersionMismatch(cause)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
202 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
203 System.out.println("Triggering cross VM version support...");
a61af66fc99e Initial load
duke
parents:
diff changeset
204 }
a61af66fc99e Initial load
duke
parents:
diff changeset
205 return loadVirtualMachineImplClass(getVMVersion(cause));
a61af66fc99e Initial load
duke
parents:
diff changeset
206 }
a61af66fc99e Initial load
duke
parents:
diff changeset
207 } catch (Exception exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
209 System.out.println("failed to load VirtualMachineImpl class");
a61af66fc99e Initial load
duke
parents:
diff changeset
210 exp.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
211 }
a61af66fc99e Initial load
duke
parents:
diff changeset
212 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214 cause = cause.getCause();
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
217 }
a61af66fc99e Initial load
duke
parents:
diff changeset
218
a61af66fc99e Initial load
duke
parents:
diff changeset
219 protected void checkNativeLink(SecurityManager sm, String os) {
5936
970cbbba54b0 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 2471
diff changeset
220 if (os.equals("SunOS") || os.equals("Linux") || os.contains("OS X")) {
970cbbba54b0 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 2471
diff changeset
221 // link "saproc" - SA native library on SunOS, Linux, and Mac OS X
0
a61af66fc99e Initial load
duke
parents:
diff changeset
222 sm.checkLink("saproc");
a61af66fc99e Initial load
duke
parents:
diff changeset
223 } else if (os.startsWith("Windows")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
224 // link "sawindbg" - SA native library on Windows.
a61af66fc99e Initial load
duke
parents:
diff changeset
225 sm.checkLink("sawindbg");
a61af66fc99e Initial load
duke
parents:
diff changeset
226 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
227 throw new RuntimeException(os + " is not yet supported");
a61af66fc99e Initial load
duke
parents:
diff changeset
228 }
a61af66fc99e Initial load
duke
parents:
diff changeset
229 }
a61af66fc99e Initial load
duke
parents:
diff changeset
230
a61af66fc99e Initial load
duke
parents:
diff changeset
231 // we set an observer to detect VirtualMachineImpl.dispose call
a61af66fc99e Initial load
duke
parents:
diff changeset
232 // and on dispose we add corresponding VirtualMachineImpl.class to
a61af66fc99e Initial load
duke
parents:
diff changeset
233 // free VirtualMachimeImpl Class list.
a61af66fc99e Initial load
duke
parents:
diff changeset
234 protected static void setVMDisposeObserver(final Object vm) {
a61af66fc99e Initial load
duke
parents:
diff changeset
235 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
236 Method setDisposeObserverMethod = vm.getClass().getDeclaredMethod("setDisposeObserver",
a61af66fc99e Initial load
duke
parents:
diff changeset
237 new Class[] { java.util.Observer.class });
a61af66fc99e Initial load
duke
parents:
diff changeset
238 setDisposeObserverMethod.setAccessible(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
239 setDisposeObserverMethod.invoke(vm,
a61af66fc99e Initial load
duke
parents:
diff changeset
240 new Object[] {
a61af66fc99e Initial load
duke
parents:
diff changeset
241 new Observer() {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 public void update(Observable o, Object data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
243 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
244 System.out.println("got VM.dispose notification");
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246 addFreeVMImplClass(vm.getClass());
a61af66fc99e Initial load
duke
parents:
diff changeset
247 }
a61af66fc99e Initial load
duke
parents:
diff changeset
248 }
a61af66fc99e Initial load
duke
parents:
diff changeset
249 });
a61af66fc99e Initial load
duke
parents:
diff changeset
250 } catch (Exception exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
251 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 System.out.println("setVMDisposeObserver() got an exception:");
a61af66fc99e Initial load
duke
parents:
diff changeset
253 exp.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
254 }
a61af66fc99e Initial load
duke
parents:
diff changeset
255 }
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257
a61af66fc99e Initial load
duke
parents:
diff changeset
258 public Map defaultArguments() {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 Map defaults = new LinkedHashMap();
a61af66fc99e Initial load
duke
parents:
diff changeset
260 Collection values = defaultArguments.values();
a61af66fc99e Initial load
duke
parents:
diff changeset
261
a61af66fc99e Initial load
duke
parents:
diff changeset
262 Iterator iter = values.iterator();
a61af66fc99e Initial load
duke
parents:
diff changeset
263 while (iter.hasNext()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
264 ArgumentImpl argument = (ArgumentImpl)iter.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
265 defaults.put(argument.name(), argument.clone());
a61af66fc99e Initial load
duke
parents:
diff changeset
266 }
a61af66fc99e Initial load
duke
parents:
diff changeset
267 return defaults;
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 void addStringArgument(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
271 String defaultValue, boolean mustSpecify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
272 defaultArguments.put(name,
a61af66fc99e Initial load
duke
parents:
diff changeset
273 new StringArgumentImpl(name, label,
a61af66fc99e Initial load
duke
parents:
diff changeset
274 description,
a61af66fc99e Initial load
duke
parents:
diff changeset
275 defaultValue,
a61af66fc99e Initial load
duke
parents:
diff changeset
276 mustSpecify));
a61af66fc99e Initial load
duke
parents:
diff changeset
277 }
a61af66fc99e Initial load
duke
parents:
diff changeset
278
a61af66fc99e Initial load
duke
parents:
diff changeset
279 void addBooleanArgument(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
280 boolean defaultValue, boolean mustSpecify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
281 defaultArguments.put(name,
a61af66fc99e Initial load
duke
parents:
diff changeset
282 new BooleanArgumentImpl(name, label,
a61af66fc99e Initial load
duke
parents:
diff changeset
283 description,
a61af66fc99e Initial load
duke
parents:
diff changeset
284 defaultValue,
a61af66fc99e Initial load
duke
parents:
diff changeset
285 mustSpecify));
a61af66fc99e Initial load
duke
parents:
diff changeset
286 }
a61af66fc99e Initial load
duke
parents:
diff changeset
287
a61af66fc99e Initial load
duke
parents:
diff changeset
288 void addIntegerArgument(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
289 String defaultValue, boolean mustSpecify,
a61af66fc99e Initial load
duke
parents:
diff changeset
290 int min, int max) {
a61af66fc99e Initial load
duke
parents:
diff changeset
291 defaultArguments.put(name,
a61af66fc99e Initial load
duke
parents:
diff changeset
292 new IntegerArgumentImpl(name, label,
a61af66fc99e Initial load
duke
parents:
diff changeset
293 description,
a61af66fc99e Initial load
duke
parents:
diff changeset
294 defaultValue,
a61af66fc99e Initial load
duke
parents:
diff changeset
295 mustSpecify,
a61af66fc99e Initial load
duke
parents:
diff changeset
296 min, max));
a61af66fc99e Initial load
duke
parents:
diff changeset
297 }
a61af66fc99e Initial load
duke
parents:
diff changeset
298
a61af66fc99e Initial load
duke
parents:
diff changeset
299 void addSelectedArgument(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
300 String defaultValue, boolean mustSpecify,
a61af66fc99e Initial load
duke
parents:
diff changeset
301 List list) {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 defaultArguments.put(name,
a61af66fc99e Initial load
duke
parents:
diff changeset
303 new SelectedArgumentImpl(name, label,
a61af66fc99e Initial load
duke
parents:
diff changeset
304 description,
a61af66fc99e Initial load
duke
parents:
diff changeset
305 defaultValue,
a61af66fc99e Initial load
duke
parents:
diff changeset
306 mustSpecify, list));
a61af66fc99e Initial load
duke
parents:
diff changeset
307 }
a61af66fc99e Initial load
duke
parents:
diff changeset
308
a61af66fc99e Initial load
duke
parents:
diff changeset
309 ArgumentImpl argument(String name, Map arguments)
a61af66fc99e Initial load
duke
parents:
diff changeset
310 throws IllegalConnectorArgumentsException {
a61af66fc99e Initial load
duke
parents:
diff changeset
311
a61af66fc99e Initial load
duke
parents:
diff changeset
312 ArgumentImpl argument = (ArgumentImpl)arguments.get(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (argument == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 throw new IllegalConnectorArgumentsException(
a61af66fc99e Initial load
duke
parents:
diff changeset
315 "Argument missing", name);
a61af66fc99e Initial load
duke
parents:
diff changeset
316 }
a61af66fc99e Initial load
duke
parents:
diff changeset
317 String value = argument.value();
a61af66fc99e Initial load
duke
parents:
diff changeset
318 if (value == null || value.length() == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
319 if (argument.mustSpecify()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
320 throw new IllegalConnectorArgumentsException(
a61af66fc99e Initial load
duke
parents:
diff changeset
321 "Argument unspecified", name);
a61af66fc99e Initial load
duke
parents:
diff changeset
322 }
a61af66fc99e Initial load
duke
parents:
diff changeset
323 } else if(!argument.isValid(value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
324 throw new IllegalConnectorArgumentsException(
a61af66fc99e Initial load
duke
parents:
diff changeset
325 "Argument invalid", name);
a61af66fc99e Initial load
duke
parents:
diff changeset
326 }
a61af66fc99e Initial load
duke
parents:
diff changeset
327
a61af66fc99e Initial load
duke
parents:
diff changeset
328 return argument;
a61af66fc99e Initial load
duke
parents:
diff changeset
329 }
a61af66fc99e Initial load
duke
parents:
diff changeset
330
a61af66fc99e Initial load
duke
parents:
diff changeset
331 String getString(String key) {
a61af66fc99e Initial load
duke
parents:
diff changeset
332 //fixme jjh; needs i18n
a61af66fc99e Initial load
duke
parents:
diff changeset
333 // this is not public return ((VirtualMachineManagerImpl)manager).getString(key);
a61af66fc99e Initial load
duke
parents:
diff changeset
334 return key;
a61af66fc99e Initial load
duke
parents:
diff changeset
335 }
a61af66fc99e Initial load
duke
parents:
diff changeset
336
a61af66fc99e Initial load
duke
parents:
diff changeset
337 public String toString() {
a61af66fc99e Initial load
duke
parents:
diff changeset
338 String string = name() + " (defaults: ";
a61af66fc99e Initial load
duke
parents:
diff changeset
339 Iterator iter = defaultArguments().values().iterator();
a61af66fc99e Initial load
duke
parents:
diff changeset
340 boolean first = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
341 while (iter.hasNext()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
342 ArgumentImpl argument = (ArgumentImpl)iter.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
343 if (!first) {
a61af66fc99e Initial load
duke
parents:
diff changeset
344 string += ", ";
a61af66fc99e Initial load
duke
parents:
diff changeset
345 }
a61af66fc99e Initial load
duke
parents:
diff changeset
346 string += argument.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
347 first = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
348 }
a61af66fc99e Initial load
duke
parents:
diff changeset
349 return string + ")";
a61af66fc99e Initial load
duke
parents:
diff changeset
350 }
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 abstract class ArgumentImpl implements Connector.Argument, Cloneable, Serializable {
a61af66fc99e Initial load
duke
parents:
diff changeset
353 private String name;
a61af66fc99e Initial load
duke
parents:
diff changeset
354 private String label;
a61af66fc99e Initial load
duke
parents:
diff changeset
355 private String description;
a61af66fc99e Initial load
duke
parents:
diff changeset
356 private String value;
a61af66fc99e Initial load
duke
parents:
diff changeset
357 private boolean mustSpecify;
a61af66fc99e Initial load
duke
parents:
diff changeset
358
a61af66fc99e Initial load
duke
parents:
diff changeset
359 ArgumentImpl(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
360 String value,
a61af66fc99e Initial load
duke
parents:
diff changeset
361 boolean mustSpecify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
362 this.name = name;
a61af66fc99e Initial load
duke
parents:
diff changeset
363 this.label = label;
a61af66fc99e Initial load
duke
parents:
diff changeset
364 this.description = description;
a61af66fc99e Initial load
duke
parents:
diff changeset
365 this.value = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
366 this.mustSpecify = mustSpecify;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368
a61af66fc99e Initial load
duke
parents:
diff changeset
369 public abstract boolean isValid(String value);
a61af66fc99e Initial load
duke
parents:
diff changeset
370
a61af66fc99e Initial load
duke
parents:
diff changeset
371 public String name() {
a61af66fc99e Initial load
duke
parents:
diff changeset
372 return name;
a61af66fc99e Initial load
duke
parents:
diff changeset
373 }
a61af66fc99e Initial load
duke
parents:
diff changeset
374
a61af66fc99e Initial load
duke
parents:
diff changeset
375 public String label() {
a61af66fc99e Initial load
duke
parents:
diff changeset
376 return label;
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378
a61af66fc99e Initial load
duke
parents:
diff changeset
379 public String description() {
a61af66fc99e Initial load
duke
parents:
diff changeset
380 return description;
a61af66fc99e Initial load
duke
parents:
diff changeset
381 }
a61af66fc99e Initial load
duke
parents:
diff changeset
382
a61af66fc99e Initial load
duke
parents:
diff changeset
383 public String value() {
a61af66fc99e Initial load
duke
parents:
diff changeset
384 return value;
a61af66fc99e Initial load
duke
parents:
diff changeset
385 }
a61af66fc99e Initial load
duke
parents:
diff changeset
386
a61af66fc99e Initial load
duke
parents:
diff changeset
387 public void setValue(String value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
388 if (value == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
389 throw new NullPointerException("Can't set null value");
a61af66fc99e Initial load
duke
parents:
diff changeset
390 }
a61af66fc99e Initial load
duke
parents:
diff changeset
391 this.value = value;
a61af66fc99e Initial load
duke
parents:
diff changeset
392 }
a61af66fc99e Initial load
duke
parents:
diff changeset
393
a61af66fc99e Initial load
duke
parents:
diff changeset
394 public boolean mustSpecify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
395 return mustSpecify;
a61af66fc99e Initial load
duke
parents:
diff changeset
396 }
a61af66fc99e Initial load
duke
parents:
diff changeset
397
a61af66fc99e Initial load
duke
parents:
diff changeset
398 public boolean equals(Object obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
399 if ((obj != null) && (obj instanceof Connector.Argument)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
400 Connector.Argument other = (Connector.Argument)obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
401 return (name().equals(other.name())) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
402 (description().equals(other.description())) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
403 (mustSpecify() == other.mustSpecify()) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
404 (value().equals(other.value()));
a61af66fc99e Initial load
duke
parents:
diff changeset
405 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
406 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 }
a61af66fc99e Initial load
duke
parents:
diff changeset
408 }
a61af66fc99e Initial load
duke
parents:
diff changeset
409
a61af66fc99e Initial load
duke
parents:
diff changeset
410 public int hashCode() {
a61af66fc99e Initial load
duke
parents:
diff changeset
411 return description().hashCode();
a61af66fc99e Initial load
duke
parents:
diff changeset
412 }
a61af66fc99e Initial load
duke
parents:
diff changeset
413
a61af66fc99e Initial load
duke
parents:
diff changeset
414 public Object clone() {
a61af66fc99e Initial load
duke
parents:
diff changeset
415 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
416 return super.clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
417 } catch (CloneNotSupportedException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
418 // Object should always support clone
a61af66fc99e Initial load
duke
parents:
diff changeset
419 throw (InternalException) new InternalException().initCause(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
420 }
a61af66fc99e Initial load
duke
parents:
diff changeset
421 }
a61af66fc99e Initial load
duke
parents:
diff changeset
422
a61af66fc99e Initial load
duke
parents:
diff changeset
423 public String toString() {
a61af66fc99e Initial load
duke
parents:
diff changeset
424 return name() + "=" + value();
a61af66fc99e Initial load
duke
parents:
diff changeset
425 }
a61af66fc99e Initial load
duke
parents:
diff changeset
426 }
a61af66fc99e Initial load
duke
parents:
diff changeset
427
a61af66fc99e Initial load
duke
parents:
diff changeset
428 class BooleanArgumentImpl extends ConnectorImpl.ArgumentImpl
a61af66fc99e Initial load
duke
parents:
diff changeset
429 implements Connector.BooleanArgument {
a61af66fc99e Initial load
duke
parents:
diff changeset
430
a61af66fc99e Initial load
duke
parents:
diff changeset
431 BooleanArgumentImpl(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
432 boolean value,
a61af66fc99e Initial load
duke
parents:
diff changeset
433 boolean mustSpecify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
434 super(name, label, description, null, mustSpecify);
a61af66fc99e Initial load
duke
parents:
diff changeset
435 if(trueString == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
436 trueString = getString("true");
a61af66fc99e Initial load
duke
parents:
diff changeset
437 falseString = getString("false");
a61af66fc99e Initial load
duke
parents:
diff changeset
438 }
a61af66fc99e Initial load
duke
parents:
diff changeset
439 setValue(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
440 }
a61af66fc99e Initial load
duke
parents:
diff changeset
441
a61af66fc99e Initial load
duke
parents:
diff changeset
442 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
443 * Sets the value of the argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
444 */
a61af66fc99e Initial load
duke
parents:
diff changeset
445 public void setValue(boolean value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
446 setValue(stringValueOf(value));
a61af66fc99e Initial load
duke
parents:
diff changeset
447 }
a61af66fc99e Initial load
duke
parents:
diff changeset
448
a61af66fc99e Initial load
duke
parents:
diff changeset
449 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
450 * Performs basic sanity check of argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
451 * @return <code>true</code> if value is a string
a61af66fc99e Initial load
duke
parents:
diff changeset
452 * representation of a boolean value.
a61af66fc99e Initial load
duke
parents:
diff changeset
453 * @see #stringValueOf(boolean)
a61af66fc99e Initial load
duke
parents:
diff changeset
454 */
a61af66fc99e Initial load
duke
parents:
diff changeset
455 public boolean isValid(String value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
456 return value.equals(trueString) || value.equals(falseString);
a61af66fc99e Initial load
duke
parents:
diff changeset
457 }
a61af66fc99e Initial load
duke
parents:
diff changeset
458
a61af66fc99e Initial load
duke
parents:
diff changeset
459 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
460 * Return the string representation of the <code>value</code>
a61af66fc99e Initial load
duke
parents:
diff changeset
461 * parameter.
a61af66fc99e Initial load
duke
parents:
diff changeset
462 * Does not set or examine the value or the argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
463 * @return the localized String representation of the
a61af66fc99e Initial load
duke
parents:
diff changeset
464 * boolean value.
a61af66fc99e Initial load
duke
parents:
diff changeset
465 */
a61af66fc99e Initial load
duke
parents:
diff changeset
466 public String stringValueOf(boolean value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
467 return value? trueString : falseString;
a61af66fc99e Initial load
duke
parents:
diff changeset
468 }
a61af66fc99e Initial load
duke
parents:
diff changeset
469
a61af66fc99e Initial load
duke
parents:
diff changeset
470 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
471 * Return the value of the argument as a boolean. Since
a61af66fc99e Initial load
duke
parents:
diff changeset
472 * the argument may not have been set or may have an invalid
a61af66fc99e Initial load
duke
parents:
diff changeset
473 * value {@link #isValid(String)} should be called on
a61af66fc99e Initial load
duke
parents:
diff changeset
474 * {@link #value()} to check its validity. If it is invalid
a61af66fc99e Initial load
duke
parents:
diff changeset
475 * the boolean returned by this method is undefined.
a61af66fc99e Initial load
duke
parents:
diff changeset
476 * @return the value of the argument as a boolean.
a61af66fc99e Initial load
duke
parents:
diff changeset
477 */
a61af66fc99e Initial load
duke
parents:
diff changeset
478 public boolean booleanValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
479 return value().equals(trueString);
a61af66fc99e Initial load
duke
parents:
diff changeset
480 }
a61af66fc99e Initial load
duke
parents:
diff changeset
481 }
a61af66fc99e Initial load
duke
parents:
diff changeset
482
a61af66fc99e Initial load
duke
parents:
diff changeset
483 class IntegerArgumentImpl extends ConnectorImpl.ArgumentImpl
a61af66fc99e Initial load
duke
parents:
diff changeset
484 implements Connector.IntegerArgument {
a61af66fc99e Initial load
duke
parents:
diff changeset
485
a61af66fc99e Initial load
duke
parents:
diff changeset
486 private final int min;
a61af66fc99e Initial load
duke
parents:
diff changeset
487 private final int max;
a61af66fc99e Initial load
duke
parents:
diff changeset
488
a61af66fc99e Initial load
duke
parents:
diff changeset
489 IntegerArgumentImpl(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
490 String value,
a61af66fc99e Initial load
duke
parents:
diff changeset
491 boolean mustSpecify, int min, int max) {
a61af66fc99e Initial load
duke
parents:
diff changeset
492 super(name, label, description, value, mustSpecify);
a61af66fc99e Initial load
duke
parents:
diff changeset
493 this.min = min;
a61af66fc99e Initial load
duke
parents:
diff changeset
494 this.max = max;
a61af66fc99e Initial load
duke
parents:
diff changeset
495 }
a61af66fc99e Initial load
duke
parents:
diff changeset
496
a61af66fc99e Initial load
duke
parents:
diff changeset
497 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
498 * Sets the value of the argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
499 * The value should be checked with {@link #isValid(int)}
a61af66fc99e Initial load
duke
parents:
diff changeset
500 * before setting it; invalid values will throw an exception
a61af66fc99e Initial load
duke
parents:
diff changeset
501 * when the connection is established - for example,
a61af66fc99e Initial load
duke
parents:
diff changeset
502 * on {@link LaunchingConnector#launch}
a61af66fc99e Initial load
duke
parents:
diff changeset
503 */
a61af66fc99e Initial load
duke
parents:
diff changeset
504 public void setValue(int value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
505 setValue(stringValueOf(value));
a61af66fc99e Initial load
duke
parents:
diff changeset
506 }
a61af66fc99e Initial load
duke
parents:
diff changeset
507
a61af66fc99e Initial load
duke
parents:
diff changeset
508 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
509 * Performs basic sanity check of argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
510 * @return <code>true</code> if value represents an int that is
a61af66fc99e Initial load
duke
parents:
diff changeset
511 * <code>{@link #min()} &lt;= value &lt;= {@link #max()}</code>
a61af66fc99e Initial load
duke
parents:
diff changeset
512 */
a61af66fc99e Initial load
duke
parents:
diff changeset
513 public boolean isValid(String value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
514 if (value == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
515 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
516 }
a61af66fc99e Initial load
duke
parents:
diff changeset
517 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
518 return isValid(Integer.decode(value).intValue());
a61af66fc99e Initial load
duke
parents:
diff changeset
519 } catch(NumberFormatException exc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
520 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
521 }
a61af66fc99e Initial load
duke
parents:
diff changeset
522 }
a61af66fc99e Initial load
duke
parents:
diff changeset
523
a61af66fc99e Initial load
duke
parents:
diff changeset
524 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
525 * Performs basic sanity check of argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
526 * @return <code>true</code> if
a61af66fc99e Initial load
duke
parents:
diff changeset
527 * <code>{@link #min()} &lt;= value &lt;= {@link #max()}</code>
a61af66fc99e Initial load
duke
parents:
diff changeset
528 */
a61af66fc99e Initial load
duke
parents:
diff changeset
529 public boolean isValid(int value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
530 return min <= value && value <= max;
a61af66fc99e Initial load
duke
parents:
diff changeset
531 }
a61af66fc99e Initial load
duke
parents:
diff changeset
532
a61af66fc99e Initial load
duke
parents:
diff changeset
533 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
534 * Return the string representation of the <code>value</code>
a61af66fc99e Initial load
duke
parents:
diff changeset
535 * parameter.
a61af66fc99e Initial load
duke
parents:
diff changeset
536 * Does not set or examine the value or the argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
537 * @return the String representation of the
a61af66fc99e Initial load
duke
parents:
diff changeset
538 * int value.
a61af66fc99e Initial load
duke
parents:
diff changeset
539 */
a61af66fc99e Initial load
duke
parents:
diff changeset
540 public String stringValueOf(int value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
541 // *** Should this be internationalized????
a61af66fc99e Initial load
duke
parents:
diff changeset
542 // *** Even Brian Beck was unsure if an Arabic programmer
a61af66fc99e Initial load
duke
parents:
diff changeset
543 // *** would expect port numbers in Arabic numerals,
a61af66fc99e Initial load
duke
parents:
diff changeset
544 // *** so punt for now.
a61af66fc99e Initial load
duke
parents:
diff changeset
545 return ""+value;
a61af66fc99e Initial load
duke
parents:
diff changeset
546 }
a61af66fc99e Initial load
duke
parents:
diff changeset
547
a61af66fc99e Initial load
duke
parents:
diff changeset
548 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
549 * Return the value of the argument as a int. Since
a61af66fc99e Initial load
duke
parents:
diff changeset
550 * the argument may not have been set or may have an invalid
a61af66fc99e Initial load
duke
parents:
diff changeset
551 * value {@link #isValid(String)} should be called on
a61af66fc99e Initial load
duke
parents:
diff changeset
552 * {@link #value()} to check its validity. If it is invalid
a61af66fc99e Initial load
duke
parents:
diff changeset
553 * the int returned by this method is undefined.
a61af66fc99e Initial load
duke
parents:
diff changeset
554 * @return the value of the argument as a int.
a61af66fc99e Initial load
duke
parents:
diff changeset
555 */
a61af66fc99e Initial load
duke
parents:
diff changeset
556 public int intValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
557 if (value() == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
558 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
559 }
a61af66fc99e Initial load
duke
parents:
diff changeset
560 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
561 return Integer.decode(value()).intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
562 } catch(NumberFormatException exc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
563 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
564 }
a61af66fc99e Initial load
duke
parents:
diff changeset
565 }
a61af66fc99e Initial load
duke
parents:
diff changeset
566
a61af66fc99e Initial load
duke
parents:
diff changeset
567 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
568 * The upper bound for the value.
a61af66fc99e Initial load
duke
parents:
diff changeset
569 * @return the maximum allowed value for this argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
570 */
a61af66fc99e Initial load
duke
parents:
diff changeset
571 public int max() {
a61af66fc99e Initial load
duke
parents:
diff changeset
572 return max;
a61af66fc99e Initial load
duke
parents:
diff changeset
573 }
a61af66fc99e Initial load
duke
parents:
diff changeset
574
a61af66fc99e Initial load
duke
parents:
diff changeset
575 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
576 * The lower bound for the value.
a61af66fc99e Initial load
duke
parents:
diff changeset
577 * @return the minimum allowed value for this argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
578 */
a61af66fc99e Initial load
duke
parents:
diff changeset
579 public int min() {
a61af66fc99e Initial load
duke
parents:
diff changeset
580 return min;
a61af66fc99e Initial load
duke
parents:
diff changeset
581 }
a61af66fc99e Initial load
duke
parents:
diff changeset
582 }
a61af66fc99e Initial load
duke
parents:
diff changeset
583
a61af66fc99e Initial load
duke
parents:
diff changeset
584 class StringArgumentImpl extends ConnectorImpl.ArgumentImpl
a61af66fc99e Initial load
duke
parents:
diff changeset
585 implements Connector.StringArgument {
a61af66fc99e Initial load
duke
parents:
diff changeset
586
a61af66fc99e Initial load
duke
parents:
diff changeset
587 StringArgumentImpl(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
588 String value,
a61af66fc99e Initial load
duke
parents:
diff changeset
589 boolean mustSpecify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
590 super(name, label, description, value, mustSpecify);
a61af66fc99e Initial load
duke
parents:
diff changeset
591 }
a61af66fc99e Initial load
duke
parents:
diff changeset
592
a61af66fc99e Initial load
duke
parents:
diff changeset
593 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
594 * Performs basic sanity check of argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
595 * @return <code>true</code> always
a61af66fc99e Initial load
duke
parents:
diff changeset
596 */
a61af66fc99e Initial load
duke
parents:
diff changeset
597 public boolean isValid(String value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
598 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
599 }
a61af66fc99e Initial load
duke
parents:
diff changeset
600 }
a61af66fc99e Initial load
duke
parents:
diff changeset
601
a61af66fc99e Initial load
duke
parents:
diff changeset
602 class SelectedArgumentImpl extends ConnectorImpl.ArgumentImpl
a61af66fc99e Initial load
duke
parents:
diff changeset
603 implements Connector.SelectedArgument {
a61af66fc99e Initial load
duke
parents:
diff changeset
604
a61af66fc99e Initial load
duke
parents:
diff changeset
605 private final List choices;
a61af66fc99e Initial load
duke
parents:
diff changeset
606
a61af66fc99e Initial load
duke
parents:
diff changeset
607 SelectedArgumentImpl(String name, String label, String description,
a61af66fc99e Initial load
duke
parents:
diff changeset
608 String value,
a61af66fc99e Initial load
duke
parents:
diff changeset
609 boolean mustSpecify, List choices) {
a61af66fc99e Initial load
duke
parents:
diff changeset
610 super(name, label, description, value, mustSpecify);
a61af66fc99e Initial load
duke
parents:
diff changeset
611 this.choices = Collections.unmodifiableList(
a61af66fc99e Initial load
duke
parents:
diff changeset
612 new ArrayList(choices));
a61af66fc99e Initial load
duke
parents:
diff changeset
613 }
a61af66fc99e Initial load
duke
parents:
diff changeset
614
a61af66fc99e Initial load
duke
parents:
diff changeset
615 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
616 * Return the possible values for the argument
a61af66fc99e Initial load
duke
parents:
diff changeset
617 * @return {@link List} of {@link String}
a61af66fc99e Initial load
duke
parents:
diff changeset
618 */
a61af66fc99e Initial load
duke
parents:
diff changeset
619 public List choices() {
a61af66fc99e Initial load
duke
parents:
diff changeset
620 return choices;
a61af66fc99e Initial load
duke
parents:
diff changeset
621 }
a61af66fc99e Initial load
duke
parents:
diff changeset
622
a61af66fc99e Initial load
duke
parents:
diff changeset
623 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
624 * Performs basic sanity check of argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
625 * @return <code>true</code> if value is one of {@link #choices()}.
a61af66fc99e Initial load
duke
parents:
diff changeset
626 */
a61af66fc99e Initial load
duke
parents:
diff changeset
627 public boolean isValid(String value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
628 return choices.contains(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
629 }
a61af66fc99e Initial load
duke
parents:
diff changeset
630 }
a61af66fc99e Initial load
duke
parents:
diff changeset
631 }