001: package org.gomba;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005:
006: import org.gomba.domains.BLOBColumnDomain;
007: import org.gomba.domains.CLOBColumnDomain;
008: import org.gomba.domains.ColumnDomain;
009: import org.gomba.domains.HeaderDomain;
010: import org.gomba.domains.ParamDomain;
011: import org.gomba.domains.ParamValuesDomain;
012: import org.gomba.domains.PathDomain;
013: import org.gomba.domains.RequestScopeDomain;
014: import org.gomba.domains.SystemPropertiesDomain;
015:
016: /**
017: * Represents a parameter.
018: *
019: * @author Flavio Tordini
020: * @version $Id: ParameterDefinition.java,v 1.1.1.1 2004/06/16 13:15:12
021: * flaviotordini Exp $
022: */
023: public class ParameterDefinition {
024:
025: private final static Map domains = new HashMap();
026:
027: private final ParameterDomain domain;
028:
029: private final String name;
030:
031: private final Class type;
032:
033: private final Object defaultValue;
034:
035: private final boolean nullable;
036:
037: static {
038: try {
039:
040: registerDomain(new ParamDomain());
041: registerDomain(new ParamValuesDomain());
042: registerDomain(new PathDomain());
043: registerDomain(new HeaderDomain());
044: registerDomain(new RequestScopeDomain());
045: registerDomain(new ColumnDomain());
046: registerDomain(new BLOBColumnDomain());
047: registerDomain(new CLOBColumnDomain());
048: registerDomain(new SystemPropertiesDomain());
049: // add new domains here
050:
051: } catch (Exception e) {
052: System.err.println(e.getMessage());
053: e.printStackTrace();
054: }
055: }
056:
057: private static final void registerDomain(ParameterDomain domain)
058: throws Exception {
059: Object previous = domains.put(domain.getName(), domain);
060: if (previous != null) {
061: throw new Exception("A domain with name '"
062: + domain.getName() + "' is already registered.");
063: }
064: }
065:
066: /**
067: * Constructor. Valid parameter domains: path, param, paramValues, header,
068: * column, requestScope.
069: *
070: * @param domain
071: * The parameter domain.
072: * @param name
073: * The parameter name.
074: * @param type
075: * The parameter Java type. May be null.
076: * @param defaultValue
077: * The parameter default value. May be null.
078: */
079: public ParameterDefinition(final String domain, final String name,
080: final Class type, final Object defaultValue,
081: final boolean nullable) throws Exception {
082: if (domain == null) {
083: throw new IllegalArgumentException("Domain cannot be null.");
084: }
085: if (name == null) {
086: throw new IllegalArgumentException("Name cannot be null.");
087: }
088:
089: // the default value requires also the type to be specified
090: if (type == null && defaultValue != null) {
091: throw new IllegalArgumentException(
092: "Cannot specify a default value without specifying the parameter type.");
093: }
094:
095: // make sure the default value type matches the parameter type
096: if (type != null && defaultValue != null
097: && defaultValue.getClass() != type) {
098: throw new IllegalArgumentException(
099: "The default value does not match the parameter type.");
100: }
101:
102: this .domain = (ParameterDomain) domains.get(domain);
103: if (this .domain == null) {
104: throw new IllegalArgumentException("Invalid domain: "
105: + domain);
106: }
107: this .name = name;
108: this .type = type;
109: this .defaultValue = defaultValue;
110: this .nullable = nullable;
111: }
112:
113: /**
114: * @return Returns the domain.
115: */
116: public ParameterDomain getDomain() {
117: return this .domain;
118: }
119:
120: /**
121: * @return Returns the name.
122: */
123: public String getName() {
124: return this .name;
125: }
126:
127: /**
128: * @return Returns the type, if any.
129: */
130: public Class getType() {
131: return this .type;
132: }
133:
134: /**
135: * @return Returns the defaultValue.
136: */
137: public Object getDefaultValue() {
138: return this .defaultValue;
139: }
140:
141: /**
142: * @return Returns true if the parameter is nullable.
143: */
144: public boolean isNullable() {
145: return this .nullable;
146: }
147:
148: /**
149: * @see java.lang.Object#toString()
150: */
151: public String toString() {
152: StringBuffer sb = new StringBuffer();
153: sb.append(this .domain.getName()).append('.').append(this .name);
154: if (this .type != null) {
155: sb.append(' ').append(this .type.getName());
156: if (this .defaultValue != null) {
157: sb.append(' ').append(this.defaultValue);
158: }
159: }
160: return sb.toString();
161: }
162:
163: }
|