001 /* 002 * Copyright 2005-2007 The Kuali Foundation 003 * 004 * 005 * Licensed under the Educational Community License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.opensource.org/licenses/ecl2.php 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.kuali.rice.kew.engine.node; 018 019 import java.io.StringReader; 020 021 import javax.xml.parsers.DocumentBuilder; 022 import javax.xml.parsers.DocumentBuilderFactory; 023 024 import org.apache.log4j.Level; 025 import org.apache.log4j.Logger; 026 import org.kuali.rice.kew.engine.RouteContext; 027 import org.kuali.rice.kew.engine.RouteHelper; 028 import org.kuali.rice.kew.engine.node.var.PropertyScheme; 029 import org.w3c.dom.Document; 030 import org.w3c.dom.Element; 031 import org.w3c.dom.NodeList; 032 import org.xml.sax.InputSource; 033 034 035 /** 036 * A node which Logs messages to Log4j. 037 * 038 * @author Kuali Rice Team (rice.collab@kuali.org) 039 */ 040 public class LogNode implements SimpleNode { 041 private static final Logger LOG = Logger.getLogger(LogNode.class); 042 043 public SimpleResult process(RouteContext context, RouteHelper helper) throws Exception { 044 LOG.error("processing"); 045 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 046 String contentFragment = context.getNodeInstance().getRouteNode().getContentFragment(); 047 LOG.error("contentFragment=" + contentFragment); 048 Document d = db.parse(new InputSource(new StringReader(contentFragment))); 049 Element e = d.getDocumentElement(); 050 String name = null; 051 NodeList list = e.getElementsByTagName("log"); 052 if (list != null && list.getLength() > 0) { 053 name = list.item(0).getTextContent(); 054 } 055 list = e.getElementsByTagName("level"); 056 String level = "info"; 057 if (list != null && list.getLength() > 0) { 058 level = list.item(0).getTextContent().toLowerCase(); 059 } 060 LOG.error("doc content: "+ context.getDocument().getDocContent()); 061 String valueRef = e.getElementsByTagName("message").item(0).getTextContent(); 062 Object retrievedVal = PropertiesUtil.retrieveProperty(valueRef, PropertyScheme.LITERAL_SCHEME, context); 063 LOG.error("retrieved value '" + retrievedVal + " for message '" + valueRef); 064 Logger l; 065 if (name == null) { 066 l = LOG; 067 } else { 068 l = Logger.getLogger(name); 069 } 070 l.log(Level.toLevel(level), retrievedVal); 071 return new SimpleResult(true); 072 } 073 074 }