/*** * * PROJECT * * RUNES * * VERSION * * $Revision: 1.1 $ * * DATE * * $Date: 2006/02/15 $ * * AUTHOR * * $Author: r.ramdhay $ * * LOG * * $Log: InterceptorAdderOne.java $ * * * * * * * * RUNES Middleware Architecture * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, you may find a copy at the FSF web * * site at 'www.gnu.org' or 'www.fsf.org', or you may write to the * * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * * Boston, MA 02111-1307, USA ***/ package runes.sampleApp; import runes.kernel.Receptacle; import runes.kernel.defaultImpl.SingleReceptacle; import runes.kernel.exceptions.ComponentException; import runes.kernel.exceptions.ReceptacleException; import runes.kernel.interceptImpl.InterceptorComponent; public class InterceptorAdderOne extends InterceptorComponent implements IAdder { private SingleReceptacle rAdder; public InterceptorAdderOne() { super(); } /** * @see runes.kernel.defaultImpl.BaseComponent#construct() */ public void construct() throws ComponentException { rAdder = createSingleReceptacle("runes.sampleApp.IAdder"); System.out.println("InterceptorAdderOne component instantiated"); } /** * @see runes.kernel.defaultImpl.BaseComponent#destroy() */ public void destroy() throws ComponentException { } /** * Pre-interception code to be placed in this method */ public void pre(Object[] args) { // pre-interception computation e.g. subtract 1 from arguments System.out.print("InterceptorAdderOne - pre()-> "); args[0] = new Integer(((Integer)args[0]).intValue()-1); args[1] = new Integer(((Integer)args[1]).intValue()-1); // result of pre-interception computation args[2] = new Integer(((Integer)args[0]).intValue() + ((Integer)args[1]).intValue()); System.out.println(args[0] + "+" + args[1] + "=" + args[2]); } /** * Post-interception code to be placed in this method */ public void post(Object[] args) { // pre-interception computation e.g. add 1 to arguments System.out.print("InterceptorAdderOne - post()->"); args[0] = new Integer(((Integer)args[0]).intValue()+1); args[1] = new Integer(((Integer)args[1]).intValue()+1); // result of post-interception computation args[2] = new Integer(((Integer)args[0]).intValue() + ((Integer)args[1]).intValue()); System.out.println(args[0] + "+" + args[1] + "=" + args[2]); } /** * @see runes.sampleApp.IAdder#add(int, int) */ public int add(int x, int y) { int addResult=0; Object[] args = {new Integer(x), new Integer(y), new Integer(addResult)}; try { ///////////// pre-interception //////////// if (InterceptOptions.get(0)) { pre(args); } /////////////////////////////////////////// ///////// actual method call ////////////// if (!(InterceptOptions.get(1))) addResult = ((IAdder) rAdder.getSingleConnectedInterface()).add( ((Integer)args[0]).intValue(), ((Integer)args[1]).intValue()); else if ((InterceptOptions.get(1)) && (!lastInterceptor)) { addResult = ((IAdder) rAdder.getSingleConnectedInterface()).add( ((Integer)args[0]).intValue(), ((Integer)args[1]).intValue());; } else if ((InterceptOptions.get(1)) && (lastInterceptor)) { return (((Integer)args[0]).intValue() + ((Integer)args[1]).intValue()); } //////////////////////////////////////////// /////////// post-interception ///////////// if (InterceptOptions.get(2)) post(args); /////////////////////////////////////////// return addResult; } catch(ReceptacleException e) { if(e.getKind()==ReceptacleException.NOT_CONNECTED) System.err.println("Interceptor Component not connected!"); } return 0; } }