/**
   * Create DataSource, there are two ways, if the application has a security policy then it will
   * create from that policy properties, otherwise it will use <code>connection.datasource</code>
   * property defined in <code>hibernate.cfg.xml</code> file.
   *
   * @param props
   * @return DataSource
   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  private DataSource createDataSource(Properties props) throws Exception {
    try {
      Properties dsProperties = null;
      InitialContext ic = new InitialContext();
      Subject subject = (Subject) ic.lookup(SECURITY_SUBJECT);
      ObjectName oname = findObjectName(ic, props);
      MBeanServer server = null;
      List servers = MBeanServerFactory.findMBeanServer(null);
      if (servers.size() > 0) {
        if (servers.size() > 1) {
          //	Iterates over servers list untill AonMainDeployerMBean is found.
          //	TODO Isolate application server.
          Iterator it = servers.iterator();
          while (it.hasNext()) {
            server = (MBeanServer) it.next();
            try {
              ObjectName jbossname = new ObjectName("jboss.admin:service=AonMainDeployer");
              server.getObjectInstance(jbossname);
              break;
            } catch (Exception e) {
            }
          }
        } else {
          server = (MBeanServer) servers.get(0);
        }
      }
      if (subject == null) throw new NamingException("");

      Principal principal = subject.getPrincipals().iterator().next();
      dsProperties =
          (Properties)
              server.invoke(
                  oname,
                  DSMD_METHOD_NAME,
                  new Object[] {principal},
                  new String[] {Principal.class.getName()});
      return BasicDataSourceFactory.createDataSource(dsProperties);
    } catch (NamingException ne) {
      String jndiName = props.getProperty(Environment.DATASOURCE);
      DataSource ds;
      try {
        ds = (DataSource) NamingHelper.getInitialContext(props).lookup(jndiName);
      } catch (NamingException e) {
        throw new HibernateException("Could not find datasource", e);
      }
      if (ds == null) {
        throw new HibernateException("Could not find datasource: " + jndiName);
      }
      return ds;
    }
  }
  public void configure(Properties props) throws HibernateException {

    String jndiName = props.getProperty(Environment.DATASOURCE);
    if (jndiName == null) {
      String msg = "datasource JNDI name was not specified by property " + Environment.DATASOURCE;
      log.fatal(msg);
      throw new HibernateException(msg);
    }

    user = props.getProperty(Environment.USER);
    pass = props.getProperty(Environment.PASS);

    try {
      ds = (DataSource) NamingHelper.getInitialContext(props).lookup(jndiName);
    } catch (Exception e) {
      log.fatal("Could not find datasource: " + jndiName, e);
      throw new HibernateException("Could not find datasource", e);
    }
    if (ds == null) {
      throw new HibernateException("Could not find datasource: " + jndiName);
    }
    log.info("Using datasource: " + jndiName);
  }