001: /**
002: * $Id: ConsumerBean.java,v 1.10 2005/09/21 15:30:00 fo160993 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.wsrp.consumer;
014:
015: import java.util.ArrayList;
016: import java.util.Collections;
017: import java.util.HashMap;
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Set;
023: import java.util.logging.Level;
024:
025: import javax.faces.context.FacesContext;
026: import javax.faces.el.ValueBinding;
027: import javax.management.ObjectName;
028:
029: import com.sun.data.provider.FieldKey;
030: import com.sun.data.provider.RowKey;
031: import com.sun.data.provider.impl.ObjectListDataProvider;
032: import com.sun.portal.admin.common.AttrOptionConstants;
033: import com.sun.portal.admin.common.util.AdminUtil;
034: import com.sun.web.ui.component.TableRowGroup;
035: import com.sun.web.ui.event.TableSelectPhaseListener;
036: import com.sun.web.ui.model.Option;
037:
038: public class ConsumerBean extends ConsumerBaseBean implements
039: CPAttributes {
040: public static final String TRUE = Boolean.TRUE.toString();
041: public static final String FALSE = Boolean.FALSE.toString();
042: public static final FieldKey ID = new FieldKey("id");
043: private Option[] statusOptions = null;
044: private String status = null;
045: private TableSelectPhaseListener tspl0 = null;
046: private TableRowGroup tableRowGroup0 = null;
047: private String name = null;
048: private TableSelectPhaseListener tspl = null;
049: private TableRowGroup tableRowGroup = null;
050: private ObjectListDataProvider configuredProducers = null;
051:
052: public ConsumerBean() {
053: tspl0 = new TableSelectPhaseListener();
054: tspl = new TableSelectPhaseListener();
055:
056: NewConfiguredProducerBean newConfiguredProducerBean = (NewConfiguredProducerBean) getSessionAttribute("NewConfiguredProducerBean");
057:
058: if (newConfiguredProducerBean != null) {
059: newConfiguredProducerBean.clearData();
060: }
061: }
062:
063: public boolean isGlobal() {
064: return GLOBAL_LOCATION_DN.equals(getCurrentDN());
065: }
066:
067: public boolean isOrg() {
068: return isOrgDN((String) getCurrentDN());
069: }
070:
071: public boolean isAlert() {
072: if (!isGlobal() && !isOrg()) {
073: showAlert();
074: setAlertType(ERROR);
075: setAlertSummary(rb.getString("home.invalidDN.summary"));
076: setAlertDetail(rb.getString("home.invalidDN.detail"));
077: }
078:
079: return super .isAlert();
080: }
081:
082: public String getPageHelpText() {
083: if (isGlobal()) {
084: return rb.getString("home.global.help");
085: } else if (isOrg()) {
086: return rb.getString("home.org.help");
087: } else {
088: return "";
089: }
090: }
091:
092: public Option[] getStatusOptions() {
093: if (statusOptions == null) {
094: statusOptions = new Option[] {
095: new Option(TRUE, rb
096: .getString("home.label.allDisabled")),
097: new Option(FALSE, rb
098: .getString("home.label.notAllDisabled")) };
099: }
100:
101: return statusOptions;
102: }
103:
104: public String getStatus() {
105: if (isGlobal()) {
106: Map optionsMap = new HashMap();
107: optionsMap
108: .put(AttrOptionConstants.OPT_COMPONENT, COMPONENT);
109:
110: optionsMap.put(AttrOptionConstants.OPT_ATTR_NAME,
111: ConsumerAttributes.IS_DISABLED);
112:
113: String[] signature = { Map.class.getName() };
114: Object[] params = { optionsMap };
115:
116: try {
117: ObjectName objectName = AdminUtil
118: .getPortalMBeanObjectName(getDomain(), portalID);
119: List result = (List) getMBeanServerConnection().invoke(
120: objectName, "getAttribute", params, signature);
121:
122: status = (String) result.get(0);
123: } catch (Exception e) {
124: log(Level.SEVERE, "ConsumerBean.getStatus()", e);
125: showAlert();
126: setAlertType(ERROR);
127: setAlertSummary(rb
128: .getString("home.load.failed.summary"));
129: setAlertDetail(rb.getString("home.load.failed.detail"));
130: }
131: }
132:
133: return status;
134: }
135:
136: public void setStatus(String status) {
137: this .status = status;
138: }
139:
140: public TableRowGroup getTableRowGroup0() {
141: return tableRowGroup0;
142: }
143:
144: public void setTableRowGroup0(TableRowGroup tableRowGroup0) {
145: this .tableRowGroup0 = tableRowGroup0;
146: }
147:
148: public Object getSelected0() {
149: return tspl0.getSelected(getTableRow0());
150: }
151:
152: public void setSelected0(Object object) {
153: RowKey rowKey = getTableRow0();
154:
155: if (rowKey != null) {
156: tspl0.setSelected(rowKey, object);
157: }
158: }
159:
160: public ObjectListDataProvider getUserProfileMapping() {
161: ObjectListDataProvider userProfileMapping = (ObjectListDataProvider) getSessionAttribute(ATTR_UPM);
162:
163: if (isGlobal() && (userProfileMapping == null)) {
164: userProfileMapping = loadUserProfileMapping();
165: setSessionAttribute(ATTR_UPM, userProfileMapping);
166: }
167:
168: if (userProfileMapping == null) {
169: userProfileMapping = new ObjectListDataProvider(
170: Collections.EMPTY_LIST);
171: userProfileMapping.setObjectType(SimpleUPMBean.class);
172: }
173:
174: return userProfileMapping;
175: }
176:
177: public void setUserProfileMapping(
178: ObjectListDataProvider userProfileMapping) {
179: setSessionAttribute(ATTR_UPM, userProfileMapping);
180: }
181:
182: public void addUPM() {
183: ObjectListDataProvider userProfileMapping = getUserProfileMapping();
184:
185: if (userProfileMapping.canAppendRow()) {
186: userProfileMapping.appendRow();
187: }
188:
189: userProfileMapping.commitChanges();
190: }
191:
192: public void deleteUPM() {
193: ObjectListDataProvider userProfileMapping = getUserProfileMapping();
194: RowKey[] rowKeys = tableRowGroup0.getRenderedRowKeys();
195:
196: if (rowKeys != null) {
197: for (int i = 0; i < rowKeys.length; i++) {
198: RowKey rowKey = rowKeys[i];
199:
200: if (tspl0.isSelected(rowKey)
201: && userProfileMapping.isRowAvailable(rowKey)
202: && userProfileMapping.canRemoveRow(rowKey)) {
203:
204: userProfileMapping.removeRow(rowKey);
205: }
206: }
207:
208: userProfileMapping.commitChanges();
209: tableRowGroup0.setFirst(0);
210: tspl0.clear();
211: }
212: }
213:
214: public String getName() {
215: if (isOrg() && (name == null)) {
216: removeFromSession(ATTR_UPM);
217: Map optionsMap = new HashMap();
218: optionsMap
219: .put(AttrOptionConstants.OPT_COMPONENT, COMPONENT);
220: optionsMap.put(AttrOptionConstants.OPT_DN, getCurrentDN());
221:
222: optionsMap.put(AttrOptionConstants.OPT_ATTR_NAME,
223: ConsumerAttributes.NAME);
224:
225: String[] signature = { Map.class.getName() };
226: Object[] params = { optionsMap };
227:
228: try {
229: ObjectName objectName = AdminUtil
230: .getPortalMBeanObjectName(getDomain(), portalID);
231: List result = (List) getMBeanServerConnection().invoke(
232: objectName, "getAttribute", params, signature);
233:
234: name = result.isEmpty() ? "" : (String) result.get(0);
235: } catch (Exception e) {
236: log(Level.SEVERE, "ConsumerBean.getName()", e);
237: showAlert();
238: setAlertType(ERROR);
239: setAlertSummary(rb
240: .getString("home.load.failed.summary"));
241: setAlertDetail(rb.getString("home.load.failed.detail"));
242: }
243: }
244:
245: return name;
246: }
247:
248: public void save() {
249: Map optionsMap = new HashMap();
250: optionsMap.put(AttrOptionConstants.OPT_COMPONENT, COMPONENT);
251: Map nameValues = new HashMap();
252:
253: nameValues.put(ConsumerAttributes.IS_DISABLED, Collections
254: .singletonList(status));
255:
256: ObjectListDataProvider userProfileMapping = getUserProfileMapping();
257: List upm = new ArrayList();
258: userProfileMapping.commitChanges();
259: List upmList = userProfileMapping.getList();
260:
261: for (int i = 0; i < upmList.size(); i++) {
262: SimpleUPMBean userProfile = (SimpleUPMBean) upmList.get(i);
263: String wsrp = userProfile.getWSRP();
264: String ldap = userProfile.getLDAP();
265: wsrp = (wsrp == null) ? "" : wsrp.trim();
266: ldap = (ldap == null) ? "" : ldap.trim();
267:
268: if ((wsrp.length() > 0) && (ldap.length() > 0)) {
269: upm.add(wsrp + "|" + ldap);
270: }
271: }
272:
273: nameValues.put(ConsumerAttributes.USER_PROFILE_MAPPING, upm);
274: String[] signature = { Map.class.getName(), Map.class.getName() };
275: Object[] params = { nameValues, optionsMap };
276:
277: try {
278: ObjectName objectName = AdminUtil.getPortalMBeanObjectName(
279: getDomain(), portalID);
280:
281: getMBeanServerConnection().invoke(objectName,
282: "setAttributes", params, signature);
283:
284: showAlert();
285: setAlertType(INFORMATION);
286: setAlertSummary(rb
287: .getString("generic.save.success.summary"));
288: setAlertDetail("");
289: } catch (Exception e) {
290: log(Level.SEVERE, "ConsumerBean.save()", e);
291: showAlert();
292: setAlertType(WARNING);
293: setAlertSummary(rb.getString("generic.save.failed.summary"));
294: setAlertDetail(rb.getString("generic.save.failed.detail"));
295: }
296: }
297:
298: public String reset() {
299: removeFromSession(ATTR_UPM);
300: return "success";
301: }
302:
303: public TableRowGroup getTableRowGroup() {
304: return tableRowGroup;
305: }
306:
307: public void setTableRowGroup(TableRowGroup tableRowGroup) {
308: this .tableRowGroup = tableRowGroup;
309: }
310:
311: public Object getSelected() {
312: return tspl.getSelected(getTableRow());
313: }
314:
315: public void setSelected(Object object) {
316: RowKey rowKey = getTableRow();
317:
318: if (rowKey != null) {
319: tspl.setSelected(rowKey, object);
320: }
321: }
322:
323: public ObjectListDataProvider getConfiguredProducers() {
324: List cps = new ArrayList();
325:
326: if (isOrg()) {
327: loadConfiguredProducers(cps);
328: }
329:
330: configuredProducers = new ObjectListDataProvider(cps);
331: configuredProducers.setObjectType(SimpleCPBean.class);
332: return configuredProducers;
333: }
334:
335: private void loadConfiguredProducers(List cps) {
336: // MBean path of Consumer.
337: List path = getConsumerPath();
338:
339: try {
340: // Object name of Consumer.
341: ObjectName objectName = AdminUtil
342: .getResourceMBeanObjectName(CONSUMER_TYPE, path);
343:
344: String[] signature = { String.class.getName() };
345: Object[] params = { getCurrentDN() };
346:
347: Map producerEntities = (Map) getMBeanServerConnection()
348: .invoke(objectName, "listConfiguredProducers",
349: params, signature);
350:
351: if (producerEntities != null) {
352: Set entrySet = producerEntities.entrySet();
353: Set names = new HashSet();
354: names.add(ENABLED);
355: Map optionsMap = new HashMap();
356: optionsMap.put(AttrOptionConstants.OPT_COMPONENT,
357: COMPONENT);
358: optionsMap.put(AttrOptionConstants.OPT_DN,
359: getCurrentDN());
360: optionsMap.put(AttrOptionConstants.OPT_ATTR_NAMES,
361: names);
362: signature = new String[] { Map.class.getName() };
363: params = new Object[] { optionsMap };
364:
365: // Object name of Portal.
366: objectName = AdminUtil.getPortalMBeanObjectName(
367: getDomain(), portalID);
368:
369: for (Iterator i = entrySet.iterator(); i.hasNext();) {
370: Map.Entry entry = (Map.Entry) i.next();
371: String id = (String) entry.getKey();
372: String name = (String) entry.getValue();
373: optionsMap.put("producer", id);
374:
375: Map props = (Map) getMBeanServerConnection()
376: .invoke(objectName, "getAttributes",
377: params, signature);
378:
379: String enabledString = (String) ((List) props
380: .get(ENABLED)).get(0);
381: boolean enabled = Boolean.valueOf(enabledString)
382: .booleanValue();
383:
384: String status = enabled ? rb
385: .getString("generic.label.enabled") : rb
386: .getString("generic.label.disabled");
387:
388: SimpleCPBean configuredProducer = new SimpleCPBean();
389: configuredProducer.setName(name);
390: configuredProducer.setId(id);
391: configuredProducer.setStatus(status);
392: cps.add(configuredProducer);
393: }
394: }
395: } catch (Exception e) {
396: log(Level.SEVERE, "ConsumerBean.loadConfiguredProducers()",
397: e);
398: showAlert();
399: setAlertType(ERROR);
400: setAlertSummary(rb.getString("home.loadCPs.failed.summary"));
401: setAlertDetail(rb.getString("home.loadCPs.failed.detail"));
402: }
403: }
404:
405: public void setConfiguredProducers(
406: ObjectListDataProvider configuredProducers) {
407: this .configuredProducers = configuredProducers;
408: }
409:
410: public String deleteConfiguredProducers() {
411: configuredProducers = getConfiguredProducers();
412: RowKey[] rowKeys = tableRowGroup.getRenderedRowKeys();
413:
414: if (rowKeys != null) {
415: List path = getConsumerPath();
416:
417: String[] signature = { String.class.getName(),
418: String.class.getName() };
419:
420: for (int i = 0; i < rowKeys.length; i++) {
421: RowKey rowKey = rowKeys[i];
422:
423: if (tspl.isSelected(rowKey)) {
424: String configuredProducerID = (String) configuredProducers
425: .getValue(ID, rowKey);
426:
427: Object[] params = { getCurrentDN(),
428: configuredProducerID };
429:
430: try {
431: ObjectName objectName = AdminUtil
432: .getResourceMBeanObjectName(
433: CONSUMER_TYPE, path);
434:
435: getMBeanServerConnection().invoke(objectName,
436: "deleteConfiguredProducer", params,
437: signature);
438:
439: if (configuredProducers.isRowAvailable(rowKey)
440: && configuredProducers
441: .canRemoveRow(rowKey)) {
442:
443: configuredProducers.removeRow(rowKey);
444: }
445: } catch (Exception e) {
446: log(
447: Level.SEVERE,
448: "ConsumerBean.deleteConfiguredProducers()",
449: e);
450: showAlert();
451: setAlertType(WARNING);
452: setAlertSummary(rb
453: .getString("home.deleteCPs.failed.summary"));
454: setAlertDetail(rb
455: .getString("home.deleteCPs.failed.detail"));
456: }
457: }
458: }
459:
460: configuredProducers.commitChanges();
461: tableRowGroup.setFirst(0);
462: tspl.clear();
463: }
464:
465: return "success";
466: }
467:
468: public String editConfiguredProducer() {
469: setRequestAttributeInSession(ATTR_SELECTED_CONFIGURED_PRODUCER);
470: return "editConfiguredProducer";
471: }
472:
473: private ObjectListDataProvider loadUserProfileMapping() {
474: List upmList = new ArrayList();
475: Map optionsMap = new HashMap();
476: optionsMap.put(AttrOptionConstants.OPT_COMPONENT, COMPONENT);
477:
478: optionsMap.put(AttrOptionConstants.OPT_ATTR_NAME,
479: ConsumerAttributes.USER_PROFILE_MAPPING);
480:
481: String[] signature = { Map.class.getName() };
482: Object[] params = { optionsMap };
483:
484: try {
485: ObjectName objectName = AdminUtil.getPortalMBeanObjectName(
486: getDomain(), portalID);
487:
488: List upm = (List) getMBeanServerConnection().invoke(
489: objectName, "getAttribute", params, signature);
490:
491: for (int i = 0; i < upm.size(); i++) {
492: String up = (String) upm.get(i);
493: int index = up.indexOf("|");
494: String wsrp = up.substring(0, index);
495: String ldap = up.substring(index + 1);
496: SimpleUPMBean userProfile = new SimpleUPMBean();
497: userProfile.setWSRP(wsrp);
498: userProfile.setLDAP(ldap);
499: upmList.add(userProfile);
500: }
501: } catch (Exception e) {
502: log(Level.SEVERE, "ConsumerBean.loadUserProfileMapping()",
503: e);
504: showAlert();
505: setAlertType(ERROR);
506: setAlertSummary(rb.getString("home.load.failed.summary"));
507: setAlertDetail(rb.getString("home.load.failed.detail"));
508: }
509:
510: ObjectListDataProvider result = new ObjectListDataProvider(
511: upmList);
512: result.setObjectType(SimpleUPMBean.class);
513: return result;
514: }
515:
516: private RowKey getTableRow0() {
517: return getTableRow("#{upm.tableRow}");
518: }
519:
520: private RowKey getTableRow() {
521: return getTableRow("#{configuredProducer.tableRow}");
522: }
523: }
|