Beispiel #1
0
  public static StorageManager getStorage(VSensorConfig config) {
    // StringBuilder sb = new StringBuilder("get storage for: ").append(config == null ? null :
    // config.getName()).append(" -> use ");
    StorageManager sm = storagesConfigs.get(config == null ? null : config);
    if (sm != null) return sm;

    DBConnectionInfo dci = null;
    if (config == null || config.getStorage() == null || !config.getStorage().isDefined()) {
      // Use the default storage
      //    sb.append("(default) config: ").append(config);
      sm = mainStorage;
    } else {
      //    sb.append("(specific) ");
      // Use the virtual sensor specific storage
      if (config.getStorage().isIdentifierDefined()) {
        // TODO get the connection info with the identifier.
        throw new IllegalArgumentException("Identifiers for storage is not supported yet.");
      } else {
        dci =
            new DBConnectionInfo(
                config.getStorage().getJdbcDriver(),
                config.getStorage().getJdbcURL(),
                config.getStorage().getJdbcUsername(),
                config.getStorage().getJdbcPassword());
      }
      sm = storages.get(dci.hashCode());
      if (sm == null) {
        sm =
            StorageManagerFactory.getInstance(
                config.getStorage().getJdbcDriver(),
                config.getStorage().getJdbcUsername(),
                config.getStorage().getJdbcPassword(),
                config.getStorage().getJdbcURL(),
                DEFAULT_MAX_DB_CONNECTIONS);
        storages.put(dci.hashCode(), sm);
        storagesConfigs.put(config, sm);
      }
    }
    // sb.append("storage: ").append(sm.toString()).append(" dci: ").append(dci).append(" code:
    // ").append(dci == null ? null : dci.hashCode());
    // if (logger.isDebugEnabled())
    // logger.warn(sb.toString());
    return sm;
  }
Beispiel #2
0
 public static BasicDataSource getDataSource(DBConnectionInfo dci) {
   BasicDataSource ds = null;
   try {
     ds = (BasicDataSource) GSNContext.getMainContext().lookup(Integer.toString(dci.hashCode()));
     if (ds == null) {
       ds = new BasicDataSource();
       ds.setDriverClassName(dci.getDriverClass());
       ds.setUsername(dci.getUserName());
       ds.setPassword(dci.getPassword());
       ds.setUrl(dci.getUrl());
       // ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
       // ds.setAccessToUnderlyingConnectionAllowed(true);
       GSNContext.getMainContext().bind(Integer.toString(dci.hashCode()), ds);
       logger.warn("Created a DataSource to: " + ds.getUrl());
     }
   } catch (NamingException e) {
     logger.error(e.getMessage(), e);
   }
   return ds;
 }