001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.admin.console.search;
013:
014: import java.util.*;
015: import java.util.logging.Level;
016: import java.io.*;
017: import java.text.*;
018:
019: import javax.faces.context.FacesContext;
020: import javax.faces.application.FacesMessage;
021: import javax.faces.component.UIComponent;
022: import javax.faces.validator.*;
023: import javax.faces.el.ValueBinding;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.management.*;
026:
027: import com.sun.web.ui.model.*;
028: import com.sun.web.ui.event.*;
029: import com.sun.web.ui.component.*;
030:
031: import com.sun.data.provider.*;
032: import com.sun.data.provider.impl.ObjectListDataProvider;
033:
034: import com.sun.cacao.agent.JmxClient;
035:
036: import com.sun.portal.admin.common.util.AdminClientUtil;
037: import com.sun.portal.admin.console.common.PSBaseBean;
038:
039: public class EditDatabaseAttributesBean extends PSBaseBean {
040:
041: private ObjectListDataProvider attributes = null;
042: private TableRowGroup tableRowGroup = null;
043: private TableSelectPhaseListener tspl = null;
044:
045: public EditDatabaseAttributesBean() {
046: ObjectListDataProvider attributes = (ObjectListDataProvider) getSessionAttribute("search.editdatabaseattributes.attributes");
047: if (attributes == null) {
048: retrieveAttributes();
049: }
050:
051: tspl = new TableSelectPhaseListener();
052: }
053:
054: public boolean getShowError() {
055: String value = (String) getSessionAttribute("search.editdatabaseattributes.showerror");
056: if (value == null || value.equals("false")) {
057: return false;
058: } else {
059: return true;
060: }
061: }
062:
063: public String getPageTitle() {
064: MessageFormat mf = new MessageFormat("");
065: mf.applyPattern(getLocalizedString("search",
066: "search.editdatabase.pagetitle"));
067: return mf
068: .format(new Object[] { (String) getSessionAttribute("search.database.selected") });
069: }
070:
071: public ObjectListDataProvider getAttributes() {
072: return (ObjectListDataProvider) getSessionAttribute("search.editdatabaseattributes.attributes");
073: }
074:
075: public void setAttributes(ObjectListDataProvider attributes) {
076: setSessionAttribute("search.editdatabaseattributes.attributes",
077: attributes);
078: }
079:
080: public TableRowGroup getTableRowGroup() {
081: return tableRowGroup;
082: }
083:
084: public void setTableRowGroup(TableRowGroup tableRowGroup) {
085: this .tableRowGroup = tableRowGroup;
086: }
087:
088: public Object getSelected() {
089: return tspl.getSelected(getTableRow());
090: }
091:
092: public void setSelected(Object object) {
093: tspl.setSelected(getTableRow(), object);
094: }
095:
096: public String addAttribute() {
097: attributes = (ObjectListDataProvider) getSessionAttribute("search.editdatabaseattributes.attributes");
098: if (attributes == null) {
099: retrieveAttributes();
100: }
101:
102: if (attributes.canAppendRow()) {
103: attributes.appendRow();
104: attributes.commitChanges();
105: setSessionAttribute(
106: "search.editdatabaseattributes.attributes",
107: attributes);
108: }
109: return null;
110: }
111:
112: public String deleteAttribute() {
113: attributes = (ObjectListDataProvider) getSessionAttribute("search.editdatabaseattributes.attributes");
114: if (attributes == null) {
115: retrieveAttributes();
116: }
117:
118: RowKey[] rowKeys = tableRowGroup.getRenderedRowKeys();
119: if (rowKeys != null) {
120: for (int index = 0; index < rowKeys.length; index++) {
121: RowKey rowKey = rowKeys[index];
122: if (tspl.isSelected(rowKey)
123: && attributes.isRowAvailable(rowKey)
124: && attributes.canRemoveRow(rowKey)) {
125: attributes.removeRow(rowKey);
126: }
127: }
128: attributes.commitChanges();
129: setSessionAttribute(
130: "search.editdatabaseattributes.attributes",
131: attributes);
132: tableRowGroup.setFirst(0);
133: tspl.clear();
134: }
135:
136: return null;
137: }
138:
139: public String save() {
140: attributes = (ObjectListDataProvider) getSessionAttribute("search.editdatabaseattributes.attributes");
141: if (attributes == null) {
142: retrieveAttributes();
143: }
144:
145: try {
146: LinkedList path = new LinkedList();
147: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
148: path
149: .addFirst((String) getSessionAttribute("search.server.selected"));
150: path.addFirst("database");
151: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
152: AdminClientUtil.SEARCH_DATABASE_MBEAN_TYPE, path);
153:
154: Properties p = new Properties();
155: attributes.commitChanges();
156: List l = attributes.getList();
157: for (int index = 0; index < l.size(); index++) {
158: DatabaseAttributeBean dab = (DatabaseAttributeBean) l
159: .get(index);
160: if (!dab.key.equals("")) {
161: p.setProperty(dab.key, dab.value);
162: }
163: }
164:
165: Object[] params = {
166: (String) getSessionAttribute("search.database.selected"),
167: p };
168: String[] signatures = { "java.lang.String",
169: "java.util.Properties" };
170: getMBeanServerConnection().invoke(on, "setAttributes",
171: params, signatures);
172:
173: setSessionAttribute(
174: "search.editdatabaseattributes.showerror", "false");
175: return "gotoDatabasesHome";
176: } catch (Exception e) {
177: log(Level.SEVERE,
178: "EditDatabaseAttributesBean.save() : Exception ", e);
179: setSessionAttribute(
180: "search.editdatabaseattributes.showerror", "true");
181: return null;
182: }
183: }
184:
185: public String cancel() {
186: setSessionAttribute("search.editdatabaseattributes.showerror",
187: "false");
188: return "gotoDatabasesHome";
189: }
190:
191: private void retrieveAttributes() {
192: ArrayList al = new ArrayList();
193: try {
194: LinkedList path = new LinkedList();
195: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
196: path
197: .addFirst((String) getSessionAttribute("search.server.selected"));
198: path.addFirst("database");
199: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
200: AdminClientUtil.SEARCH_DATABASE_MBEAN_TYPE, path);
201:
202: Object[] params = { (String) getSessionAttribute("search.database.selected") };
203: String[] signatures = { "java.lang.String" };
204: Properties p = (Properties) getMBeanServerConnection()
205: .invoke(on, "getAttributes", params, signatures);
206:
207: Enumeration e = p.propertyNames();
208: while (e.hasMoreElements()) {
209: String key = (String) e.nextElement();
210: String value = p.getProperty(key);
211:
212: DatabaseAttributeBean dab = new DatabaseAttributeBean();
213: dab.initialize(key, value);
214: al.add(dab);
215: }
216: } catch (Exception e) {
217: log(
218: Level.SEVERE,
219: "EditDatabaseAttributesBean.retrieveAttributes() : Exception ",
220: e);
221: }
222: attributes = new ObjectListDataProvider(al);
223: attributes.setObjectType(DatabaseAttributeBean.class);
224:
225: setSessionAttribute("search.editdatabaseattributes.attributes",
226: attributes);
227: }
228:
229: private RowKey getTableRow() {
230: FacesContext fc = FacesContext.getCurrentInstance();
231: ValueBinding vb = fc.getApplication().createValueBinding(
232: "#{databaseAttribute.tableRow}");
233: return (RowKey) vb.getValue(fc);
234: }
235:
236: }
|