|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.tools.jdi; |
|
|
|
import com.sun.tools.jdi.*; |
|
import com.sun.jdi.*; |
|
import com.sun.jdi.connect.*; |
|
import com.sun.jdi.InternalException; |
|
import java.util.Collections; |
|
import java.util.Collection; |
|
import java.util.Map; |
|
import java.util.List; |
|
import java.util.ArrayList; |
|
import java.util.Iterator; |
|
import java.util.ResourceBundle; |
|
import java.io.Serializable; |
|
|
|
abstract class ConnectorImpl implements Connector { |
|
Map<String,Argument> defaultArguments = new java.util.LinkedHashMap<String,Argument>(); |
|
|
|
|
|
static String trueString = null; |
|
static String falseString; |
|
|
|
public Map<String,Argument> defaultArguments() { |
|
Map<String,Argument> defaults = new java.util.LinkedHashMap<String,Argument>(); |
|
Collection<Argument> values = defaultArguments.values(); |
|
|
|
Iterator<Argument> iter = values.iterator(); |
|
while (iter.hasNext()) { |
|
ArgumentImpl argument = (ArgumentImpl)iter.next(); |
|
defaults.put(argument.name(), (Argument)argument.clone()); |
|
} |
|
return defaults; |
|
} |
|
|
|
void addStringArgument(String name, String label, String description, |
|
String defaultValue, boolean mustSpecify) { |
|
defaultArguments.put(name, |
|
new StringArgumentImpl(name, label, |
|
description, |
|
defaultValue, |
|
mustSpecify)); |
|
} |
|
|
|
void addBooleanArgument(String name, String label, String description, |
|
boolean defaultValue, boolean mustSpecify) { |
|
defaultArguments.put(name, |
|
new BooleanArgumentImpl(name, label, |
|
description, |
|
defaultValue, |
|
mustSpecify)); |
|
} |
|
|
|
void addIntegerArgument(String name, String label, String description, |
|
String defaultValue, boolean mustSpecify, |
|
int min, int max) { |
|
defaultArguments.put(name, |
|
new IntegerArgumentImpl(name, label, |
|
description, |
|
defaultValue, |
|
mustSpecify, |
|
min, max)); |
|
} |
|
|
|
void addSelectedArgument(String name, String label, String description, |
|
String defaultValue, boolean mustSpecify, |
|
List<String> list) { |
|
defaultArguments.put(name, |
|
new SelectedArgumentImpl(name, label, |
|
description, |
|
defaultValue, |
|
mustSpecify, list)); |
|
} |
|
|
|
ArgumentImpl argument(String name, Map<String, ? extends Argument> arguments) |
|
throws IllegalConnectorArgumentsException { |
|
|
|
ArgumentImpl argument = (ArgumentImpl)arguments.get(name); |
|
if (argument == null) { |
|
throw new IllegalConnectorArgumentsException( |
|
"Argument missing", name); |
|
} |
|
String value = argument.value(); |
|
if (value == null || value.length() == 0) { |
|
if (argument.mustSpecify()) { |
|
throw new IllegalConnectorArgumentsException( |
|
"Argument unspecified", name); |
|
} |
|
} else if(!argument.isValid(value)) { |
|
throw new IllegalConnectorArgumentsException( |
|
"Argument invalid", name); |
|
} |
|
|
|
return argument; |
|
} |
|
|
|
|
|
private ResourceBundle messages = null; |
|
|
|
String getString(String key) { |
|
if (messages == null) { |
|
messages = ResourceBundle.getBundle("com.sun.tools.jdi.resources.jdi"); |
|
} |
|
return messages.getString(key); |
|
} |
|
|
|
public String toString() { |
|
String string = name() + " (defaults: "; |
|
Iterator<Argument> iter = defaultArguments().values().iterator(); |
|
boolean first = true; |
|
while (iter.hasNext()) { |
|
ArgumentImpl argument = (ArgumentImpl)iter.next(); |
|
if (!first) { |
|
string += ", "; |
|
} |
|
string += argument.toString(); |
|
first = false; |
|
} |
|
string += ")"; |
|
return string; |
|
} |
|
|
|
abstract class ArgumentImpl implements Connector.Argument, Cloneable, Serializable { |
|
private String name; |
|
private String label; |
|
private String description; |
|
private String value; |
|
private boolean mustSpecify; |
|
|
|
ArgumentImpl(String name, String label, String description, |
|
String value, |
|
boolean mustSpecify) { |
|
this.name = name; |
|
this.label = label; |
|
this.description = description; |
|
this.value = value; |
|
this.mustSpecify = mustSpecify; |
|
} |
|
|
|
public abstract boolean isValid(String value); |
|
|
|
public String name() { |
|
return name; |
|
} |
|
|
|
public String label() { |
|
return label; |
|
} |
|
|
|
public String description() { |
|
return description; |
|
} |
|
|
|
public String value() { |
|
return value; |
|
} |
|
|
|
public void setValue(String value) { |
|
if (value == null) { |
|
throw new NullPointerException("Can't set null value"); |
|
} |
|
this.value = value; |
|
} |
|
|
|
public boolean mustSpecify() { |
|
return mustSpecify; |
|
} |
|
|
|
public boolean equals(Object obj) { |
|
if ((obj != null) && (obj instanceof Connector.Argument)) { |
|
Connector.Argument other = (Connector.Argument)obj; |
|
return (name().equals(other.name())) && |
|
(description().equals(other.description())) && |
|
(mustSpecify() == other.mustSpecify()) && |
|
(value().equals(other.value())); |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
public int hashCode() { |
|
return description().hashCode(); |
|
} |
|
|
|
public Object clone() { |
|
try { |
|
return super.clone(); |
|
} catch (CloneNotSupportedException e) { |
|
|
|
throw new InternalException(); |
|
} |
|
} |
|
|
|
public String toString() { |
|
return name() + "=" + value(); |
|
} |
|
} |
|
|
|
class BooleanArgumentImpl extends ConnectorImpl.ArgumentImpl |
|
implements Connector.BooleanArgument { |
|
private static final long serialVersionUID = 1624542968639361316L; |
|
BooleanArgumentImpl(String name, String label, String description, |
|
boolean value, |
|
boolean mustSpecify) { |
|
super(name, label, description, null, mustSpecify); |
|
if(trueString == null) { |
|
trueString = getString("true"); |
|
falseString = getString("false"); |
|
} |
|
setValue(value); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void setValue(boolean value) { |
|
setValue(stringValueOf(value)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isValid(String value) { |
|
return value.equals(trueString) || value.equals(falseString); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String stringValueOf(boolean value) { |
|
return value? trueString : falseString; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean booleanValue() { |
|
return value().equals(trueString); |
|
} |
|
} |
|
|
|
class IntegerArgumentImpl extends ConnectorImpl.ArgumentImpl |
|
implements Connector.IntegerArgument { |
|
private static final long serialVersionUID = 763286081923797770L; |
|
private final int min; |
|
private final int max; |
|
|
|
IntegerArgumentImpl(String name, String label, String description, |
|
String value, |
|
boolean mustSpecify, int min, int max) { |
|
super(name, label, description, value, mustSpecify); |
|
this.min = min; |
|
this.max = max; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setValue(int value) { |
|
setValue(stringValueOf(value)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isValid(String value) { |
|
if (value == null) { |
|
return false; |
|
} |
|
try { |
|
return isValid(Integer.decode(value).intValue()); |
|
} catch(NumberFormatException exc) { |
|
return false; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isValid(int value) { |
|
return min <= value && value <= max; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String stringValueOf(int value) { |
|
// *** Should this be internationalized???? |
|
// *** Even Brian Beck was unsure if an Arabic programmer |
|
// *** would expect port numbers in Arabic numerals, |
|
|
|
return ""+value; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int intValue() { |
|
if (value() == null) { |
|
return 0; |
|
} |
|
try { |
|
return Integer.decode(value()).intValue(); |
|
} catch(NumberFormatException exc) { |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public int max() { |
|
return max; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public int min() { |
|
return min; |
|
} |
|
} |
|
|
|
class StringArgumentImpl extends ConnectorImpl.ArgumentImpl |
|
implements Connector.StringArgument { |
|
private static final long serialVersionUID = 7500484902692107464L; |
|
StringArgumentImpl(String name, String label, String description, |
|
String value, |
|
boolean mustSpecify) { |
|
super(name, label, description, value, mustSpecify); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isValid(String value) { |
|
return true; |
|
} |
|
} |
|
|
|
class SelectedArgumentImpl extends ConnectorImpl.ArgumentImpl |
|
implements Connector.SelectedArgument { |
|
private static final long serialVersionUID = -5689584530908382517L; |
|
private final List<String> choices; |
|
|
|
SelectedArgumentImpl(String name, String label, String description, |
|
String value, |
|
boolean mustSpecify, List<String> choices) { |
|
super(name, label, description, value, mustSpecify); |
|
this.choices = Collections.unmodifiableList(new ArrayList<String>(choices)); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public List<String> choices() { |
|
return choices; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isValid(String value) { |
|
return choices.contains(value); |
|
} |
|
} |
|
} |