001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.generator;
021:
022: import java.io.BufferedOutputStream;
023: import java.io.IOException;
024: import java.io.OutputStreamWriter;
025: import java.io.Writer;
026: import java.text.DateFormat;
027: import java.text.SimpleDateFormat;
028: import java.util.Arrays;
029: import java.util.Date;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.TreeSet;
033:
034: import org.apache.log4j.Logger;
035:
036: import de.schlund.pfixxml.resources.FileResource;
037: import de.schlund.pfixxml.resources.ResourceUtil;
038:
039: /**
040: * IWrapperImpl.java
041: *
042: *
043: * Created: Wed Aug 8 14:19:39 2001
044: *
045: * @author <a href="mailto: "Jens Lautenbacher</a>
046: *
047: *
048: */
049:
050: public abstract class IWrapperImpl implements IWrapper {
051: protected RequestData req;
052: protected String prefix = "__undef";
053: protected Integer order = new Integer(0);
054: private Logger LOG = Logger.getLogger(this .getClass());
055: private FileResource logdir = null;
056: private String pagename = null;
057: private String visitid = null;
058:
059: protected HashMap<String, IWrapperParam> params = null; // single static parameters (of the form PREFIX.NAME)
060: protected HashMap<String, IWrapperParam> errors = null; // errors on single parameters
061: protected HashMap<String, IWrapperIndexedParam> idxprms = null; // array like indexed parameters (of the form PREFIX.NAME.INDEX)
062: protected IHandler handler = null; // Make sure that you set the handler in the
063:
064: // constructor of a derived class
065:
066: public void initLogging(FileResource logdir, String pagename,
067: String visitid) {
068: LOG.debug("*** Logging input for " + prefix + " into " + logdir
069: + " " + pagename + " " + visitid + " ***");
070: this .logdir = logdir;
071: this .pagename = pagename;
072: this .visitid = visitid;
073: }
074:
075: public void tryErrorLogging() throws IOException {
076: if (logdir != null && pagename != null && visitid != null) {
077: FileResource log = ResourceUtil.getFileResource(logdir,
078: pagename + "#" + prefix);
079: Writer out = new OutputStreamWriter(
080: new BufferedOutputStream(log.getOutputStream(true)));
081: IWrapperParam[] tmperrors = gimmeAllParamsWithErrors();
082: if (tmperrors != null && tmperrors.length > 0) {
083: StringBuffer buff = getLogBuffer("ERRORS");
084: for (int j = 0; j < tmperrors.length; j++) {
085: IWrapperParam param = tmperrors[j];
086: StatusCodeInfo[] scodes = param
087: .getStatusCodeInfos();
088: if (scodes != null) {
089: appendErrorLog(param, buff);
090: }
091: }
092: out.write(buff.toString() + "\n");
093: out.flush();
094: }
095: }
096: }
097:
098: public void tryParamLogging() throws IOException {
099: if (logdir != null && pagename != null && visitid != null) {
100: FileResource log = ResourceUtil.getFileResource(logdir,
101: pagename + "#" + prefix);
102: Writer out = new OutputStreamWriter(
103: new BufferedOutputStream(log.getOutputStream(true)));
104: StringBuffer buff = getLogBuffer("VALUES");
105: for (Iterator<IWrapperParam> iter = params.values()
106: .iterator(); iter.hasNext();) {
107: appendParamLog(iter.next(), buff);
108: }
109: for (Iterator<IWrapperIndexedParam> iter = idxprms.values()
110: .iterator(); iter.hasNext();) {
111: IWrapperIndexedParam pindex = iter.next();
112: IWrapperParam[] pinfoarr = pindex.getAllParams();
113: for (int i = 0; i < pinfoarr.length; i++) {
114: appendParamLog(pinfoarr[i], buff);
115: }
116: }
117: out.write(buff.toString() + "\n");
118: out.flush();
119: }
120: }
121:
122: public final void init(String prefix) throws Exception {
123: params = new HashMap<String, IWrapperParam>();
124: errors = new HashMap<String, IWrapperParam>();
125: idxprms = new HashMap<String, IWrapperIndexedParam>();
126: this .prefix = prefix;
127: registerParams();
128: }
129:
130: public final void load(RequestData req) throws Exception {
131: this .req = req;
132:
133: for (Iterator<IWrapperParam> i = params.values().iterator(); i
134: .hasNext();) {
135: IWrapperParam pinfo = i.next();
136: if (LOG.isDebugEnabled()) {
137: LOG.debug("===> Doing init for Param: "
138: + pinfo.getName());
139: }
140: pinfo.initValueFromRequest(prefix, req);
141: if (pinfo.errorHappened()) {
142: if (LOG.isDebugEnabled()) {
143: LOG.debug("*** ERROR happened for Param: "
144: + pinfo.getName());
145: }
146: synchronized (errors) {
147: errors.put(pinfo.getName(), pinfo);
148: }
149: }
150: }
151: for (Iterator<IWrapperIndexedParam> i = idxprms.values()
152: .iterator(); i.hasNext();) {
153: IWrapperIndexedParam pindex = i.next();
154: if (LOG.isDebugEnabled()) {
155: LOG.debug("===> Doing init for IndexedParam: "
156: + pindex.getName());
157: }
158: pindex.initValueFromRequest(prefix, req);
159: // error handling happens inside the IWRapperIndexedParam...
160: }
161: }
162:
163: public final void loadFromStringValues() throws Exception {
164: for (IWrapperParam pinfo : params.values())
165: pinfo.initFromStringValue();
166: for (IWrapperIndexedParam pindex : idxprms.values())
167: pindex.initFromStringValue();
168: }
169:
170: public final IHandler gimmeIHandler() {
171: if (handler == null) {
172: throw new RuntimeException(
173: "ERROR: You need to define a IHandler for IWrapper "
174: + this .getClass().getName());
175: }
176: return handler;
177: }
178:
179: public final String gimmePrefix() {
180: return prefix;
181: }
182:
183: public final void defineOrder(int order) {
184: this .order = new Integer(order);
185: }
186:
187: public final Integer gimmeOrder() {
188: return order;
189: }
190:
191: public final boolean errorHappened() {
192: boolean noerr = errors.isEmpty();
193: if (noerr) {
194: synchronized (idxprms) {
195: for (Iterator<IWrapperIndexedParam> i = idxprms
196: .values().iterator(); i.hasNext();) {
197: IWrapperIndexedParam pindx = i.next();
198: if (pindx.errorHappened()) {
199: noerr = false;
200: break;
201: }
202: }
203: }
204: }
205: return !noerr;
206: }
207:
208: public final IWrapperParamDefinition[] gimmeAllParamDefinitions() {
209: TreeSet<IWrapperParamDefinition> retpar = new TreeSet<IWrapperParamDefinition>();
210: synchronized (params) {
211: retpar.addAll(params.values());
212: }
213: synchronized (idxprms) {
214: retpar.addAll(idxprms.values());
215: }
216: return (IWrapperParamDefinition[]) retpar
217: .toArray(new IWrapperParamDefinition[] {});
218: }
219:
220: public final IWrapperParam[] gimmeAllParams() {
221: TreeSet<IWrapperParam> retpar = new TreeSet<IWrapperParam>();
222: synchronized (params) {
223: retpar.addAll(params.values());
224: }
225: synchronized (idxprms) {
226: for (Iterator<IWrapperIndexedParam> i = idxprms.values()
227: .iterator(); i.hasNext();) {
228: IWrapperIndexedParam pindex = i.next();
229: retpar.addAll(Arrays.asList(pindex.getAllParams()));
230: }
231: }
232: return (IWrapperParam[]) retpar.toArray(new IWrapperParam[] {});
233: }
234:
235: public final IWrapperParam[] gimmeAllParamsWithErrors() {
236: TreeSet<IWrapperParam> retpar = new TreeSet<IWrapperParam>();
237: synchronized (errors) {
238: retpar.addAll(errors.values());
239: }
240: synchronized (idxprms) {
241: for (Iterator<IWrapperIndexedParam> i = idxprms.values()
242: .iterator(); i.hasNext();) {
243: IWrapperIndexedParam pindex = i.next();
244: retpar.addAll(Arrays.asList(pindex
245: .getAllParamsWithErrors()));
246: }
247: }
248: return (IWrapperParam[]) retpar.toArray(new IWrapperParam[] {});
249: }
250:
251: public void addSCode(IWrapperParam param,
252: de.schlund.util.statuscodes.StatusCode scode,
253: String args[], String level) {
254: param.addSCode(scode, args, level);
255: synchronized (errors) {
256: errors.put(param.getName(), param);
257: }
258: }
259:
260: protected final IWrapperParam gimmeParamForKey(String key) {
261: synchronized (params) {
262: return (IWrapperParam) params.get(key);
263: }
264: }
265:
266: protected final IWrapperIndexedParam gimmeIndexedParamForKey(
267: String key) {
268: synchronized (idxprms) {
269: return (IWrapperIndexedParam) idxprms.get(key);
270: }
271: }
272:
273: protected void registerParams() {
274: // DO NOTHING.
275: // This could be abstract, but descendents should be able to call
276: // super.registerParams() without worrying.
277: }
278:
279: public final int compareTo(IWrapper in) {
280: return (gimmeOrder().compareTo(in.gimmeOrder()));
281: }
282:
283: private StringBuffer getLogBuffer(String init) {
284: StringBuffer buff = new StringBuffer(256);
285: long now = System.currentTimeMillis();
286: DateFormat fmt = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
287: buff.append(fmt.format(new Date(now)) + "|" + visitid + "|"
288: + init);
289: return buff;
290: }
291:
292: private void appendParamLog(IWrapperParam pinfo, StringBuffer buff) {
293: String name = pinfo.getName();
294: String[] value = pinfo.getStringValue();
295: buff.append("|" + name + "=");
296: if (value != null) {
297: for (int i = 0; i < value.length; i++) {
298: buff.append(value[i]);
299: if (i < (value.length - 1)) {
300: buff.append("&");
301: }
302: }
303: } else {
304: buff.append("NULL");
305: }
306: }
307:
308: private void appendErrorLog(IWrapperParam pinfo, StringBuffer buff) {
309: String name = pinfo.getName();
310: StatusCodeInfo[] scodes = pinfo.getStatusCodeInfos();
311: if (scodes != null) {
312: buff.append("|" + name + ":");
313: for (int i = 0; i < scodes.length; i++) {
314: buff.append(scodes[i]);
315: if (i < (scodes.length - 1)) {
316: buff.append(";");
317: }
318: }
319: }
320: }
321:
322: public String toString() {
323: StringBuffer sb = new StringBuffer(255);
324: String name = getClass().getName();
325: int ind = name.lastIndexOf('.');
326: if (ind > -1)
327: name = name.substring(ind + 1);
328: sb.append("*** All wrapper-data for " + name + " {\n");
329: IWrapperParam[] params = gimmeAllParams();
330: for (IWrapperParam param : params) {
331: if (param.getFrequency().equals("single")) {
332: sb.append(param.getName() + " = " + param.getValue())
333: .append("\n");
334: } else {
335: if (param.getValueArr() == null) {
336: sb.append(param.getName() + "[] = NULL");
337: } else {
338: Object[] values = param.getValueArr();
339: for (int i = 0; i < values.length; i++) {
340: sb.append(
341: param.getName() + "[" + i + "] = "
342: + values[i]).append("\n");
343: }
344: }
345: }
346: }
347: sb.append("}\n");
348: return sb.toString();
349: }
350:
351: } // IWrapperImpl
|