Example #1
0
  /**
   * Syncs replication data between two OpenDJ sms and user stores. $ dsreplication initialize
   * --baseDN "dc=example,dc=com" --adminUID admin --adminPassword pass --hostSource host1
   * --portSource 1389 --hostDestination host2 --portDestination 2389 --trustAll
   *
   * @param map Map of properties collected by the configurator.
   * @return status : 0 == success, !0 == failure
   */
  public static int setupReplicationInitialize(Map map) {
    String[] initializeCmd = {
      "initialize", // 0
      "--no-prompt", // 1
      "--baseDN", // 2
      Constants.DEFAULT_ROOT_SUFFIX, // 3 Placeholder
      "--adminUID", // 4
      "admin", // 5
      "--adminPassword", // 6
      "xxxxxxxx", // 7
      "--hostSource", // 8
      "localhost", // 9
      "--portSource", // 10
      "50389", // 11
      "--hostDestination", // 12
      "localhost", // 13
      "--portDestination", // 14
      "51389", // 15
      "--trustAll", // 16
      "--configFile", // 17
      "path/to/config.ldif" // 18
    };
    initializeCmd[3] = (String) map.get(SetupConstants.CONFIG_VAR_ROOT_SUFFIX);
    initializeCmd[9] = (String) map.get(SetupConstants.DS_EMB_REPL_HOST2);
    initializeCmd[11] = (String) map.get(SetupConstants.DS_EMB_REPL_ADMINPORT2);
    initializeCmd[13] = getOpenDJHostName(map);
    initializeCmd[15] = (String) map.get(SetupConstants.CONFIG_VAR_DIRECTORY_ADMIN_SERVER_PORT);
    initializeCmd[18] = getOpenDJConfigFile(map);

    Object[] params = {concat(initializeCmd)};
    SetupProgress.reportStart("emb.replcommand", params);

    initializeCmd[7] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_PWD);
    int ret =
        ReplicationCliMain.mainCLI(
            initializeCmd,
            false,
            SetupProgress.getOutputStream(),
            SetupProgress.getOutputStream(),
            null);

    if (ret == 0) {
      SetupProgress.reportEnd("emb.success", null);
    } else {
      SetupProgress.reportEnd("emb.failed", null);
    }
    return ret;
  }
Example #2
0
 public static void setupReplication(Map map) throws Exception {
   // Setup replication
   SetupProgress.reportStart("emb.creatingreplica", null);
   int ret = setupReplicationEnable(map);
   if (ret == 0) {
     ret = setupReplicationInitialize(map);
     SetupProgress.reportEnd("emb.success", null);
     Debug.getInstance(SetupConstants.DEBUG_NAME)
         .message("EmbeddedOpenDS.setupReplication: replication setup succeeded.");
   } else {
     Object[] params = {Integer.toString(ret)};
     SetupProgress.reportEnd("emb.failed.param", params);
     Debug.getInstance(SetupConstants.DEBUG_NAME)
         .error("EmbeddedOpenDS.setupReplication. Error setting up replication");
     throw new ConfiguratorException("configurator.embreplfailed");
   }
 }
