View Javadoc

1   /**
2    * Copyright 2005-2011 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  public class ResultLogger {
28  	private EventListenerList listenerList = new EventListenerList();
29  	
30  	private ResultLogger(){}
31  	
32  	/*using inner class provides thread safety.	 */
33  	private static class KRMSLoggerLoader{
34  		private static final ResultLogger INSTANCE = new ResultLogger();
35  	}
36  	
37  	public static ResultLogger getInstance(){
38  		return KRMSLoggerLoader.INSTANCE;
39  	}
40  	
41  	public void addListener(ResultListener l) {
42  		listenerList.add(ResultListener.class, l);		
43  	}
44  	
45  	public void removeListener(ResultListener l){
46  		listenerList.remove(ResultListener.class, l);
47  	}
48  
49  	public void logResult(ResultEvent event){
50  		if (isEnabled(event.getEnvironment())){
51  			// fire event to listeners
52  			Object[] listeners = listenerList.getListenerList();
53  			for (int i=1; i<listeners.length; i+=2){
54  				((ResultListener) listeners[i]).handleEvent(event);
55  			}
56  		}
57  	}
58  
59  	public boolean isEnabled(ExecutionEnvironment environment){
60  	    return (
61  	            environment != null 
62  	            && environment.getExecutionOptions() != null 
63  	            && environment.getExecutionOptions().getFlag(ExecutionFlag.LOG_EXECUTION)
64  	    );
65  	}
66  }