< prev index next >

src/com/sun/javatest/tool/ConfigManager.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


 194         if (isMatch(cmd, WorkDirectoryCommand.getNames())) {
 195             ctx.addCommand(new WorkDirectoryCommand(argIter));
 196             return true;
 197         }
 198 
 199         if (isMatch(cmd, WriteConfigCommand.getName())) {
 200             ctx.addCommand(new WriteConfigCommand(argIter));
 201             return true;
 202         }
 203 
 204 
 205         return false;
 206     }
 207 
 208     static Command getOpenCommand(File file)
 209         throws Command.Fault
 210     {
 211         return new OpenCommand(file);
 212     }
 213 
 214     private static Map commandFactory;
 215     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(ConfigManager.class);
 216 
 217     //--------------------------------------------------------------------------
 218 
 219     private static class ConcurrencyCommand extends Command
 220     {
 221         static String getName() {
 222             return "concurrency";
 223         }
 224 
 225         ConcurrencyCommand(ListIterator<String> argIter) throws Fault {
 226             super(getName());
 227 
 228             if (!argIter.hasNext())
 229                 throw new Fault(i18n, "cnfg.conc.missingArg");
 230 
 231             String arg = nextArg(argIter);
 232 
 233             NumberFormat fmt = NumberFormat.getIntegerInstance(); // will be locale-specific
 234             ParsePosition pos = new ParsePosition(0);


 862                 throw new Fault(i18n, "cnfg.set.insufficientArgs");
 863 
 864             String arg = nextArg(argIter);
 865             if (arg.equals("-f") || arg.equals("-file")) {
 866                 if (!argIter.hasNext())
 867                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
 868                 file = new File(nextArg(argIter));
 869             }
 870             else {
 871                 tag = arg;
 872                 if (!argIter.hasNext())
 873                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
 874                 value = nextArg(argIter);
 875             }
 876         }
 877 
 878         public void run(CommandContext ctx) throws Fault {
 879             InterviewParameters p = getConfig(ctx);
 880             Question[] path = p.getPath();
 881             if (file != null) {
 882                 Map values = loadFile(file);
 883                 for (int i = 0; i < path.length; i++) {
 884                     Question q = path[i];
 885                     String v = (String) (values.get(q.getTag()));
 886                     if (v != null) {
 887                         setValue(q, v);
 888                         path = p.getPath();
 889                     }
 890                 }
 891             }
 892             else {
 893                 for (int i = 0; i < path.length; i++) {
 894                     Question q = path[i];
 895                     if (q.getTag().equals(tag)) {
 896                         setValue(q, value);
 897                         return;
 898                     }
 899                 }
 900 
 901                 // The following is not ideal but works for now.
 902                 // It is arguably bad form to return such a long detail
 903                 // string, rather than providing an extra method
 904                 // to generate the trace if required -- i.e. the Fault
 905                 // equivalent of e.printStackTrace()


 950                 Question q = path[i];
 951                 sb.append(q.getTag());
 952                 if (!(q instanceof NullQuestion)) {
 953                     String s = q.getStringValue();
 954                     sb.append(" (");
 955                     if (s == null)
 956                         sb.append("null");
 957                     else if (s.length() < 32)
 958                         sb.append(s);
 959                     else {
 960                         sb.append(s.substring(0, 32));
 961                         sb.append("...");
 962                     }
 963                     sb.append(")");
 964                 }
 965                 sb.append(lineSep); // arguably better to do it later when printing to terminal
 966             }
 967             return (sb.toString());
 968         }
 969 
 970         private Map loadFile(File file) throws Fault {
 971             try (FileInputStream fis = new FileInputStream(file);
 972                  InputStream in = new BufferedInputStream(fis)) {
 973                 Properties props = new Properties();
 974                 props.load(in);
 975                 return props;
 976             }
 977             catch (FileNotFoundException e) {
 978                 throw new Fault(i18n, "cnfg.set.cantFindFile", file);
 979             }
 980             catch (IOException e) {
 981                 throw new Fault(i18n, "cnfg.set.cantReadFile",
 982                                 new Object[] { file, e.getMessage() });
 983             }
 984         }
 985 
 986         private File file;
 987         private String tag;
 988         private String value;
 989     }
 990 
 991     //--------------------------------------------------------------------------
 992 
 993     /**
 994      * Sets "external" interview values from the command line.
 995      */


