KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > LogRegister


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util;
6
7 import com.opensymphony.util.TextUtils;
8
9 import com.opensymphony.workflow.Register;
10 import com.opensymphony.workflow.WorkflowContext;
11 import com.opensymphony.workflow.spi.WorkflowEntry;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import java.util.Map JavaDoc;
17
18
19 /**
20  * This is a register, which helps logging using commons-logging.
21  * It wraps a Logger instance which is linked to the "OSWorkflow/<workflow_name>/<id>" category
22  *
23  * Following optional arguments available:
24  * <ul>
25  * <li>addInstanceId=true/false - if the instance id of the workflow should be added to the category. Defaults to false</li>
26  * <li>Category="OSWorkflow" - change the name of the log category other than "OSWorkflow"</li>
27  * </ul>
28  *
29  *
30  * If you register this class as "Logger", then you may use it from a Beanshell script like:
31  * <pre>
32  * logger = transientVars.get("logger");
33  * logger.debug("hello logger!");
34  * </pre>
35  *
36  * @author Zoltan Luspai
37  */

38 public class LogRegister implements Register {
39     //~ Methods ////////////////////////////////////////////////////////////////
40

41     /**
42      * @see com.opensymphony.workflow.Register#registerVariable(WorkflowContext, WorkflowEntry, Map)
43      */

44     public Object JavaDoc registerVariable(WorkflowContext context, WorkflowEntry entry, Map JavaDoc args) {
45         String JavaDoc workflowname = "unknown";
46         long workflow_id = -1;
47
48         if (entry != null) {
49             workflowname = entry.getWorkflowName();
50             workflow_id = entry.getId();
51         }
52
53         boolean groupByInstance = false;
54         String JavaDoc useInstance = (String JavaDoc) args.get("addInstanceId");
55
56         if (useInstance != null) {
57             groupByInstance = TextUtils.parseBoolean(useInstance);
58         }
59
60         String JavaDoc categoryName = "OSWorkflow";
61
62         if (args.get("Category") != null) {
63             categoryName = (String JavaDoc) args.get("Category");
64         }
65
66         String JavaDoc category = categoryName + "." + workflowname;
67
68         if (groupByInstance) {
69             category += ("." + (Long.toString(workflow_id)));
70         }
71
72         Log log = LogFactory.getLog(category);
73
74         return log;
75     }
76 }
77
Popular Tags