001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.wsrp.consumer.admin.mbeans;
013:
014: import java.net.URL;
015: import java.net.URLEncoder;
016:
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.LinkedList;
020: import java.util.Map;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import javax.management.MBeanServer;
025: import javax.management.ObjectName;
026:
027: import com.sun.portal.admin.common.util.AdminUtil;
028: import com.sun.portal.admin.server.PASModule;
029: import com.sun.portal.log.common.PortalLogger;
030: import com.sun.portal.wsrp.common.IdentityPropagationConstants;
031:
032: public class WSSSOConfigurator implements IdentityPropagationConstants {
033:
034: private String domainId = null;
035: private String portalId = null;
036: private LinkedList path = new LinkedList();
037: private MBeanServer mbs = null;
038: private ObjectName mBeanName = null;
039:
040: public static final String CONFIG_NAME = "OASIS-Username-Token-Profile";
041: public static final String TEMPLATE_NAME = "OASIS-USERNAME-TOKEN-PROFILE";
042: public static final String SEPARATOR = "_";
043:
044: private static final String OPERATION_DELETE_CONF = "deleteConfiguration";
045: private static final String OPERATION_CREATE_CONF = "createConfiguration";
046: private static final String OPERATION_GET_CONF_PROPS = "getConfigurationProperties";
047: private static final String OPERATION_SET_CONF_PROPS = "setConfigurationProperties";
048:
049: private static Logger debugLogger = PortalLogger
050: .getLogger(WSSSOConfigurator.class);
051:
052: public WSSSOConfigurator(String domainId, String portalId) {
053: this .domainId = domainId;
054: this .portalId = portalId;
055: path.addFirst(domainId);
056: path.addFirst("ssoadapter");
057: //path.add(portalId);
058: init();
059: }
060:
061: public void createUserConfiguration(URL wsURL, String dn, Map props) {
062: String newName = getConfigurationName(wsURL);
063:
064: if (newName == null) {
065: return;
066: }
067:
068: Map oldProps = getConfigurationProperties(newName, dn);
069:
070: //Configuration already exists for this producer URL
071: if (!oldProps.isEmpty()) {
072: return;
073: }
074:
075: createConfiguration(newName, dn);
076: oldProps = getConfigurationProperties(newName, dn);
077: Map modifiedProps = modifyProperties(oldProps, props);
078: setConfigurationProperties(newName, dn, modifiedProps);
079:
080: }
081:
082: public void deleteUserConfiguration(URL wsURL, String dn) {
083: String newName = getConfigurationName(wsURL);
084: if (newName == null) {
085: return;
086: }
087: Object[] params = { dn, newName };
088: String[] signatures = { "java.lang.String", "java.lang.String" };
089: invokeRemoteOperation(OPERATION_DELETE_CONF, params, signatures);
090: }
091:
092: private void createConfiguration(String name, String dn) {
093: Object[] params = { dn, name, TEMPLATE_NAME };
094: String[] signatures = { "java.lang.String", "java.lang.String",
095: "java.lang.String" };
096: invokeRemoteOperation(OPERATION_CREATE_CONF, params, signatures);
097: }
098:
099: private String getConfigurationName(URL wsURL) {
100: int port = wsURL.getPort();
101: if (port == -1) {
102: port = wsURL.getDefaultPort();
103: }
104: String newURL = wsURL.getProtocol() + "://" + wsURL.getHost()
105: + ":" + port + wsURL.getPath();
106: try {
107: String encoded = URLEncoder.encode(newURL, "UTF-8");
108: return CONFIG_NAME + SEPARATOR + portalId + SEPARATOR
109: + encoded;
110: } catch (Exception e) {
111: debugLogger.log(Level.SEVERE, "", e);
112: }
113: return null;
114: }
115:
116: private Map modifyProperties(Map props, Map newProps) {
117: HashMap newConfigProps = new HashMap(props.size());
118: String propertyValue = null;
119:
120: for (Iterator itr = props.keySet().iterator(); itr.hasNext();) {
121: Object key = itr.next();
122: String values[] = (String[]) props.get(key);
123:
124: Object newValue = newProps.get(key);
125: if (newValue != null) {
126: propertyValue = newValue.toString();
127: values = new String[] { propertyValue };
128: }
129:
130: newConfigProps.put(key, values);
131: }
132:
133: return newConfigProps;
134:
135: }
136:
137: private Map getConfigurationProperties(String name, String dn) {
138: Object[] params = { dn, name };
139: String[] signatures = { "java.lang.String", "java.lang.String" };
140: Object o = invokeRemoteOperation(OPERATION_GET_CONF_PROPS,
141: params, signatures);
142: if (o != null) {
143: return (Map) o;
144: }
145: return null;
146: }
147:
148: private void setConfigurationProperties(String name, String dn,
149: Map props) {
150: Object[] params = { dn, name, props };
151: String[] signatures = { "java.lang.String", "java.lang.String",
152: "java.util.Map" };
153: invokeRemoteOperation(OPERATION_SET_CONF_PROPS, params,
154: signatures);
155: }
156:
157: private void init() {
158: try {
159: mbs = PASModule.getMBeanServer();
160:
161: mBeanName = AdminUtil.getResourceMBeanObjectName(
162: AdminUtil.SSOADAPTER_MBEAN_TYPE, path);
163: } catch (Exception e) {
164: debugLogger.log(Level.SEVERE, "", e);
165: }
166:
167: }
168:
169: private Object invokeRemoteOperation(String operationName,
170: Object[] params, String[] signature) {
171: //throws PSMBeanException {
172: try {
173: Object o = mbs.invoke(mBeanName, operationName, params,
174: signature);
175: return o;
176: } catch (Exception e) {
177: debugLogger.log(Level.SEVERE, "", e);
178: }
179: return null;
180:
181: }
182: }
|