Example #3
0
  /**
   * Starts the embedded <code>OpenDJ</code> instance.
   *
   * @param odsRoot File system directory where <code>OpenDJ</code> is installed.
   * @throws Exception upon encountering errors.
   */
  public static void startServer(String odsRoot) throws Exception {
    if (isStarted()) {
      return;
    }
    Debug debug = Debug.getInstance(SetupConstants.DEBUG_NAME);
    debug.message("EmbeddedOpenDS.startServer(" + odsRoot + ")");

    DirectoryEnvironmentConfig config = new DirectoryEnvironmentConfig();
    config.setServerRoot(new File(odsRoot));
    config.setForceDaemonThreads(true);
    config.setConfigClass(ConfigFileHandler.class);
    config.setConfigFile(new File(odsRoot + "/config", "config.ldif"));
    debug.message("EmbeddedOpenDS.startServer:starting DS Server...");
    EmbeddedUtils.startServer(config);
    debug.message("...EmbeddedOpenDS.startServer:DS Server started.");

    int sleepcount = 0;
    while (!EmbeddedUtils.isRunning() && (sleepcount < 60)) {
      sleepcount++;
      SetupProgress.reportStart("emb.waitingforstarted", null);
      Thread.sleep(1000);
    }

    if (EmbeddedUtils.isRunning()) {
      SetupProgress.reportEnd("emb.success", null);
    } else {
      SetupProgress.reportEnd("emb.failed", null);
    }

    serverStarted = true;

    ShutdownManager shutdownMan = com.sun.identity.common.ShutdownManager.getInstance();

    shutdownMan.addShutdownListener(
        new ShutdownListener() {
          public void shutdown() {
            try {
              shutdownServer("Graceful Shutdown");
            } catch (Exception ex) {
              Debug debug = Debug.getInstance(SetupConstants.DEBUG_NAME);
              debug.error("EmbeddedOpenDS:shutdown hook failed", ex);
            }
          }
        },
        ShutdownPriority.LOWEST);
  }
Example #4
0
  /**
   * Runs the OpenDJ setup command like this: $ ./setup --cli --adminConnectorPort 4444 --baseDN
   * dc=openam,dc=forgerock,dc=org --rootUserDN "cn=directory manager" --doNotStart --ldapPort 50389
   * --skipPortCheck --rootUserPassword xxxxxxx --jmxPort 1689 --no-prompt
   *
   * @param map Map of properties collected by the configurator.
   * @return status : 0 == success, !0 == failure
   */
  public static int runOpenDSSetup(Map map) {
    String[] setupCmd = {
      "--cli", // 0
      "--adminConnectorPort", // 1
      "4444", // 2
      "--baseDN", // 3
      Constants.DEFAULT_ROOT_SUFFIX, // 4
      "--rootUserDN", // 5
      "cn=Directory Manager", // 6
      "--ldapPort", // 7
      "50389", // 8
      "--skipPortCheck", // 9
      "--rootUserPassword", // 10
      "xxxxxxx", // 11
      "--jmxPort", // 12
      "1689", // 13
      "--no-prompt", // 14
      "--doNotStart", // 15
      "--hostname", // 16
      "hostname" // 17
    };

    setupCmd[2] = (String) map.get(SetupConstants.CONFIG_VAR_DIRECTORY_ADMIN_SERVER_PORT);
    setupCmd[4] = (String) map.get(SetupConstants.CONFIG_VAR_ROOT_SUFFIX);
    setupCmd[6] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_DN);
    setupCmd[8] = (String) map.get(SetupConstants.CONFIG_VAR_DIRECTORY_SERVER_PORT);
    setupCmd[13] = (String) map.get(SetupConstants.CONFIG_VAR_DIRECTORY_JMX_SERVER_PORT);
    setupCmd[17] = getOpenDJHostName(map);

    Object[] params = {concat(setupCmd)};
    SetupProgress.reportStart("emb.setupcommand", params);

    setupCmd[11] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_PWD);

    int ret =
        InstallDS.mainCLI(
            setupCmd, SetupProgress.getOutputStream(), SetupProgress.getOutputStream(), null);

    if (ret == 0) {
      SetupProgress.reportEnd("emb.success", null);
    } else {
      SetupProgress.reportEnd("emb.failed", null);
    }

    return ret;
  }
Example #5
0
  /**
   * Runs the OpenDJ setup command to create our instance
   *
   * @param map The map of configuration options
   * @throws Exception upon encountering errors.
   */
  public static void setupOpenDS(Map map) throws Exception {
    SetupProgress.reportStart("emb.setupopends", null);

    int ret = runOpenDSSetup(map);

    if (ret == 0) {
      SetupProgress.reportEnd("emb.setupopends.success", null);
      Debug.getInstance(SetupConstants.DEBUG_NAME)
          .message("EmbeddedOpenDS.setupOpenDS: OpenDS setup succeeded.");
    } else {
      Object[] params = {Integer.toString(ret)};
      SetupProgress.reportEnd("emb.setupopends.failed.param", params);
      Debug.getInstance(SetupConstants.DEBUG_NAME)
          .error("EmbeddedOpenDS.setupOpenDS. Error setting up OpenDS");
      throw new ConfiguratorException("configurator.embsetupopendsfailed");
    }
  }
