001: /**
002: * $Id: ConfigurationAttributesBean.java,v 1.7 2005/10/11 16:28:58 sorensen 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.ssoa;
014:
015: import java.util.*;
016: import java.util.logging.Level;
017: import java.io.IOException;
018:
019: import javax.faces.context.FacesContext;
020: import javax.servlet.http.HttpServletRequest;
021:
022: import com.sun.data.provider.DataProvider;
023: import com.sun.data.provider.FieldKey;
024: import com.sun.data.provider.impl.ObjectListDataProvider;
025: import com.sun.web.ui.component.RadioButton;
026:
027: import com.sun.portal.admin.common.util.AdminClientUtil;
028:
029: import javax.management.ObjectName;
030: import javax.management.MBeanServerConnection;
031: import javax.management.MalformedObjectNameException;
032:
033: import com.sun.portal.admin.console.common.PSBaseBean;
034:
035: // This class should be instantiated on a request-basis
036: public class ConfigurationAttributesBean extends PSBaseBean {
037:
038: // public static final String ATTR_SELECTED_CONFIG = "ssoa.config.selected";
039: // public static final String ATTR_SELECTED_DN = "ssoa.dn.selected";
040:
041: private ObjectListDataProvider attributesDataProvider = null;
042: private ObjectName objectName = null;
043: private String name = null;
044: private String dn = null;
045: private String configDesc = null;
046: private String basis = null;
047: public static final String RB_NAME = "ssoa";
048: protected Map rbMap;
049:
050: /** Creates a new instance of ConfigurationAttributesBean*/
051: public ConfigurationAttributesBean() {
052: log(Level.FINEST,
053: "ConfigurationAttributesBean.constructor start");
054: rbMap = getResourceStringMap(RB_NAME);
055: // MBean path of SSOAdapter.
056: LinkedList path = new LinkedList();
057: path.addFirst(getDomain());
058: path.addFirst("ssoadapter");
059: try {
060: objectName = AdminClientUtil.getResourceMBeanObjectName(
061: AdminClientUtil.SSOADAPTER_MBEAN_TYPE, path);
062: } catch (MalformedObjectNameException e) {
063: log(
064: Level.SEVERE,
065: "ConfigurationAttributesBean.constructor: Exception when getting MBean object name",
066: e);
067: }
068: }
069:
070: // Reset the data values - only called when a reset is necessary.
071: private void resetBean() {
072: name = (String) getSessionAttribute(ATTR_SELECTED_CONFIG);
073: dn = (String) getSessionAttribute(ATTR_CURRENT_LOCATION_DN);
074: log(Level.FINEST,
075: "ConfigurationAttributesBean.resetBean: name is "
076: + name + ", dn is " + dn);
077: Map attributes = null;
078: try {
079: String[] signature = { String.class.getName(),
080: String.class.getName() };
081: Object[] params = { dn, name };
082: MBeanServerConnection connection = getMBeanServerConnection();
083: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
084: // ReflectionException, RuntimeOperationsException, or IOException
085: attributes = (Map) connection.invoke(objectName,
086: "getConfigurationProperties", params, signature);
087: } catch (Exception ex) {
088: log(
089: Level.SEVERE,
090: "ConfigurationAttributesBean.resetBean: Exception when getting the list of attributes for template "
091: + name, ex);
092: }
093: log(Level.FINEST,
094: "ConfigurationAttributesBean.resetBean: retrieved attributes");
095: Set keys = attributes.keySet();
096: log(Level.FINEST,
097: "ConfigurationAttributesBean.resetBean: attribute keys are: "
098: + keys);
099: String[] basisArray = (String[]) attributes
100: .remove("configDesc");
101: basis = (basisArray != null) ? basisArray[0] : "";
102: String[] encoded = (String[]) attributes.remove("encoded");
103: List encodedList = (encoded != null) ? Arrays.asList(encoded)
104: : Collections.EMPTY_LIST;
105: LinkedList attributesList = new LinkedList();
106: for (Iterator i = keys.iterator(); i.hasNext();) {
107: String key = (String) i.next();
108: String[] values = (String[]) attributes.get(key);
109: String value = (values != null) ? values[0] : null;
110: boolean encrypted = encodedList.contains(key);
111: AttributeBean aBean = new AttributeBean(key, value,
112: encrypted);
113: attributesList.add(aBean);
114: }
115: attributesDataProvider = new ObjectListDataProvider(
116: attributesList);
117: }
118:
119: public DataProvider getAttributes() {
120: log(Level.FINEST,
121: "ConfigurationAttributesBean.getAttributes: Start");
122: if (needsReset()) {
123: resetBean();
124: }
125: return attributesDataProvider;
126: }
127:
128: // Check if the data provider is null, or if the name or dn has changed. Needed because
129: // this is now a session-based bean
130: private boolean needsReset() {
131: if ((dn == null) || (name == null)
132: || (attributesDataProvider == null))
133: return true;
134: if (!dn
135: .equals((String) getSessionAttribute(ATTR_CURRENT_LOCATION_DN)))
136: return true;
137: if (!name
138: .equals((String) getSessionAttribute(ATTR_SELECTED_CONFIG)))
139: return true;
140: // Catch the case when a template is modified after a configuration is viewed
141: if (getSessionAttribute("resetSSOAConfigs") != null) {
142: log(Level.FINEST, "resetSSOAConfigs is non-null");
143: removeFromSession("resetSSOAConfigs");
144: return true;
145: }
146: return false;
147: }
148:
149: public void setAttributes(DataProvider atts) {
150: log(Level.FINEST,
151: "ConfigurationAttributesBean.setAttributes: Start");
152: this .attributesDataProvider = (ObjectListDataProvider) atts;
153: }
154:
155: // Per UI recommendation, go back one screen after saving
156: public String save() {
157: log(Level.FINEST,
158: "ConfigurationAttributesBean.saveAttributes: Start");
159: attributesDataProvider.commitChanges();
160: HashMap attMap = new HashMap();
161: ArrayList encryptedList = new ArrayList();
162:
163: List attList = attributesDataProvider.getList();
164: for (int i = 0; i < attList.size(); i++) {
165: AttributeBean ab = (AttributeBean) attList.get(i);
166: String attname = ab.getName();
167: String value = ab.getSetting();
168: if ((value == null) || (value.length() == 0)) {
169: // Empty value set - do not put name in the return map
170: log(Level.FINEST,
171: "ConfigurationAttributesBean.saveAttributes: Skipping "
172: + attname
173: + " because value is null or empty");
174: } else {
175: log(Level.FINEST,
176: "ConfigurationAttributesBean.saveAttributes: Saving "
177: + attname + "=" + value);
178: String[] values = { value };
179: attMap.put(attname, values);
180: }
181: /*
182: String value = (ab.getSetting()!=null)?ab.getSetting():"";
183: log(Level.FINEST, "ConfigurationAttributesBean.saveAttributes: saving "+ab.getName()+":"+value);
184: String [] values = {value};
185: attMap.put(ab.getName(),values);
186: */
187: }
188: try {
189: String[] signature = { String.class.getName(),
190: String.class.getName(), Map.class.getName() };
191: Object[] params = { dn, name, attMap };
192: MBeanServerConnection connection = getMBeanServerConnection();
193: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
194: // ReflectionException, RuntimeOperationsException, or IOException
195: connection.invoke(objectName, "setConfigurationProperties",
196: params, signature);
197: } catch (Exception ex) {
198: log(
199: Level.SEVERE,
200: "ConfigurationAttributesBean.saveTemplate: Exception when saving the attributes for template "
201: + name, ex);
202: }
203: return cancel();
204: }
205:
206: /* public String addAttribute() {
207: return "addAttribute";
208: }
209: public void removeAttribute() {
210: String name = (String)RadioButton.getSelected("rb");
211: log(Level.FINEST, "ConfigurationAttributesBean.removeAttribute: Selected attribute "+name);
212: // Check admin, then user
213: boolean found = false;
214: List aal = attributesDataProvider.getList();
215: for (int i=0; i<aal.size(); i++) {
216: AttributeBean ab = (AttributeBean) aal.get(i);
217: if (ab.getName().equals(name)){
218: log(Level.FINEST, "ConfigurationAttributesBean.removeAttribute: Found "+name);
219: aal.remove(i);
220: found=true;
221: }
222: }
223: if (!found){
224: log(Level.WARNING, "ConfigurationAttributesBean.removeAttribute: Attempted to remove non-existant attribute "+name);
225: } else {
226: saveAttributes();
227: }
228: }
229: */
230: public String getName() {
231: return name;
232: }
233:
234: public String getDn() {
235: return dn;
236: }
237:
238: public String getDnLabel() {
239: if (GLOBAL_LOCATION_DN.equals(dn)) {
240: return (String) rbMap.get("ssoa.global.dn.label");
241: }
242: return dn;
243: }
244:
245: public String getBasis() {
246: return basis;
247: }
248:
249: public String cancel() {
250: return "cancel";
251: }
252:
253: }
|