View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.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 org.kuali.rice.kew.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.kuali.rice.kew.engine.RouteContext;
27  import org.kuali.rice.kew.engine.RouteHelper;
28  import org.kuali.rice.kew.engine.node.var.PropertyScheme;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.NodeList;
32  import org.xml.sax.InputSource;
33  
34  
35  /**
36   * A node which Logs messages to Log4j.
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
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) throws Exception {
44          LOG.error("processing");
45          DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
46          String contentFragment = context.getNodeInstance().getRouteNode().getContentFragment();
47          LOG.error("contentFragment=" + contentFragment);
48          Document d = db.parse(new InputSource(new StringReader(contentFragment)));
49          Element e = d.getDocumentElement();
50          String name = null;
51          NodeList list = e.getElementsByTagName("log");
52          if (list != null && list.getLength() > 0) {
53              name = list.item(0).getTextContent();
54          }
55          list = e.getElementsByTagName("level");
56          String level = "info";
57          if (list != null && list.getLength() > 0) {
58              level = list.item(0).getTextContent().toLowerCase();
59          }
60          LOG.error("doc content: "+ context.getDocument().getDocContent());
61          String valueRef = e.getElementsByTagName("message").item(0).getTextContent();
62          Object retrievedVal = PropertiesUtil.retrieveProperty(valueRef, PropertyScheme.LITERAL_SCHEME, context);
63          LOG.error("retrieved value '" + retrievedVal + " for message '" + valueRef);
64          Logger l;
65          if (name == null) {
66              l = LOG;
67          } else {
68              l = Logger.getLogger(name);
69          }
70          l.log(Level.toLevel(level), retrievedVal);
71          return new SimpleResult(true);
72      }
73  
74  }