/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.ggf.drmaa.JobTemplate;
import org.ggf.drmaa.Session;
import org.ggf.drmaa.SessionFactory;

/*
 * Introspection.java
 *
 * Created on February 6, 2007, 9:05 AM
 * @author dan.templeton@sun.com
 */
public class Introspection {
   private static final String N1GE61_PROP = "something";
   private static final String N1GE60_PROP = "somethingSimilar";
   private static final String CONDOR_PROP = "somethingDifferent";
   private static final String N1GE61_METH = "setSomething";
   private static final String N1GE60_METH = "setSomethingSimilar";
   private static final String CONDOR_METH = "setSomethingDifferent";
   private static Session session = null;
   private static JobTemplate jt = null;
   private static String drm = null;

   private static void main(String[] args) throws Exception {
      session = SessionFactory.getFactory().getSession();
      session.init("");
      jt = session.createJobTemplate();
      drm = session.getDrmSystem();

      indirectExample();
      directExample();

      session.deleteJobTemplate(jt);
      session.exit();
   }

   /**
    * This method uses the JavaBeans framework to find and set a DRM-specific
    * property value.
    */
   private static void indirectExample() throws Exception {
      Class clazz = jt.getClass();
      BeanInfo info = Introspector.getBeanInfo(clazz);
      PropertyDescriptor[] props = info.getPropertyDescriptors();
      String propName = null;
      Method setter = null;

      if (drm.equals("N1GE 6.1")) {
         propName = N1GE61_PROP;
      } else if (drm.equals("N1GE 6.0")) {
         propName = N1GE60_PROP;
      } else if (drm.equals("Condor")) {
         propName = CONDOR_PROP;
      }

      if (propName != null) {
         for (int i = 0; i < props.length; i++) {
            if (props[i].getName().equals(propName)) {
               setter = props[i].getWriteMethod();
            }
         }

         if (setter != null) {
            setter.invoke(jt, "value");
            // With J2SE 1.4, the previous line would have to be:
            // setter.invoke(jt, new Object[] {"value"});
         }
      }
   }

   /**
    * This method uses reflection directly to set the value of a DRM-specific
    * property.
    */
   private static void directExample() throws Exception {
      Class clazz = jt.getClass();
      String methName = null;

      if (drm.equals("N1GE 6.1")) {
         methName = N1GE61_METH;
      } else if (drm.equals("N1GE 6.0")) {
         methName = N1GE60_METH;
      } else if (drm.equals("Condor")) {
         methName = CONDOR_METH;
      }

      Method setter = clazz.getMethod(methName, String.class);

      if (setter != null) {
         setter.invoke(jt, "value");
         // With J2SE 1.4, the previous line would have to be:
         // setter.invoke(jt, new Object[] {"value"});
      }
   }
}