Example #6
0
  /**
   * Utility function to preload data in the embedded instance. Must be called when the directory
   * instance is shutdown.
   *
   * @param odsRoot Local directory where <code>OpenDJ</code> is installed.
   * @param ldif Full path of the ldif file to be loaded.
   */
  public static int loadLDIF(Map map, String odsRoot, String ldif) {
    int ret = 0;

    Debug debug = Debug.getInstance(SetupConstants.DEBUG_NAME);
    File ldifFile = new File(ldif);
    if (!ldifFile.exists()) {
      debug.error("LDIF File:" + ldifFile.getAbsolutePath() + " does not exist, unable to load!");
      return -1;
    }
    try {
      if (debug.messageEnabled()) {
        debug.message("EmbeddedOpenDS:loadLDIF(" + ldif + ")");
      }

      String[] args1 = {
        "-C", // 0
        "org.opends.server.extensions.ConfigFileHandler", // 1
        "-f", // 2
        odsRoot + "/config/config.ldif", // 3
        "-n", // 4
        "userRoot", // 5
        "-l", // 6
        ldif, // 7
        "--trustAll", // 8
        "-D", // 9
        "cn=Directory Manager", // 10
        "-w", // 11
        "password" // 12
      };
      args1[10] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_DN);
      args1[12] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_PWD);
      ret =
          org.opends.server.tools.ImportLDIF.mainImportLDIF(
              args1, false, SetupProgress.getOutputStream(), SetupProgress.getOutputStream());

      if (debug.messageEnabled()) {
        debug.message("EmbeddedOpenDS:loadLDIF Success");
      }
    } catch (Exception ex) {
      debug.error("EmbeddedOpenDS:loadLDIF:ex=", ex);
    }

    return ret;
  }
Example #7
0
  /**
   * Setups replication between two OpenDJ sms and user stores. $ dsreplication enable --no-prompt
   * --host1 host1 --port1 1389 --bindDN1 "cn=Directory Manager" --bindPassword1 password
   * --replicationPort1 8989 --host2 host2 --port2 2389 --bindDN2 "cn=Directory Manager"
   * --bindPassword2 password --replicationPort2 8990 --adminUID admin --adminPassword password
   * --baseDN "dc=example,dc=com"
   *
   * @param map Map of properties collected by the configurator.
   * @return status : 0 == success, !0 == failure
   */
  public static int setupReplicationEnable(Map map) {
    String[] enableCmd = {
      "enable", // 0
      "--no-prompt", // 1
      "--host1", // 2
      "host1val", // 3
      "--port1", // 4
      "port1ival", // 5
      "--bindDN1", // 6
      "cn=Directory Manager", // 7
      "--bindPassword1", // 8
      "xxxxxxxx", // 9
      "--replicationPort1", // 10
      "8989", // 11
      "--host2", // 12
      "host2val", // 13
      "--port2", // 14
      "port2ival", // 15
      "--bindDN2", // 16
      "cn=Directory Manager", // 17
      "--bindPassword2", // 18
      "xxxxxxxx", // 19
      "--replicationPort2", // 20
      "8989", // 21
      "--adminUID", // 22
      "admin", // 23
      "--adminPassword", // 24
      "xxxxxxxx", // 25
      "--baseDN", // 26
      "dc=example,dc=com", // 27
      "--trustAll", // 28
      "--configFile", // 29
      "path/to/config.ldif" // 30
    };
    enableCmd[3] = (String) map.get(SetupConstants.DS_EMB_REPL_HOST2);
    enableCmd[5] = (String) map.get(SetupConstants.DS_EMB_REPL_ADMINPORT2);
    enableCmd[11] = (String) map.get(SetupConstants.DS_EMB_REPL_REPLPORT2);
    enableCmd[13] = getOpenDJHostName(map);
    enableCmd[15] = (String) map.get(SetupConstants.CONFIG_VAR_DIRECTORY_ADMIN_SERVER_PORT);
    enableCmd[21] = (String) map.get(SetupConstants.DS_EMB_REPL_REPLPORT1);
    enableCmd[27] = (String) map.get(SetupConstants.CONFIG_VAR_ROOT_SUFFIX);
    enableCmd[30] = getOpenDJConfigFile(map);

    Object[] params = {concat(enableCmd)};
    SetupProgress.reportStart("emb.replcommand", params);

    enableCmd[9] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_PWD);
    enableCmd[19] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_PWD);
    enableCmd[25] = (String) map.get(SetupConstants.CONFIG_VAR_DS_MGR_PWD);

    Debug debug = Debug.getInstance(SetupConstants.DEBUG_NAME);
    if (debug.messageEnabled()) {
      debug.message("EmbeddedOpenDS.setupReplicationEnable: " + "Host 1 " + enableCmd[3]);
      debug.message("EmbeddedOpenDS.setupReplicationEnable: " + "Host 2 " + enableCmd[13]);
      debug.message("EmbeddedOpenDS.setupReplicationEnable: " + "Port 1 " + enableCmd[5]);
      debug.message("EmbeddedOpenDS.setupReplicationEnable: " + "Port 2 " + enableCmd[15]);
    }
    int ret =
        ReplicationCliMain.mainCLI(
            enableCmd,
            false,
            SetupProgress.getOutputStream(),
            SetupProgress.getOutputStream(),
            null);

    if (ret == 0) {
      SetupProgress.reportEnd("emb.success", null);
    } else {
      SetupProgress.reportEnd("emb.failed", null);
    }
    return ret;
  }
