EmbeddedOpenDS.java revision 8bb3e4cda81612f01c0254c263155f65506a018c
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: EmbeddedOpenDS.java,v 1.27 2010/01/15 01:22:39 goodearth Exp $
*
*/
/*
* Portions Copyrighted 2010-2012 ForgeRock AS
*/
// OpenDS does not have APIs to install and setup replication yet
/**
* This class encapsulates all <code>OpenDS</code> dependencies.
* All the interfaces are invoked from <code>AMSetupServlet</code> class
* at different points : initial installation, normal startup and
* normal shutdown of the embedded <code>OpenDS</code> instance.
*/
public class EmbeddedOpenDS {
private static boolean serverStarted = false;
/**
* Returns <code>true</code> if the server has already been started.
*
* @return <code>true</code> if the server has already been started.
*/
public static boolean isStarted() {
return serverStarted;
}
/**
* Sets up embedded opends during initial installation :
* <ul>
* <li>lays out the filesystem directory structure needed by opends
* <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.
*/
throws Exception {
// Determine Cipher to be used
throw new Exception("No transformation found");
} else {
}
try {
}
} catch (IOException ioe) {
"EmbeddedOpenDS.setup(): Error copying zip file", ioe);
throw ioe;
} finally {
try {
//No handling requried
}
}
try {
//No handling requried
}
}
}
while (files.hasMoreElements()) {
if (file.isDirectory()) {
f.mkdir();
continue;
}
try {
}
} catch (IOException ioe) {
"EmbeddedOpenDS.setup(): Error loading ldifs", ioe);
throw ioe;
} finally {
try {
//No handling requried
}
}
try {
//No handling requried
}
}
}
f.setExecutable(true);
}
}
// copy OpenDJ jar file
// TODO Make this Dynamic, so we can eliminate versions on Jars.
String[] opendsJarFiles = {
"OpenDJ-2012-20-02.jar", // Was OpenDJ.jar before Maven Support.
"sleepycat-je-2011-04-07.jar", // Was je.jar before Maven Support.
"mail-1.4.5.jar" // Was mail.jar before Maven Support.
};
"OpenDJ-2012-20-02.jar", // Since OpenDJ seems to need je.jar by name
"je.jar",
"mail-1.4.5.jar"
};
FileChannel outChannel = new FileOutputStream(odsRoot + "/lib/" + NewOpendsJarFiles[i]).getChannel();
try {
} catch (IOException ioe) {
"EmbeddedOpenDS.setup(): Error copying zip file", ioe);
throw ioe;
} finally {
try {
//No handling requried
}
}
if (outChannel != null) {
try {
outChannel.close();
//No handling requried
}
}
}
}
/*
for (int i = 0 ; i < opendsJarFiles.length; i++) {
BufferedInputStream jin = new BufferedInputStream(
AMSetupServlet.getResourceAsStream(servletCtx, jarFileName), 10000);
BufferedOutputStream jout = new BufferedOutputStream(
new FileOutputStream(odsRoot + "/lib/" + opendsJarFiles[i]), 10000);
try {
while (jin.available() > 0) {
jout.write(jin.read());
}
} catch (IOException ioe) {
Debug.getInstance(SetupConstants.DEBUG_NAME).error(
"EmbeddedOpenDS.setup(): Error copying zip file", ioe);
throw ioe;
} finally {
if (jin != null) {
try {
jin.close();
} catch (Exception ex) {
//No handling requried
}
}
if (jout != null) {
try {
jout.close();
} catch (Exception ex) {
//No handling requried
}
}
}
}*/
// create tag swapped files
String[] tagSwapFiles = {
};
char[] cbuf = new char[1024];
int len;
}
try {
} catch (IOException e) {
"EmbeddedOpenDS.setup(): Error tag swapping files", e);
throw e;
} finally {
try {
//No handling requried
}
}
try {
//No handling requried
}
}
}
}
// remove zip
"EmbeddedOpenDS.setup(): Unable to delete zip file");
}
// now setup OpenDS
// Check: If adding a new server to a existing cluster
if (!isMultiServer(map)) {
// Default: single / first server.
//EmbeddedOpenDS.shutdownServer("to load ldif");
if (ret == 0) {
} else {
"EmbeddedOpenDS.setupOpenDS. Error loading OpenAM suffix");
throw new ConfiguratorException(
"emb.creatingfamsuffix.failure");
}
//EmbeddedOpenDS.startServer(odsRoot);
}
}
throws IOException {
}
while (buffer.hasRemaining()) {
}
}
/**
* Preferred transforms
*/
final static String[] preferredTransforms =
{
"RSA/ECB/OAEPWithSHA1AndMGF1Padding", // Sun JCE
"RSA/ /OAEPPADDINGSHA-1", // IBMJCE
"RSA/ECB/OAEPWithSHA-1AndMGF-1Padding", // BouncyCastle
"RSA/ECB/PKCS1Padding" // Fallback
};
/**
* Traverses <code>preferredTransforms</code> list in order to
* find a Cipher supported by underlying JCE providers.`
* @returns transformation available.
*/
private static String getSupportedTransformation() {
try {
return preferredTransforms[i];
}
catch ( NoSuchAlgorithmException ex) {
}
catch ( NoSuchPaddingException ex) {
}
}
return null;
}
/**
* Runs the OpenDS setup command to create our instance
*
* @param map The map of configuration options
* @throws Exception upon encountering errors.
*/
throws Exception {
if (ret == 0) {
"EmbeddedOpenDS.setupOpenDS: OpenDS setup succeeded.");
} else {
"EmbeddedOpenDS.setupOpenDS. Error setting up OpenDS");
throw new ConfiguratorException(
"configurator.embsetupopendsfailed");
}
}
/**
* Runs the OpenDS setup command like this:
* $ ./setup --cli --adminConnectorPort 4444
* --baseDN dc=openam,dc=java,dc=net --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
*/
"--cli", // 0
"--adminConnectorPort", // 1
"4444", // 2
"--baseDN", // 3
"--rootUserDN", // 5
"cn=Directory Manager", // 6
"--ldapPort", // 7
"50389", // 8
"--skipPortCheck", // 9
"--rootUserPassword", // 10
"xxxxxxx", // 11
"--jmxPort", // 12
"1689", // 13
"--no-prompt", // 14
"--configFile", // 15
"/path/to/config.ldif", // 16
"--doNotStart", // 17
"--hostname", // 18
"hostname" // 19
};
setupCmd, true,
null);
if (ret == 0) {
} else {
}
return ret;
}
/**
* Starts the embedded <code>OpenDS</code> instance.
*
* @param odsRoot File system directory where <code>OpenDS</code>
* is installed.
*
* @throws Exception upon encountering errors.
*/
if (isStarted()) {
return;
}
config.setForceDaemonThreads(true);
int sleepcount = 0;
sleepcount++;
}
if (EmbeddedUtils.isRunning()) {
} else {
}
serverStarted = true;
if (shutdownMan.acquireValidLock()) {
try {
public void shutdown() {
try {
shutdownServer("Graceful Shutdown");
ex);
}
}
}, ShutdownPriority.LOWEST);
} finally {
}
}
}
/**
* Gracefully shuts down the embedded opends instance.
*
* @param reason string representing reasn why shutdown was called.
*
* @throws Exception on encountering errors.
*/
if (isStarted()) {
"com.sun.identity.setup.EmbeddedOpenDS",
int sleepcount = 0;
sleepcount++;
}
serverStarted = false;
}
}
{
// Setup replication
if (ret == 0) {
"EmbeddedOpenDS.setupReplication: replication setup succeeded.");
} else {
"EmbeddedOpenDS.setupReplication. Error setting up replication");
throw new ConfiguratorException(
"configurator.embreplfailed");
}
}
/**
* Setups replication between two opends 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
*/
{
"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
};
if (debug.messageEnabled()) {
}
enableCmd, false,
null);
if (ret == 0) {
} else {
}
return ret;
}
/**
* Syncs replication data between two opends 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
*/
{
String[] initializeCmd= {
"initialize", // 0
"--no-prompt", // 1
"--baseDN", // 2
"--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
};
null);
if (ret == 0) {
} else {
}
return ret;
}
/**
* Returns Replication Status by invoking opends <code>dsreplication</code>
* CLI
* @param port LDAP port number of embedded opends
* @param passwd Directory Manager password
* @param oo Standard output
* @param err : Standard error
* @return <code>dsreplication</code> CLI exit code.
*/
{
"status","--no-prompt",
"-h", "localhost",
"-p", port,
"--adminUID", "admin",
"--adminPassword", passwd,
"-s",
"--configFile",
baseDir + "/opends/config/config.ldif"
};
if (debug.messageEnabled()) {
+dbgcmd);
}
if (debug.messageEnabled()) {
+ret);
}
return ret;
}
/**
* @return true if multi server option is selected in the configurator.
*/
{
return true;
}
return false;
}
{
return ret;
}
/**
* Utility function to preload data in the embedded instance.
* Must be called when the directory instance is shutdown.
*
* @param odsRoot Local directory where <code>OpenDS</code> is installed.
* @param ldif Full path of the ldif file to be loaded.
*
*/
{
int ret = 0;
try {
if (debug.messageEnabled()) {
}
{
"-C", // 0
"org.opends.server.extensions.ConfigFileHandler", // 1
"-f", // 2
"-n", // 4
"userRoot", // 5
"-l", // 6
ldif, // 7
"-Q", // 8
"--trustAll", // 9
"-D", // 10
"cn=Directory Manager", // 11
"-w", // 12
"password" // 13
};
if (debug.messageEnabled()) {
}
}
return ret;
}
/**
* Returns a one-way hash for passwd using SSHA512 scheme.
*
* @param p Clear password string
* @return hash value
*/
{
try {
}
return str;
}
/**
* Get replication port
* @param username
* @param password
* @param hostname
* @param port
* @return port number if replication is setup, null if not or on error.
*/
public static String getReplicationPort(
) {
"cn=replication server,cn=Multimaster Synchronization,cn=Synchronization Providers,cn=config";
try {
// We'll use Directory Manager
username = "cn=Directory Manager";
port,
);
}
}
}
}
"EmbeddedOpenDS.getReplicationPort(). Error getting replication port:", ex);
} finally {
}
return replPort;
}
/**
* Get admin port of the OpenDS server
*
* @param username The username of the directory admin
* @param password The password of the directory admin
* @param hostname The hostname of the directory server
* @param port The port of the directory server
* @return The admin port
*/
public static String getAdminPort(
) {
try {
port,
);
}
}
}
}
"EmbeddedOpenDS.getAdminPort(). Error getting admin port:", ex);
} finally {
}
return adminPort;
}
/**
* Synchronizes replication server info with current list of opensso servers.
*/
public static boolean syncReplicatedServers(
{
"-h", "localhost",
"-D", "cn=directory manager",
"list-replication-server",
"--provider-name", "Multimaster Synchronization",
"--property", "replication-server",
"--property", "replication-port","--no-prompt", "--trustAll"
};
if (debug.messageEnabled()) {
+dbgcmd);
}
+ stre);
return false;
} else {
if (debug.messageEnabled()) {
+ stre);
}
}
try {
}
return false;
}
try {
// Check if this server is part of server list
int numremoved = 0;
while (stok.hasMoreTokens()) {
numremoved++;
}
}
if (numremoved > 0) {
if (debug.messageEnabled()) {
dbgcmd1);
}
bos = new ByteArrayOutputStream();
boe = new ByteArrayOutputStream();
if (debug.messageEnabled()) {
str);
}
+stre);
}
}
return false;
}
return true;
}
/**
* Synchronizes replication domain info with current list of opensso servers.
*/
public static boolean syncReplicatedDomains(
{
"-h", "localhost",
"-D", "cn=directory manager",
"list-replication-domains",
"--provider-name", "Multimaster Synchronization",
"--property", "replication-server",
"--no-prompt",
"--trustAll"
};
if (debug.messageEnabled()) {
+dbgcmd);
}
}
try {
try {
if (debug.messageEnabled()) {
}
// Check if this server is part of server list
int numremoved = 0;
while (stok.hasMoreTokens()) {
numremoved++;
}
}
if (numremoved > 0) {
if (debug.messageEnabled()) {
dbgcmd1);
}
bos = new ByteArrayOutputStream();
boe = new ByteArrayOutputStream();
}
if (debug.messageEnabled()) {
str);
}
}
return false;
}
}
return false;
}
return true;
}
/**
* Synchronizes replication domain info with current list of opensso servers.
*/
public static boolean syncReplicatedServerList(
{
try {
"localhost",
port,
"cn=Directory Manager",
);
return false;
}
return false;
} finally {
}
return true;
}
/**
* Helper method to return Ldap connection to a embedded opends
* server.
* @return Ldap connection
*/
private static LDAPConnection getLDAPConnection(
) {
try {
ld = new LDAPConnection();
} catch (LDAPException ex) {
"EmbeddedOpenDS.setup(). Error getting LDAPConnection:", ex);
}
return ld;
}
/**
* Helper method to disconnect from Directory Server.
*/
try {
ld.disconnect();
} catch (LDAPException e) {
}
}
}
"cn=all-servers,cn=Server Groups,cn=admin data";
/**
* Removes host:port from opends replication
*/
public static void delOpenDSServer(
) {
"Could not connect to local opends instance."+replServerDN);
return;
}
try {
}
}
",cn=instance keys,cn=admin data";
} else {
"Could not find trustkey for:"+replServerDN);
}
" Error getting replication key:", ex);
}
try {
}
try {
}
}
/**
* Gets list of replicated servers from local opends directory.
*/
public static Set getServerSet(
) {
try {
// strip "cn="
}
}
return hostSet;
} else {
"Could not find trustkey for:"+replDN);
}
} else {
"Could not connect to local opends instance.");
}
" Error getting replication key:", ex);
}
return null;
}
// Programmatic way of rebuilding indexes in OpenDS.
// This method simulates the OpenDS cli command rebuild-index.
// eg., rebuild-index -b dc=example,dc=com -i uid -i mail
int ret = 0;
shutdownServer("Rebuild index");
"--configClass",
"org.opends.server.extensions.ConfigFileHandler",
"--configFile",
"--baseDN",
"--index",
"sunxmlkeyvalue",
"--index",
"memberof",
"--index",
"iplanet-am-user-federation-info-key",
"--index",
"sun-fm-saml2-nameid-infokey"};
TimeThread.start();
TimeThread.stop();
errStr);
}
if (debug.messageEnabled()) {
if (idx >= 0) {
}
outStr);
}
return ret;
}
/**
* @return true if installed OpenDS is version 1.0.2
*/
public static boolean isOpenDSVer1Installed() {
boolean openDSVer1x = false;
openDSVer1x = true;
}
return openDSVer1x;
}
/**
* @return true if installed OpenDS is version 2.3.0BACKPORT2
*/
public static boolean isOpenDSVer230Installed() {
boolean openDSVer230b2 = false;
openDSVer230b2 = true;
}
return openDSVer230b2;
}
public static String getOpenDSVersion() {
//@Override -- Not Allowed Here.
}
});
} else {
}
} else {
if (debug.warningEnabled()) {
}
}
if (debug.messageEnabled()) {
}
return version;
}
/**
* Initialises OpenDS for client use, used by the CLI tools
*/
public static void initializeForClientUse() {
}
// Returns the installation directory for the embedded OpenDJ.
}
// Returns the configuration file name for the embedded OpenDJ.
}
// Returns the host name for the embedded OpenDJ.
}
return dirHost;
}
}