001: /**
002: * $Id: AMLocationBarBean.java,v 1.13 2006/04/06 22:39:03 rt94277 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.common;
014:
015: import java.util.Map;
016: import java.util.HashMap;
017: import java.util.TreeMap;
018: import java.util.Set;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.ArrayList;
022: import java.util.LinkedList;
023: import java.util.logging.Level;
024:
025: import java.io.IOException;
026:
027: //JMX
028: import javax.management.MBeanServerConnection;
029: import javax.management.ObjectName;
030: import javax.management.MBeanException;
031: import javax.management.ReflectionException;
032: import javax.management.MalformedObjectNameException;
033: import javax.management.InstanceNotFoundException;
034: import javax.faces.validator.ValidatorException;
035: import javax.faces.context.FacesContext;
036: import javax.faces.component.UIComponent;
037: import javax.faces.application.FacesMessage;
038: import javax.faces.event.ValueChangeEvent;
039:
040: import com.sun.web.ui.model.Option;
041: import com.sun.web.ui.component.Checkbox;
042: import com.sun.portal.admin.common.util.AdminClientUtil;
043:
044: /**
045: * @author rt94277
046: */
047: public class AMLocationBarBean extends PSBaseBean {
048:
049: private List locationDNs = null;
050: private Object newLocationDN = "";
051: private String defaultOrg = null;
052: private String rootSuffix = null;
053: private Map orgDNs = null;
054: private String locationText = null;
055: private Object currentDN = null;
056: private Option emptyItem = null;
057: private List dropdowndns = null;
058: //flag set by the AMObjectSearchBean from the bookmarks page
059: //when the bookmarks page switches the currentDN.
060: private boolean emptyDNSet = false;
061:
062: private static final String RB_NAME = "common";
063: private static final String SPACE_STRING = " ";
064: private static final String EMPTY_DN = "emptyDN";
065:
066: private String jsCalls = null;
067:
068: public AMLocationBarBean() {
069: defaultOrg = getDefaultOrgDN();
070: rootSuffix = getRootSuffix();
071: orgDNs = getOrgDNs();
072: if (getSessionAttribute(ATTR_CURRENT_LOCATION_DN) == null) {
073: setSessionAttribute(ATTR_CURRENT_LOCATION_DN, defaultOrg);
074: }
075: currentDN = getSessionAttribute(ATTR_CURRENT_LOCATION_DN);
076: locationText = getLocalizedString("common",
077: "locationbar.label.locationtext");
078: newLocationDN = locationText;
079: emptyItem = new Option(EMPTY_DN, "======");
080: emptyItem.setDisabled(true);
081: dropdowndns = new ArrayList();
082: }
083:
084: public List getLocationDNs() {
085: log(Level.FINEST, "getting locations dns for dropdown");
086: locationDNs = new ArrayList();
087: locationDNs.add(emptyItem);
088: Map bookmarks = getBookmarkDNs();
089: int j = 0;
090: for (Iterator i = bookmarks.keySet().iterator(); i.hasNext(); j++) {
091: String dn = (String) i.next();
092: String label = (String) bookmarks.get(dn);
093: locationDNs.add(new Option(dn, label));
094: dropdowndns.add(dn);
095: }
096: return locationDNs;
097: }
098:
099: //location dn selected in the dn dropdown
100: public Object getSelectedLocationDN() {
101: log(Level.FINEST, "currentDN in getSelectedLocationDN ="
102: + currentDN);
103: //set the selection to empty if the current dn from session is not in the
104: //dropdown list of items.
105: if (dropdowndns != null && !dropdowndns.isEmpty()
106: && !dropdowndns.contains(getCurrentLocationDN())
107: && !currentDN.equals(EMPTY_DN)) {
108: log(Level.FINEST,
109: "returning empty dn for dropdown selection");
110: return EMPTY_DN;
111: }
112: return currentDN;
113: }
114:
115: public void setSelectedLocationDN(Object locationDN) {
116: //set the selection only when the emptyDNset flag is not set
117: if (locationDN != null && ((String) locationDN).length() != 0
118: && !currentDN.equals(locationDN) && !emptyDNSet) {
119: currentDN = locationDN;
120: //set only when dn is not an empty item
121: if (locationDN != null
122: && !((String) locationDN).equals(EMPTY_DN)) {
123: log(Level.FINEST, "setting selected location dn to "
124: + locationDN);
125: setCurrentLocationDN(locationDN);
126: }
127: }
128: //reset the emptydnset flag.
129: emptyDNSet = false;
130: }
131:
132: public Object getCurrentLocationDN() {
133: log(Level.FINEST, "getting session attribute:" + getCurrentDN());
134: return getCurrentDN();
135: }
136:
137: public void setCurrentLocationDN(Object locationDN) {
138: log(Level.FINEST, "setting session atrribute:" + locationDN);
139: setCurrentDN(locationDN);
140: }
141:
142: //always return the location text for the textfield
143: public Object getNewLocationDN() {
144: return locationText;
145: }
146:
147: //set the new location dn from the textfield
148: public void setNewLocationDN(Object newLocationDN) {
149: if (!newLocationDN.equals(locationText)
150: && !newLocationDN.equals("")) {
151: log(Level.FINEST, "setting location from textfield:"
152: + newLocationDN);
153: this .newLocationDN = newLocationDN;
154: log(Level.FINEST, "setting new location dn to :"
155: + newLocationDN);
156: setCurrentLocationDN(newLocationDN);
157: currentDN = newLocationDN;
158: this .newLocationDN = locationText;
159: }
160: }
161:
162: //set a flag so that the valuechangelistener doesn't change
163: //the value back to the previous DN when the page is refreshed
164: //programmatically.
165: public void setEmptyDN() {
166: emptyDNSet = true;
167: }
168:
169: public String getJavaScript() {
170: String currentjs = jsCalls;
171: jsCalls = null;
172: return currentjs;
173: }
174:
175: public String switchCurrentDN() {
176: log(Level.FINEST, "switching DN");
177: List selectedcbs = Checkbox.getSelected("dncb");
178: if (selectedcbs != null && !selectedcbs.isEmpty()) {
179: String newdn = (String) selectedcbs.get(0);
180: setCurrentLocationDN(newdn);
181: currentDN = newdn;
182: setEmptyDN();
183: }
184:
185: jsCalls = "refreshParent();";
186: return "";
187: }
188:
189: public String changeLocationDN() {
190: return "";
191: }
192:
193: public String getDnDisplayName() {
194: String dn = (String) getCurrentDN();
195: if (isGlobalDN(dn)) {
196: dn = GLOBAL_LABEL;
197: }
198: return dn;
199: }
200:
201: public Map getBookmarkDNs() {
202: Map bookmarks = new TreeMap();
203: bookmarks.put(GLOBAL_LOCATION_DN, GLOBAL_LABEL
204: + SPACE_STRING
205: + getLocalizedString(RB_NAME,
206: "locationbar.label.globalobject"));
207: Map orgBookmarks = new TreeMap();
208: Map roleBookmarks = new TreeMap();
209: Map userBookmarks = new TreeMap();
210: if (orgDNs != null) {
211: Set dnSet = orgDNs.keySet();
212: Iterator iter = dnSet.iterator();
213: int j = 0;
214: while (iter.hasNext() && j < 10) {
215: String key = (String) iter.next();
216: orgBookmarks.put(key, (String) orgDNs.get(key)
217: + SPACE_STRING
218: + getLocalizedString(RB_NAME,
219: "locationbar.label.orgobject"));
220: j++;
221: }
222: }
223: Map savedBkmks = getBookmarks(getUID());
224: if (!savedBkmks.isEmpty()) {
225: Set keys = savedBkmks.keySet();
226: Iterator i = keys.iterator();
227: while (i.hasNext()) {
228: String dn = (String) i.next();
229: String name = (String) savedBkmks.get(dn);
230: String dntype = null;
231: if (isRoleDN(dn)) {
232: dntype = getLocalizedString(RB_NAME,
233: "locationbar.label.roleobject");
234: roleBookmarks.put(dn, name + SPACE_STRING + dntype);
235: } else if (isUserDN(dn)) {
236: dntype = getLocalizedString(RB_NAME,
237: "locationbar.label.userobject");
238: userBookmarks.put(dn, name + SPACE_STRING + dntype);
239: } else if (isOrgDN(dn)) {
240: dntype = getLocalizedString(RB_NAME,
241: "locationbar.label.orgobject");
242: orgBookmarks.put(dn, name + SPACE_STRING + dntype);
243: } else if (isFilteredRoleDN(dn)) {
244: dntype = getLocalizedString(RB_NAME,
245: "locationbar.label.filteredroleobject");
246: orgBookmarks.put(dn, name + SPACE_STRING + dntype);
247: } else {
248: dntype = "";
249: bookmarks.put(dn, name + SPACE_STRING + dntype);
250: }
251:
252: }
253: }
254: bookmarks.putAll(orgBookmarks);
255: bookmarks.putAll(roleBookmarks);
256: bookmarks.putAll(userBookmarks);
257: return bookmarks;
258: }
259:
260: public void validateDN(FacesContext ctx, UIComponent comp,
261: Object value) {
262: String dn = (String) value;
263: if (dn != null && !dn.equals(EMPTY_DN)
264: && !dn.equals(locationText)) {
265: if (!isValidEntry(dn)) {
266: throw new ValidatorException(new FacesMessage(
267: "validation error", "Please Enter a Valid DN"));
268: }
269: }
270: }
271:
272: private boolean isValidEntry(String dn) {
273: boolean valid = false;
274: MBeanServerConnection msc = null;
275: try {
276: msc = getMBeanServerConnection();
277: ObjectName objName = getAMObjectSearchMBeanObjectName();
278: Object[] params = new Object[] { dn };
279: String[] signature = new String[] { "java.lang.String" };
280: valid = ((Boolean) msc.invoke(objName, "isValidDN", params,
281: signature)).booleanValue();
282: } catch (InstanceNotFoundException infe) {
283: log(Level.SEVERE, "Exception message is: "
284: + infe.getCause().getMessage());
285: //throw infe
286: } catch (MBeanException me) {
287: log(Level.SEVERE, "Exception message is: "
288: + me.getCause().getMessage());
289: //throw me
290: } catch (ReflectionException re) {
291: log(Level.SEVERE, "Exception message is: "
292: + re.getCause().getMessage());
293: //throw re
294: } catch (IOException ioe) {
295: log(Level.SEVERE, "Exception message is: "
296: + ioe.getCause().getMessage());
297: //throw ioe
298: } catch (Exception e) {
299: log(Level.SEVERE, "Exception message is: "
300: + e.getCause().getMessage());
301: }
302: return valid;
303: }
304:
305: private String getDefaultOrgDN() {
306: String orgDN = null;
307: MBeanServerConnection msc = null;
308: try {
309: msc = getMBeanServerConnection();
310: ObjectName objName = getAMObjectSearchMBeanObjectName();
311: Object[] params = new Object[] {};
312: String[] signature = new String[] {};
313: orgDN = (String) msc.invoke(objName, "queryDefaultOrgDN",
314: params, signature);
315: } catch (InstanceNotFoundException infe) {
316: log(Level.SEVERE,
317: "Exception in AMLocationBarBean.getDefaultOrgDN()",
318: infe);
319: } catch (MBeanException me) {
320: log(Level.SEVERE,
321: "Exception in AMLocationBarBean.getDefaultOrgDN()",
322: me);
323: } catch (ReflectionException re) {
324: log(Level.SEVERE,
325: "Exception in AMLocationBarBean.getDefaultOrgDN()",
326: re);
327: } catch (IOException ioe) {
328: log(Level.SEVERE,
329: "Exception in AMLocationBarBean.getDefaultOrgDN()",
330: ioe);
331: } catch (Exception e) {
332: log(Level.SEVERE,
333: "Exception in AMLocationBarBean.getDefaultOrgDN()",
334: e);
335: }
336: return orgDN;
337:
338: }
339:
340: private String getRootSuffix() {
341: String rootDN = null;
342: MBeanServerConnection msc = null;
343: try {
344: msc = getMBeanServerConnection();
345: ObjectName objName = getAMObjectSearchMBeanObjectName();
346: Object[] params = new Object[] {};
347: String[] signature = new String[] {};
348: rootDN = (String) msc.invoke(objName, "queryRootSuffix",
349: params, signature);
350: } catch (InstanceNotFoundException infe) {
351: log(Level.SEVERE,
352: "Exception in AMLocationBarBean.getRootSuffix()",
353: infe);
354:
355: } catch (MBeanException me) {
356: log(Level.SEVERE,
357: "Exception in AMLocationBarBean.getRootSuffix()",
358: me);
359:
360: } catch (ReflectionException re) {
361: log(Level.SEVERE,
362: "Exception in AMLocationBarBean.getRootSuffix()",
363: re);
364:
365: } catch (IOException ioe) {
366: log(Level.SEVERE,
367: "Exception in AMLocationBarBean.getRootSuffix()",
368: ioe);
369:
370: } catch (Exception e) {
371: log(Level.SEVERE,
372: "Exception in AMLocationBarBean.getRootSuffix()", e);
373:
374: }
375: return rootDN;
376:
377: }
378:
379: private Map getOrgDNs() {
380: Map orgDNs = null;
381: MBeanServerConnection msc = null;
382: try {
383: msc = getMBeanServerConnection();
384: ObjectName objName = getAMObjectSearchMBeanObjectName();
385: Object[] params = new Object[] { rootSuffix, "*",
386: new Integer(2), new Integer(2) };
387: String[] signature = new String[] { "java.lang.String",
388: "java.lang.String", "java.lang.Integer",
389: "java.lang.Integer" };
390: orgDNs = (Map) msc.invoke(objName, "searchObjects", params,
391: signature);
392: } catch (InstanceNotFoundException infe) {
393: log(Level.SEVERE,
394: "Exception in AMLocationBarBean.getDefaultOrgDN()",
395: infe);
396:
397: } catch (MBeanException me) {
398: log(Level.SEVERE,
399: "Exception in AMLocationBarBean.getDefaultOrgDN()",
400: me);
401:
402: } catch (ReflectionException re) {
403: log(Level.SEVERE,
404: "Exception in AMLocationBarBean.getDefaultOrgDN()",
405: re);
406:
407: } catch (IOException ioe) {
408: log(Level.SEVERE,
409: "Exception in AMLocationBarBean.getDefaultOrgDN()",
410: ioe);
411:
412: } catch (Exception e) {
413: log(Level.SEVERE,
414: "Exception in AMLocationBarBean.getDefaultOrgDN()",
415: e);
416: }
417: return orgDNs;
418:
419: }
420:
421: private ObjectName getAMObjectSearchMBeanObjectName() {
422: ObjectName objName = null;
423: try {
424: LinkedList path = new LinkedList();
425: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
426: path.addFirst("amobjsearch");
427: objName = AdminClientUtil.getResourceMBeanObjectName(
428: AdminClientUtil.AMOBJECTSEARCH_MBEAN_TYPE, path);
429: } catch (MalformedObjectNameException mone) {
430: log(
431: Level.SEVERE,
432: "Exception getting MBean Object in AMLocationBarBean.getAMObjectSearchMBeanObjectName()",
433: mone);
434: }
435: return objName;
436: }
437:
438: public void processLocationDNChange(ValueChangeEvent evt) {
439: log(Level.FINEST, "Inside valueChangelistener");
440: String dn = (String) evt.getNewValue();
441: //change the value only when the emptyDN flag is not set.
442: if (dn != null && dn.length() != 0 && !dn.equals(EMPTY_DN)
443: && !emptyDNSet) {
444: log(Level.FINEST,
445: "Processing Value Change for Location DN: New DN="
446: + dn);
447: setCurrentLocationDN(dn);
448: currentDN = dn;
449: }
450: }
451:
452: }
|