Example #8
0
  /**
   * Sets up embedded OpenDJ during initial installation :
   *
   * <ul>
   *   <li>lays out the filesystem directory structure needed by OpenDJ
   *   <li>sets up port numbers for ldap and replication
   *   <li>invokes <code>EmbeddedUtils</code> to start the embedded server.
   * </ul>
   *
   * @param map Map of properties collected by the configurator.
   * @param servletCtx Servlet Context to read deployed war contents.
   * @throws Exception on encountering errors.
   */
  public static void setup(Map map, ServletContext servletCtx) throws Exception {
    // Determine Cipher to be used
    SetupProgress.reportStart("emb.installingemb.null", null);
    String xform = getSupportedTransformation();

    if (xform == null) {
      SetupProgress.reportEnd("emb.noxform", null);
      throw new Exception("No transformation found");
    } else {
      map.put(OPENDS_TRANSFORMATION, xform);
      Object[] params = {xform};
      SetupProgress.reportEnd("emb.success.param", params);
    }

    String basedir = (String) map.get(SetupConstants.CONFIG_VAR_BASE_DIR);
    String odsRoot = getOpenDJBaseDir(map);
    new File(basedir).mkdir();
    new File(odsRoot).mkdir();

    SetupProgress.reportStart("emb.opends.start", null);
    String zipFileName = "/WEB-INF/template/opendj/opendj.zip";
    BufferedInputStream bin =
        new BufferedInputStream(AMSetupServlet.getResourceAsStream(servletCtx, zipFileName), 10000);
    BufferedOutputStream bout =
        new BufferedOutputStream(new FileOutputStream(odsRoot + "/opendj.zip"), 10000);

    try {
      while (bin.available() > 0) {
        bout.write(bin.read());
      }
    } catch (IOException ioe) {
      Debug.getInstance(SetupConstants.DEBUG_NAME)
          .error("EmbeddedOpenDS.setup(): Error copying zip file", ioe);
      throw ioe;
    } finally {
      IOUtils.closeIfNotNull(bin);
      IOUtils.closeIfNotNull(bout);
    }

    ZipFile opendsZip = new ZipFile(odsRoot + "/opendj.zip");
    Enumeration files = opendsZip.entries();
    // Process the OpenDJ Archive File.
    while (files.hasMoreElements()) {
      ZipEntry file = (ZipEntry) files.nextElement();
      File f = new File(odsRoot + "/" + file.getName());

      if (file.isDirectory()) {
        f.mkdir();
        continue;
      }

      BufferedInputStream is = new BufferedInputStream(opendsZip.getInputStream(file), 10000);
      BufferedOutputStream fos = new BufferedOutputStream(new java.io.FileOutputStream(f), 10000);

      try {
        while (is.available() > 0) {
          fos.write(is.read());
        }
      } catch (IOException ioe) {
        Debug.getInstance(SetupConstants.DEBUG_NAME)
            .error("EmbeddedOpenDS.setup(): Error loading ldifs", ioe);
        throw ioe;
      } finally {
        IOUtils.closeIfNotNull(is);
        IOUtils.closeIfNotNull(fos);
      } // End of Inner Finally.

      if (file.getName().endsWith("sh") || file.getName().startsWith("bin")) {
        f.setExecutable(true);
      }
    } // End of File Elements from Zip for OpenDJ.

    // create tag swapped files
    String[] tagSwapFiles = {"template/ldif/openam_suffix.ldif.template"};

    for (int i = 0; i < tagSwapFiles.length; i++) {
      String fileIn = odsRoot + "/" + tagSwapFiles[i];
      FileReader fin = new FileReader(fileIn);

      StringBuilder sbuf = new StringBuilder();
      char[] cbuf = new char[1024];
      int len;
      while ((len = fin.read(cbuf)) > 0) {
        sbuf.append(cbuf, 0, len);
      }
      FileWriter fout = null;

      try {
        fout =
            new FileWriter(
                odsRoot + "/" + tagSwapFiles[i].substring(0, tagSwapFiles[i].indexOf(".template")));
        String inpStr = sbuf.toString();
        fout.write(ServicesDefaultValues.tagSwap(inpStr));
      } catch (IOException e) {
        Debug.getInstance(SetupConstants.DEBUG_NAME)
            .error("EmbeddedOpenDS.setup(): Error tag swapping files", e);
        throw e;
      } finally {
        IOUtils.closeIfNotNull(fin);
        IOUtils.closeIfNotNull(fout);
      }
    }

    // remove zip
    File toDelete = new File(odsRoot + "/opendj.zip");
    if (!toDelete.delete()) {
      Debug.getInstance(SetupConstants.DEBUG_NAME)
          .error("EmbeddedOpenDS.setup(): Unable to delete zip file:" + toDelete.getAbsolutePath());
    }

    SetupProgress.reportEnd("emb.opends.stop", null);

    // now setup OpenDJ
    System.setProperty("org.opends.quicksetup.Root", odsRoot);
    System.setProperty(ServerConstants.PROPERTY_SERVER_ROOT, odsRoot);
    System.setProperty(ServerConstants.PROPERTY_INSTANCE_ROOT, odsRoot);
    EmbeddedOpenDS.setupOpenDS(map);

    Object[] params = {odsRoot};
    SetupProgress.reportStart("emb.installingemb", params);
    EmbeddedOpenDS.startServer(odsRoot);

    // Check: If adding a new server to a existing cluster
    if (!isMultiServer(map)) {
      // Default: single / first server.
      SetupProgress.reportStart("emb.creatingfamsuffix", null);
      int ret = EmbeddedOpenDS.loadLDIF(map, odsRoot, odsRoot + "/ldif/openam_suffix.ldif");

      if (ret == 0) {
        SetupProgress.reportEnd("emb.creatingfamsuffix.success", null);
      } else {
        Object[] error = {Integer.toString(ret)};
        SetupProgress.reportEnd("emb.creatingfamsuffix.failure", error);
        Debug.getInstance(SetupConstants.DEBUG_NAME)
            .error("EmbeddedOpenDS.setupOpenDS. Error loading OpenAM suffix");
        throw new ConfiguratorException("emb.creatingfamsuffix.failure");
      }
    } // End of single / first server check.
  }