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.ejb.beans.configuration;
034:
035: import com.flexive.shared.configuration.Parameter;
036: import com.flexive.shared.configuration.ParameterScope;
037: import com.flexive.shared.exceptions.*;
038: import com.flexive.shared.interfaces.*;
039:
040: import javax.ejb.*;
041: import java.io.Serializable;
042: import java.util.*;
043:
044: /**
045: * Configuration wrapper implementation.
046: *
047: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: */
049: @Stateless(name="ConfigurationEngine")
050: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
051: @TransactionManagement(TransactionManagementType.CONTAINER)
052: public class ConfigurationEngineBean implements ConfigurationEngine,
053: ConfigurationEngineLocal {
054: @EJB
055: private DivisionConfigurationEngineLocal divisionConfiguration;
056: @EJB
057: private UserConfigurationEngineLocal userConfiguration;
058: @EJB
059: private GlobalConfigurationEngineLocal globalConfiguration;
060:
061: /**
062: * {@inheritDoc}
063: */
064: @TransactionAttribute(TransactionAttributeType.REQUIRED)
065: public <T extends Serializable> T get(Parameter<T> parameter,
066: String key) throws FxApplicationException {
067: return get(parameter, key, false);
068: }
069:
070: /**
071: * {@inheritDoc}
072: */
073: @TransactionAttribute(TransactionAttributeType.REQUIRED)
074: public <T extends Serializable> T get(Parameter<T> parameter)
075: throws FxApplicationException {
076: return get(parameter, parameter.getData().getKey());
077: }
078:
079: /**
080: * {@inheritDoc}
081: */
082: @TransactionAttribute(TransactionAttributeType.REQUIRED)
083: public <T extends Serializable> T get(Parameter<T> parameter,
084: String key, boolean ignoreDefault)
085: throws FxApplicationException {
086: for (GenericConfigurationEngine config : getAvailableConfigurations(parameter
087: .getScope())) {
088: try {
089: return config.get(parameter, key, true);
090: // CHECKSTYLE:OFF
091: } catch (FxNotFoundException e) {
092: // try next config
093: // CHECKSTYLE:ON
094: }
095: }
096: // parameter does not exist in any configuration, use default value if available
097: if (!ignoreDefault && parameter.getDefaultValue() != null) {
098: return parameter.getDefaultValue();
099: } else {
100: throw new FxNotFoundException(
101: "ex.configuration.parameter.notfound", parameter
102: .getData().getPath().getValue(), key);
103: }
104: }
105:
106: /**
107: * {@inheritDoc}
108: */
109: @TransactionAttribute(TransactionAttributeType.REQUIRED)
110: public <T extends Serializable> void putInSource(
111: Parameter<T> parameter, String key, T value)
112: throws FxApplicationException {
113: getSource(parameter, key).put(parameter, key, value);
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: @TransactionAttribute(TransactionAttributeType.REQUIRED)
120: public <T extends Serializable> void putInSource(
121: Parameter<T> parameter, T value)
122: throws FxApplicationException {
123: putInSource(parameter, parameter.getData().getKey(), value);
124: }
125:
126: private <T extends Serializable> GenericConfigurationEngine getSource(
127: Parameter<T> parameter, String key)
128: throws FxApplicationException {
129: for (GenericConfigurationEngine config : getAvailableConfigurations(parameter
130: .getScope())) {
131: try {
132: config.get(parameter, key, true);
133: return config;
134: // CHECKSTYLE:OFF
135: } catch (FxNotFoundException e) {
136: // try next config
137: // CHECKSTYLE:ON
138: }
139: }
140: throw new FxNotFoundException(
141: "ex.configuration.parameter.notfound", parameter
142: .getPath().getValue(), key);
143: }
144:
145: /**
146: * {@inheritDoc}
147: */
148: @TransactionAttribute(TransactionAttributeType.REQUIRED)
149: public <T extends Serializable> Map<String, T> getAll(
150: Parameter<T> parameter) throws FxApplicationException {
151: List<GenericConfigurationEngine> configs;
152: try {
153: configs = getAvailableConfigurations(parameter.getScope());
154: } catch (FxInvalidParameterException e) {
155: throw new FxLoadException(
156: "ex.configuration.parameter.load.ex", e);
157: }
158: // reverse configs to process fallback configs first
159: Collections.reverse(configs);
160: HashMap<String, T> result = new HashMap<String, T>();
161: for (GenericConfigurationEngine config : configs) {
162: Map<String, T> params = config.getAll(parameter);
163: result.putAll(params);
164: }
165: return result;
166: }
167:
168: /**
169: * {@inheritDoc}
170: */
171: @TransactionAttribute(TransactionAttributeType.REQUIRED)
172: public <T extends Serializable> Collection<String> getKeys(
173: Parameter<T> parameter) throws FxApplicationException {
174: return getAll(parameter).keySet();
175: }
176:
177: /**
178: * {@inheritDoc}
179: */
180: @TransactionAttribute(TransactionAttributeType.REQUIRED)
181: public <T extends Serializable> void put(Parameter<T> parameter,
182: String key, T value) throws FxApplicationException {
183: try {
184: getPrimaryConfiguration(parameter.getScope()).put(
185: parameter, key, value);
186: } catch (FxInvalidParameterException e) {
187: throw new FxUpdateException("ex.configuration.update.ex", e);
188: }
189: }
190:
191: /**
192: * {@inheritDoc}
193: */
194: @TransactionAttribute(TransactionAttributeType.REQUIRED)
195: public <T extends Serializable> void put(Parameter<T> parameter,
196: T value) throws FxApplicationException {
197: try {
198: getPrimaryConfiguration(parameter.getScope()).put(
199: parameter, value);
200: } catch (FxInvalidParameterException e) {
201: throw new FxUpdateException("ex.configuration.update.ex", e);
202: }
203: }
204:
205: /**
206: * {@inheritDoc}
207: */
208: @TransactionAttribute(TransactionAttributeType.REQUIRED)
209: public <T extends Serializable> void remove(Parameter<T> parameter,
210: String key) throws FxApplicationException {
211: try {
212: getPrimaryConfiguration(parameter.getScope()).remove(
213: parameter, key);
214: } catch (FxInvalidParameterException e) {
215: throw new FxRemoveException("ex.configuration.delete.ex", e);
216: }
217: }
218:
219: /**
220: * {@inheritDoc}
221: */
222: @TransactionAttribute(TransactionAttributeType.REQUIRED)
223: public <T extends Serializable> void remove(Parameter<T> parameter)
224: throws FxApplicationException {
225: try {
226: getPrimaryConfiguration(parameter.getScope()).remove(
227: parameter);
228: } catch (FxInvalidParameterException e) {
229: throw new FxRemoveException("ex.configuration.delete.ex", e);
230: }
231: }
232:
233: /**
234: * {@inheritDoc}
235: */
236: @TransactionAttribute(TransactionAttributeType.REQUIRED)
237: public <T extends Serializable> void removeAll(
238: Parameter<T> parameter) throws FxApplicationException {
239: try {
240: getPrimaryConfiguration(parameter.getScope()).removeAll(
241: parameter);
242: } catch (FxInvalidParameterException e) {
243: throw new FxRemoveException("ex.configuration.delete.ex", e);
244: }
245: }
246:
247: /**
248: * Returns the available list of configurations to be used for looking
249: * up a parameter with the given scope. The first list entry is to
250: * be queried first, the second only if the parameter does not exist
251: * in the first config, and so on.
252: *
253: * @param scope parameter scope
254: * @return ordered list of configurations to be used for looking up the parameter
255: * @throws FxInvalidParameterException if the given scope is invalid
256: */
257: private List<GenericConfigurationEngine> getAvailableConfigurations(
258: ParameterScope scope) throws FxInvalidParameterException {
259: List<GenericConfigurationEngine> result = new ArrayList<GenericConfigurationEngine>();
260: result.add(getPrimaryConfiguration(scope));
261: List<ParameterScope> fallbacks = scope.getFallbacks();
262: for (ParameterScope fallback : fallbacks) {
263: result.add(getPrimaryConfiguration(fallback));
264: }
265: return result;
266: }
267:
268: /**
269: * Returns the primary configuration to be used for the given scope.
270: *
271: * @param scope scope to be queried
272: * @return the primary configuration to be used for the given scope.
273: * @throws FxInvalidParameterException if the scope is invalid
274: */
275: private GenericConfigurationEngine getPrimaryConfiguration(
276: ParameterScope scope) throws FxInvalidParameterException {
277: if (scope == ParameterScope.GLOBAL) {
278: return globalConfiguration;
279: } else if (scope == ParameterScope.DIVISION
280: || scope == ParameterScope.DIVISION_ONLY) {
281: return divisionConfiguration;
282: } else if (scope == ParameterScope.USER
283: || scope == ParameterScope.USER_ONLY) {
284: return userConfiguration;
285: } else {
286: throw new FxInvalidParameterException("SCOPE",
287: "ex.configuration.parameter.scope.invalid", scope);
288: }
289: }
290: }
|