Пример #1
0
  public static void invokeSetMethod(Object obj, String prop, String value)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Class cl = obj.getClass();
    // change first letter to uppercase
    String setMeth = "set" + prop.substring(0, 1).toUpperCase() + prop.substring(1);

    // try string method
    try {
      Class[] cldef = {String.class};
      Method meth = cl.getMethod(setMeth, cldef);
      Object[] params = {value};
      meth.invoke(obj, params);
      return;
    } catch (NoSuchMethodException ex) {
      try {
        // try int method
        Class[] cldef = {Integer.TYPE};
        Method meth = cl.getMethod(setMeth, cldef);
        Object[] params = {Integer.valueOf(value)};
        meth.invoke(obj, params);
        return;
      } catch (NoSuchMethodException nsmex) {
        // try boolean method
        Class[] cldef = {Boolean.TYPE};
        Method meth = cl.getMethod(setMeth, cldef);
        Object[] params = {Boolean.valueOf(value)};
        meth.invoke(obj, params);
        return;
      }
    }
  }
Пример #2
0
  public static void invokeSetMethodCaseInsensitive(Object obj, String prop, String value)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String alternateMethodName = null;
    Class cl = obj.getClass();

    String setMeth = "set" + prop;

    Method[] methodsList = cl.getMethods();
    boolean methodFound = false;
    int i = 0;
    for (i = 0; i < methodsList.length; ++i) {
      if (methodsList[i].getName().equalsIgnoreCase(setMeth) == true) {
        Class[] parameterTypes = methodsList[i].getParameterTypes();
        if (parameterTypes.length == 1) {
          if (parameterTypes[0].getName().equals("java.lang.String")) {
            methodFound = true;
            break;
          } else alternateMethodName = methodsList[i].getName();
        }
      }
    }
    if (methodFound == true) {
      Object[] params = {value};
      methodsList[i].invoke(obj, params);
      return;
    }
    if (alternateMethodName != null) {
      try {
        // try int method
        Class[] cldef = {Integer.TYPE};
        Method meth = cl.getMethod(alternateMethodName, cldef);
        Object[] params = {Integer.valueOf(value)};
        meth.invoke(obj, params);
        return;
      } catch (NoSuchMethodException nsmex) {
        // try boolean method
        Class[] cldef = {Boolean.TYPE};
        Method meth = cl.getMethod(alternateMethodName, cldef);
        Object[] params = {Boolean.valueOf(value)};
        meth.invoke(obj, params);
        return;
      }

    } else throw new NoSuchMethodException(setMeth);
  }
