Ejemplo n.º 1
0
  public boolean init(StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface) {
    if (super.init(stepMetaInterface, stepDataInterface)) {
      m_meta = (MongoDbOutputMeta) stepMetaInterface;
      m_data = (MongoDbOutputData) stepDataInterface;

      String hostname = environmentSubstitute(m_meta.getHostname());
      int port = Const.toInt(environmentSubstitute(m_meta.getPort()), 27017);
      String db = environmentSubstitute(m_meta.getDBName());
      String collection = environmentSubstitute(m_meta.getCollection());

      try {

        m_data.connect(hostname, port);
        m_data.setDB(m_data.getConnection().getDB(db));

        String realUser = environmentSubstitute(m_meta.getUsername());
        String realPass =
            Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(m_meta.getPassword()));

        if (!Const.isEmpty(realUser) || !Const.isEmpty(realPass)) {
          if (!m_data.getDB().authenticate(realUser, realPass.toCharArray())) {
            throw new KettleException(
                BaseMessages.getString(PKG, "MongoDbOutput.Messages.Error.UnableToAuthenticate"));
          }
        }

        if (Const.isEmpty(collection)) {
          throw new KettleException(
              BaseMessages.getString(PKG, "MongoDbOutput.Messages.Error.NoCollectionSpecified"));
        }
        m_data.createCollection(collection);
        m_data.setCollection(m_data.getDB().getCollection(collection));

        return true;
      } catch (UnknownHostException ex) {
        logError(
            BaseMessages.getString(PKG, "MongoDbOutput.Messages.Error.UnknownHost", hostname), ex);
        return false;
      } catch (Exception e) {
        logError(
            BaseMessages.getString(
                PKG, "MongoDbOutput.Messages.Error.ProblemConnecting", hostname, "" + port),
            e);
        return false;
      }
    }

    return false;
  }