01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.engine.node;
18:
19: import java.io.StringReader;
20:
21: import javax.xml.parsers.DocumentBuilder;
22: import javax.xml.parsers.DocumentBuilderFactory;
23:
24: import org.apache.log4j.Level;
25: import org.apache.log4j.Logger;
26: import org.w3c.dom.Document;
27: import org.w3c.dom.Element;
28: import org.w3c.dom.NodeList;
29: import org.xml.sax.InputSource;
30:
31: import edu.iu.uis.eden.engine.RouteContext;
32: import edu.iu.uis.eden.engine.RouteHelper;
33: import edu.iu.uis.eden.engine.node.var.PropertyScheme;
34:
35: /**
36: * A node which Logs messages to Log4j.
37: *
38: * @author ahamid
39: */
40: public class LogNode implements SimpleNode {
41: private static final Logger LOG = Logger.getLogger(LogNode.class);
42:
43: public SimpleResult process(RouteContext context, RouteHelper helper)
44: throws Exception {
45: LOG.error("processing");
46: DocumentBuilder db = DocumentBuilderFactory.newInstance()
47: .newDocumentBuilder();
48: String contentFragment = context.getNodeInstance()
49: .getRouteNode().getContentFragment();
50: LOG.error("contentFragment=" + contentFragment);
51: Document d = db.parse(new InputSource(new StringReader(
52: contentFragment)));
53: Element e = d.getDocumentElement();
54: String name = null;
55: NodeList list = e.getElementsByTagName("log");
56: if (list != null && list.getLength() > 0) {
57: name = list.item(0).getTextContent();
58: }
59: list = e.getElementsByTagName("level");
60: String level = "info";
61: if (list != null && list.getLength() > 0) {
62: level = list.item(0).getTextContent().toLowerCase();
63: }
64: LOG.error("doc content: "
65: + context.getDocument().getDocContent());
66: String valueRef = e.getElementsByTagName("message").item(0)
67: .getTextContent();
68: Object retrievedVal = PropertiesUtil.retrieveProperty(valueRef,
69: PropertyScheme.LITERAL_SCHEME, context);
70: LOG.error("retrieved value '" + retrievedVal + " for message '"
71: + valueRef);
72: Logger l;
73: if (name == null) {
74: l = LOG;
75: } else {
76: l = Logger.getLogger(name);
77: }
78: l.log(Level.toLevel(level), retrievedVal);
79: return new SimpleResult(true);
80: }
81:
82: }
|