001: /**
002: * $Id: AMObjectSearchBean.java,v 1.14 2006/02/08 20:38:22 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.Iterator;
018: import java.util.List;
019: import java.util.ArrayList;
020: import java.util.LinkedList;
021: import java.util.Set;
022: import java.util.HashSet;
023: import java.util.logging.Level;
024:
025: import java.text.MessageFormat;
026:
027: import java.io.IOException;
028:
029: import javax.faces.context.FacesContext;
030: import javax.faces.el.VariableResolver;
031:
032: //JMX
033: import javax.management.MBeanServerConnection;
034: import javax.management.ObjectName;
035: import javax.management.MBeanException;
036: import javax.management.ReflectionException;
037: import javax.management.MalformedObjectNameException;
038: import javax.management.InstanceNotFoundException;
039:
040: import com.sun.web.ui.model.Option;
041: import com.sun.web.ui.component.Checkbox;
042:
043: import com.sun.data.provider.RowKey;
044: import com.sun.data.provider.impl.ListDataProvider;
045: import com.sun.data.provider.impl.ObjectListDataProvider;
046: import com.sun.data.provider.TableDataListener;
047: import com.sun.data.provider.DataProvider;
048: import com.sun.data.provider.TableDataProvider;
049: import com.sun.data.provider.FieldKey;
050:
051: import com.sun.portal.admin.common.util.AdminClientUtil;
052:
053: /**
054: * @author rt94277
055: */
056: public class AMObjectSearchBean extends PSBaseBean implements
057: TableDataListener {
058:
059: private static final int RESULTS_MAX_SIZE = 25;
060:
061: private Option[] objTypes = null;
062: private String objType = null;
063: private Map objectTypes = null;
064: private String searchFilter = "";
065:
066: private ObjectListDataProvider amLocations = null;
067: private ObjectListDataProvider bookmarks = null;
068:
069: private Map rbMap = null;
070:
071: private boolean alert;
072: private String alertSummary;
073: private String alertDetail;
074: private String alertType;
075:
076: private boolean bookmarksdeleted = false;
077:
078: private String jsCalls = null;
079:
080: public AMObjectSearchBean() {
081: rbMap = getResourceStringMap("common");
082: //get objTypes for Bookmarks page
083: objectTypes = queryObjectTypes();
084: objTypes = new Option[objectTypes.size()];
085: int j = 0;
086: for (Iterator i = objectTypes.keySet().iterator(); i.hasNext(); j++) {
087: String key = (String) i.next();
088: String label = getLocalizedString("common",
089: "bookmarks.label." + key);
090: String value = ((Integer) objectTypes.get(key)).toString();
091: if (objType == null) {
092: objType = value;
093: }
094: objTypes[j] = new Option(value, label);
095: }
096:
097: }
098:
099: public Option[] getObjTypes() {
100: return objTypes;
101: }
102:
103: public String getObjType() {
104: return objType;
105: }
106:
107: public void setObjType(String value) {
108: objType = value;
109: }
110:
111: public String getSearchFilter() {
112: return searchFilter;
113: }
114:
115: public void setSearchFilter(String value) {
116: if (!value.equals(searchFilter)) {
117: searchFilter = value;
118: resetbean();
119: }
120: }
121:
122: public void resetbean() {
123: amLocations = null;
124: alert = false;
125: Map resultMap = getAMObjects();
126: if (resultMap != null) {
127: //get the resultcount first.
128: int numResults = resultMap.size();
129: if (numResults > RESULTS_MAX_SIZE) {
130: //setup alert
131: String sm = (String) rbMap
132: .get("bookmarks.searchresults.exceeded");
133: String dm = (String) rbMap
134: .get("bookmarks.searchresults.exceeded.detail");
135: Object[] tokens = { new Integer(numResults).toString() };
136: MessageFormat mf = new MessageFormat(dm);
137: dm = mf.format(tokens);
138:
139: setAlert(true);
140: setAlertSummary(sm);
141: setAlertDetail(dm);
142: setAlertType("information");
143: }
144:
145: List locationList = new ArrayList();
146: Set dnset = resultMap.keySet();
147: Iterator i = dnset.iterator();
148: int j = 0;
149: while (i.hasNext() && j < RESULTS_MAX_SIZE) {
150: String key = (String) i.next();
151: locationList.add(new AMLocation(key, (String) resultMap
152: .get(key)));
153: j++;
154: }
155: amLocations = new ObjectListDataProvider(locationList);
156: ((TableDataProvider) amLocations)
157: .addTableDataListener(this );
158: } else {
159: setupAlert(null, "bookmarks.searchfailed",
160: "bookmarks.searchfailed.cause", "error");
161: amLocations = new ObjectListDataProvider();
162: }
163:
164: }
165:
166: public DataProvider getAmLocations() {
167: if (searchFilter == null || searchFilter.length() == 0) {
168: amLocations = new ObjectListDataProvider();
169: alert = false;
170: }
171: return amLocations;
172: }
173:
174: private Map getAMObjects() {
175: Map resultMap = null;
176: int iotype = 1;
177: String baseDN = getRootSuffix();
178: try {
179: if (objType != null) {
180: iotype = Integer.parseInt(objType);
181: }
182: alert = false;
183: MBeanServerConnection msc = getMBeanServerConnection();
184: ObjectName objName = getAMObjectSearchMBeanObjectName();
185: Object[] params = new Object[] { baseDN, searchFilter,
186: new Integer(iotype), new Integer(2) };
187: String[] signature = new String[] { "java.lang.String",
188: "java.lang.String", "java.lang.Integer",
189: "java.lang.Integer" };
190: resultMap = (Map) msc.invoke(objName, "searchObjects",
191: params, signature);
192: } catch (InstanceNotFoundException infe) {
193: log(Level.SEVERE,
194: "Exception in AMObjectSearchBean.getAMObjects()",
195: infe);
196: //throw infe
197: } catch (MBeanException me) {
198: log(Level.SEVERE,
199: "Exception in AMObjectSearchBean.getAMObjects()",
200: me);
201: //throw me;
202: } catch (ReflectionException re) {
203: log(Level.SEVERE,
204: "Exception in AMObjectSearchBean.getAMObjects()",
205: re);
206: //throw re;
207: } catch (IOException ioe) {
208: log(Level.SEVERE,
209: "Exception in AMObjectSearchBean.getAMObjects()",
210: ioe);
211: //throw ioe
212: } catch (Exception e) {
213: log(Level.SEVERE,
214: "Exception in AMObjectSearchBean.getAMObjects()", e);
215: //throw e;
216: }
217: return resultMap;
218: }
219:
220: public String searchObjects() {
221: resetbean();
222: return "";
223: }
224:
225: public String addBookmarks() {
226: log(Level.FINEST, "adding bookmarks");
227: RowKey[] rkey = amLocations.getRowKeys(1000, null);
228: List selectedcbs = Checkbox.getSelected("dncb");
229: List l = amLocations.getList();
230: Map bkmks = getBookmarks(getUID());
231: for (int index = 0; index < l.size(); index++) {
232: AMLocation location = (AMLocation) l.get(index);
233: if (selectedcbs != null && !selectedcbs.isEmpty()
234: && selectedcbs.contains(location.getDn())) {
235: log(Level.FINEST, "AMLocation selected :"
236: + location.getDn());
237: bkmks.put(location.getDn(), location.getName());
238: }
239: }
240: saveBookmarks(getUID(), bkmks);
241: jsCalls = "refreshParent();";
242: return "";
243: }
244:
245: public DataProvider getBookmarks() {
246: if (bookmarksdeleted) {
247: bookmarksdeleted = false;
248: return bookmarks;
249: }
250: Map bkmks = getBookmarks(getUID());
251: log(Level.FINEST, "bookmarks shown for deletion are: " + bkmks);
252: if (bkmks != null && !bkmks.isEmpty()) {
253: List bkmkList = new ArrayList();
254: Set keys = bkmks.keySet();
255: Iterator i = keys.iterator();
256: while (i.hasNext()) {
257: String bkmk = (String) i.next();
258: String name = (String) bkmks.get(bkmk);
259: bkmkList.add(new AMLocation(bkmk, name));
260: }
261: bookmarks = new ObjectListDataProvider(bkmkList);
262: } else {
263: return new ListDataProvider();
264: }
265: return bookmarks;
266: }
267:
268: public String deleteBookmarks() {
269: RowKey[] rkey = bookmarks.getRowKeys(1000, null);
270: List selectedcbs = Checkbox.getSelected("dncb");
271: List l = bookmarks.getList();
272: Map bkmks = getBookmarks(getUID());
273: for (int index = 0; index < l.size(); index++) {
274: AMLocation location = (AMLocation) l.get(index);
275: if (selectedcbs != null && !selectedcbs.isEmpty()
276: && selectedcbs.contains(location.getDn())) {
277: log(Level.FINEST, "AMLocation selected for deletion :"
278: + location.getDn());
279: bkmks.remove(location.getDn());
280: bookmarks.removeRow(rkey[index]);
281: }
282: }
283: saveBookmarks(getUID(), bkmks);
284: bookmarks.commitChanges();
285: bookmarksdeleted = true;
286: jsCalls = "refreshParent();";
287: return "";
288: }
289:
290: public String getJavaScript() {
291: String currentjs = jsCalls;
292: jsCalls = null;
293: return currentjs;
294: }
295:
296: //============================================================
297: // Alert methods
298: //============================================================
299: public boolean isAlert() {
300: boolean show = alert;
301: alert = false;
302: return show;
303: }
304:
305: public void setAlert(boolean value) {
306: alert = value;
307: }
308:
309: public void setupAlert(String token, String summary, String detail,
310: String type) {
311:
312: String sm = (String) rbMap.get(summary);
313: String dm = (String) rbMap.get(detail);
314:
315: if (token != null) {
316: dm = dm + token;
317: }
318: setAlert(true);
319: setAlertSummary(sm);
320: setAlertDetail(dm);
321: setAlertType(type);
322: }
323:
324: private String getRootSuffix() {
325: String orgDN = null;
326: MBeanServerConnection msc = null;
327: try {
328: msc = getMBeanServerConnection();
329: ObjectName objName = getAMObjectSearchMBeanObjectName();
330: Object[] params = new Object[] {};
331: String[] signature = new String[] {};
332: orgDN = (String) msc.invoke(objName, "queryRootSuffix",
333: params, signature);
334: } catch (InstanceNotFoundException infe) {
335: log(
336: Level.SEVERE,
337: "Exception in AMObjectSearchBean.getDefaultOrgDN()",
338: infe);
339: //throw infe
340: } catch (MBeanException me) {
341: log(
342: Level.SEVERE,
343: "Exception in AMObjectSearchBean.getDefaultOrgDN()",
344: me);
345: //throw me;
346: } catch (ReflectionException re) {
347: log(
348: Level.SEVERE,
349: "Exception in AMObjectSearchBean.getDefaultOrgDN()",
350: re);
351: //throw re;
352: } catch (IOException ioe) {
353: log(
354: Level.SEVERE,
355: "Exception in AMObjectSearchBean.getDefaultOrgDN()",
356: ioe);
357: //throw ioe
358: } catch (Exception e) {
359: log(
360: Level.SEVERE,
361: "Exception in AMObjectSearchBean.getDefaultOrgDN()",
362: e);
363: //throw e;
364: }
365: return orgDN;
366:
367: }
368:
369: private Map queryObjectTypes() {
370: Map objs = null;
371: MBeanServerConnection msc = null;
372: try {
373: msc = getMBeanServerConnection();
374: ObjectName objName = getAMObjectSearchMBeanObjectName();
375: Object[] params = new Object[] {};
376: String[] signature = new String[] {};
377: objs = (Map) msc.invoke(objName, "queryObjectTypes",
378: params, signature);
379: } catch (InstanceNotFoundException infe) {
380: log(
381: Level.SEVERE,
382: "Exception in AMObjectSearchBean.queryObjectTypes()",
383: infe);
384: //throw infe
385: } catch (MBeanException me) {
386: log(
387: Level.SEVERE,
388: "Exception in AMObjectSearchBean.queryObjectTypes()",
389: me);
390: //throw me;
391: } catch (ReflectionException re) {
392: log(
393: Level.SEVERE,
394: "Exception in AMObjectSearchBean.queryObjectTypes()",
395: re);
396: //throw re;
397: } catch (IOException ioe) {
398: log(
399: Level.SEVERE,
400: "Exception in AMObjectSearchBean.queryObjectTypes()",
401: ioe);
402: //throw ioe
403: } catch (Exception e) {
404: log(
405: Level.SEVERE,
406: "Exception in AMObjectSearchBean.queryObjectTypes()",
407: e);
408: //throw e;
409: }
410: return objs;
411: }
412:
413: private ObjectName getAMObjectSearchMBeanObjectName() {
414: ObjectName objName = null;
415: try {
416: LinkedList path = new LinkedList();
417: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
418: path.addFirst("amobjsearch");
419: objName = AdminClientUtil.getResourceMBeanObjectName(
420: AdminClientUtil.AMOBJECTSEARCH_MBEAN_TYPE, path);
421: } catch (MalformedObjectNameException mone) {
422: //throw mone
423: log(
424: Level.SEVERE,
425: "Exception getting MBean Object in AMObjectSearchBean.getAMObjectSearchMBeanObjectName()",
426: mone);
427: }
428: return objName;
429: }
430:
431: public void rowAdded(TableDataProvider provider, RowKey row) {
432: }
433:
434: public void rowRemoved(TableDataProvider provider, RowKey row) {
435: }
436:
437: public void providerChanged(DataProvider provider) {
438: }
439:
440: public void valueChanged(TableDataProvider provider,
441: FieldKey fieldKey, RowKey row, Object oldValue,
442: Object newValue) {
443: AMLocation changedlocation = (AMLocation) ((ObjectListDataProvider) provider)
444: .getObject(row);
445: if (newValue != null) {
446: log(Level.FINEST, "new value=" + newValue);
447: String id = fieldKey.getFieldId();
448: List l = amLocations.getList();
449: for (int index = 0; index < l.size(); index++) {
450: AMLocation location = (AMLocation) l.get(index);
451: if (location.getDn().equals(changedlocation.getDn())) {
452: if (id.equals("name")) {
453: location.setName((String) newValue);
454: }
455: }
456: }
457: }
458: amLocations.commitChanges();
459: }
460:
461: public void valueChanged(DataProvider provider, FieldKey fieldKey,
462: Object oldValue, Object newValue) {
463: }
464:
465: }
|