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