Subject: Java-GSS: type-safe constants

From: Keith Burdis <[email-masked]>
Date: Fri, 9 Jul 1999 13:37:43 +0200

Hi there

  I'd like to propose that type-safe constants be used in the JGSS API
  instead of the integer constants that are currently being used. I see the
  benefits as being:

  1) The compiler can check the validity of constants passed as parameter at
     compile-time. (This seems particularly useful in the GSSException class).
     This avoids code like:

	throw new GSSException(4242);

     being accepted at compile time.

  2) Implementors do not need to provide logic to check whether or not such a
     parameter is valid (except perhaps to check for a null parameter).

  Such type-safe constants can be implemented so that they are not cumbersome
  to use by application programmers, and do not clutter up the API with
  additional classes that an application programmer needs to be aware of.

  Here's an extract from my GSSException class which illustrates this
  proposal:

    public class GSSException
      extends java.lang.Exception
    {
      public static final Type BAD_BINDINGS = Type.BAD_BINDINGS;
      public static final Type BAD_MECH = Type.BAD_MECH;
  
      public GSSException(Type majorCode)
  
      public GSSException(Type majorCode,
                          int minorCode,
			  String minorString)
  
      public Type getMajor()
  
      public int getMinor()
  
      public String getMajorString()
  
      public String getMinorString() 
  
      public void setMinor(int minorCode,
			   String message)
  
      public String toString()
  
      public String getMessage()
  
      private static final class Type
      {
        private static final Type BAD_BINDINGS =
	  new Type("BAD_BINDINGS");
        private static final Type BAD_MECH =
	  new Type("BAD_MECH");
  
        private Type(String name)
  
        private String getMessage()
  
        private String getName()
  
        public String toString()
      }
    }

  Similarly for IGSSCredential and IGSSName.
  You can see the full class at:

    http://yoda.cs.ru.ac.za/java/JGSS/krb/GSSException.java

  One difference using type-safe constants is that you can no longer use
  switch statements like:

    switch (exception.getMajor()) {
      case GSSException.BAD_BINDINGS: {
	System.out.println("BAD_BINDINGS");
	break;
      }
      case GSSException.BAD_MECH: {
	System.out.println("BAD_MECH");
	break;
      }
    }

  Instead you have to use:

    if (exception.getMajor() == GSSException.BAD_BINDINGS)
      System.out.println("BAD_BINDINGS");
    else if (exception.getMajor() == GSSException.BAD_MECH)
      System.out.println("BAD_MECH");

  Other than that application programmers should be able to use these
  constants in the same way that they use the integer ones. But, now we have
  the benefit of compile-time checks.

  Such constants are being used in other Java APIs such as JavaMail (eg.
  Flags.Flag) and JAAS (eg. AppConfigurationEntry.LoginModuleFlag).

  Comments?

    - Keith
-- 
Keith Burdis - MSc (Com Sci) - Rhodes University, South Africa  
Email   : [email-masked]
WWW     : http://www.rucus.ru.ac.za/~keith/
IRC     : Panthras                                          JAPH

"Any technology sufficiently advanced is indistinguishable from a perl script"

Standard disclaimer.
---
==========================================================================
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ietf-cat-wg" to [email-masked]