001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/framework/trigger/TriggerProvider.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.framework.trigger;
044:
045: import java.lang.reflect.Constructor;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.HashMap;
049: import java.util.List;
050: import java.util.Map;
051:
052: import org.deegree.framework.log.ILogger;
053: import org.deegree.framework.log.LoggerFactory;
054: import org.deegree.framework.trigger.TriggerCapabilities.TRIGGER_TYPE;
055: import org.deegree.framework.util.BootLogger;
056: import org.deegree.i18n.Messages;
057:
058: /**
059: *
060: *
061: *
062: * @version $Revision: 9339 $
063: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064: * @author last edited by: $Author: apoth $
065: *
066: * @version 1.0. $Revision: 9339 $, $Date: 2007-12-27 04:31:52 -0800 (Thu, 27 Dec 2007) $
067: *
068: * @since 2.0
069: */
070: public class TriggerProvider {
071:
072: private static Map<String, TriggerProvider> providerMap = new HashMap<String, TriggerProvider>();
073:
074: private static TriggerCapabilities triggerCapabilities;
075:
076: private static final ILogger LOG = LoggerFactory
077: .getLogger(TriggerProvider.class);
078:
079: static {
080: try {
081: URL url = TriggerProvider.class
082: .getResource("triggerConfiguration.xml");
083: TriggerConfigurationDocument doc = new TriggerConfigurationDocument();
084: doc.load(url);
085: triggerCapabilities = doc.parseTriggerCapabilities();
086: // try reading trigger definitions from root that may overrides
087: // default trigger
088: url = TriggerProvider.class
089: .getResource("/triggerConfiguration.xml");
090: try {
091:
092: if (url != null) {
093: LOG
094: .logDebug("Trying to create trigger from local configuration: "
095: + url);
096: doc = new TriggerConfigurationDocument();
097: doc.load(url);
098: TriggerCapabilities temp = doc
099: .parseTriggerCapabilities();
100: triggerCapabilities.merge(temp);
101: } else {
102: LOG.logDebug("No local configuration found.");
103: }
104:
105: } catch (Exception e) {
106: e.printStackTrace();
107: BootLogger
108: .log("!!! BOOTLOG: No valid trigger configuration available from root.");
109: }
110: } catch (Exception e) {
111: e.printStackTrace();
112: }
113: }
114:
115: private String className = null;
116:
117: private TriggerProvider(String className) {
118: this .className = className;
119: }
120:
121: /**
122: *
123: * @param clss
124: */
125: public static TriggerProvider create(Class clss) {
126: String s = clss.getName();
127: if (providerMap.get(s) == null) {
128: providerMap.put(s, new TriggerProvider(s));
129: }
130: return providerMap.get(s);
131: }
132:
133: /**
134: * @return all pre triggers assigend to the calling method
135: */
136: public List<Trigger> getPreTrigger() throws TriggerException {
137:
138: List<Trigger> trigger = new ArrayList<Trigger>();
139:
140: StackTraceElement[] st = Thread.currentThread().getStackTrace();
141:
142: String mn = null;
143: for (int i = 0; i < st.length; i++) {
144: if (st[i].getClassName().equals(className)) {
145: mn = st[i].getMethodName();
146: break;
147: }
148: }
149: if (mn != null) {
150: TriggerCapability tc = triggerCapabilities
151: .getTriggerCapability(className, mn,
152: TRIGGER_TYPE.PRE);
153: if (tc != null) {
154: appendTrigger(trigger, tc);
155:
156: List<TriggerCapability> tCaps = tc.getTrigger();
157: for (int i = 0; i < tCaps.size(); i++) {
158: appendTrigger(trigger, tCaps.get(i));
159: }
160: }
161: }
162:
163: return trigger;
164: }
165:
166: /**
167: * returns all post triggers assigend to the calling method
168: *
169: * @return all post triggers assigend to the calling method
170: */
171: public List<Trigger> getPostTrigger() throws TriggerException {
172: List<Trigger> trigger = new ArrayList<Trigger>();
173:
174: StackTraceElement[] st = Thread.currentThread().getStackTrace();
175: String mn = null;
176: for (int i = 0; i < st.length; i++) {
177: if (st[i].getClassName().equals(className)) {
178: mn = st[i].getMethodName();
179: break;
180: }
181: }
182:
183: if (mn != null) {
184: TriggerCapability tc = triggerCapabilities
185: .getTriggerCapability(className, mn,
186: TRIGGER_TYPE.POST);
187:
188: if (tc != null) {
189: appendTrigger(trigger, tc);
190:
191: List<TriggerCapability> tCaps = tc.getTrigger();
192: for (int i = 0; i < tCaps.size(); i++) {
193: appendTrigger(trigger, tCaps.get(i));
194: }
195: }
196: }
197: return trigger;
198: }
199:
200: /**
201: * creates a Trigger instance from the passed TriggerCapability and add it to the passed list. If the
202: * TriggerCapability contains futher TriggerCapability entries they will also be added within a recursion
203: *
204: * @param trigger
205: * @param tc
206: * @return extended list
207: * @throws TriggerException
208: */
209: private List<Trigger> appendTrigger(List<Trigger> trigger,
210: TriggerCapability tc) throws TriggerException {
211: Class clss = tc.getPerformingClass();
212: List<String> paramNames = tc.getInitParameterNames();
213: Class[] initClasses = new Class[paramNames.size()];
214: Object[] paramVals = new Object[paramNames.size()];
215: for (int i = 0; i < initClasses.length; i++) {
216: paramVals[i] = tc.getInitParameterValue(paramNames.get(i));
217: initClasses[i] = paramVals[i].getClass();
218: }
219: try {
220: Constructor cstrtr = clss.getConstructor(initClasses);
221: LOG.logDebug("Trying to instantiate new class");
222: trigger.add((Trigger) cstrtr.newInstance(paramVals));
223: LOG
224: .logDebug("Succesfully instantiated configured trigger class.");
225: } catch (Exception e) {
226: e.printStackTrace();
227: throw new TriggerException(Messages.getMessage(
228: "FRAMEWORK_ERROR_INITIALIZING_TRIGGER", clss));
229: }
230: return trigger;
231: }
232:
233: /**
234: * performs pre triggers assigend to the calling method
235: *
236: * @param caller
237: * @param obj
238: * @return changed object passed to the trigger(s)
239: * @throws TriggerException
240: */
241: public Object[] doPreTrigger(Object caller, Object... obj)
242: throws TriggerException {
243: List<Trigger> list = getPreTrigger();
244: if (LOG.getLevel() == ILogger.LOG_DEBUG) {
245: LOG.logDebug("list of appliable pretriggers: " + list);
246: }
247: for (int i = 0; i < list.size(); i++) {
248: obj = list.get(i).doTrigger(caller, obj);
249: }
250: return obj;
251: }
252:
253: /**
254: * performs post triggers assigend to the calling method
255: *
256: * @param caller
257: * @param obj
258: * @return changed object passed to the trigger(s)
259: */
260: public Object[] doPostTrigger(Object caller, Object... obj) {
261: List<Trigger> list = getPostTrigger();
262: if (LOG.getLevel() == ILogger.LOG_DEBUG) {
263: LOG.logDebug("list of appliable posttriggers: " + list);
264: }
265: for (int i = 0; i < list.size(); i++) {
266: obj = list.get(i).doTrigger(caller, obj);
267: }
268: return obj;
269: }
270:
271: /**
272: * returns the root capabilities
273: *
274: * @return the root capabilities
275: */
276: public TriggerCapabilities getCapabilities() {
277: return triggerCapabilities;
278: }
279:
280: }
|