001 /** 002 * Copyright 2005-2013 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 */ 016 package org.kuali.rice.krms.framework.engine; 017 018 import javax.swing.event.EventListenerList; 019 020 import org.kuali.rice.krms.api.engine.ExecutionEnvironment; 021 import org.kuali.rice.krms.api.engine.ExecutionFlag; 022 import org.kuali.rice.krms.api.engine.ResultEvent; 023 import org.kuali.rice.krms.framework.engine.result.EngineResultListener; 024 import org.kuali.rice.krms.framework.engine.result.Log4jResultListener; 025 import org.kuali.rice.krms.framework.engine.result.ResultListener; 026 027 /** 028 * A ResultLogger which invokes its listener's handleEvent method (passing in the {@link ResultEvent}) if the event's 029 * Environment is enabled. 030 * @author Kuali Rice Team (rice.collab@kuali.org) 031 */ 032 public class ResultLogger { 033 private EventListenerList listenerList = new EventListenerList(); 034 035 private ResultLogger(){} 036 037 /*using inner class provides thread safety. */ 038 private static class KRMSLoggerLoader{ 039 private static final ResultLogger INSTANCE = new ResultLogger(); 040 } 041 042 /** 043 * @return KRMSLoggerLoader.INSTANCE 044 */ 045 public static ResultLogger getInstance(){ 046 return KRMSLoggerLoader.INSTANCE; 047 } 048 049 /** 050 * Add a {@link ResultListener} see logResult 051 * @param resultListener {@link ResultListener} to add 052 */ 053 public void addListener(ResultListener resultListener) { 054 listenerList.add(ResultListener.class, resultListener); 055 } 056 057 /** 058 * Be kind, please rewind. $1 charge for removing un-removed {@link ResultListener} 059 * @param resultListener {@link ResultListener} to remove 060 */ 061 public void removeListener(ResultListener resultListener){ 062 listenerList.remove(ResultListener.class, resultListener); 063 } 064 065 /** 066 * Invoke the handleEvent method of the listeners if the event's Environment is enabled. 067 * @param event {@link ResultEvent} to invoke with listeners handleEvent if the event's Environment is enabled. 068 */ 069 public void logResult(ResultEvent event){ 070 if (isEnabled(event.getEnvironment())){ 071 // fire event to listeners 072 Object[] listeners = listenerList.getListenerList(); 073 for (int i=1; i<listeners.length; i+=2){ 074 ((ResultListener) listeners[i]).handleEvent(event); 075 } 076 } 077 } 078 079 /** 080 * Returns true if the {@link ExecutionEnvironment}'s execution options {@link ExecutionFlag.LOG_EXECUTION} flag has been set. 081 * @param environment {@link ExecutionEnvironment} to test for being enabled. 082 * @return 083 */ 084 public boolean isEnabled(ExecutionEnvironment environment){ 085 return ( 086 environment != null 087 && environment.getExecutionOptions() != null 088 && environment.getExecutionOptions().getFlag(ExecutionFlag.LOG_EXECUTION) 089 ); 090 } 091 }