예제 #1
0
  @Test
  public void testCreateFromURI() throws Exception {
    URI uri = testFile.toURI();
    sm.register("test", uri);

    assertTrue(sm.getSource("test").isFileSource());

    uri =
        URI.create(
            "postgresql://www.host.com:1234/db_name?table=toto&schema=tata&"
                + "user=me&password=changeme");

    sm.register("test2", uri);
    assertTrue(sm.getSource("test2").isDBSource());

    DBSource s = sm.getSource("test2").getDBSource();
    assertEquals("www.host.com", s.getHost());
    assertEquals("db_name", s.getDbName());
    assertEquals(1234, s.getPort());
    assertEquals("jdbc:postgresql", s.getPrefix());
    assertEquals("toto", s.getTableName());
    assertEquals("tata", s.getSchemaName());
    assertEquals("me", s.getUser());
    assertEquals("changeme", s.getPassword());
  }
  public boolean isConnected() throws Exception {
    File f = new File(sqlScriptFile);
    if (!f.exists()) {
      return false;
    }

    Class.forName(jdbcDriver);
    String connectionString = dbSource.getPrefix() + ":";
    if (dbSource.getHost() != null) {
      connectionString += "//" + dbSource.getHost();

      if (dbSource.getPort() != -1) {
        connectionString += (":" + dbSource.getPort());
      }
      connectionString += "/";
    }

    connectionString += (dbSource.getDbName());
    try {
      Connection c =
          DriverManager.getConnection(connectionString, dbSource.getUser(), dbSource.getPassword());
    } catch (SQLException ex) {
      return false;
    }
    return true;
  }
  public void create(DataSourceFactory dsf) throws Exception {
    FileInputStream fis = new FileInputStream(sqlScriptFile);
    DataInputStream dis = new DataInputStream(fis);
    byte[] buffer = new byte[(int) fis.getChannel().size()];
    dis.readFully(buffer);
    String script = new String(buffer);

    Class.forName(jdbcDriver);
    String connectionString = dbSource.getPrefix() + ":";
    if (dbSource.getHost() != null) {
      connectionString += "//" + dbSource.getHost();

      if (dbSource.getPort() != -1) {
        connectionString += (":" + dbSource.getPort());
      }
      connectionString += "/";
    }

    connectionString += (dbSource.getDbName());

    DriverManager.setLoginTimeout(2);
    Connection c =
        DriverManager.getConnection(connectionString, dbSource.getUser(), dbSource.getPassword());

    Statement st = c.createStatement();
    String[] statements = script.split("\\Q;\\E");
    for (String statement : statements) {
      try {
        st.execute(statement);
      } catch (SQLException e) {
        System.err.println(statement);
        e.printStackTrace();
      }
    }
    st.close();
    c.close();

    DBTableSourceDefinition def = new DBTableSourceDefinition(dbSource);
    dsf.getSourceManager().remove(name);
    dsf.getSourceManager().register(name, def);
  }