View Javadoc
1   /**
2    * Copyright 2005-2016 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.krms.framework.engine;
17  
18  import javax.swing.event.EventListenerList;
19  
20  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
21  import org.kuali.rice.krms.api.engine.ExecutionFlag;
22  import org.kuali.rice.krms.api.engine.ResultEvent;
23  import org.kuali.rice.krms.framework.engine.result.EngineResultListener;
24  import org.kuali.rice.krms.framework.engine.result.Log4jResultListener;
25  import org.kuali.rice.krms.framework.engine.result.ResultListener;
26  
27  /**
28   * A ResultLogger which invokes its listener's handleEvent method (passing in the {@link ResultEvent}) if the event's
29   * Environment is enabled.
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class ResultLogger {
33  	private EventListenerList listenerList = new EventListenerList();
34  	
35  	private ResultLogger(){}
36  	
37  	/*using inner class provides thread safety.	 */
38  	private static class KRMSLoggerLoader{
39  		private static final ResultLogger INSTANCE = new ResultLogger();
40  	}
41  
42      /**
43       * @return KRMSLoggerLoader.INSTANCE
44       */
45  	public static ResultLogger getInstance(){
46  		return KRMSLoggerLoader.INSTANCE;
47  	}
48  
49      /**
50       * Add a {@link ResultListener} see logResult
51       * @param resultListener {@link ResultListener} to add
52       */
53  	public void addListener(ResultListener resultListener) {
54  		listenerList.add(ResultListener.class, resultListener);
55  	}
56  
57      /**
58       * Be kind, please rewind.  $1 charge for removing un-removed {@link ResultListener}
59       * @param resultListener {@link ResultListener} to remove
60       */
61  	public void removeListener(ResultListener resultListener){
62  		listenerList.remove(ResultListener.class, resultListener);
63  	}
64  
65      /**
66       * Invoke the handleEvent method of the listeners if the event's Environment is enabled.
67       * @param event {@link ResultEvent} to invoke with listeners handleEvent if the event's Environment is enabled.
68       */
69  	public void logResult(ResultEvent event){
70  		if (isEnabled(event.getEnvironment())){
71  			// fire event to listeners
72  			Object[] listeners = listenerList.getListenerList();
73  			for (int i=1; i<listeners.length; i+=2){
74  				((ResultListener) listeners[i]).handleEvent(event);
75  			}
76  		}
77  	}
78  
79      /**
80       * Returns true if the {@link ExecutionEnvironment}'s execution options {@link ExecutionFlag.LOG_EXECUTION} flag has been set.
81       * @param environment {@link ExecutionEnvironment} to test for being enabled.
82       * @return
83       */
84  	public boolean isEnabled(ExecutionEnvironment environment){
85  	    return (
86  	            environment != null 
87  	            && environment.getExecutionOptions() != null 
88  	            && environment.getExecutionOptions().getFlag(ExecutionFlag.LOG_EXECUTION)
89  	    );
90  	}
91  }