001: /**
002: * $Id: AdminServerUtil.java,v 1.7 2005/11/23 21:39:22 yue Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.server;
014:
015: import java.io.IOException;
016: import java.security.AccessControlContext;
017: import java.security.AccessController;
018: import java.util.LinkedList;
019: import java.util.Set;
020:
021: import javax.management.remote.JMXConnector;
022: import javax.security.auth.Subject;
023:
024: import com.iplanet.am.util.AMPasswordUtil;
025: import com.iplanet.sso.SSOToken;
026: import com.sun.cacao.agent.auth.CacaoCallbackHandler;
027: import com.sun.cacao.agent.auth.Credential;
028: import com.sun.portal.admin.common.context.PortalDomainContext;
029: import com.sun.portal.admin.common.context.PortalDomainContextException;
030: import com.sun.portal.admin.common.context.PortalDomainContextFactory;
031: import com.sun.portal.admin.common.util.AdminUtil;
032:
033: /**
034: * This class provides misc. utility methods for portal MBeans.
035: */
036: public class AdminServerUtil extends AdminUtil {
037: /**
038: * Returns a connected JMXConnector to the connector server
039: * running on the given host. The connection is authenticated
040: * using the JASS credentials from the current context. This
041: * method is typically used by a portal MBean to make a connection
042: * to the Portal Admin Server running on another host.
043: * <p>
044: * The caller is responsible for obtaining the
045: * MBeanServerConnection and closing the returned JMXConnector.
046: *
047: * @param host the host where the JMX connector server is running on.
048: * @return a connected JMXConnector.
049: * @exception NullPointerException if host is null.
050: * @exception SecurityException if an authentication error occurs.
051: * @exception IOException if a connection error occurs.
052: */
053: public static JMXConnector getJMXConnector(String host)
054: throws SecurityException, IOException {
055:
056: if (host == null) {
057: throw new NullPointerException("host is null.");
058: }
059:
060: // recover the credentials we need to open this connection...
061: AccessControlContext acc = AccessController.getContext();
062: Subject subject = Subject.getSubject(acc);
063: Set publicCreds = subject
064: .getPublicCredentials(Credential.class);
065: Set privateCreds = subject
066: .getPrivateCredentials(Credential.class);
067:
068: // we expect only one match to this query
069: if ((publicCreds.size() != 1) || (privateCreds.size() != 1)) {
070: throw new SecurityException("Cannot get unique credentials");
071: }
072:
073: String authID = ((Credential) publicCreds.iterator().next())
074: .getValue();
075: String passwd = ((Credential) privateCreds.iterator().next())
076: .getValue();
077: String[] authPieces = authID
078: .split(CacaoCallbackHandler.SEPARATOR);
079:
080: if (CacaoCallbackHandler.getMechanisms().get(authPieces[0]) == null) {
081: throw new IllegalArgumentException(
082: "Invalid JAAS credentials");
083: }
084:
085: return getConnector(host, authID, passwd);
086: }
087:
088: /**
089: * Returns the password of the currently authenticated user. If
090: * the user has not been authenticated, <code>null</code> is returned.
091: *
092: * @return the password of the currently authenticated user;
093: * <code>null</code> if the user has not been authenticated.
094: */
095: public static String getPassword() {
096: AccessControlContext acc = AccessController.getContext();
097: Subject subject = Subject.getSubject(acc);
098:
099: if (subject == null) {
100: return null;
101: }
102:
103: Set credentials = subject
104: .getPrivateCredentials(Credential.class);
105:
106: if ((credentials == null) || credentials.isEmpty()) {
107: return null;
108: }
109:
110: return ((Credential) credentials.iterator().next()).getValue();
111: }
112:
113: /**
114: * Returns the SSO token of the currently authenticated user. If
115: * the user has not been authenticated (i.e. no SSO token),
116: * <code>null</code> is returned.
117: *
118: * @return the SSOToken of the currently authenticated user;
119: * <code>null</code> if the user has not been authenticated.
120: */
121: public static SSOToken getSSOToken() {
122: AccessControlContext acc = AccessController.getContext();
123: Subject subject = Subject.getSubject(acc);
124:
125: if (subject == null) {
126: return null;
127: }
128:
129: Set principals = subject.getPrincipals(PASPrincipal.class);
130:
131: if ((principals == null) || principals.isEmpty()) {
132: return null;
133: }
134:
135: return ((PASPrincipal) principals.iterator().next())
136: .getSSOToken();
137: }
138:
139: /**
140: * Encrypts the given clear text.
141: *
142: * @param clearText the clear text to be encrypted.
143: * @return the encrypted text.
144: */
145: public static String encrypt(String clearText) {
146: return AMPasswordUtil.encrypt(clearText);
147: }
148:
149: /**
150: * Decrypts the given encrypted text.
151: *
152: * @param encryptedText the encrypted text to be decrypted.
153: * @return the decrypted clear text.
154: */
155: public static String decrypt(String encryptedText) {
156: return AMPasswordUtil.decrypt(encryptedText);
157: }
158:
159: /**
160: * Returns the portal server instances in the portal with the
161: * given portal ID and in the portal domain with the given domain ID.
162: *
163: * @param domainID domain ID of the portal domain.
164: * @param portalID portal ID of the portal.
165: * @return the IDs of the portal server instances.
166: * @exception NullPointerException if domainID or portalID is
167: * <code>null</code>.
168: * @exception NoSuchElementException if there is no portal domain
169: * with the given domain ID or no
170: * portal with the given portal ID.
171: * @exception PortalDomainContextException if an error occurs when
172: * reading the instance IDs.
173: */
174: public static Set getPortalServerInstances(String domainID,
175: String portalID) throws PortalDomainContextException {
176:
177: PortalDomainContext pdc = PortalDomainContextFactory
178: .getPortalDomainContext(domainID);
179:
180: LinkedList path = new LinkedList();
181: path.addFirst(domainID);
182: path.addFirst(portalID);
183: return pdc.getResourceIDs(PORTAL_SERVER_INSTANCE_MBEAN_TYPE,
184: path);
185: }
186: }
|