@Test
 public void testDeleteConnections() {
   try {
     InMemoryConnectionServiceImpl serv = new InMemoryConnectionServiceImpl();
     List<IDatabaseConnection> conns = serv.getConnections();
     assertTrue(conns != null && conns.size() == 0);
     IDatabaseConnection connection = new DatabaseConnection();
     connection.setName("Connection 1");
     serv.addConnection(connection);
     connection = new DatabaseConnection();
     connection.setName("Connection 2");
     serv.addConnection(connection);
     conns = serv.getConnections();
     assertTrue(conns != null && conns.size() == 2);
     try {
       serv.deleteConnection(connection);
       connection = serv.getConnectionByName("Connection 2");
       fail();
     } catch (Exception ex) {
     }
     try {
       serv.deleteConnection("Connection 1");
       connection = serv.getConnectionByName("Connection 1");
       fail();
     } catch (Exception ex) {
     }
   } catch (Exception ex) {
     fail();
   }
 }
 @Test
 public void testAddConnections() {
   try {
     InMemoryConnectionServiceImpl serv = new InMemoryConnectionServiceImpl();
     List<IDatabaseConnection> conns = serv.getConnections();
     assertTrue(conns != null && conns.size() == 0);
     IDatabaseConnection connection = new DatabaseConnection();
     connection.setName("Connection 1");
     serv.addConnection(connection);
     connection = new DatabaseConnection();
     connection.setName("Connection 1");
     try {
       // validate adding connection with existing name
       serv.addConnection(connection);
       fail();
     } catch (ConnectionServiceException e) {
     }
     connection = new DatabaseConnection();
     connection.setName("Connection 2");
     serv.addConnection(connection);
     conns = serv.getConnections();
     assertTrue(conns != null && conns.size() > 0);
   } catch (Exception ex) {
     fail();
   }
 }
  private void afterOverlay(DataHandler dataHandler, IDatabaseType database) {
    XulTextbox portBox = (XulTextbox) document.getElementById("port-number-text"); // $NON-NLS-1$
    Object data = dataHandler.getData();
    String portValue = null;
    IDatabaseConnection databaseConnection = null;
    // Extract the stored value for port number in the model
    if (data instanceof IDatabaseConnection) {
      databaseConnection = (IDatabaseConnection) data;
      portValue = databaseConnection.getDatabasePort();
    }
    if (portBox != null) {
      // If the model has the port number use it other wise use the default one for the selected
      // database
      int port =
          (portValue != null && portValue.length() > 0)
              ? Integer.parseInt(portValue)
              : database.getDefaultDatabasePort();
      if (port > 0) {
        portBox.setValue(Integer.toString(port));
      }
    }

    if (dataHandler != null) {
      dataHandler.popCache();
    }

    GwtGroupBox box1 = (GwtGroupBox) document.getElementById("database-options-box");

    XulHbox box = (XulHbox) document.getElementById("connection-access-list-box");

    box1.layout();

    ((GwtHbox) box).layout();
  }
 private static IDatabaseConnection createConnectionObject() {
   IDatabaseConnection connection = new DatabaseConnection();
   connection.setAccessType(DatabaseAccessType.NATIVE);
   //    connection.setDriverClass("org.hsqldb.jdbcDriver");
   //    connection.setUrl("jdbc:hsqldb:hsql://localhost:9001/sampledata");
   connection.setUsername("pentaho_user");
   connection.setPassword("password");
   connection.setName(CONNECTION_NAME);
   return connection;
 }
  public static void main(String[] args) {
    IDatabaseConnection conn = getConnectionByName(CONNECTION_NAME);

    if (conn == null) add();
    else update();
    conn = getConnectionByName(CONNECTION_NAME);
    if (conn != null) {
      System.out.println("GetConn " + conn.getName());
      delete();
    }
  }
  @Override
  public String testConnection(IDatabaseConnection connection) {
    try {

      connection.setPassword(
          ConnectionServiceHelper.getConnectionPassword(
              connection.getName(), connection.getPassword()));
      return super.testConnection(connection);
    } catch (ConnectionServiceException e) {
      return super.testConnection(connection);
    }
  }
 @Override
 public String getURL(IDatabaseConnection connection) throws DatabaseDialectException {
   if (connection.getAccessType() == DatabaseAccessType.ODBC) {
     return "jdbc:odbc:" + connection.getDatabaseName();
   } else {
     return getNativeJdbcPre()
         + connection.getHostname()
         + ":"
         + connection.getDatabasePort()
         + "/"
         + connection.getDatabaseName();
   }
 }
 @Test
 public void testUpdateConnection() {
   try {
     InMemoryConnectionServiceImpl serv = new InMemoryConnectionServiceImpl();
     List<IDatabaseConnection> conns = serv.getConnections();
     assertTrue(conns != null && conns.size() == 0);
     IDatabaseConnection connection = new DatabaseConnection();
     connection.setName("Connection 1");
     connection.setUsername("admin");
     connection.setPassword("password");
     serv.addConnection(connection);
     conns = serv.getConnections();
     assertTrue(conns.get(0).getUsername().equals("admin"));
     assertTrue(conns.get(0).getPassword().equals("password"));
     connection = new DatabaseConnection();
     connection.setName("Connection 1");
     connection.setUsername("root");
     connection.setPassword("pass");
     serv.updateConnection(connection);
     List<IDatabaseConnection> conns1 = serv.getConnections();
     assertTrue(conns1 != null && conns1.size() > 0);
     assertTrue(conns1.size() == conns.size());
     assertTrue(conns1.get(0).getUsername().equals("root"));
     assertTrue(conns1.get(0).getPassword().equals("pass"));
   } catch (Exception ex) {
     fail();
   }
 }
 public static String getConnectionPassword(String connectionName, String password)
     throws ConnectionServiceException {
   try {
     IDatabaseConnection datasource = datasourceMgmtSvc.getDatasourceByName(connectionName);
     if (datasource != null && !hasPasswordChanged(password)) {
       return datasource.getPassword();
     } else {
       return password;
     }
   } catch (Exception e) {
     logger.error(
         Messages.getErrorString(
             "ConnectionServiceHelper.ERROR_0001_UNABLE_TO_GET_CONNECTION_PASSWORD", //$NON-NLS-1$
             connectionName,
             e.getLocalizedMessage()));
     throw new ConnectionServiceException(
         Messages.getErrorString(
             "ConnectionServiceHelper.ERROR_0001_UNABLE_TO_GET_CONNECTION_PASSWORD",
             connectionName,
             e.getLocalizedMessage()),
         e); //$NON-NLS-1$
   }
 }
 @Override
 public String getURL(IDatabaseConnection connection) throws DatabaseDialectException {
   // jdbc:informix-sqli://192.168.149.128:9088/stores:INFORMIXSERVER=demo_on
   if (connection.getAccessType() == DatabaseAccessType.ODBC) {
     return "jdbc:odbc:" + connection.getDatabaseName();
   } else {
     return "jdbc:informix-sqli://"
         + connection.getHostname()
         + ":"
         + connection.getDatabasePort()
         + "/"
         + connection.getDatabaseName()
         + ":INFORMIXSERVER="
         + connection.getInformixServername()
         + ";DELIMIDENT=Y";
   }
 }
  public boolean startup(final IPentahoSession session) {
    try {

      Logger.debug(this, "DatasourceSystemListener: called for startup ..."); // $NON-NLS-1$

      ICacheManager cacheManager = addCacheRegions();

      List<IDatabaseConnection> databaseConnections = getListOfDatabaseConnections(session);

      String dsName = "";
      DataSource ds = null;

      for (IDatabaseConnection databaseConnection : databaseConnections) {

        if (databaseConnection != null) {

          Logger.debug(this, "  Setting up datasource - " + databaseConnection); // $NON-NLS-1$

          // isPortUsedByServer should NOT be called on a JNDI data source
          // http://jira.pentaho.com/browse/BISERVER-12244
          if (!databaseConnection.getAccessType().equals(DatabaseAccessType.JNDI)) {
            // if connection's port used by server there is no sense to get DataSource for this
            ds =
                isPortUsedByServer(databaseConnection)
                    ? null
                    : setupDataSourceForConnection(databaseConnection);
          } else {
            Logger.debug(
                this,
                "(Datasource \""
                    + IDBDatasourceService.JDBC_DATASOURCE // $NON-NLS-1$
                    + dsName
                    + "\" not cached)"); //$NON-NLS-1$
            continue;
          }

          dsName = databaseConnection.getName();

          cacheManager.putInRegionCache(IDBDatasourceService.JDBC_DATASOURCE, dsName, ds);

          Logger.debug(
              this,
              "(Storing datasource under key \""
                  + IDBDatasourceService.JDBC_DATASOURCE // $NON-NLS-1$
                  + dsName
                  + "\")"); //$NON-NLS-1$
        }
      }

      Logger.debug(this, "DatasourceSystemListener: Completed startup."); // $NON-NLS-1$

      return true;

    } catch (ObjectFactoryException objface) {

      Logger.error(
          this,
          Messages.getInstance()
              .getErrorString("DatasourceSystemListener.ERROR_0001_UNABLE_TO_INSTANTIATE_OBJECT"),
          objface); //$NON-NLS-1$

      return false;

    } catch (DatasourceMgmtServiceException dmse) {

      Logger.error(
          this,
          Messages.getInstance()
              .getErrorString("DatasourceSystemListener.ERROR_0002_UNABLE_TO_GET_DATASOURCE"),
          dmse); //$NON-NLS-1$

      return false;
    }
  }
  @VisibleForTesting
  protected boolean isPortUsedByServer(IDatabaseConnection databaseConnection) {
    // get connection IP address
    String connectionHostName = databaseConnection.getHostname();
    InetAddress connectionAddress = null;
    try {
      connectionAddress = getAdressFromString(connectionHostName);
    } catch (UnknownHostException e) {
      Logger.warn(
          this,
          Messages.getInstance()
              .getErrorString(
                  "DatasourceSystemListener.WARN_0001_UNABLE_TO_GET_CONNECTION_ADDRESS"),
          e); //$NON-NLS-1$
      return false;
    }
    // get connection port
    String stringConnectionPort = databaseConnection.getDatabasePort();

    // get server URL
    String fullyQualifiedServerURL =
        PentahoSystem.getApplicationContext().getFullyQualifiedServerURL();
    URL url = null;
    try {
      url = new URL(fullyQualifiedServerURL);
    } catch (MalformedURLException e) {
      Logger.warn(
          this,
          Messages.getInstance()
              .getErrorString("DatasourceSystemListener.WARN_0002_UNABLE_TO_PARSE_SERVER_URL"),
          e); //$NON-NLS-1$
      return false;
    }
    // get server IP address
    String hostNameUsedByServer = url.getHost();
    InetAddress serverAddress = null;
    try {
      serverAddress = getAdressFromString(hostNameUsedByServer);
    } catch (UnknownHostException e) {
      Logger.warn(
          this,
          Messages.getInstance()
              .getErrorString("DatasourceSystemListener.WARN_0003_UNABLE_TO_GET_SERVER_ADDRESS"),
          e); //$NON-NLS-1$
      return false;
    }
    // get server port
    int portUsedByServer = url.getPort();

    boolean isAddressesEquals = connectionAddress.equals(serverAddress);

    boolean isPortsEquals = false;
    try {
      Integer connectionPort = Integer.valueOf(stringConnectionPort);
      isPortsEquals = connectionPort.equals(portUsedByServer);
    } catch (NumberFormatException e) {
      Logger.warn(
          this,
          Messages.getInstance()
              .getErrorString("DatasourceSystemListener.WARN_0004_UNABLE_TO_GET_PORT_NUMBER"),
          e); //$NON-NLS-1$
      return false;
    }

    return isAddressesEquals && isPortsEquals;
  }
 @Override
 public String getURL(IDatabaseConnection connection) throws DatabaseDialectException {
   return "jdbc:odbc:" + connection.getDatabaseName();
 }
  public void importFile(IPlatformImportBundle bundle)
      throws PlatformImportException, DomainIdNullException, DomainAlreadyExistsException,
          DomainStorageException, IOException {

    RepositoryFileImportBundle importBundle = (RepositoryFileImportBundle) bundle;
    ZipInputStream zipImportStream = new ZipInputStream(bundle.getInputStream());
    SolutionRepositoryImportSource importSource =
        new SolutionRepositoryImportSource(zipImportStream);
    LocaleFilesProcessor localeFilesProcessor = new LocaleFilesProcessor();
    setOverwriteFile(bundle.overwriteInRepository());
    // importSession.set(ImportSession.getSession());

    IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);

    cachedImports = new HashMap<String, RepositoryFileImportBundle.Builder>();

    // Process Manifest Settings
    ExportManifest manifest = getImportSession().getManifest();
    String manifestVersion = null;
    if (manifest != null) {
      manifestVersion = manifest.getManifestInformation().getManifestVersion();
    }
    // Process Metadata
    if (manifest != null) {

      // import the users
      Map<String, List<String>> roleToUserMap = importUsers(manifest.getUserExports());
      // import the roles
      importRoles(manifest.getRoleExports(), roleToUserMap);

      List<ExportManifestMetadata> metadataList = manifest.getMetadataList();
      for (ExportManifestMetadata exportManifestMetadata : metadataList) {

        String domainId = exportManifestMetadata.getDomainId();
        boolean overWriteInRepository = isOverwriteFile();
        RepositoryFileImportBundle.Builder bundleBuilder =
            new RepositoryFileImportBundle.Builder()
                .charSet("UTF-8")
                .hidden(false)
                // let the parent bundle control whether or not to preserve DSW settings
                .preserveDsw(bundle.isPreserveDsw())
                .overwriteFile(overWriteInRepository)
                .mime("text/xmi+xml")
                .withParam("domain-id", domainId);

        cachedImports.put(exportManifestMetadata.getFile(), bundleBuilder);
      }

      // Process Mondrian
      List<ExportManifestMondrian> mondrianList = manifest.getMondrianList();
      for (ExportManifestMondrian exportManifestMondrian : mondrianList) {

        String catName = exportManifestMondrian.getCatalogName();
        Parameters parametersMap = exportManifestMondrian.getParameters();
        StringBuilder parametersStr = new StringBuilder();
        for (String s : parametersMap.keySet()) {
          parametersStr.append(s).append("=").append(parametersMap.get(s)).append(sep);
        }

        RepositoryFileImportBundle.Builder bundleBuilder =
            new RepositoryFileImportBundle.Builder()
                .charSet("UTF_8")
                .hidden(false)
                .name(catName)
                .overwriteFile(isOverwriteFile())
                .mime("application/vnd.pentaho.mondrian+xml")
                .withParam("parameters", parametersStr.toString())
                .withParam("domain-id", catName); // TODO: this is
        // definitely
        // named wrong
        // at the very
        // least.
        // pass as param if not in parameters string
        String xmlaEnabled = "" + exportManifestMondrian.isXmlaEnabled();
        bundleBuilder.withParam("EnableXmla", xmlaEnabled);

        cachedImports.put(exportManifestMondrian.getFile(), bundleBuilder);

        String annotationsFile = exportManifestMondrian.getAnnotationsFile();
        if (annotationsFile != null) {
          RepositoryFileImportBundle.Builder annotationsBundle =
              new RepositoryFileImportBundle.Builder()
                  .path(
                      MondrianCatalogRepositoryHelper.ETC_MONDRIAN_JCR_FOLDER
                          + RepositoryFile.SEPARATOR
                          + catName)
                  .name("annotations.xml")
                  .charSet("UTF_8")
                  .overwriteFile(isOverwriteFile())
                  .mime("text/xml")
                  .hidden(false)
                  .withParam("domain-id", catName);
          cachedImports.put(annotationsFile, annotationsBundle);
        }
      }
    }

    importMetaStore(manifest, bundle.overwriteInRepository());

    for (IRepositoryFileBundle file : importSource.getFiles()) {
      String fileName = file.getFile().getName();
      String actualFilePath = file.getPath();
      if (manifestVersion != null) {
        fileName = ExportFileNameEncoder.decodeZipFileName(fileName);
        actualFilePath = ExportFileNameEncoder.decodeZipFileName(actualFilePath);
      }
      String repositoryFilePath =
          RepositoryFilenameUtils.concat(
              PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);

      if (this.cachedImports.containsKey(repositoryFilePath)) {

        byte[] bytes = IOUtils.toByteArray(file.getInputStream());
        RepositoryFileImportBundle.Builder builder = cachedImports.get(repositoryFilePath);
        builder.input(new ByteArrayInputStream(bytes));

        importer.importFile(build(builder));
        continue;
      }
      RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();

      InputStream bundleInputStream = null;

      String decodedFilePath = file.getPath();
      RepositoryFile decodedFile = file.getFile();
      if (manifestVersion != null) {
        decodedFile =
            new RepositoryFile.Builder(decodedFile)
                .path(decodedFilePath)
                .name(fileName)
                .title(fileName)
                .build();
        decodedFilePath = ExportFileNameEncoder.decodeZipFileName(file.getPath());
      }

      if (file.getFile().isFolder()) {
        bundleBuilder.mime("text/directory");
        bundleBuilder.file(decodedFile);
        fileName = repositoryFilePath;
        repositoryFilePath = importBundle.getPath();
      } else {
        byte[] bytes = IOUtils.toByteArray(file.getInputStream());
        bundleInputStream = new ByteArrayInputStream(bytes);
        // If is locale file store it for later processing.
        if (localeFilesProcessor.isLocaleFile(file, importBundle.getPath(), bytes)) {
          log.trace("Skipping [" + repositoryFilePath + "], it is a locale property file");
          continue;
        }
        bundleBuilder.input(bundleInputStream);
        bundleBuilder.mime(solutionHelper.getMime(fileName));

        String filePath =
            (decodedFilePath.equals("/") || decodedFilePath.equals("\\")) ? "" : decodedFilePath;
        repositoryFilePath = RepositoryFilenameUtils.concat(importBundle.getPath(), filePath);
      }

      bundleBuilder.name(fileName);
      bundleBuilder.path(repositoryFilePath);

      String sourcePath;
      if (decodedFilePath.startsWith("/")) {
        sourcePath = RepositoryFilenameUtils.concat(decodedFilePath.substring(1), fileName);
      } else {
        if (file.getFile().isFolder()) {
          sourcePath = fileName;
        } else {
          sourcePath = RepositoryFilenameUtils.concat(decodedFilePath, fileName);
        }
      }

      // This clause was added for processing ivb files so that it would not try process acls on
      // folders that the user
      // may not have rights to such as /home or /public
      if (manifest != null
          && manifest.getExportManifestEntity(sourcePath) == null
          && file.getFile().isFolder()) {
        continue;
      }

      getImportSession().setCurrentManifestKey(sourcePath);

      bundleBuilder.charSet(bundle.getCharset());
      bundleBuilder.overwriteFile(bundle.overwriteInRepository());
      bundleBuilder.hidden(isFileHidden(bundle, sourcePath));
      bundleBuilder.applyAclSettings(bundle.isApplyAclSettings());
      bundleBuilder.retainOwnership(bundle.isRetainOwnership());
      bundleBuilder.overwriteAclSettings(bundle.isOverwriteAclSettings());
      bundleBuilder.acl(getImportSession().processAclForFile(sourcePath));
      IPlatformImportBundle platformImportBundle = build(bundleBuilder);
      importer.importFile(platformImportBundle);

      if (bundleInputStream != null) {
        bundleInputStream.close();
        bundleInputStream = null;
      }
    }
    if (manifest != null) {
      List<JobScheduleRequest> scheduleList = manifest.getScheduleList();
      if (scheduleList != null) {
        SchedulerResource schedulerResource = new SchedulerResource();
        for (JobScheduleRequest jobScheduleRequest : scheduleList) {
          try {
            Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
            if (response.getStatus() == Response.Status.OK.getStatusCode()) {
              if (response.getEntity() != null) {
                // get the schedule job id from the response and add it to the import session
                ImportSession.getSession()
                    .addImportedScheduleJobId(response.getEntity().toString());
              }
            }
          } catch (Exception e) {
            // there is a scenario where if the file scheduled has a space in the path, that it
            // won't work. the di server
            // replaces spaces with underscores and the export mechanism can't determine if it needs
            // this to happen or not
            // so, if we failed to import and there is a space in the path, try again but this time
            // with replacing the space(s)
            if (jobScheduleRequest.getInputFile().contains(" ")
                || jobScheduleRequest.getOutputFile().contains(" ")) {
              log.info(
                  "Could not import schedule, attempting to replace spaces with underscores and retrying: "
                      + jobScheduleRequest.getInputFile());

              jobScheduleRequest.setInputFile(
                  jobScheduleRequest.getInputFile().replaceAll(" ", "_"));
              jobScheduleRequest.setOutputFile(
                  jobScheduleRequest.getOutputFile().replaceAll(" ", "_"));
              try {
                Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
                if (response.getStatus() == Response.Status.OK.getStatusCode()) {
                  if (response.getEntity() != null) {
                    // get the schedule job id from the response and add it to the import session
                    ImportSession.getSession()
                        .addImportedScheduleJobId(response.getEntity().toString());
                  }
                }
              } catch (Exception ex) {
                throw new PlatformImportException(
                    Messages.getInstance()
                        .getString(
                            "SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE",
                            e.getMessage()));
              }
            } else {
              throw new PlatformImportException(
                  Messages.getInstance()
                      .getString(
                          "SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE",
                          e.getMessage()));
            }
          }
        }
      }

      // Add Pentaho Connections
      List<org.pentaho.database.model.DatabaseConnection> datasourceList =
          manifest.getDatasourceList();
      if (datasourceList != null) {
        IDatasourceMgmtService datasourceMgmtSvc = PentahoSystem.get(IDatasourceMgmtService.class);
        for (org.pentaho.database.model.DatabaseConnection databaseConnection : datasourceList) {
          if (databaseConnection.getDatabaseType() == null) {
            // don't try to import the connection if there is no type it will cause an error
            // However, if this is the DI Server, and the connection is defined in a ktr, it will
            // import automatically
            log.warn(
                "Can't import connection "
                    + databaseConnection.getName()
                    + " because it doesn't have a databaseType");
            continue;
          }
          try {
            IDatabaseConnection existingDBConnection =
                datasourceMgmtSvc.getDatasourceByName(databaseConnection.getName());
            if (existingDBConnection != null && existingDBConnection.getName() != null) {
              if (isOverwriteFile()) {
                databaseConnection.setId(existingDBConnection.getId());
                datasourceMgmtSvc.updateDatasourceByName(
                    databaseConnection.getName(), databaseConnection);
              }
            } else {
              datasourceMgmtSvc.createDatasource(databaseConnection);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    }
    // Process locale files.
    localeFilesProcessor.processLocaleFiles(importer);
  }