01: /*
02: *
03: * Copyright (c) 2003 Adrian Price. All rights reserved.
04: */
05:
06: package org.obe.spi.model;
07:
08: import org.obe.OBERuntimeException;
09: import org.obe.engine.WorkflowEngine;
10: import org.obe.xpdl.model.data.DataTypes;
11: import org.obe.xpdl.model.data.Type;
12:
13: import java.beans.PropertyDescriptor;
14: import java.io.Serializable;
15: import java.lang.reflect.InvocationTargetException;
16: import java.lang.reflect.Method;
17:
18: /**
19: * Abstract base class for system attributes
20: *
21: * @author Adrian Price
22: */
23: public abstract class AbstractSystemAttribute implements
24: AttributeInstance, Serializable {
25:
26: private static final long serialVersionUID = -9079162336408806114L;
27:
28: // TODO: regenerate the property descriptor after deserialization.
29: private transient PropertyDescriptor _propDesc;
30:
31: protected AbstractSystemAttribute(PropertyDescriptor propDesc) {
32: _propDesc = propDesc;
33: }
34:
35: public String getName() {
36: return _propDesc.getName();
37: }
38:
39: public int getType() {
40: // TODO: consider whether to optimize speed or memory usage.
41: // This implementation optimizes memory because it avoids the overhead
42: // of an additional int type instance field. Alternatively could
43: // optimize performance by doing this lookup in the ctor and storing the
44: // result in a private instance field.
45: return DataTypes.typeForClass(_propDesc.getPropertyType());
46: }
47:
48: public Object getValue() {
49: try {
50: // Cast required to suppress JDK1.5 varargs compiler warning.
51: return _propDesc.getReadMethod().invoke(getOwner(),
52: (Object[]) null);
53: } catch (IllegalAccessException e) {
54: throw new OBERuntimeException(e);
55: } catch (InvocationTargetException e) {
56: throw new OBERuntimeException(e);
57: }
58: }
59:
60: public void setValue(int type, Object value)
61: throws AttributeReadOnlyException {
62:
63: Method writeMethod = _propDesc.getWriteMethod();
64: if (writeMethod == null) {
65: throw new AttributeReadOnlyException(
66: "System attribute cannot be set directly: "
67: + getName());
68: }
69: if (type != Type.DEFAULT_TYPE)
70: throw new IllegalArgumentException(String.valueOf(type));
71:
72: // Convert the value to the appropriate type.
73: if (value != null) {
74: value = WorkflowEngine.getEngine().getServiceManager()
75: .getDataConverter().convertValue(value,
76: _propDesc.getPropertyType());
77: }
78:
79: // Store the data.
80: try {
81: writeMethod.invoke(getOwner(), new Object[] { value });
82: } catch (IllegalAccessException e) {
83: throw new OBERuntimeException(e);
84: } catch (InvocationTargetException e) {
85: throw new OBERuntimeException(e);
86: }
87: }
88: }
|