001: /**
002: * $Id: GenericDSAMEAttributeHandler.java,v 1.9 2006/02/08 11:42:32 fo160993 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.fabric.common;
014:
015: import java.util.Map;
016: import java.util.HashMap;
017: import java.util.HashSet;
018: import java.util.Set;
019: import java.util.Iterator;
020: import java.util.Collections;
021: import java.util.List;
022: import java.util.ArrayList;
023: import com.sun.portal.admin.common.AttrOptionConstants;
024: import com.sun.portal.admin.server.AdminServerUtil;
025: import com.sun.portal.admin.common.PSMBeanException;
026:
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029: import java.util.logging.LogRecord;
030: import com.sun.portal.log.common.PortalLogger;
031:
032: public class GenericDSAMEAttributeHandler implements AttributeHandler {
033: public static final String COS_PRIORITY_STRING = DSAMEAttributeOperations.COS_PRIORITY_STRING;
034:
035: protected String compName = null;
036: protected String domainId = null;
037: protected String portalId = null;
038: protected AttributeOperations attrOps = null;
039:
040: private static Logger logger = PortalLogger
041: .getLogger(GenericDSAMEAttributeHandler.class);
042:
043: public void init(String component, String domainId, String portalId)
044: throws PSMBeanException {
045: compName = component;
046: this .domainId = domainId;
047: this .portalId = portalId;
048: try {
049: attrOps = new DSAMEAttributeOperations(AdminServerUtil
050: .getSSOToken());
051: } catch (Exception e) {
052: logger.log(Level.SEVERE, "PSFB_CSPFCM0016", e);
053: throw new PSMBeanException(
054: "error.psadmin.handler.init.failed",
055: e.getMessage(), e);
056: }
057: }
058:
059: /*
060: * Validates the basic options for get and set operations
061: * Component subclasses may override
062: * This method does not do anything
063: */
064: public void validate(List values, Map optionsMap)
065: throws PSMBeanException {
066: return;
067: }
068:
069: /**
070: * Returns the real name of the attribute
071: * This method returns the user friendly name as-is.
072: * The component handlers extending from this class
073: * must override this method to return the real name
074: * ex. DesktopType = sunPortal<portalId>DesktopType
075: *
076: * @param userFriendlyName alias of the real attr name
077: * @return translated real name
078: */
079: public String getAttributeName(String userFriendlyName) {
080: return userFriendlyName;
081: }
082:
083: /**
084: * Returns the real name of the component
085: * This method returns the user friendly name as-is.
086: * The component handlers extending from this class
087: * must override this method to return the real name
088: * ex. desktop = SunPortal<portalId>DesktopService
089: */
090: public String getComponentName(String userFriendlyName) {
091: return userFriendlyName;
092: }
093:
094: /**
095: * Subclasses must override this method for it to be useful.
096: */
097: public Map listAttributes(Map optionsMap) throws PSMBeanException {
098: return Collections.EMPTY_MAP;
099: }
100:
101: /**
102: * Handler subclasses may override this method and manipulate the
103: * optionsMap appropriately and then call super.getAttribute and
104: * let this method do the real work
105: */
106: public List getAttribute(Map optionsMap) throws PSMBeanException {
107: String comp = (String) optionsMap
108: .get(AttrOptionConstants.OPT_COMPONENT);
109: String attr = (String) optionsMap
110: .get(AttrOptionConstants.OPT_ATTR_NAME);
111:
112: List values = null;
113: try {
114: values = attrOps.getAttribute(getComponentName(comp),
115: getAttributeName(attr), buildExtraInfo(optionsMap));
116: } catch (Exception e) {
117: logger.log(Level.SEVERE, "PSFB_CSPFCM0017", new Object[] {
118: attr, comp });
119: throw new PSMBeanException("error.psadmin.get.failed", e
120: .getMessage(), e);
121: }
122: return values == null ? Collections.EMPTY_LIST : values;
123: }
124:
125: /**
126: * returns a mapping of friendly name to values
127: */
128: public Map getAttributes(Map optionsMap) throws PSMBeanException {
129: String comp = (String) optionsMap
130: .get(AttrOptionConstants.OPT_COMPONENT);
131: Set attrNames = (Set) optionsMap
132: .get(AttrOptionConstants.OPT_ATTR_NAMES);
133:
134: //atrNames are user-friendly names, so convert into real names first
135: //also keep a mapping of friendly-to-real names so that it can be
136: //converted back and returned
137:
138: //number of attrs
139: int nNames = attrNames.size();
140:
141: //mapping of friendly-to-real names
142: Map nameMap = new HashMap(nNames);
143:
144: //set of real names
145: Set realNames = new HashSet(nNames);
146:
147: //now construct nameMap and real name set
148: Iterator iter = attrNames.iterator();
149: while (iter.hasNext()) {
150: String fName = (String) iter.next();
151: String realName = getAttributeName(fName);
152: nameMap.put(realName, fName);
153: realNames.add(realName);
154: }
155:
156: Map rNameValues = null;
157: try {
158: rNameValues = attrOps.getAttributes(getComponentName(comp),
159: realNames, buildExtraInfo(optionsMap));
160: } catch (Exception e) {
161: logger.log(Level.INFO, "PSFB_CSPFCM0018", new Object[] {
162: attrNames, comp });
163: throw new PSMBeanException("error.psadmin.get.failed", e
164: .getMessage(), e);
165: }
166:
167: Map nameValues = new HashMap(nNames);
168: //the name keys are all real names
169: //so convert back to user-friendly names
170: if (rNameValues != null) {
171: Iterator riter = rNameValues.keySet().iterator();
172: while (riter.hasNext()) {
173: String rName = (String) riter.next();
174: nameValues.put(nameMap.get(rName), rNameValues
175: .get(rName));
176: }
177: }
178:
179: return nameValues == null ? Collections.EMPTY_MAP : nameValues;
180: }
181:
182: public void setAttribute(List values, Map optionsMap)
183: throws PSMBeanException {
184: String comp = (String) optionsMap
185: .get(AttrOptionConstants.OPT_COMPONENT);
186: String attr = (String) optionsMap
187: .get(AttrOptionConstants.OPT_ATTR_NAME);
188:
189: try {
190: attrOps.setAttribute(getComponentName(comp),
191: getAttributeName(attr), values,
192: buildExtraInfo(optionsMap));
193: } catch (Exception e) {
194: if (logger.isLoggable(Level.SEVERE)) {
195: LogRecord record = new LogRecord(Level.SEVERE,
196: "PSFB_CSPFCM0019");
197: record.setParameters(new Object[] { attr, comp });
198: record.setThrown(e);
199: record.setLoggerName(logger.getName());
200: logger.log(record);
201: }
202: throw new PSMBeanException("error.psadmin.set.failed", e
203: .getMessage(), e);
204: }
205: }
206:
207: /**
208: * Does not support add/remove/inherit operations.
209: */
210: public void setAttributes(Map nameValues, Map optionsMap)
211: throws PSMBeanException {
212: String comp = (String) optionsMap
213: .get(AttrOptionConstants.OPT_COMPONENT);
214:
215: Set fNameSet = nameValues.keySet();
216: //atrNames are user-friendly names, so convert into real names first
217: //number of attrs
218: int nNames = fNameSet.size();
219: //map of real names to values
220: Map rNameValues = new HashMap(nNames);
221:
222: //now construct real name-values map
223: Iterator iter = fNameSet.iterator();
224: while (iter.hasNext()) {
225: String fName = (String) iter.next();
226: String realName = getAttributeName(fName);
227: rNameValues.put(realName, nameValues.get(fName));
228: }
229:
230: try {
231: attrOps.setAttributes(getComponentName(comp), rNameValues,
232: buildExtraInfo(optionsMap));
233: } catch (Exception e) {
234: if (logger.isLoggable(Level.SEVERE)) {
235: LogRecord record = new LogRecord(Level.SEVERE,
236: "PSFB_CSPFCM0021");
237: record.setParameters(new Object[] { fNameSet, comp });
238: record.setThrown(e);
239: record.setLoggerName(logger.getName());
240: logger.log(record);
241: }
242: throw new PSMBeanException("error.psadmin.set.failed", e
243: .getMessage(), e);
244: }
245: }
246:
247: protected Map buildExtraInfo(Map optionsMap) {
248: //Build the extra info from options map
249: Map extraInfo = new HashMap();
250: boolean global = Boolean
251: .valueOf(
252: (String) optionsMap
253: .get(AttrOptionConstants.OPT_GLOBAL))
254: .booleanValue();
255: boolean org = Boolean.valueOf(
256: (String) optionsMap.get(AttrOptionConstants.OPT_ORG))
257: .booleanValue();
258: String dn = (String) optionsMap.get(AttrOptionConstants.OPT_DN);
259:
260: if (global) {
261: extraInfo.put(DSAMEAttributeOperations.SCHEMA_TYPE,
262: new Integer(DSAMEAttributeOperations.GLOBAL));
263: } else if (org) {
264: extraInfo.put(DSAMEAttributeOperations.SCHEMA_TYPE,
265: new Integer(DSAMEAttributeOperations.ORG));
266: }
267:
268: if (dn != null) {
269: extraInfo.put(DSAMEAttributeOperations.DN, dn);
270: }
271:
272: List addValues = (List) optionsMap
273: .get(AttrOptionConstants.OPT_ADD);
274: List removeValues = (List) optionsMap
275: .get(AttrOptionConstants.OPT_REMOVE);
276: String inheritStr = (String) optionsMap
277: .get(AttrOptionConstants.OPT_INHERIT);
278: extraInfo.put(DSAMEAttributeOperations.INHERIT, new Boolean(
279: inheritStr));
280:
281: if (addValues != null) {
282: extraInfo.put(DSAMEAttributeOperations.ADD, new HashSet(
283: addValues));
284: }
285: if (removeValues != null) {
286: extraInfo.put(DSAMEAttributeOperations.REMOVE, new HashSet(
287: removeValues));
288: }
289:
290: logger.log(Level.FINEST, "PSFB_CSPFCM0020", extraInfo);
291: return extraInfo;
292: }
293: }
|