001: /**
002: * $Id: NewProducerBean.java,v 1.6 2005/09/21 15:30:01 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.producer;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017: import java.util.Properties;
018: import java.util.logging.Level;
019:
020: import javax.faces.application.FacesMessage;
021: import javax.faces.component.UIComponent;
022: import javax.faces.context.FacesContext;
023: import javax.faces.validator.ValidatorException;
024: import javax.management.ObjectName;
025:
026: import com.sun.data.provider.FieldKey;
027: import com.sun.data.provider.RowKey;
028: import com.sun.data.provider.impl.ObjectListDataProvider;
029: import com.sun.portal.admin.common.util.AdminUtil;
030: import com.sun.web.ui.component.TableRowGroup;
031: import com.sun.web.ui.event.TableSelectPhaseListener;
032: import com.sun.web.ui.event.WizardEvent;
033: import com.sun.web.ui.event.WizardEventListener;
034: import com.sun.web.ui.model.Option;
035:
036: public class NewProducerBean extends ProducerBaseBean implements
037: WizardEventListener {
038:
039: public static final String PREFIX = "RegistrationPropertyDescriptions[";
040: private String name = "";
041: private Option[] registrationOptions = null;
042: private String registration = REQUIRED;
043: private Option[] inbandOptions = null;
044: private String inband = SUPPORTED;
045: private TableSelectPhaseListener tspl = null;
046: private TableRowGroup tableRowGroup = null;
047: private ObjectListDataProvider registrationPropertyDesc = null;
048:
049: public NewProducerBean() {
050: tspl = new TableSelectPhaseListener();
051: }
052:
053: public String getName() {
054: return name;
055: }
056:
057: public void setName(String name) {
058: this .name = name;
059: }
060:
061: public Option[] getRegistrationOptions() {
062: if (registrationOptions == null) {
063: registrationOptions = getRegistrationOptionArray();
064: }
065:
066: return registrationOptions;
067: }
068:
069: public String getRegistration() {
070: return registration;
071: }
072:
073: public void setRegistration(String registration) {
074: this .registration = registration;
075: }
076:
077: public String getRegistrationText() {
078: return REQUIRED.equals(registration) ? rb
079: .getString("generic.label.required") : rb
080: .getString("generic.label.notRequired");
081: }
082:
083: public Option[] getInbandOptions() {
084: if (inbandOptions == null) {
085: inbandOptions = getSupportOptionArray();
086: }
087:
088: return inbandOptions;
089: }
090:
091: public String getInband() {
092: return inband;
093: }
094:
095: public void setInband(String inband) {
096: this .inband = inband;
097: }
098:
099: public String getInbandText() {
100: return SUPPORTED.equals(inband) ? rb
101: .getString("generic.label.supported") : rb
102: .getString("generic.label.unsupported");
103: }
104:
105: public TableRowGroup getTableRowGroup() {
106: return tableRowGroup;
107: }
108:
109: public void setTableRowGroup(TableRowGroup tableRowGroup) {
110: this .tableRowGroup = tableRowGroup;
111: }
112:
113: public Object getSelected() {
114: return tspl.getSelected(getTableRow());
115: }
116:
117: public void setSelected(Object object) {
118: RowKey rowKey = getTableRow();
119:
120: if (rowKey != null) {
121: tspl.setSelected(rowKey, object);
122: }
123: }
124:
125: public ObjectListDataProvider getRegistrationPropertyDesc() {
126: if (registrationPropertyDesc == null) {
127: registrationPropertyDesc = new ObjectListDataProvider(
128: new ArrayList());
129:
130: registrationPropertyDesc.setObjectType(SimpleRPBean.class);
131: }
132:
133: registrationPropertyDesc.commitChanges();
134: return registrationPropertyDesc;
135: }
136:
137: public void setRegistrationPropertyDesc(
138: ObjectListDataProvider registrationPropertyDesc) {
139: this .registrationPropertyDesc = registrationPropertyDesc;
140: }
141:
142: public void addRPD() {
143: if (registrationPropertyDesc.canAppendRow()) {
144: registrationPropertyDesc.appendRow();
145: }
146:
147: registrationPropertyDesc.commitChanges();
148: }
149:
150: public void deleteRPDs() {
151: RowKey[] rowKeys = tableRowGroup.getRenderedRowKeys();
152:
153: if (rowKeys != null) {
154: for (int i = 0; i < rowKeys.length; i++) {
155: RowKey rowKey = rowKeys[i];
156:
157: if (tspl.isSelected(rowKey)
158: && registrationPropertyDesc
159: .isRowAvailable(rowKey)
160: && registrationPropertyDesc
161: .canRemoveRow(rowKey)) {
162:
163: registrationPropertyDesc.removeRow(rowKey);
164: }
165: }
166:
167: registrationPropertyDesc.commitChanges();
168: tableRowGroup.setFirst(0);
169: tspl.clear();
170: }
171: }
172:
173: public void validate(FacesContext context, UIComponent component,
174: Object value) throws ValidatorException {
175:
176: boolean fail = false;
177: String message = null;
178: String id = component.getId();
179: String val = ((String) value).trim();
180:
181: if (id.equals("name")) {
182: for (int i = 0; i < val.length(); i++) {
183: char c = val.charAt(i);
184:
185: if (!Character.isLetterOrDigit(c) && (c != '_')) {
186: fail = true;
187: message = rb
188: .getString("newProducer1.label.nameHelpText");
189: break;
190: }
191: }
192: }
193:
194: if (fail) {
195: throw new ValidatorException(new FacesMessage(
196: FacesMessage.SEVERITY_ERROR,
197: "WizardValidator error", message + " '" + value
198: + "'"));
199: }
200: }
201:
202: public boolean handleEvent(WizardEvent event) {
203: switch (event.getNavigationEvent()) {
204: case WizardEvent.CANCEL:
205: case WizardEvent.CLOSE:
206: clearData();
207: break;
208: case WizardEvent.FINISH:
209: createProducer();
210: break;
211: default:
212: break;
213: }
214:
215: return true;
216: }
217:
218: public boolean isTransient() {
219: return false;
220: }
221:
222: public void setTransient(boolean flag) {
223: }
224:
225: public Object saveState(FacesContext context) {
226: return null;
227: }
228:
229: public void restoreState(FacesContext context, Object state) {
230: }
231:
232: public void clearData() {
233: name = "";
234: registration = REQUIRED;
235: inband = SUPPORTED;
236: registrationPropertyDesc = null;
237: tableRowGroup.setFirst(0);
238: tspl.clear();
239: }
240:
241: private RowKey getTableRow() {
242: return getTableRow("#{rpd.tableRow}");
243: }
244:
245: private void createProducer() {
246: Properties prop = new Properties();
247:
248: prop.setProperty(ProducerAttributes.REGISTRATION_REQUIRED,
249: Boolean.toString(REQUIRED.equals(registration)));
250:
251: prop.setProperty(
252: ProducerAttributes.IN_BAND_REGISTRATION_SUPPORTED,
253: Boolean.toString(SUPPORTED.equals(inband)));
254:
255: registrationPropertyDesc.commitChanges();
256: List rpds = registrationPropertyDesc.getList();
257:
258: for (int i = 0; i < rpds.size(); i++) {
259: SimpleRPBean registrationProperty = (SimpleRPBean) rpds
260: .get(i);
261: String name = registrationProperty.getName();
262: String description = registrationProperty.getDescription();
263: name = (name == null) ? "" : name.trim();
264: description = (description == null) ? "" : description
265: .trim();
266:
267: if (name.length() > 0) {
268: prop.setProperty(PREFIX + name + "]", description);
269: }
270: }
271:
272: // MBean path of ProducerManager.
273: List path = getProducerManagerPath();
274:
275: try {
276: // Object name of ProducerManager.
277: ObjectName objectName = AdminUtil
278: .getResourceMBeanObjectName(PRODUCER_MANAGER_TYPE,
279: path);
280:
281: String[] signature = { String.class.getName(),
282: String.class.getName(), Properties.class.getName() };
283:
284: Object[] params = { getCurrentDN(), name, prop };
285:
286: getMBeanServerConnection().invoke(objectName,
287: "createProducer", params, signature);
288:
289: setAlertType(INFORMATION);
290: setAlertSummary(rb
291: .getString("newProducerResult.success.summary"));
292: setAlertDetail(rb
293: .getString("newProducerResult.success.detail"));
294: } catch (Exception e) {
295: log(Level.SEVERE, "NewProducerBean.createProducer()", e);
296: setAlertType(ERROR);
297: setAlertSummary(rb
298: .getString("newProducerResult.failed.summary"));
299: setAlertDetail(rb
300: .getString("newProducerResult.failed.detail"));
301: }
302: }
303: }
|