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