1005             if (!argIter.hasNext())
1006                 throw new Fault(i18n, "cnfg.set.insufficientArgs");
1007 
1008             String arg = nextArg(argIter);
1009             if (arg.equals("-f") || arg.equals("-file")) {
1010                 if (!argIter.hasNext())
1011                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
1012                 file = new File(nextArg(argIter));
1013             }
1014             else {
1015                 name = arg;
1016                 if (!argIter.hasNext())
1017                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
1018                 value = nextArg(argIter);
1019             }
1020         }
1021 
1022         public void run(CommandContext ctx) throws Fault {
1023             InterviewParameters p = getConfig(ctx);
1024             if (file != null) {
1025                 Map values = loadFile(file);
1026                 Set keys = values.keySet();
1027                 Iterator it = keys.iterator();
1028                 String name = null;
1029                 for (int i = 0; it.hasNext(); i++) {
1030                     name = (String)(it.next());
1031                     /*  could do it this way to reject unknown props
1032                     String v = p.retrieveProperty(name);
1033                     if (v != null) {
1034                         p.storeProperty(name, (String)(values.get(name)));
1035                     }
1036                     */
1037                     p.storeProperty(name, (String)(values.get(name)));
1038                 }
1039             }
1040             else {
1041                 p.storeProperty(name, value);
1042             }
1043         }
1044 
1045         private Map loadFile(File file) throws Fault {
1046             try (FileInputStream fis = new FileInputStream(file);
1047                 InputStream in = new BufferedInputStream(fis)) {
1048                 Properties props = new Properties();
1049                 props.load(in);
1050                 return props;
1051             }
1052             catch (FileNotFoundException e) {
1053                 throw new Fault(i18n, "cnfg.set.cantFindFile", file);
1054             }
1055             catch (IOException e) {
1056                 throw new Fault(i18n, "cnfg.set.cantReadFile",
1057                                 new Object[] { file, e.getMessage() });
1058             }
1059         }
1060 
1061         private File file;
1062         private String name;
1063         private String value;
1064     }
1065 
1066     //--------------------------------------------------------------------------
1067 
1068     private static class TestsCommand extends Command
1069     {
1070         static String getName() {




 194         if (isMatch(cmd, WorkDirectoryCommand.getNames())) {
 195             ctx.addCommand(new WorkDirectoryCommand(argIter));
 196             return true;
 197         }
 198 
 199         if (isMatch(cmd, WriteConfigCommand.getName())) {
 200             ctx.addCommand(new WriteConfigCommand(argIter));
 201             return true;
 202         }
 203 
 204 
 205         return false;
 206     }
 207 
 208     static Command getOpenCommand(File file)
 209         throws Command.Fault
 210     {
 211         return new OpenCommand(file);
 212     }
 213 

 214     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(ConfigManager.class);
 215 
 216     //--------------------------------------------------------------------------
 217 
 218     private static class ConcurrencyCommand extends Command
 219     {
 220         static String getName() {
 221             return "concurrency";
 222         }
 223 
 224         ConcurrencyCommand(ListIterator<String> argIter) throws Fault {
 225             super(getName());
 226 
 227             if (!argIter.hasNext())
 228                 throw new Fault(i18n, "cnfg.conc.missingArg");
 229 
 230             String arg = nextArg(argIter);
 231 
 232             NumberFormat fmt = NumberFormat.getIntegerInstance(); // will be locale-specific
 233             ParsePosition pos = new ParsePosition(0);


 861                 throw new Fault(i18n, "cnfg.set.insufficientArgs");
 862 
 863             String arg = nextArg(argIter);
 864             if (arg.equals("-f") || arg.equals("-file")) {
 865                 if (!argIter.hasNext())
 866                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
 867                 file = new File(nextArg(argIter));
 868             }
 869             else {
 870                 tag = arg;
 871                 if (!argIter.hasNext())
 872                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
 873                 value = nextArg(argIter);
 874             }
 875         }
 876 
 877         public void run(CommandContext ctx) throws Fault {
 878             InterviewParameters p = getConfig(ctx);
 879             Question[] path = p.getPath();
 880             if (file != null) {
 881                 Map<String, String> values = loadFile(file);
 882                 for (int i = 0; i < path.length; i++) {
 883                     Question q = path[i];
 884                     String v = (values.get(q.getTag()));
 885                     if (v != null) {
 886                         setValue(q, v);
 887                         path = p.getPath();
 888                     }
 889                 }
 890             }
 891             else {
 892                 for (int i = 0; i < path.length; i++) {
 893                     Question q = path[i];
 894                     if (q.getTag().equals(tag)) {
 895                         setValue(q, value);
 896                         return;
 897                     }
 898                 }
 899 
 900                 // The following is not ideal but works for now.
 901                 // It is arguably bad form to return such a long detail
 902                 // string, rather than providing an extra method
 903                 // to generate the trace if required -- i.e. the Fault
 904                 // equivalent of e.printStackTrace()


 949                 Question q = path[i];
 950                 sb.append(q.getTag());
 951                 if (!(q instanceof NullQuestion)) {
 952                     String s = q.getStringValue();
 953                     sb.append(" (");
 954                     if (s == null)
 955                         sb.append("null");
 956                     else if (s.length() < 32)
 957                         sb.append(s);
 958                     else {
 959                         sb.append(s.substring(0, 32));
 960                         sb.append("...");
 961                     }
 962                     sb.append(")");
 963                 }
 964                 sb.append(lineSep); // arguably better to do it later when printing to terminal
 965             }
 966             return (sb.toString());
 967         }
 968 
 969         private Map<String, String> loadFile(File file) throws Fault {
 970             try (FileInputStream fis = new FileInputStream(file);
 971                  InputStream in = new BufferedInputStream(fis)) {
 972                 return com.sun.javatest.util.Properties.load(in);


 973             }
 974             catch (FileNotFoundException e) {
 975                 throw new Fault(i18n, "cnfg.set.cantFindFile", file);
 976             }
 977             catch (IOException e) {
 978                 throw new Fault(i18n, "cnfg.set.cantReadFile",
 979                                 new Object[] { file, e.getMessage() });
 980             }
 981         }
 982 
 983         private File file;
 984         private String tag;
 985         private String value;
 986     }
 987 
 988     //--------------------------------------------------------------------------
 989 
 990     /**
 991      * Sets "external" interview values from the command line.
 992      */


1002             if (!argIter.hasNext())
1003                 throw new Fault(i18n, "cnfg.set.insufficientArgs");
1004 
1005             String arg = nextArg(argIter);
1006             if (arg.equals("-f") || arg.equals("-file")) {
1007                 if (!argIter.hasNext())
1008                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
1009                 file = new File(nextArg(argIter));
1010             }
1011             else {
1012                 name = arg;
1013                 if (!argIter.hasNext())
1014                     throw new Fault(i18n, "cnfg.set.insufficientArgs");
1015                 value = nextArg(argIter);
1016             }
1017         }
1018 
1019         public void run(CommandContext ctx) throws Fault {
1020             InterviewParameters p = getConfig(ctx);
1021             if (file != null) {
1022                 Map<String, String> values = loadFile(file);
1023                 Set<String> keys = values.keySet();
1024                 Iterator<String> it = keys.iterator();
1025                 String name = null;
1026                 for (int i = 0; it.hasNext(); i++) {
1027                     name = it.next();
1028                     /*  could do it this way to reject unknown props
1029                     String v = p.retrieveProperty(name);
1030                     if (v != null) {
1031                         p.storeProperty(name, (String)(values.get(name)));
1032                     }
1033                     */
1034                     p.storeProperty(name, (values.get(name)));
1035                 }
1036             }
1037             else {
1038                 p.storeProperty(name, value);
1039             }
1040         }
1041 
1042         private Map<String, String> loadFile(File file) throws Fault {
1043             try (FileInputStream fis = new FileInputStream(file);
1044                 InputStream in = new BufferedInputStream(fis)) {
1045                 return com.sun.javatest.util.Properties.load(in);


1046             }
1047             catch (FileNotFoundException e) {
1048                 throw new Fault(i18n, "cnfg.set.cantFindFile", file);
1049             }
1050             catch (IOException e) {
1051                 throw new Fault(i18n, "cnfg.set.cantReadFile",
1052                                 new Object[] { file, e.getMessage() });
1053             }
1054         }
1055 
1056         private File file;
1057         private String name;
1058         private String value;
1059     }
1060 
1061     //--------------------------------------------------------------------------
1062 
1063     private static class TestsCommand extends Command
1064     {
1065         static String getName() {


< prev index next >