001: /**
002: * $Id: TemplateAttributesBean.java,v 1.9 2005/11/03 23:55:10 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.Checkbox;
026: import com.sun.web.ui.model.Option;
027:
028: import com.sun.portal.admin.common.util.AdminClientUtil;
029:
030: import javax.management.ObjectName;
031: import javax.management.MBeanServerConnection;
032: import javax.management.MalformedObjectNameException;
033:
034: import com.sun.portal.admin.console.common.PSBaseBean;
035:
036: // This class should be instantiated on a request-basis
037: public class TemplateAttributesBean extends PSBaseBean {
038:
039: public static final String ATTR_SELECTED_TEMPLATE = "ssoa.template.selected";
040: public static final String RB_NAME = "ssoa";
041: // ATTR_TYPE is used to determine the type of attribute to create in
042: // CreateAttribute.jsp
043: public static final String ATTR_TYPE = "ssoa.attribute.type";
044: public static final String ADMIN_TYPE = "admin";
045: public static final String USER_TYPE = "user";
046: protected Map rbMap;
047:
048: protected ObjectListDataProvider adminAttributes = null;
049: protected ObjectListDataProvider userAttributes = null;
050: private ObjectName objectName = null;
051: private String configName = null;
052:
053: protected Option[] encryptOptions = null;
054:
055: /** Creates a new instance of TemplateAttributesBean*/
056: public TemplateAttributesBean() {
057: log(Level.FINEST, "TemplateAttributesBean.constructor start");
058: // Get resource bundle
059: rbMap = getResourceStringMap(RB_NAME);
060: // MBean path of SSOAdapter.
061: LinkedList path = new LinkedList();
062: path.addFirst(getDomain());
063: path.addFirst("ssoadapter");
064: // configName = (String)getSessionAttribute(ATTR_SELECTED_TEMPLATE);
065: try {
066: objectName = AdminClientUtil.getResourceMBeanObjectName(
067: AdminClientUtil.SSOADAPTER_MBEAN_TYPE, path);
068: } catch (MalformedObjectNameException e) {
069: log(
070: Level.SEVERE,
071: "TemplateAttributesBean.constructor: Exception when getting MBean object name",
072: e);
073: }
074:
075: encryptOptions = new Option[2];
076: encryptOptions[0] = new Option(Boolean.FALSE, (String) rbMap
077: .get("option.no"));
078: encryptOptions[1] = new Option(Boolean.TRUE, (String) rbMap
079: .get("option.yes"));
080: }
081:
082: protected void resetAttributes() {
083: configName = (String) getSessionAttribute(ATTR_SELECTED_TEMPLATE);
084: log(Level.FINEST,
085: "TemplateAttributesBean.resetAttributes: configName is "
086: + configName);
087: Map attributes = null;
088: try {
089: String[] signature = { String.class.getName() };
090: Object[] params = { configName };
091: MBeanServerConnection connection = getMBeanServerConnection();
092: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
093: // ReflectionException, RuntimeOperationsException, or IOException
094: attributes = (Map) connection.invoke(objectName,
095: "getTemplateProperties", params, signature);
096: } catch (Exception ex) {
097: log(
098: Level.SEVERE,
099: "TemplateAttributesBean.resetAttributes: Exception when getting the list of attributes for template "
100: + configName, ex);
101: }
102: log(Level.FINEST,
103: "TemplateAttributesBean.resetAttributes: retrieved attributes");
104: log(Level.FINEST,
105: "TemplateAttributesBean.resetAttributes: attribute keys are: "
106: + attributes.keySet());
107: // Create empty array for missing data
108: String[] emptyArray = {};
109:
110: // Get the list encrypted attributes
111: String[] encryptedProps = (String[]) attributes
112: .remove("encoded");
113: if (encryptedProps == null) {
114: // Use an empty array
115: log(Level.FINEST,
116: "Setting encryptedProps to an empty array");
117: encryptedProps = emptyArray;
118: }
119: List encryptedList = Arrays.asList(encryptedProps);
120: log(Level.FINEST,
121: "TemplateAttributesBean.resetAttributes: have "
122: + encryptedProps.length
123: + " encoded properties:" + encryptedList);
124:
125: // Get User defined attributes
126: LinkedList userAttributesList = new LinkedList();
127: String[] userProps = (String[]) attributes.remove("merge");
128: if (userProps == null) {
129: log(Level.FINEST, "Setting userProps to an empty array");
130: userProps = emptyArray;
131: }
132: List userList = Arrays.asList(userProps);
133: log(Level.FINEST,
134: "TemplateAttributesBean.resetAttributes: have "
135: + userProps.length + " user properties");
136: for (int i = 0; i < userProps.length; i++) {
137: // User properties will not have values
138: String name = userProps[i];
139: // boolean encoded = (Arrays.binarySearch(encryptedProps,name) >= 0) ? true : false;
140: boolean encoded = encryptedList.contains(name);
141: // log(Level.FINEST, "TemplateAttributesBean.resetAttributes: user properties "+i+": "+userProps[i]+" encoded: "+encoded);
142: userAttributesList.add(new AttributeBean(userProps[i],
143: null, encoded));
144: }
145: if (userAttributesList.size() > 0)
146: Collections.sort(userAttributesList);
147: userAttributes = new ObjectListDataProvider(userAttributesList);
148:
149: // Get Admin defined attributes
150: LinkedList adminAttributesList = new LinkedList();
151: String[] adminProps = (String[]) attributes.remove("default");
152: if (adminProps == null) {
153: log(Level.FINEST, "Setting adminProps to an empty array");
154: adminProps = emptyArray;
155: }
156: log(Level.FINEST,
157: "TemplateAttributesBean.resetAttributes: have "
158: + adminProps.length + " admin properties");
159: for (int i = 0; i < adminProps.length; i++) {
160: // Admin properties should have values
161: String name = adminProps[i];
162: // Make sure property is not defined in both lists
163: if (userList.contains(name)) {
164: log(Level.WARNING, "Dual-type property found: " + name);
165: } else {
166: boolean encoded = encryptedList.contains(name);
167: log(Level.FINEST, "Search result for " + name + " in "
168: + encryptedList + " is "
169: + encryptedList.contains(name));
170:
171: String[] values = (String[]) attributes.remove(name);
172: // String value = (values != null)?values[0]:null;
173: String value = (values != null) ? values[0] : "";
174: log(Level.FINEST, "Admin property " + i + ": " + name
175: + " value: " + value + " encoded: " + encoded);
176: AttributeBean aBean = new AttributeBean(name, value,
177: encoded);
178: adminAttributesList.add(aBean);
179: }
180: }
181:
182: // Check for leftovers
183: Iterator iter = attributes.keySet().iterator();
184: while (iter.hasNext()) {
185: String name = (String) iter.next();
186: if (userList.contains(name)) {
187: log(Level.WARNING,
188: "Ignoring user attribute with value defined: "
189: + name);
190: } else {
191: boolean encoded = encryptedList.contains(name);
192: String[] values = (String[]) attributes.get(name);
193: // String value = (values != null)?values[0]:null;
194: String value = (values != null) ? values[0] : "";
195: log(Level.WARNING,
196: "Adding leftover attribute to admin-defined attributes: "
197: + name + " has value: " + value);
198: AttributeBean aBean = new AttributeBean(name, value,
199: encoded);
200: adminAttributesList.add(aBean);
201: }
202: }
203: if (adminAttributesList.size() > 0)
204: Collections.sort(adminAttributesList);
205: adminAttributes = new ObjectListDataProvider(
206: adminAttributesList);
207: }
208:
209: // Check if the data provider is null, or if the name or dn has changed. Needed because
210: // this is now a session-based bean
211: private boolean needsReset() {
212: if ((configName == null) || (adminAttributes == null)
213: || (userAttributes == null))
214: return true;
215: if (!configName
216: .equals((String) getSessionAttribute(ATTR_SELECTED_TEMPLATE)))
217: return true;
218: if (getSessionAttribute("resetSSOA") != null) {
219: log(Level.FINEST, "resetSSOA is non-null");
220: removeFromSession("resetSSOA");
221: return true;
222: }
223: return false;
224: }
225:
226: public DataProvider getAdminAttributes() {
227: //if (adminAttributes == null) resetAttributes();
228: log(Level.FINEST,
229: "TemplateAttributesBean.getAdminAttributes: Start");
230: if (needsReset())
231: resetAttributes();
232: return adminAttributes;
233: }
234:
235: public Option[] getEncryptOptions() {
236: return encryptOptions;
237: }
238:
239: /*public void setAdminAttributes(DataProvider aadp) {
240: log(Level.FINEST, "TemplateAttributesBean.setAdminAttributes: Start");
241: this.adminAttributes=aadp;
242: }*/
243:
244: public DataProvider getUserAttributes() {
245: log(Level.FINEST,
246: "TemplateAttributesBean.getUserAttributes: Start");
247: if (needsReset())
248: resetAttributes();
249: // if (userAttributes == null) resetAttributes();
250: return userAttributes;
251: }
252:
253: /*public void setUserAttributes(DataProvider uadp) {
254: log(Level.FINEST, "TemplateAttributesBean.setUserAttributes: Start");
255: this.userAttributes=uadp;
256: }*/
257:
258: public void saveAttributes() {
259: log(Level.FINEST,
260: "TemplateAttributesBean.saveAttributes: Start");
261: saveTemplate();
262: }
263:
264: // Per UI recommendation, go back one screen after saving
265: public String save() {
266: log(Level.FINEST, "TemplateAttributesBean.save: Start");
267: // New requirement for objectlistdataproviders when saving from an html page
268: // This used to be part of saveTemplate(), but manipulating the dataproviders then
269: // calling commitChanges() was resulting in an arrayoutofbounds exception.
270: userAttributes.commitChanges();
271: adminAttributes.commitChanges();
272: saveTemplate();
273: resetAttributes(); // Too much caching going on.
274: return cancel();
275: }
276:
277: public void saveTemplate() {
278: log(Level.FINEST, "TemplateAttributesBean.saveTemplate: Start");
279: HashMap c = new HashMap();
280: ArrayList encryptedList = new ArrayList();
281: // User Defined properties
282: List ual = userAttributes.getList();
283: ArrayList uaal = new ArrayList(ual.size());
284: for (int i = 0; i < ual.size(); i++) {
285: AttributeBean ab = (AttributeBean) ual.get(i);
286: uaal.add(ab.getName());
287: log(Level.FINEST,
288: "TemplateAttributesBean.saveTemplate: User attribute:"
289: + ab.getName() + " encrypt:"
290: + ab.getEncoded());
291: if (ab.getEncoded()) {
292: encryptedList.add(ab.getName());
293: }
294: }
295: c.put("merge", (String[]) uaal.toArray(new String[0]));
296: // Admin Defined properties
297: List aal = adminAttributes.getList();
298: ArrayList aaal = new ArrayList(aal.size());
299: for (int i = 0; i < aal.size(); i++) {
300: AttributeBean ab = (AttributeBean) aal.get(i);
301: String attname = ab.getName();
302: aaal.add(attname);
303: //log(Level.FINEST, "TemplateAttributesBean.saveTemplate: Admin attribute:"+attname+" value:"+ab.getSetting()+" encrypt:"+ab.getEncoded());
304: if (ab.getEncoded()) {
305: log(Level.FINEST,
306: "TemplateAttributesBean.saveTemplate: Adding to encoded admin attribute:"
307: + attname);
308: encryptedList.add(attname);
309: }
310: String value = ab.getSetting();
311: if ((value == null) || (value.length() == 0)) {
312: // Empty value set - do not put name in the return map
313: log(Level.FINEST,
314: "TemplateAttributesBean.saveAttributes: Skipping "
315: + attname
316: + " because value is null or empty");
317: } else {
318: log(Level.FINEST,
319: "TemplateAttributesBean.saveAttributes: Saving "
320: + attname + "=" + value);
321: String[] values = { value };
322: c.put(attname, values);
323: }
324: }
325: c.put("default", (String[]) aaal.toArray(new String[0]));
326: // Encoded properties
327: log(Level.FINEST,
328: "TemplateAttributesBean.saveTemplate: Encoded array size:"
329: + encryptedList.size());
330: c.put("encoded", (String[]) encryptedList
331: .toArray(new String[0]));
332: try {
333: String[] signature = { String.class.getName(),
334: Map.class.getName() };
335: Object[] params = { configName, c };
336: MBeanServerConnection connection = getMBeanServerConnection();
337: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
338: // ReflectionException, RuntimeOperationsException, or IOException
339: connection.invoke(objectName, "setTemplateProperties",
340: params, signature);
341: } catch (Exception ex) {
342: log(
343: Level.SEVERE,
344: "TemplateAttributesBean.saveTemplate: Exception when saving the attributes for template "
345: + configName, ex);
346: }
347: // Make sure the ConfigurationAttributesBean gets reset
348: setSessionAttribute("resetSSOAConfigs", "true");
349: }
350:
351: public void removeAdminAttributes() {
352: log(Level.FINEST,
353: "TemplateAttributesBean.removeAdminAttributes: Start");
354: //String name = (String)RadioButton.getSelected("rb");
355: List selectedcbs = Checkbox.getSelected("admincb");
356: List attributeList = adminAttributes.getList();
357: removeAttributes(selectedcbs, attributeList);
358: }
359:
360: public void removeUserAttributes() {
361: log(Level.FINEST,
362: "TemplateAttributesBean.removeUserAttributes: Start");
363: List selectedcbs = Checkbox.getSelected("usercb");
364: List attributeList = userAttributes.getList();
365: removeAttributes(selectedcbs, attributeList);
366: }
367:
368: public void removeAttributes(List attributesToRemove,
369: List attributesList) {
370: log(Level.FINEST,
371: "TemplateAttributesBean.removeAttributes: Start");
372: int removalCount = 0;
373: for (Iterator i = attributesToRemove.iterator(); i.hasNext();) {
374: String name = (String) i.next();
375: boolean found = false;
376: for (int j = 0; j < attributesList.size() && !found; j++) {
377: AttributeBean ab = (AttributeBean) attributesList
378: .get(j);
379: if (ab.getName().equals(name)) {
380: log(Level.FINEST,
381: "TemplateAttributesBean.removeAttribute: Removing "
382: + name);
383: attributesList.remove(j);
384: found = true;
385: }
386: }
387: if (found) {
388: removalCount++;
389: } else {
390: log(
391: Level.WARNING,
392: "TemplateAttributesBean.removeUserAttributes: Attempted to remove non-existant attribute "
393: + name);
394: }
395: }
396: if (removalCount > 0) {
397: log(Level.FINEST,
398: "TemplateAttributesBean.removeUserAttributes: Saving template");
399: saveTemplate();
400: }
401: }
402:
403: public void makeUserAttribute() {
404: log(Level.FINEST,
405: "TemplateAttributesBean.makeUserAttribute: Start");
406: List selectedcbs = Checkbox.getSelected("admincb");
407: log(Level.FINEST, "TemplateAttributesBean.makeUserAttribute: "
408: + selectedcbs.size() + " selected attributes");
409: int removalCount = 0;
410: for (Iterator i = selectedcbs.iterator(); i.hasNext();) {
411: //String name = (String)RadioButton.getSelected("rb");
412: String name = (String) i.next();
413: log(Level.FINEST,
414: "TemplateAttributesBean.makeUserAttribute: Selected attribute "
415: + name);
416: boolean found = false;
417: List aal = adminAttributes.getList();
418: for (int j = 0; j < aal.size() && !found; j++) {
419: AttributeBean ab = (AttributeBean) aal.get(j);
420: if (ab.getName().equals(name)) {
421: log(Level.FINEST,
422: "TemplateAttributesBean.makeUserAttribute: Found "
423: + name);
424: List ual = userAttributes.getList();
425: ual.add(aal.remove(j));
426: //userAttributes.setList(ual);
427: //adminAttributes.setList(aal);
428: found = true;
429: }
430: }
431: if (found) {
432: removalCount++;
433: } else {
434: log(Level.WARNING,
435: "TemplateAttributesBean.makeUserAttribute: Could not find admin attribute "
436: + name);
437: }
438: }
439: if (removalCount > 0) {
440: log(Level.FINEST,
441: "TemplateAttributesBean.makeUserAttribute: Saving template");
442: saveTemplate();
443: }
444: }
445:
446: public void makeAdminAttribute() {
447: log(Level.FINEST,
448: "TemplateAttributesBean.makeAdminAttribute: Start");
449: List selectedcbs = Checkbox.getSelected("usercb");
450: int removalCount = 0;
451: for (Iterator i = selectedcbs.iterator(); i.hasNext();) {
452: //String name = (String)RadioButton.getSelected("rb");
453: String name = (String) i.next();
454: log(Level.FINEST,
455: "TemplateAttributesBean.makeAdminAttribute: Selected attribute "
456: + name);
457: boolean found = false;
458: List ual = userAttributes.getList();
459: for (int j = 0; j < ual.size() && !found; j++) {
460: AttributeBean ab = (AttributeBean) ual.get(j);
461: if (ab.getName().equals(name)) {
462: log(Level.FINEST,
463: "TemplateAttributesBean.makeAdminAttribute: Found "
464: + name);
465: List aal = adminAttributes.getList();
466: aal.add(ual.remove(j));
467: //userAttributes.setList(ual);
468: //adminAttributes.setList(aal);
469: found = true;
470: }
471: }
472: if (found) {
473: removalCount++;
474: } else {
475: log(Level.WARNING,
476: "TemplateAttributesBean.makeAdminAttribute: Could not find user attribute "
477: + name);
478: }
479: }
480: if (removalCount > 0) {
481: log(Level.FINEST,
482: "TemplateAttributesBean.makeAdminAttribute: Saving template");
483: saveTemplate();
484: }
485: }
486:
487: /*
488: public void makeAdminAttribute() {
489: String name = (String)RadioButton.getSelected("rb");
490: log(Level.FINEST, "TemplateAttributesBean.makeAdminAttribute: Selected attribute "+name);
491: List ual = userAttributes.getList();
492: for (int i=0; i<ual.size(); i++) {
493: AttributeBean ab = (AttributeBean) ual.get(i);
494: if (ab.getName().equals(name)){
495: log(Level.FINEST, "TemplateAttributesBean.makeAdminAttribute: Found "+name);
496: List aal = adminAttributes.getList();
497: aal.add(ual.remove(i));
498: //userAttributes.setList(ual);
499: //adminAttributes.setList(aal);
500: saveTemplate();
501: return;
502: }
503: }
504: log(Level.WARNING, "TemplateAttributesBean.makeUserAttribute: Could not find admin attribute "+name);
505: }*/
506:
507: public String cancel() {
508: return "cancel";
509: }
510:
511: public String gotoAddAdminAttribute() {
512: setSessionAttribute(ATTR_TYPE, ADMIN_TYPE);
513: return "gotoAddMetaAttribute";
514: }
515:
516: public String gotoAddUserAttribute() {
517: setSessionAttribute(ATTR_TYPE, USER_TYPE);
518: return "gotoAddAdapterAttribute";
519: }
520: }
|