001: /*
002: * Copyright 2004,2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.axis2.description;
017:
018: import org.apache.axiom.om.OMAbstractFactory;
019: import org.apache.axiom.om.OMElement;
020: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
021: import org.apache.axis2.util.ObjectStateUtils;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import javax.xml.stream.XMLInputFactory;
026: import javax.xml.stream.XMLStreamReader;
027: import java.io.ByteArrayInputStream;
028: import java.io.Externalizable;
029: import java.io.IOException;
030: import java.io.ObjectInput;
031: import java.io.ObjectOutput;
032:
033: /**
034: * Class Parameter
035: */
036: public class Parameter implements Externalizable {
037:
038: /*
039: * setup for logging
040: */
041: private static final Log log = LogFactory.getLog(Parameter.class);
042:
043: private static final String myClassName = "Parameter";
044:
045: /**
046: * @serial The serialization version ID tracks the version of the class.
047: * If a class definition changes, then the serialization/externalization
048: * of the class is affected. If a change to the class is made which is
049: * not compatible with the serialization/externalization of the class,
050: * then the serialization version ID should be updated.
051: * Refer to the "serialVer" utility to compute a serialization
052: * version ID.
053: */
054: private static final long serialVersionUID = -6601664200673063531L;
055:
056: /**
057: * @serial Tracks the revision level of a class to identify changes to the
058: * class definition that are compatible to serialization/externalization.
059: * If a class definition changes, then the serialization/externalization
060: * of the class is affected.
061: * Refer to the writeExternal() and readExternal() methods.
062: */
063: // supported revision levels, add a new level to manage compatible changes
064: private static final int REVISION_1 = 1;
065: // current revision level of this object
066: private static final int revisionID = REVISION_1;
067:
068: /**
069: * Field ANY_PARAMETER
070: */
071: public final static int ANY_PARAMETER = 0;
072: /**
073: * Field TEXT_PARAMETER
074: */
075: public final static int TEXT_PARAMETER = 1;
076:
077: /**
078: * Field OM_PARAMETER
079: */
080: public final static int OM_PARAMETER = 2;
081:
082: /**
083: * Field type
084: */
085: private int type = TEXT_PARAMETER;
086:
087: /**
088: * Field locked
089: */
090: private boolean locked;
091:
092: /**
093: * Field name
094: */
095: private String name;
096:
097: /**
098: * to store the parameter element
099: * <parameter name="ServiceClass1">
100: * org.apache.axis2.sample.echo.EchoImpl</parameter>
101: */
102: private OMElement parameterElement;
103:
104: /**
105: * Field value
106: */
107: private Object value;
108:
109: //To check whether the parameter is editable or not ,
110: // if the value is false then no one can call setvalue
111: private boolean editable = true;
112:
113: /**
114: * Constructor.
115: */
116: public Parameter() {
117: }
118:
119: /**
120: * Constructor from name and value.
121: *
122: * @param name
123: * @param value
124: */
125: public Parameter(String name, Object value) {
126: this .name = name;
127: this .value = value;
128: parseValueForType(this .value);
129: }
130:
131: /**
132: * Method getName.
133: *
134: * @return Returns String.
135: */
136: public String getName() {
137: return name;
138: }
139:
140: public OMElement getParameterElement() {
141: return this .parameterElement;
142: }
143:
144: /**
145: * Method getParameterType.
146: *
147: * @return Returns int.
148: */
149: public int getParameterType() {
150: return type;
151: }
152:
153: /**
154: * Method getValue.
155: *
156: * @return Returns Object.
157: */
158: public Object getValue() {
159: return value;
160: }
161:
162: /**
163: * Method isLocked.
164: *
165: * @return Returns boolean.
166: */
167: public boolean isLocked() {
168: return locked;
169: }
170:
171: /**
172: * Method setLocked.
173: *
174: * @param value
175: */
176: public void setLocked(boolean value) {
177: locked = value;
178: }
179:
180: /**
181: * Method setName.
182: *
183: * @param name
184: */
185: public void setName(String name) {
186: this .name = name;
187: }
188:
189: public void setParameterElement(OMElement element) {
190: this .parameterElement = element;
191: }
192:
193: public void setParameterType(int type) {
194: this .type = type;
195: }
196:
197: /**
198: * Method setValue.
199: *
200: * @param value
201: */
202: public void setValue(Object value) {
203: if (!editable) {
204: log.debug("Parameter " + getName() + " can not be edit");
205: return;
206: }
207: parseValueForType(value);
208: this .value = value;
209: }
210:
211: /**
212: * The purpose of this method is to pars the injected value for its type.
213: * Type will be set using setParameterType.
214: * @param value
215: */
216: private void parseValueForType(Object value) {
217: if (value instanceof String) {
218: setParameterType(TEXT_PARAMETER);
219: } else if (value instanceof OMElement) {
220: setParameterType(OM_PARAMETER);
221: } else {
222: setParameterType(ANY_PARAMETER);
223: }
224: }
225:
226: public String toString() {
227: return "Parameter : " + name + "=" + value;
228: }
229:
230: public boolean equals(Object obj) {
231: if (this == obj) {
232: return true;
233: }
234: if (obj instanceof Parameter) {
235: return ((Parameter) obj).name.equals(name);
236: }
237: return false;
238: }
239:
240: public int hashCode() {
241: return name.hashCode();
242: }
243:
244: /* ===============================================================
245: * Externalizable support
246: * ===============================================================
247: */
248:
249: /**
250: * Save the contents of this object.
251: * <p/>
252: * NOTE: Transient fields and static fields are not saved.
253: * Also, objects that represent "static" data are
254: * not saved, except for enough information to be
255: * able to find matching objects when the message
256: * context is re-constituted.
257: *
258: * @param out The stream to write the object contents to
259: * @throws IOException
260: */
261: public void writeExternal(ObjectOutput out) throws IOException {
262: // write out contents of this object
263:
264: //---------------------------------------------------------
265: // in order to handle future changes to the message
266: // context definition, be sure to maintain the
267: // object level identifiers
268: //---------------------------------------------------------
269: // serialization version ID
270: out.writeLong(serialVersionUID);
271:
272: // revision ID
273: out.writeInt(revisionID);
274:
275: //---------------------------------------------------------
276: // simple fields
277: //---------------------------------------------------------
278:
279: out.writeInt(type);
280: out.writeBoolean(locked);
281:
282: ObjectStateUtils.writeString(out, name, "Parameter.name");
283:
284: //---------------------------------------------------------
285: // object fields
286: //---------------------------------------------------------
287:
288: // TODO: investigate serializing the OMElement more efficiently
289: // This currently will basically serialize the given OMElement
290: // to a String but will build the OMTree in the memory
291:
292: String tmp = null;
293:
294: if (parameterElement != null) {
295: tmp = parameterElement.toString();
296: }
297:
298: // treat as an object, don't do UTF
299: ObjectStateUtils.writeObject(out, tmp,
300: "Parameter.parameterElement");
301:
302: // TODO: error handling if this can't be serialized
303: ObjectStateUtils.writeObject(out, value, "Parameter.value");
304:
305: }
306:
307: /**
308: * Restore the contents of the object that was previously saved.
309: * <p/>
310: * NOTE: The field data must read back in the same order and type
311: * as it was written. Some data will need to be validated when
312: * resurrected.
313: *
314: * @param in The stream to read the object contents from
315: * @throws IOException
316: * @throws ClassNotFoundException
317: */
318: public void readExternal(ObjectInput in) throws IOException,
319: ClassNotFoundException {
320: // trace point
321: if (log.isTraceEnabled()) {
322: log
323: .trace(myClassName
324: + ":readExternal(): BEGIN bytes available in stream ["
325: + in.available() + "] ");
326: }
327:
328: // serialization version ID
329: long suid = in.readLong();
330:
331: // revision ID
332: int revID = in.readInt();
333:
334: // make sure the object data is in a version we can handle
335: if (suid != serialVersionUID) {
336: throw new ClassNotFoundException(
337: ObjectStateUtils.UNSUPPORTED_SUID);
338: }
339:
340: // make sure the object data is in a revision level we can handle
341: if (revID != REVISION_1) {
342: throw new ClassNotFoundException(
343: ObjectStateUtils.UNSUPPORTED_REVID);
344: }
345:
346: //---------------------------------------------------------
347: // simple fields
348: //---------------------------------------------------------
349:
350: type = in.readInt();
351: locked = in.readBoolean();
352:
353: name = ObjectStateUtils.readString(in, "Parameter.name");
354:
355: //---------------------------------------------------------
356: // object fields
357: //---------------------------------------------------------
358:
359: // TODO: investigate serializing the OMElement more efficiently
360: // This currently will basically serialize the given OMElement
361: // to a String but will build the OMTree in the memory
362:
363: // treat as an object, don't do UTF
364: String tmp = (String) ObjectStateUtils.readObject(in,
365: "Parameter.parameterElement");
366:
367: // convert to an OMElement
368: if (tmp != null) {
369: try {
370: ByteArrayInputStream bais = new ByteArrayInputStream(
371: tmp.getBytes());
372:
373: XMLStreamReader parser = XMLInputFactory.newInstance()
374: .createXMLStreamReader(bais);
375:
376: // TODO: the StAXOMBuilder is an impl class - is there a better mechanism rather than an impl class ?
377: StAXOMBuilder builder = new StAXOMBuilder(
378: OMAbstractFactory.getOMFactory(), parser);
379:
380: OMElement docElement = builder.getDocumentElement();
381:
382: if (docElement != null) {
383: parameterElement = docElement;
384: } else {
385: // TODO: error handling if can't create an OMElement
386: parameterElement = null;
387: }
388: } catch (Exception exc) {
389: // TODO: error handling if can't create an OMElement
390: parameterElement = null;
391: }
392: } else {
393: parameterElement = null;
394: }
395:
396: // TODO: error handling if this can't be serialized
397: value = ObjectStateUtils.readObject(in, "Parameter.value");
398:
399: //---------------------------------------------------------
400: // done
401: //---------------------------------------------------------
402:
403: }
404:
405: public void setEditable(boolean editable) {
406: this.editable = editable;
407: }
408: }
|