001: /**
002: * Takes care of request pre-processing.
003: */package com.lutris.appserver.server;
004:
005: import java.lang.reflect.Constructor;
006: import java.lang.reflect.InvocationTargetException;
007: import java.util.Vector;
008:
009: import org.enhydra.util.RequestPreProcessor;
010: import org.enhydra.util.ResponsePostProcessor;
011:
012: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
013: import com.lutris.logging.LogChannel;
014: import com.lutris.logging.Logger;
015: import com.lutris.util.Config;
016: import com.lutris.util.ConfigException;
017: import com.lutris.util.KeywordValueException;
018:
019: /**
020: * @author Slobodan Vujasinovic
021: *
022: */
023: public class RequestPreProcessingManager {
024:
025: /**
026: * RequestPreProcessingManager configuration prefix
027: */
028: public static final String CONFIG_PREFIX = "RequestPreProcessor";
029:
030: /**
031: * LogChannel used by RequestPreProcessingManager
032: */
033: private LogChannel logChannel;
034:
035: /**
036: * Vector containing registered preprocessor implementations for
037: * corresponding application.
038: */
039: private Vector preProcessors;
040:
041: /**
042: * Request pre-processing profiling level
043: *
044: * Negative - profile everything
045: * Zero - profile nothing
046: * Positive - profile only processing that lasted longer
047: * than specified number of milliseconds
048: */
049: private int profileLevel = 0;
050:
051: /**
052: * To do profiling or not
053: */
054: private boolean doProfile = false;
055:
056: /**
057: * Initialize preprocessing manager according to application configuration!
058: *
059: * @param config
060: */
061: public RequestPreProcessingManager(Config config,
062: LogChannel logChannel) {
063: this .logChannel = logChannel;
064: preProcessors = new Vector();
065: try {
066: // Profiling Initialization
067: profileLevel = config.getInt("TimeProfiler", 0);
068: if (profileLevel != 0) {
069: doProfile = true;
070: }
071:
072: String[] ppNames = config.getStrings("Names", null);
073: if (ppNames != null) {
074: for (int i = 0; i < ppNames.length; i++) {
075: Config ppConfig = null;
076: try {
077: ppConfig = config.getConfig(ppNames[i]);
078: if (config.getConfigFile() != null
079: && ppConfig != null)
080: ppConfig.setConfigFile(config
081: .getConfigFile());
082: } catch (KeywordValueException kwe) {
083: kwe.printStackTrace();
084: }
085: if (ppConfig != null) {
086: String ppClassName = ppConfig.getString(
087: "Class", null);
088: if (ppClassName != null) {
089: Class ppClass = null;
090: try {
091: ppClass = Class.forName(ppClassName);
092: } catch (ClassNotFoundException cnf) {
093: cnf.printStackTrace();
094: }
095: if (ppClass != null) {
096: Class[] ArgClassArray = new Class[] {};
097: Object[] ArgObject = new Object[] {};
098:
099: Constructor ppConstructor = null;
100: try {
101: ppConstructor = ppClass
102: .getDeclaredConstructor(ArgClassArray);
103: } catch (NoSuchMethodException nsm) {
104: nsm.printStackTrace();
105: }
106: if (ppConstructor != null) {
107: RequestPreProcessor rPP = null;
108: try {
109: rPP = (RequestPreProcessor) (ppConstructor
110: .newInstance(ArgObject));
111: rPP.setName(ppNames[i]);
112: rPP.setLogChannel(logChannel);
113: } catch (InstantiationException ie) {
114: ie.printStackTrace();
115: } catch (InvocationTargetException ite) {
116: ite.printStackTrace();
117: } catch (IllegalAccessException iae) {
118: iae.printStackTrace();
119: }
120: if (rPP != null) {
121: rPP.configure(ppConfig);
122: preProcessors.add(rPP);
123: }
124: }
125: }
126: }
127: }
128: }
129: }
130: } catch (ConfigException ce) {
131: if (logChannel != null) {
132: logChannel
133: .write(Logger.ERROR,
134: "Problem with RequestPreProcessing initialization!");
135: }
136: ce.printStackTrace();
137: }
138: }
139:
140: /**
141: * Manages request preprocessing.
142: *
143: * @param comms
144: * @return
145: */
146: public boolean manage(HttpPresentationComms comms) {
147: boolean returnValue = false;
148: if (logChannel != null) {
149: logChannel.write(Logger.DEBUG,
150: "Managing request preprocessing!");
151: }
152: for (int i = 0; i < preProcessors.size(); i++) {
153: boolean tempReturnValue = false;
154: RequestPreProcessor rPP = (RequestPreProcessor) ((RequestPreProcessor) preProcessors
155: .get(i)).clone();
156: // RequestPreProcessor rPP = (RequestPreProcessor) preProcessors
157: // .get(i);
158:
159: // Start Profiling Time
160: long processingTime = startProfiler();
161:
162: tempReturnValue = rPP.process(comms);
163:
164: // End Profiling Time
165: stopProfiler(processingTime, rPP.getName());
166:
167: returnValue = tempReturnValue || returnValue;
168: }
169: return returnValue;
170: }
171:
172: private long startProfiler() {
173: return doProfile ? System.currentTimeMillis() : -1;
174: }
175:
176: private void stopProfiler(long processingTime, String name) {
177: if (doProfile) {
178: processingTime = System.currentTimeMillis()
179: - processingTime;
180: if (processingTime > profileLevel) {
181: if (logChannel != null) {
182: logChannel
183: .write(
184: Logger.WARNING,
185: "RequestPreProcessor "
186: + name
187: + " took "
188: + processingTime
189: + " milliseconds for request processing!");
190: }
191: }
192: }
193: }
194:
195: public boolean addPreProcessor(int position, RequestPreProcessor rPP) {
196: if (position >= 0) {
197: preProcessors.add(position, rPP);
198: if (logChannel != null) {
199: logChannel.write(Logger.INFO,
200: "New RequestPreProcessor introduced!");
201: }
202: } else {
203: return preProcessors.add(rPP);
204: }
205: return true;
206: }
207:
208: public boolean removePreProcessor(int position) {
209: if (position >= 0) {
210: preProcessors.removeElementAt(position);
211: if (logChannel != null) {
212: logChannel.write(Logger.INFO,
213: "RequestPreProcessor removed!");
214: }
215: } else {
216: return false;
217: }
218: return true;
219: }
220:
221: public boolean removePreProcessor(RequestPreProcessor rPP) {
222: return preProcessors.removeElement(rPP);
223: }
224:
225: public boolean removePreProcessor(String ppName) {
226: if (ppName != null) {
227: for (int i = 0; i < preProcessors.size(); i++) {
228: if (ppName
229: .equals(((ResponsePostProcessor) preProcessors
230: .elementAt(i)).getName())) {
231: preProcessors.removeElementAt(i);
232: return true;
233: }
234: }
235: }
236: return false;
237:
238: }
239:
240: public Vector getPreProcessors() {
241: return preProcessors;
242: }
243:
244: public void setPreProcessors(Vector preProcessors) {
245: try {
246: for (int i = 0; i < preProcessors.size(); i++) {
247: RequestPreProcessor pp = (RequestPreProcessor) preProcessors
248: .elementAt(i);
249: }
250: this .preProcessors = preProcessors;
251: } catch (Exception ex) {
252: // in case of exception - do nothing
253: if (logChannel != null) {
254: logChannel
255: .write(Logger.ERROR,
256: "Problem occurred during pre-processor vector acceptance!");
257: }
258: ex.printStackTrace();
259: }
260: }
261:
262: }
|