Пример #3
0
  public static PoolConfiguration parsePoolProperties(Properties properties) {
    PoolConfiguration poolProperties = new PoolProperties();
    String value = null;

    value = properties.getProperty(PROP_DEFAULTAUTOCOMMIT);
    if (value != null) {
      poolProperties.setDefaultAutoCommit(Boolean.valueOf(value));
    }

    value = properties.getProperty(PROP_DEFAULTREADONLY);
    if (value != null) {
      poolProperties.setDefaultReadOnly(Boolean.valueOf(value));
    }

    value = properties.getProperty(PROP_DEFAULTTRANSACTIONISOLATION);
    if (value != null) {
      int level = UNKNOWN_TRANSACTIONISOLATION;
      if ("NONE".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_NONE;
      } else if ("READ_COMMITTED".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_READ_COMMITTED;
      } else if ("READ_UNCOMMITTED".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_READ_UNCOMMITTED;
      } else if ("REPEATABLE_READ".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_REPEATABLE_READ;
      } else if ("SERIALIZABLE".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_SERIALIZABLE;
      } else {
        try {
          level = Integer.parseInt(value);
        } catch (NumberFormatException e) {
          System.err.println("Could not parse defaultTransactionIsolation: " + value);
          System.err.println("WARNING: defaultTransactionIsolation not set");
          System.err.println("using default value of database driver");
          level = UNKNOWN_TRANSACTIONISOLATION;
        }
      }
      poolProperties.setDefaultTransactionIsolation(level);
    }

    value = properties.getProperty(PROP_DEFAULTCATALOG);
    if (value != null) {
      poolProperties.setDefaultCatalog(value);
    }

    value = properties.getProperty(PROP_DRIVERCLASSNAME);
    if (value != null) {
      poolProperties.setDriverClassName(value);
    }

    value = properties.getProperty(PROP_MAXACTIVE);
    if (value != null) {
      poolProperties.setMaxActive(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXIDLE);
    if (value != null) {
      poolProperties.setMaxIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINIDLE);
    if (value != null) {
      poolProperties.setMinIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_INITIALSIZE);
    if (value != null) {
      poolProperties.setInitialSize(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXWAIT);
    if (value != null) {
      poolProperties.setMaxWait(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_TESTONBORROW);
    if (value != null) {
      poolProperties.setTestOnBorrow(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TESTONRETURN);
    if (value != null) {
      poolProperties.setTestOnReturn(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TESTONCONNECT);
    if (value != null) {
      poolProperties.setTestOnConnect(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TIMEBETWEENEVICTIONRUNSMILLIS);
    if (value != null) {
      poolProperties.setTimeBetweenEvictionRunsMillis(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_NUMTESTSPEREVICTIONRUN);
    if (value != null) {
      poolProperties.setNumTestsPerEvictionRun(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINEVICTABLEIDLETIMEMILLIS);
    if (value != null) {
      poolProperties.setMinEvictableIdleTimeMillis(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_TESTWHILEIDLE);
    if (value != null) {
      poolProperties.setTestWhileIdle(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_PASSWORD);
    if (value != null) {
      poolProperties.setPassword(value);
    }

    value = properties.getProperty(PROP_URL);
    if (value != null) {
      poolProperties.setUrl(value);
    }

    value = properties.getProperty(PROP_USERNAME);
    if (value != null) {
      poolProperties.setUsername(value);
    }

    value = properties.getProperty(PROP_VALIDATIONQUERY);
    if (value != null) {
      poolProperties.setValidationQuery(value);
    }

    value = properties.getProperty(PROP_VALIDATOR_CLASS_NAME);
    if (value != null) {
      poolProperties.setValidatorClassName(value);
    }

    value = properties.getProperty(PROP_VALIDATIONINTERVAL);
    if (value != null) {
      poolProperties.setValidationInterval(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED);
    if (value != null) {
      poolProperties.setAccessToUnderlyingConnectionAllowed(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONED);
    if (value != null) {
      poolProperties.setRemoveAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONEDTIMEOUT);
    if (value != null) {
      poolProperties.setRemoveAbandonedTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_LOGABANDONED);
    if (value != null) {
      poolProperties.setLogAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_POOLPREPAREDSTATEMENTS);
    if (value != null) {
      log.warn(PROP_POOLPREPAREDSTATEMENTS + " is not a valid setting, it will have no effect.");
    }

    value = properties.getProperty(PROP_MAXOPENPREPAREDSTATEMENTS);
    if (value != null) {
      log.warn(PROP_MAXOPENPREPAREDSTATEMENTS + " is not a valid setting, it will have no effect.");
    }

    value = properties.getProperty(PROP_CONNECTIONPROPERTIES);
    if (value != null) {
      Properties p = getProperties(value);
      poolProperties.setDbProperties(p);
    } else {
      poolProperties.setDbProperties(new Properties());
    }

    if (poolProperties.getUsername() != null) {
      poolProperties.getDbProperties().setProperty("user", poolProperties.getUsername());
    }
    if (poolProperties.getPassword() != null) {
      poolProperties.getDbProperties().setProperty("password", poolProperties.getPassword());
    }

    value = properties.getProperty(PROP_INITSQL);
    if (value != null) {
      poolProperties.setInitSQL(value);
    }

    value = properties.getProperty(PROP_INTERCEPTORS);
    if (value != null) {
      poolProperties.setJdbcInterceptors(value);
    }

    value = properties.getProperty(PROP_JMX_ENABLED);
    if (value != null) {
      poolProperties.setJmxEnabled(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_FAIR_QUEUE);
    if (value != null) {
      poolProperties.setFairQueue(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_USE_EQUALS);
    if (value != null) {
      poolProperties.setUseEquals(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(OBJECT_NAME);
    if (value != null) {
      poolProperties.setName(ObjectName.quote(value));
    }

    value = properties.getProperty(PROP_ABANDONWHENPERCENTAGEFULL);
    if (value != null) {
      poolProperties.setAbandonWhenPercentageFull(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXAGE);
    if (value != null) {
      poolProperties.setMaxAge(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_USE_CON_LOCK);
    if (value != null) {
      poolProperties.setUseLock(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_DATASOURCE);
    if (value != null) {
      // this should never happen
      throw new IllegalArgumentException(
          "Can't set dataSource property as a string, this must be a javax.sql.DataSource object.");
    }

    value = properties.getProperty(PROP_DATASOURCE_JNDI);
    if (value != null) {
      poolProperties.setDataSourceJNDI(value);
    }

    value = properties.getProperty(PROP_SUSPECT_TIMEOUT);
    if (value != null) {
      poolProperties.setSuspectTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_ALTERNATE_USERNAME_ALLOWED);
    if (value != null) {
      poolProperties.setAlternateUsernameAllowed(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_COMMITONRETURN);
    if (value != null) {
      poolProperties.setCommitOnReturn(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_ROLLBACKONRETURN);
    if (value != null) {
      poolProperties.setRollbackOnReturn(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_USEDISPOSABLECONNECTIONFACADE);
    if (value != null) {
      poolProperties.setUseDisposableConnectionFacade(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_LOGVALIDATIONERRORS);
    if (value != null) {
      poolProperties.setLogValidationErrors(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_PROPAGATEINTERRUPTSTATE);
    if (value != null) {
      poolProperties.setPropagateInterruptState(Boolean.parseBoolean(value));
    }

    return poolProperties;
  }
  /** default constructor reads in the configuration file and parses values */
  public AuthenticationService() {
    // read in the properties file
    try {
      // go get the properties for this LDAP connection
      Properties configFile = new Properties();
      // load the file from the class path (moved there by ant task)
      configFile.load(this.getClass().getClassLoader().getResourceAsStream("./ldap.properties"));

      // send ouput to console
      //	enumerateContents(configFile.propertyNames());

      this.INITIAL_CONTEXT_FACTORY = configFile.getProperty("INITIAL_CONTEXT_FACTORY");

      this.SECURITY_AUTHENTICATION = configFile.getProperty("SECURITY_AUTHENTICATION");

      this.PROVIDER_TYPE = configFile.getProperty("PROVIDER_TYPE");

      if (this.PROVIDER_TYPE.equals("AD")) {
        this.PROVIDER_URL = configFile.getProperty("AD_PROVIDER_URL");
        this.AD_DOMAIN = configFile.getProperty("AD_DOMAIN");
        this.BASE_DN = configFile.getProperty("AD_BASE_DN");
        this.GROUPS_LOC = configFile.getProperty("AD_GROUPS_LOC");
        this.USERS_LOC = configFile.getProperty("AD_USERS_LOC");
        this.AD_ANON_BIND_UNAME = configFile.getProperty("AD_ANON_BIND_UNAME");
        this.AD_ANON_BIND_PWORD = configFile.getProperty("AD_ANON_BIND_PWORD");
        this.ATTRIBUTE_NAME_KEYFOB_ID = configFile.getProperty("ATTRIBUTE_NAME_KEYFOB_ID");
        this.ATTRIBUTE_NAME_UNAME = configFile.getProperty("ATTRIBUTE_NAME_UNAME");
      } else if (this.PROVIDER_TYPE.equals("LDAP")) {
        this.PROVIDER_URL = configFile.getProperty("LDAP_PROVIDER_URL");
        this.BASE_DN = configFile.getProperty("LDAP_BASE_DN");
        this.GROUPS_LOC = configFile.getProperty("LDAP_GROUPS_LOC");
        this.USERS_LOC = configFile.getProperty("LDAP_USERS_LOC");
      } else {
        throw new Exception("Provider type not found.");
      }

      // get override info
      this.LDAP_OVERRIDE = Boolean.parseBoolean(configFile.getProperty("LDAP_OVERRIDE"));
      this.LDAP_OVERRIDE_UNAME = configFile.getProperty("LDAP_OVERRIDE_UNAME");
      this.LDAP_OVERRIDE_PWORD = configFile.getProperty("LDAP_OVERRIDE_PWORD");

      // init the array list
      groups = new HashMap<String, String>();
      // load the groups into a String array
      for (Enumeration e = configFile.propertyNames(); e.hasMoreElements(); ) {
        String key = e.nextElement().toString();
        if (key.indexOf("GROUP_") == 0) { // ie key in key=value pair matches "GROUP_"
          // append the group name to the array list for checking later
          groups.put(key, configFile.getProperty(key));
        }
      }

    } catch (FileNotFoundException e) {
      //	e.printStackTrace();
      System.err.println("FileNotFoundException: " + e.getMessage());
      errorStack += e.getMessage() + "\n";
    } catch (IOException e) {
      /** @TODO set defaults, or just give up? */
      //	e.printStackTrace();
      System.err.println("IOException: " + e.getMessage());
      errorStack += e.getMessage() + "\n";
    } catch (Exception e) {
      //	e.printStackTrace();
      System.err.println("Exception: " + e.getMessage());
      errorStack += e.getMessage() + "\n";
    }
  }