001: /**
002: * $Id: LDAPDataStoreImpl.java,v 1.10 2005/09/21 15:29:56 fo160993 Exp $
003: * Copyright 2005 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.console.sra.persistance;
014:
015: import java.util.Arrays;
016: import java.util.HashMap;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.LinkedList;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.logging.Level;
023:
024: import javax.management.MBeanServerConnection;
025: import javax.management.ObjectName;
026:
027: import com.sun.portal.admin.common.AttrOptionConstants;
028: import com.sun.portal.admin.common.util.AdminClientUtil;
029: import com.sun.portal.admin.console.common.AMLocationBarBean;
030: import com.sun.portal.admin.console.common.AuthCredentialBean;
031: import com.sun.portal.admin.console.common.PSBaseBean;
032: import com.sun.portal.admin.console.common.SessionAttributeNames;
033: import com.sun.portal.admin.console.sra.exceptions.DeserializeException;
034: import com.sun.portal.admin.console.sra.ServiceNames;
035: import com.sun.portal.admin.console.sra.exceptions.PersistBeanException;
036:
037: public class LDAPDataStoreImpl implements IPersistantDataStore {
038:
039: private static final String OPT_SRA_PROFILE_NAME = "gateway-profile";
040: private static boolean DEBUG = false;
041:
042: private String selectedDN = (String) PSBaseBean
043: .getSessionAttribute(SessionAttributeNames.ATTR_CURRENT_LOCATION_DN);
044:
045: protected MBeanServerConnection getMBeanServerConnection() {
046: AuthCredentialBean abean = AuthCredentialBean
047: .getCurrentInstance();
048: if (abean != null) {
049: return abean.getMBeanServerConnection();
050: }
051: return null;
052: }
053:
054: public Map deserialize(String svcName, String[] attributeList)
055: throws DeserializeException {
056: Map map = _deserialize(svcName, null, attributeList);
057: return map;
058: }
059:
060: public Map deserialize(String svcName, String profileName,
061: String[] attributeList) throws DeserializeException {
062: Map map = _deserialize(svcName, profileName, attributeList);
063: return map;
064: }
065:
066: public void serialize(String svcName, Map map)
067: throws PersistBeanException {
068: _serialize(svcName, null, map);
069: }
070:
071: public void serialize(String svcName, String profileName, Map map)
072: throws PersistBeanException {
073: _serialize(svcName, profileName, map);
074: }
075:
076: private Map _deserialize(String svcName, String sraProfileName,
077: String[] attributeList) {
078: Map map = new HashMap();
079:
080: try {
081:
082: LinkedList path = new LinkedList();
083: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
084:
085: ObjectName objName;
086: try {
087: objName = AdminClientUtil.getResourceMBeanObjectName(
088: AdminClientUtil.PORTAL_DOMAIN_MBEAN_TYPE, path);
089:
090: HashMap optionsMap = new HashMap();
091: optionsMap.put(AttrOptionConstants.OPT_COMPONENT,
092: svcName);
093: optionsMap.put(AttrOptionConstants.OPT_ATTR_NAMES,
094: new HashSet(Arrays.asList(attributeList)));
095:
096: if (sraProfileName != null)
097: optionsMap
098: .put(OPT_SRA_PROFILE_NAME, sraProfileName);
099:
100: addSelectedDn(svcName, optionsMap);
101:
102: Object[] params = { optionsMap };
103: String method = "getAttributes";
104: String[] signature = { "java.util.Map" };
105:
106: PSBaseBean.log(Level.FINEST,
107: "LDAPDataStoreImpl.getAttributeValueListFromService: Invoking mbean operation "
108: + method + " at DN=" + selectedDN);
109: MBeanServerConnection conn = getMBeanServerConnection();
110: if (conn != null) {
111: map = (Map) conn.invoke(objName, method, params,
112: signature);
113: PSBaseBean.log(Level.FINEST, "Executed GET => "
114: + map);
115: }
116: } catch (Exception e) {
117: PSBaseBean
118: .log(
119: Level.FINEST,
120: "LDAPDataStoreImpl._deserialize(): Error in invoking getAttributes ",
121: e);
122: }
123:
124: } catch (Exception e) {
125: PSBaseBean
126: .log(
127: Level.SEVERE,
128: "LDAPDataStoreImpl.privateDeserialize: Get Attribute List operation failed",
129: e);
130: }
131: return map;
132: }
133:
134: private void addSelectedDn(String svcName, HashMap optionsMap) {
135: if (svcName.equals(ServiceNames.GATEWAY_SVC))
136: return;
137: selectedDN = (String) PSBaseBean
138: .getSessionAttribute(SessionAttributeNames.ATTR_CURRENT_LOCATION_DN);
139: if (selectedDN != null
140: && !selectedDN
141: .equals(AMLocationBarBean.GLOBAL_LOCATION_DN))
142: optionsMap.put(AttrOptionConstants.OPT_DN, selectedDN);
143: }
144:
145: /**
146: * @param svcName
147: * @param sraProfileName TODO
148: * @param map
149: */
150: private void _serialize(String svcName, String sraProfileName,
151: Map map) {
152:
153: LinkedList path = new LinkedList();
154: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
155:
156: ObjectName objName;
157: try {
158: objName = AdminClientUtil.getResourceMBeanObjectName(
159: AdminClientUtil.PORTAL_DOMAIN_MBEAN_TYPE, path);
160:
161: HashMap optionsMap = new HashMap();
162: optionsMap.put(AttrOptionConstants.OPT_COMPONENT, svcName);
163: if (sraProfileName != null)
164: optionsMap.put(OPT_SRA_PROFILE_NAME, sraProfileName);
165:
166: addSelectedDn(svcName, optionsMap);
167: if (selectedDN != null
168: && !selectedDN
169: .equals(AMLocationBarBean.GLOBAL_LOCATION_DN))
170: massageNVMap(map);
171: Object[] params = { map, optionsMap };
172: String method = "setAttributes";
173: String[] signature = { "java.util.Map", "java.util.Map" };
174:
175: PSBaseBean.log(Level.FINEST,
176: "LDAPDataStoreImpl.setAttributeValueListToService: Invoking mbean operation "
177: + method);
178: MBeanServerConnection conn = getMBeanServerConnection();
179: if (conn != null) {
180: conn.invoke(objName, method, params, signature);
181: PSBaseBean.log(Level.FINEST, "Executed SET => "
182: + method + " storing map : " + map
183: + " options=" + optionsMap);
184: }
185: } catch (Exception e) {
186: PSBaseBean
187: .log(
188: Level.FINEST,
189: "LDAPDataStoreImpl._serialize(): Error in invoking setAttributes",
190: e);
191: }
192:
193: }
194:
195: /**
196: * @param map
197: * The AM SDK which finally processes the save of the attributes ( setAttributes() mbean call uses AM SDK)
198: * does not like empty lists. If an attribute value contains nothing, it should be spelt out using a string
199: * of the form "". This supposedly only occurs during the save at a DN other than TOP. So instead of changing
200: * code in all services, the save explicitly now adds an empty string so that the AM SDK is happy.
201: *
202: * ---- Sandeep Soni ( ss150821 )
203: */
204: private void massageNVMap(Map map) {
205: Iterator iterator = map.keySet().iterator();
206: while (iterator.hasNext()) {
207: String attribName = (String) iterator.next();
208: List valueList = (List) map.get(attribName);
209: if (valueList.isEmpty()) {
210: valueList.add("");
211: map.put(attribName, valueList);
212: }
213: }
214:
215: if (DEBUG)
216: debugFunction(map);
217: }
218:
219: /**
220: * @param map
221: */
222: private void debugFunction(Map map) {
223: map.remove("COSPriority");
224:
225: }
226:
227: }
|