001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
017    
018    package org.objectweb.jac.aspects.queue;
019    
020    import org.aopalliance.intercept.ConstructorInvocation;
021    import org.aopalliance.intercept.MethodInvocation;
022    import org.objectweb.jac.core.AspectComponent;
023    import org.objectweb.jac.core.Interaction;
024    import org.objectweb.jac.core.Wrapper;
025    import org.objectweb.jac.core.rtti.FieldItem;
026    import org.objectweb.jac.core.rtti.MethodItem;
027    import org.objectweb.jac.util.Log;
028    
029    public class MessageQueueWrapper extends Wrapper  {
030       MessageQueue mqueue;
031       public MessageQueueWrapper(AspectComponent ac) {
032          super(ac);
033          mqueue = ((MessageQueueAC)ac).getMessageQueue();
034       }
035       
036       public Object fieldChange(Interaction interaction) {
037          Log.trace("mqueue","fieldChange: "+interaction);
038          FieldItem[] fields = null;
039          // values of the fields before change
040          Object[] previousValues = null;
041    
042          if (interaction.method instanceof MethodItem) {
043             fields = ((MethodItem)interaction.method).getWrittenFields();
044             previousValues = new Object[fields.length];
045             for (int i=0; i<fields.length; i++) {
046                previousValues[i] = fields[i].get(interaction.wrappee);
047             }
048          }
049    
050          Object result = proceed(interaction);
051    
052          if (fields!=null) {
053             for (int i=0; i<fields.length; i++) {
054                Object newValue = fields[i].get(interaction.wrappee);
055                if ( (newValue!=null && 
056                      !newValue.equals(previousValues[i])) ||
057                      (newValue==null && previousValues[i]!=null) ) 
058                {
059                   mqueue.fieldChanged(interaction.wrappee,fields[i],
060                                       previousValues[i], newValue);
061                }
062             }
063          }
064          return result;
065       }
066    
067       public Object invoke(MethodInvocation invocation) throws Throwable {
068               return fieldChange((Interaction) invocation);
069       }
070    
071       public Object construct(ConstructorInvocation invocation)
072               throws Throwable {
073               throw new Exception("Wrapper "+this+" does not support construction interception.");
074       }
075    }