/** * Get admin port of the OpenDJ 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( String username, String password, String hostname, String port) { final String adminConnectorDN = "cn=Administration Connector,cn=config"; final String[] attrs = {"ds-cfg-listen-port"}; String adminPort = null; LDAPConnection ld = null; try { LDAPConnection lc = getLDAPConnection(hostname, port, username, password); if (lc != null) { LDAPEntry le = lc.read(adminConnectorDN, attrs); if (le != null) { LDAPAttribute la = le.getAttribute(attrs[0]); if (la != null) { Enumeration en = la.getStringValues(); if (en != null && en.hasMoreElements()) { adminPort = (String) en.nextElement(); } } } } } catch (Exception ex) { Debug.getInstance(SetupConstants.DEBUG_NAME) .error("EmbeddedOpenDS.getAdminPort(). Error getting admin port:", ex); } finally { disconnectDServer(ld); } return adminPort; }
/** * removes the listener from the list of Persistent Search listeners of the asynchronous seach for * the given search ID. * * @param request The request returned by the addListener * @supported.api */ protected void removeListener(Request request) { LDAPConnection connection = request.getLDAPConnection(); if (connection != null) { if (debugger.messageEnabled()) { debugger.message( "EventService.removeListener(): Removing " + "listener requestID: " + request.getRequestID() + " Listener: " + request.getListener()); } try { if ((connection != null) && (connection.isConnected())) { connection.abandon(request.getId()); connection.disconnect(); } } catch (LDAPException le) { // Might have to check the reset codes and try to reset if (debugger.warningEnabled()) { debugger.warning( "EventService.removeListener(): " + "LDAPException, when trying to remove listener", le); } } } }
/** * 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( String username, String password, String hostname, String port) { final String replDN = "cn=replication server,cn=Multimaster Synchronization,cn=Synchronization Providers,cn=config"; final String[] attrs = {"ds-cfg-replication-port"}; String replPort = null; LDAPConnection ld = null; try { // We'll use Directory Manager username = "******"; LDAPConnection lc = getLDAPConnection(hostname, port, username, password); if (lc != null) { LDAPEntry le = lc.read(replDN, attrs); if (le != null) { LDAPAttribute la = le.getAttribute(attrs[0]); if (la != null) { Enumeration en = la.getStringValues(); if (en != null && en.hasMoreElements()) { replPort = (String) en.nextElement(); } } } } } catch (Exception ex) { Debug.getInstance(SetupConstants.DEBUG_NAME) .error("EmbeddedOpenDS.getReplicationPort(). Error getting replication port:", ex); } finally { disconnectDServer(ld); } return replPort; }
/** Helper method to disconnect from Directory Server. */ private static void disconnectDServer(LDAPConnection ld) { if ((ld != null) && ld.isConnected()) { try { ld.disconnect(); } catch (LDAPException e) { } } }
/** * Helper method to return Ldap connection to a embedded OpenDJ server. * * @return Ldap connection */ private static LDAPConnection getLDAPConnection( String dsHostName, String dsPort, String dsManager, String dsAdminPwd) { LDAPConnection ld = null; try { int dsPortInt = Integer.parseInt(dsPort); ld = new LDAPConnection(); ld.setConnectTimeout(300); ld.connect(3, dsHostName, dsPortInt, dsManager, dsAdminPwd); } catch (LDAPException ex) { Debug.getInstance(SetupConstants.DEBUG_NAME) .error("EmbeddedOpenDS.setup(). Error getting LDAPConnection:", ex); } return ld; }
/** Removes host:port from OpenDJ replication */ public static void delOpenDSServer(LDAPConnection lc, String delServer) { String replServerDN = "cn=" + delServer + ",cn=Servers,cn=admin data"; final String[] attrs = {"ds-cfg-key-id"}; Debug debug = Debug.getInstance(SetupConstants.DEBUG_NAME); if (lc == null) { debug.error( "EmbeddedOpenDS:syncOpenDSServer():" + "Could not connect to local OpenDJ instance." + replServerDN); return; } String trustKey = null; try { LDAPEntry le = lc.read(replServerDN, attrs); if (le != null) { LDAPAttribute la = le.getAttribute(attrs[0]); if (la != null) { Enumeration en = la.getStringValues(); if (en != null && en.hasMoreElements()) { trustKey = (String) en.nextElement(); } } String keyDN = "ds-cfg-key-id=" + trustKey + ",cn=instance keys,cn=admin data"; lc.delete(keyDN); } else { debug.error( "EmbeddedOpenDS:syncOpenDSServer():" + "Could not find trustkey for:" + replServerDN); } } catch (Exception ex) { debug.error("EmbeddedOpenDS.syncOpenDSServer()." + " Error getting replication key:", ex); } try { lc.delete(replServerDN); } catch (Exception ex) { debug.error( "EmbeddedOpenDS.syncOpenDSServer()." + " Error getting deleting server entry:" + replServerDN, ex); } try { LDAPAttribute attr = new LDAPAttribute("uniqueMember", "cn=" + delServer); LDAPModification mod = new LDAPModification(LDAPModification.DELETE, attr); lc.modify(replDN, mod); } catch (Exception ex) { debug.error("EmbeddedOpenDS.syncOpenDSServer()." + " Error getting removing :" + replDN, ex); } }
/** Gets list of replicated servers from local OpenDJ directory. */ public static Set getServerSet(LDAPConnection lc) { final String[] attrs = {"uniqueMember"}; Debug debug = Debug.getInstance(SetupConstants.DEBUG_NAME); try { if (lc != null) { LDAPEntry le = lc.read(replDN, attrs); if (le != null) { Set hostSet = new HashSet(); LDAPAttribute la = le.getAttribute(attrs[0]); if (la != null) { Enumeration en = la.getStringValues(); while (en != null && en.hasMoreElements()) { String val = (String) en.nextElement(); // strip "cn=" hostSet.add(val.substring(3, val.length())); } } return hostSet; } else { debug.error( "EmbeddedOpenDS:syncOpenDSServer():" + "Could not find trustkey for:" + replDN); } } else { debug.error( "EmbeddedOpenDS:syncOpenDSServer():" + "Could not connect to local opends instance."); } } catch (Exception ex) { debug.error("EmbeddedOpenDS.syncOpenDSServer()." + " Error getting replication key:", ex); } return null; }
/** * Adds a listener to the directory. * * @supported.api */ protected synchronized String addListener( SSOToken token, IDSEventListener listener, String base, int scope, String filter, int operations) throws LDAPException, EventException { if (_shutdownCalled) { throw new EventException(i18n.getString(IUMSConstants.DSCFG_CONNECTFAIL)); } LDAPConnection lc = null; try { // Check for SMS listener and use "sms" group if present if ((listener.getClass().getName().equals("com.sun.identity.sm.ldap.LDAPEventManager")) && (cm.getServerGroup("sms") != null)) { lc = cm.getNewConnection("sms", LDAPUser.Type.AUTH_ADMIN); } else { lc = cm.getNewAdminConnection(); } } catch (LDAPServiceException le) { throw new EventException(i18n.getString(IUMSConstants.DSCFG_CONNECTFAIL), le); } LDAPSearchConstraints cons = lc.getSearchConstraints(); // Create Persistent Search Control object LDAPPersistSearchControl psearchCtrl = new LDAPPersistSearchControl(operations, CHANGES_ONLY, RETURN_CONTROLS, IS_CRITICAL); // Add LDAPControl array to the search constraint object cons.setServerControls(psearchCtrl); cons.setBatchSize(1); // Listeners can not read attributes from the event. // Request only javaClassName to be able to determine object type String[] attrs = new String[] {"objectclass"}; LDAPSearchListener searchListener = null; // Set (asynchronous) persistent search request in the DS try { if (debugger.messageEnabled()) { debugger.message( "EventService.addListener() - Submiting " + "Persistent Search on: " + base + " for listener: " + listener); } searchListener = lc.search(base, scope, filter, attrs, false, null, cons); } catch (LDAPException le) { if ((lc != null) && lc.isConnected()) { try { lc.disconnect(); } catch (Exception ex) { // ignored } } debugger.error( "EventService.addListener() - Failed to set " + "Persistent Search" + le.getMessage()); throw le; } int[] outstandingRequests = searchListener.getMessageIDs(); int id = outstandingRequests[outstandingRequests.length - 1]; String reqID = Integer.toString(id); long startTime = System.currentTimeMillis(); Request request = new Request( id, reqID, token, base, scope, filter, attrs, operations, listener, lc, startTime); _requestList.put(reqID, request); // Add this search request to the m_msgQueue so it can be // processed by the monitor thread if (_msgQueue == null) { _msgQueue = searchListener; } else { _msgQueue.merge(searchListener); } if (!_isThreadStarted) { startMonitorThread(); } else { if (_requestList.size() == 1) { notify(); } } if (debugger.messageEnabled()) { outstandingRequests = _msgQueue.getMessageIDs(); debugger.message( "EventService.addListener(): merged Listener: " + " requestID: " + reqID + " & Request: " + request + " on to message Queue. No. of current outstanding " + "requests = " + outstandingRequests.length); } // Create new (EventService) Thread, if one doesn't exist. return reqID; }