Save This Page
Home » jboss-5.0.0.CR1-src » org.jboss.mx » interceptor » [javadoc | source]
    1   /*
    2   * JBoss, Home of Professional Open Source
    3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
    4   * by the @authors tag. See the copyright.txt in the distribution for a
    5   * full listing of individual contributors.
    6   *
    7   * This is free software; you can redistribute it and/or modify it
    8   * under the terms of the GNU Lesser General Public License as
    9   * published by the Free Software Foundation; either version 2.1 of
   10   * the License, or (at your option) any later version.
   11   *
   12   * This software is distributed in the hope that it will be useful,
   13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   15   * Lesser General Public License for more details.
   16   *
   17   * You should have received a copy of the GNU Lesser General Public
   18   * License along with this software; if not, write to the Free
   19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   21   */
   22   package org.jboss.mx.interceptor;
   23   
   24   import java.util.Arrays;
   25   
   26   import javax.management.Descriptor;
   27   import javax.management.InvalidAttributeValueException;
   28   import javax.management.ObjectName;
   29   
   30   import org.jboss.mx.modelmbean.ModelMBeanConstants;
   31   import org.jboss.mx.server.Invocation;
   32   
   33   
   34   /** This interceptor handles the ModelMBean operation caching
   35    *
   36    * @author  <a href="mailto:adrian@jboss.org">Adrian Brock</a>.
   37    * @author Scott.Stark@jboss.org
   38    * @version $Revision: 37459 $
   39    */
   40   public class ModelMBeanOperationInterceptor
   41         extends AbstractInterceptor
   42         implements ModelMBeanConstants
   43   {
   44      // Constants -----------------------------------------------------
   45      
   46      // Attributes ----------------------------------------------------
   47      
   48      private boolean trace;
   49      
   50      // Constructors --------------------------------------------------
   51      
   52      public ModelMBeanOperationInterceptor()
   53      {
   54         super("ModelMBean Operation Interceptor");
   55         trace = log.isTraceEnabled();
   56      }
   57   
   58      
   59      // Public --------------------------------------------------------
   60      
   61      public Object invoke(Invocation invocation) throws Throwable
   62      {
   63         // get the operation's descriptor
   64         Descriptor d = invocation.getDescriptor();
   65         Class clazz = invocation.getReturnTypeClass();
   66         
   67         String name = null;
   68         ObjectName objectName = null;
   69         if (trace)
   70         {
   71            if (d != null)
   72               name = (String) d.getFieldValue(NAME);
   73            objectName = invocation.getInvoker().getObjectName();
   74         }
   75   
   76         if (trace)
   77         {
   78            Object args = invocation.getArgs();
   79            if (args != null)
   80               args = Arrays.asList((Object[]) args);
   81            log.trace("Invoking objectName=" + objectName + " oper=" + name + " args=" + args + " desc=" + d);
   82         }
   83   
   84         long limit = CACHE_NEVER_LIMIT;
   85         
   86         if (d != null && clazz != null)
   87         {
   88            String timeLimit = (String) d.getFieldValue(CURRENCY_TIME_LIMIT);
   89            if (timeLimit != null)
   90               limit = Long.parseLong(timeLimit);
   91   
   92            // We are never stale
   93            if (limit == CACHE_ALWAYS_LIMIT)
   94            {
   95               String timeStamp = (String)d.getFieldValue(LAST_UPDATED_TIME_STAMP);
   96               if (timeStamp != null)
   97               {
   98                  Object value = d.getFieldValue(CACHED_VALUE);
   99                  if (trace)
  100                     log.trace("Always cache objectName=" + objectName + " oper=" + name + " value=" + value);
  101                  checkAssignable("Cached value in descriptor ", clazz, value);
  102                  return value;
  103               }
  104            }
  105   
  106            // is caching enabled
  107            if (limit != CACHE_NEVER_LIMIT)
  108            {
  109               String timeStamp = (String) d.getFieldValue(LAST_UPDATED_TIME_STAMP);
  110               long lastUpdate = (timeStamp == null) ? 0 : Long.parseLong(timeStamp);
  111              
  112               // if the value hasn't gone stale, return from the descriptor
  113               long now = System.currentTimeMillis();
  114               long expires = lastUpdate * 1000 + limit * 1000;
  115               if (now < expires)
  116               {
  117                  Object value = d.getFieldValue(CACHED_VALUE);
  118                  if (trace)
  119                     log.trace("Using cache objectName=" + objectName + " oper=" + name + " value=" + value + " now=" + now + " expires=" + expires);
  120                  checkAssignable("Cached value in descriptor ", clazz, value);
  121                  return value;
  122               }
  123               else
  124               {
  125                  if (trace)
  126                     log.trace("Cache expired objectName=" + objectName + " oper=" + name + " now=" + now + " expires=" + expires);
  127                  d.removeField(CACHED_VALUE);
  128               }
  129            }
  130            else
  131            {
  132               // Unfortunatley we have to cope with stupid users
  133               if (trace)
  134                  log.trace("Removing any cached value objectName=" + objectName + " oper=" + name + " descriptor=" + d);
  135               d.removeField(CACHED_VALUE);
  136            }
  137         }
  138   
  139         // we got here means either stale value in descriptior, or no caching
  140         Object value = invocation.invoke();            
  141         if (trace)
  142            log.trace("Got result objectName=" + objectName + " oper=" + name + " value=" + value);
  143         
  144         // update the descriptor (unless not caching)
  145         if (d !=null && limit != CACHE_NEVER_LIMIT)
  146         {
  147            String timestamp = Long.toString(System.currentTimeMillis()/1000);
  148            if (trace)
  149               log.trace("Cache result objectName=" + objectName + " oper=" + name + " value=" + value + " timestamp=" + timestamp);
  150            d.setField(CACHED_VALUE, value);
  151            d.setField(LAST_UPDATED_TIME_STAMP, timestamp);
  152         }
  153         return value;
  154      }
  155      
  156      protected void checkAssignable(String context, Class clazz, Object value) throws InvalidAttributeValueException, ClassNotFoundException
  157      {
  158         if (value != null && clazz.isAssignableFrom(value.getClass()) == false)
  159            throw new InvalidAttributeValueException(context + " has class " + value.getClass() + " loaded from " + value.getClass().getClassLoader() +
  160               " that is not assignable to attribute class " + clazz + " loaded from " + clazz.getClassLoader());
  161      }
  162   }
  163   
  164   
  165   
  166   

Save This Page
Home » jboss-5.0.0.CR1-src » org.jboss.mx » interceptor » [javadoc | source]