001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.configuration;
034:
035: import com.flexive.shared.configuration.parameters.*;
036: import com.flexive.shared.exceptions.FxCreateException;
037:
038: import java.util.HashMap;
039: import java.util.Map;
040:
041: /**
042: * <p>
043: * This factory beans provides methods for instantiating new parameter objects to be used with
044: * flexive's configuration API. To create a typesafe parameter definition, you must supply
045: * the parameter class (e.g. {@link Long}) and the attributes describing the parameter itself,
046: * such as the (global) path it's stored under and its scope (e.g. user- or division-scoped).
047: * </p>
048: * <p>
049: * :
050: * <pre>
051: * // define a boolean parameter "booleanParam" for path "/configuration" with a default value of false
052: * Parameter<Boolean> bp = ParameterFactory.newInstance(Boolean.class, "/configuration", ParameterScope.USER, "booleanParam", false);
053: *
054: * // define a long parameter with a default value of 25
055: * Parameter<Long> lp = ParameterFactory.newInstance(Long.class, "/configuration", ParameterScope.USER, "longParam", 25L);
056: *
057: * // define an object parameter for QueryRootNode.class with no default value that works by serializing the objects via XStream
058: * Parameter<QueryRootNode> DEFAULT_QUERY = ParameterFactory.newInstance(QueryRootNode.class, "/configuration", ParameterScope.USER, "defaultQuery", null);
059: * </pre>
060: * More examples can be found in {@link SystemParameters}.
061: * </p>
062: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
063: * @version $Rev: 181 $
064: */
065: public final class ParameterFactory {
066: private static final Map<Class, Class<? extends Parameter>> INSTANCES = new HashMap<Class, Class<? extends Parameter>>();
067:
068: static {
069: INSTANCES.put(Boolean.class, BooleanParameter.class);
070: INSTANCES.put(Integer.class, IntegerParameter.class);
071: INSTANCES.put(Long.class, LongParameter.class);
072: INSTANCES.put(String.class, StringParameter.class);
073: }
074:
075: private ParameterFactory() {
076: }
077:
078: public static <T> Parameter<T> newInstance(Class<T> cls,
079: ParameterData<T> data) {
080: return getImpl(cls).setData(data).freeze();
081: }
082:
083: public static <T> Parameter<T> newInstance(Class<T> cls,
084: ParameterPath path, String key, T defaultValue) {
085: return newInstance(cls, new ParameterDataBean<T>(path, key,
086: defaultValue));
087: }
088:
089: public static <T> Parameter<T> newInstance(Class<T> cls,
090: ParameterPath path, T defaultValue) {
091: return newInstance(cls, path, "", defaultValue);
092: }
093:
094: public static <T> Parameter<T> newInstance(Class<T> cls,
095: String path, ParameterScope scope, String key,
096: T defaultValue) {
097: return newInstance(cls, new ParameterPathBean(path, scope),
098: key, defaultValue);
099: }
100:
101: public static <T> Parameter<T> newInstance(Class<T> cls,
102: String path, ParameterScope scope, T defaultValue) {
103: return newInstance(cls, new ParameterPathBean(path, scope), "",
104: defaultValue);
105: }
106:
107: private static <T> Parameter<T> getImpl(Class<T> cls) {
108: if (INSTANCES.containsKey(cls)) {
109: try {
110: //noinspection unchecked
111: return INSTANCES.get(cls).newInstance();
112: } catch (Exception e) {
113: throw new FxCreateException(
114: "ex.configuration.parameter.impl.instantiate",
115: INSTANCES.get(cls), e).asRuntimeException();
116: }
117: } else {
118: // handle all non-primitive objects with generical xml-based parameters
119: return new ObjectParameter<T>(null, cls, false);
120: //throw new FxNotFoundException("ex.configuration.parameter.impl.notFound", cls).asRuntimeException();
121: }
122: }
123: }
|