001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)DescriptorSupport.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.config;
030:
031: import com.sun.jbi.StringTranslator;
032: import com.sun.jbi.management.LocalStringKeys;
033: import java.util.logging.Logger;
034:
035: /**
036: * DescriptorSupport is an extension of the JMX ModelMBean DescriptorSupport.
037: * It provides additional setters to set fields specific to the JBI Configuration.
038: * Additionaly, it overrrides the isValid operation to check for the presence of
039: * all required fields.
040: *
041: * @author Sun Microsystems, Inc.
042: */
043: public class DescriptorSupport extends
044: javax.management.modelmbean.DescriptorSupport {
045: private static final String DESCRIPTOR_TYPE = "descriptorType";
046: private static final String DESCRIPTOR_TYPE_VALUE = "attribute";
047:
048: private StringTranslator mTranslator;
049: private Logger mLog;
050:
051: /*-------------------------------------------------------------------------------*\
052: * Mandatory Fields *
053: \*-------------------------------------------------------------------------------*/
054:
055: public enum RequiredFieldName {
056: /** Attribute name */
057: ATTR_NAME("name"),
058: /** Indicates whether the attribute is static or dynamic */
059: IS_STATIC("isStatic"),
060: /** Default value of the attribute. JMX Standard field name */
061: DEFAULT("default"),
062: /** Display name for the field */
063: DISPLAY_NAME("displayName"),
064: /** The I18N Token for the Display namefield */
065: DISPLAY_NAME_ID("displayNameId"),
066: /** The I18N Token for the description of the attribute provided in the attribute info */
067: DESCRIPTION_ID("descriptionId"),
068: /** Tool tip */
069: TOOL_TIP("toolTip"),
070: /** The I18N Token for the Tool tip*/
071: TOOL_TIP_ID("tootTipId"),
072: /** Resource Bundle Name */
073: RESOURCE_BUNDLE_NAME("resourceBundleName"),
074: /** Indicates whether the attribute is a passwordfield */
075: IS_PASSWORD("isPassword");
076:
077: /** field name instance variable */
078: private String mFieldName;
079:
080: /**
081: * @param name - descriptor field name.
082: */
083: private RequiredFieldName(String name) {
084: mFieldName = name;
085: }
086:
087: /**
088: * @return the string value for the field name
089: */
090: public String getFieldName() {
091: return mFieldName;
092: }
093: }
094:
095: /*-------------------------------------------------------------------------------*\
096: * Optional Fields *
097: \*-------------------------------------------------------------------------------*/
098:
099: public enum OptionalFieldName {
100: /** Minimum value of an attribute */
101: MIN_VALUE("minValue"),
102: /** Maximum value of an attribute */
103: MAX_VALUE("maxValue"),
104: /** Range of allowed values for an attribute */
105: ENUM_VALUE("enumValue"),
106: /** Unit of mesurement for an attribute */
107: UNIT("unit");
108:
109: /** field name instance variable */
110: private String mFieldName;
111:
112: /**
113: * @param name - descriptor field name.
114: */
115: private OptionalFieldName(String name) {
116: mFieldName = name;
117: }
118:
119: /**
120: * @return the string value for the field name
121: */
122: public String getFieldName() {
123: return mFieldName;
124: }
125: };
126:
127: /**
128: * Default constructor
129: */
130: public DescriptorSupport() {
131: super ();
132: setUnmutableFields();
133: }
134:
135: /**
136: * Descriptor constructor taking a Descriptor as parameter.
137: *
138: * @param inDescriptor - the descriptor to initialize from
139: * @see javax.jbi.management.modelmbean.DescriptorSupport
140: */
141: DescriptorSupport(DescriptorSupport inDescr) {
142: super (inDescr);
143: setUnmutableFields();
144: }
145:
146: /**
147: * Descriptor constructor.
148: *
149: * @param initNumFields - initial number of fields
150: * @see javax.jbi.management.modelmbean.DescriptorSupport
151: */
152: DescriptorSupport(int initNumFields)
153: throws javax.management.MBeanException {
154: super (initNumFields);
155: setUnmutableFields();
156: }
157:
158: /**
159: * Descriptor constructor taking an XML String.
160: * @param inStr - XML string
161: * @see javax.jbi.management.modelmbean.DescriptorSupport
162: */
163: DescriptorSupport(String inStr)
164: throws javax.management.MBeanException,
165: javax.management.modelmbean.XMLParseException {
166: super (inStr);
167: setUnmutableFields();
168: }
169:
170: /**
171: * Constructor taking fields in the fieldName=fieldValue format.
172: *
173: * @param fileds - fields in the fieldName=fieldValue format.
174: * @see javax.jbi.management.modelmbean.DescriptorSupport
175: */
176: DescriptorSupport(String[] fields) {
177: super (fields);
178: setUnmutableFields();
179: }
180:
181: /**
182: * Constructor taking field names and field values.
183: */
184: DescriptorSupport(String[] fieldNames, Object[] fieldValues) {
185: super (fieldNames, fieldValues);
186: setUnmutableFields();
187: }
188:
189: /*-------------------------------------------------------------------------------*\
190: * Setters *
191: \*-------------------------------------------------------------------------------*/
192:
193: /**
194: * @param name - identification for the attribute
195: */
196: public void setAttributeName(String name) {
197: setField(RequiredFieldName.ATTR_NAME.getFieldName(), name);
198: }
199:
200: /**
201: * @param isStatic - if true, the JBI runtime has to be restarted
202: * for changes to apply.
203: */
204: public void setIsStatic(Boolean isStatic) {
205: setField(RequiredFieldName.IS_STATIC.getFieldName(), isStatic);
206: }
207:
208: /**
209: * @param isStatic - if true, this field is to be encrypted before
210: * persisting it and the value is masked when retrieved.
211: */
212: public void setIsPassword(Boolean isPassword) {
213: setField(RequiredFieldName.IS_PASSWORD.getFieldName(),
214: isPassword);
215: }
216:
217: /**
218: * @param def - default value for the object
219: */
220: public void setDefault(Object def) {
221: setField(RequiredFieldName.DEFAULT.getFieldName(), def);
222: }
223:
224: /**
225: * @param displayName - display name string for the attribute
226: */
227: public void setDisplayName(String displayName) {
228: setField(RequiredFieldName.DISPLAY_NAME.getFieldName(),
229: displayName);
230: }
231:
232: /**
233: * @param id - localzation token for display name.
234: */
235: public void setDisplayNameId(String id) {
236: setField(RequiredFieldName.DISPLAY_NAME_ID.getFieldName(), id);
237: }
238:
239: /**
240: * @param id - localzation token for description.
241: */
242: public void setDescriptionId(String id) {
243: setField(RequiredFieldName.DESCRIPTION_ID.getFieldName(), id);
244: }
245:
246: /**
247: * @param toolTip - long description for the attribute.
248: */
249: public void setToolTip(String toolTip) {
250: setField(RequiredFieldName.TOOL_TIP.getFieldName(), toolTip);
251: }
252:
253: /**
254: * @param toolTipId - localization id for the tool tip string.
255: */
256: public void setToolTipId(String toolTipId) {
257: setField(RequiredFieldName.TOOL_TIP_ID.getFieldName(),
258: toolTipId);
259: }
260:
261: /**
262: * @param bundleName - resource bundle identification
263: */
264: public void setResourceBundleName(String bundleName) {
265: setField(RequiredFieldName.RESOURCE_BUNDLE_NAME.getFieldName(),
266: bundleName);
267: }
268:
269: /**
270: * @param value - minumum value allowed
271: */
272: public void setMinValue(Object value) {
273: setField(OptionalFieldName.MIN_VALUE.getFieldName(), value);
274: }
275:
276: /**
277: * @param value - maximum value allowed
278: */
279: public void setMaxValue(Object value) {
280: setField(OptionalFieldName.MAX_VALUE.getFieldName(), value);
281: }
282:
283: /**
284: * @param value - emumeration of allowed values
285: */
286: public void setEnumValue(String value) {
287: setField(OptionalFieldName.ENUM_VALUE.getFieldName(), value);
288: }
289:
290: /**
291: * @param unit - unit as a string
292: */
293: public void setUnit(String unit) {
294: setField(OptionalFieldName.UNIT.getFieldName(), unit);
295: }
296:
297: /**
298: * For a decriptor to be valid the following has to be true :
299: *
300: * <ul>
301: * <li> The basic isValid() of the base javax.management.modelmbean.DescriptorSupport should pass. </li>
302: * <li> All required fields should have been set </li>
303: * </ul>
304: */
305: public boolean isValid()
306: throws javax.management.RuntimeOperationsException {
307: boolean isValid = false;
308: if (super .isValid()) {
309: java.util.List<String> names = java.util.Arrays.asList(this
310: .getFieldNames());
311:
312: for (RequiredFieldName fieldName : RequiredFieldName
313: .values()) {
314: if (!names.contains(fieldName.getFieldName())) {
315: String exMsg = getTranslator().getString(
316: LocalStringKeys.CS_DESCR_REQ_FIELD_MISSING,
317: fieldName.getFieldName(),
318: (String) getFieldValue("name"));
319: getLogger().warning(exMsg);
320: isValid = false;
321: break;
322: }
323: isValid = true;
324: }
325: }
326: return isValid;
327:
328: }
329:
330: /**
331: * Set the default values for the fields
332: */
333: private void setUnmutableFields() {
334: setField(DESCRIPTOR_TYPE, DESCRIPTOR_TYPE_VALUE);
335: }
336:
337: /**
338: * @return the management String translator
339: */
340: private StringTranslator getTranslator() {
341: if (mTranslator == null) {
342: com.sun.jbi.EnvironmentContext envCtx = com.sun.jbi.util.EnvironmentAccess
343: .getContext();
344: mTranslator = envCtx
345: .getStringTranslator("com.sun.jbi.management");
346: }
347: return mTranslator;
348: }
349:
350: /**
351: * @return the management logger
352: */
353: private Logger getLogger() {
354: if (mLog == null) {
355: mLog = Logger.getLogger("com.sun.jbi.management");
356: }
357: return mLog;
358: }
359: }
|