Simple code cleanups.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / utils / ResolverTest.java
index ea860d9..ba13620 100644 (file)
@@ -49,6 +49,8 @@
 
 package edu.internet2.middleware.shibboleth.utils;
 
+import jargs.gnu.CmdLineParser;
+
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintStream;
@@ -69,18 +71,14 @@ import edu.internet2.middleware.shibboleth.aa.AAAttributeSet.ShibAttributeIterat
 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolverException;
 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
-import gnu.getopt.Getopt;
-import gnu.getopt.LongOpt;
 
 /**
  * Utility for testing an Attribute Resolver configuration.
  * 
  * @author Walter Hoehn
  */
-
 public class ResolverTest {
 
-       private static Logger log = Logger.getLogger(ResolverTest.class.getName());
        private static boolean debug = false;
        private static String file = null;
        private static String requester = null;
@@ -88,51 +86,41 @@ public class ResolverTest {
 
        public static void main(String[] args) {
 
-               LongOpt[] longopts =
-                       {
-                               new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
-                               new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f'),
-                               new LongOpt("user", LongOpt.REQUIRED_ARGUMENT, null, 'u'),
-                               new LongOpt("requester", LongOpt.REQUIRED_ARGUMENT, null, 'r'),
-                               new LongOpt("debug", LongOpt.NO_ARGUMENT, null, 'd')};
-
-               Getopt getOpt = new Getopt("ResolverTest", args, ":hdf:u:r:", longopts);
-               getOpt.setOpterr(false);
-
-               for (int c;((c = getOpt.getopt()) != -1);) {
-                       switch (c) {
-
-                               case 'h' :
-                                       printUsage(System.out);
-                                       System.exit(0);
-                                       break;
-
-                               case 'd' :
-                                       debug = true;
-                                       break;
+               CmdLineParser parser = new CmdLineParser();
+               CmdLineParser.Option helpOption = parser.addBooleanOption('h', "help");
+               CmdLineParser.Option debugOption = parser.addBooleanOption('d', "debug");
+               CmdLineParser.Option fileOption = parser.addStringOption('f', "file");
+               CmdLineParser.Option userOption = parser.addStringOption('u', "user");
+               CmdLineParser.Option requesterOption = parser.addStringOption('r', "requester");
 
-                               case 'f' :
-                                       file = getOpt.getOptarg();
-                                       break;
-
-                               case 'u' :
-                                       user = getOpt.getOptarg();
-                                       break;
-
-                               case 'r' :
-                                       requester = getOpt.getOptarg();
-                                       break;
+               try {
+                       parser.parse(args);
+               } catch (CmdLineParser.OptionException e) {
+                       System.err.println(e.getMessage());
+                       try {
+                               Thread.sleep(100); //silliness to get error to print first
+                       } catch (InterruptedException ie) {
+                               //doesn't matter
+                       }
+                       printUsage(System.out);
+                       System.exit(1);
+               }
 
-                               case ':' :
-                                       System.out.println("You need an argument for option " + (char) getOpt.getOptopt());
-                                       System.exit(1);
+               Boolean helpEnabled = (Boolean) parser.getOptionValue(helpOption);
+               if (helpEnabled != null && helpEnabled.booleanValue()) {
+                       printUsage(System.out);
+                       System.exit(0);
+               }
 
-                               case '?' :
-                                       System.out.println("The option '" + (char) getOpt.getOptopt() + "' is not valid");
-                                       System.exit(1);
-                       }
+               Boolean debugEnabled = ((Boolean) parser.getOptionValue(debugOption));
+               if (debugEnabled != null) {
+                       debug = debugEnabled.booleanValue();
                }
 
+               file = (String) parser.getOptionValue(fileOption);
+               user = (String) parser.getOptionValue(userOption);
+               requester = (String) parser.getOptionValue(requesterOption);
+
                configureLogging(debug);
                checkRequired();
 
@@ -140,7 +128,7 @@ public class ResolverTest {
                configuration.setProperty(
                        "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
                        file);
-                       
+
                try {
                        AttributeResolver resolver = new AttributeResolver(configuration);
                        String[] attributes = resolver.listRegisteredAttributeDefinitionPlugIns();
@@ -154,7 +142,7 @@ public class ResolverTest {
 
                        System.out.println(
                                "Received the following back from the Attribute Resolver:" + System.getProperty("line.separator"));
-                       
+
                        for (ShibAttributeIterator iterator = attributeSet.shibAttributeIterator(); iterator.hasNext();) {
                                AAAttribute attribute = iterator.nextShibAttribute();
                                Node node = attribute.toDOM();
@@ -168,6 +156,7 @@ public class ResolverTest {
                                new XMLSerializer(xml, format).serialize((Element) node);
                                System.out.println(xml.toString() + System.getProperty("line.separator"));
                        }
+               
                } catch (AttributeResolverException e) {
                        System.err.println("Error initializing the Attribute Resolver: " + e.getMessage());
                } catch (SAMLException e) {
@@ -177,6 +166,10 @@ public class ResolverTest {
                }
        }
 
+       /**
+        * Ensures that all required parameters were specified and successfully parsed.
+        *
+        */
        private static void checkRequired() {
                if (file == null || user == null) {
                        System.err.println("Missing required parameter(s).");
@@ -190,10 +183,10 @@ public class ResolverTest {
                }
        }
 
-       private static void configureLogging(boolean debugOn) {
+       private static void configureLogging(boolean debugEnabled) {
 
                BasicConfigurator.configure();
-               if (debug) {
+               if (debugEnabled) {
                        Logger.getRootLogger().setLevel(Level.DEBUG);
                } else {
                        Logger.getRootLogger().setLevel(Level.INFO);