예제 #1
0
  public Connector getConnector(LockEngine lockEngine, Database database)
      throws MappingException, PersistenceException {

    if (_connectors.containsKey(lockEngine)) return (Connector) _connectors.get(lockEngine);

    try {
      if (database.getDriver() != null) {
        // JDO configuration file specifies a driver, use the driver
        // properties to create a new registry object.
        Properties props;
        Enumeration params;
        Param param;

        if (database.getDriver().getClassName() != null) {
          try {
            Class.forName(database.getDriver().getClassName()).newInstance();
          } catch (Exception except) {
            throw new MappingException(except);
          }
        }
        if (DriverManager.getDriver(database.getDriver().getUrl()) == null)
          throw new MappingException("jdo.missingDriver", database.getDriver().getUrl());

        props = new Properties();
        params = database.getDriver().enumerateParam();
        while (params.hasMoreElements()) {
          param = (Param) params.nextElement();
          props.put(param.getName(), param.getValue());
        }
        Connector c = new SQLConnector(database.getName(), database.getDriver().getUrl(), props);
        _connectors.put(lockEngine, c);
        return c;
      } else if (database.getDataSource() != null) {
        // JDO configuration file specifies a DataSource object, use the
        // DataSource which was configured from the JDO configuration file
        // to create a new registry object.
        DataSource ds;

        ds = (DataSource) database.getDataSource().getParams();
        if (ds == null) throw new MappingException("jdo.missingDataSource", database.getName());
        Connector c = new SQLConnector(database.getName(), ds);
        _connectors.put(lockEngine, c);
        return c;
      } else if (database.getJndi() != null) {
        // JDO configuration file specifies a DataSource lookup through JNDI,
        // locate the DataSource object frome the JNDI namespace and use it.
        Object ds;

        try {
          ds = new InitialContext().lookup(database.getJndi().getName());
        } catch (NameNotFoundException except) {
          throw new MappingException("jdo.jndiNameNotFound", database.getJndi().getName());
        } catch (NamingException except) {
          throw new MappingException(except);
        }
        if (!(ds instanceof DataSource))
          throw new MappingException("jdo.jndiNameNotFound", database.getJndi().getName());
        Connector c = new SQLConnector(database.getName(), (DataSource) ds);
        _connectors.put(lockEngine, c);
        return c;
      } else {
        throw new MappingException("jdo.missingDataSource", database.getName());
      }
    } catch (SQLException e) {
      throw new PersistenceException("Exception occurs while obtaining SQL connection", e);
    }
  }