Save This Page
Home » xwork-2.1.1-src » com.opensymphony.xwork2.config.providers » [javadoc | source]
    1   /*
    2    * Copyright (c) 2002-2006 by OpenSymphony
    3    * All rights reserved.
    4    */
    5   package com.opensymphony.xwork2.config.providers;
    6   
    7   import java.util;
    8   
    9   import com.opensymphony.xwork2.ObjectFactory;
   10   import com.opensymphony.xwork2.config.ConfigurationException;
   11   import com.opensymphony.xwork2.config.entities;
   12   import com.opensymphony.xwork2.interceptor.Interceptor;
   13   import com.opensymphony.xwork2.util.location.Location;
   14   import com.opensymphony.xwork2.util.logging.Logger;
   15   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   16   
   17   
   18   /**
   19    * Builds a list of interceptors referenced by the refName in the supplied PackageConfig.
   20    *
   21    * @author Mike
   22    * @author Rainer Hermanns
   23    * @author tmjee
   24    * @version $Date: 2007-12-09 13:03:31 +0100 (Sun, 09 Dec 2007) $ $Id: InterceptorBuilder.java 1703 2007-12-09 12:03:31Z mrdon $
   25    */
   26   public class InterceptorBuilder {
   27   
   28       private static final Logger LOG = LoggerFactory.getLogger(InterceptorBuilder.class);
   29   
   30   
   31       /**
   32        * Builds a list of interceptors referenced by the refName in the supplied PackageConfig.
   33        *
   34        * @param interceptorLocator
   35        * @param refName
   36        * @param refParams
   37        * @return list of interceptors referenced by the refName in the supplied PackageConfig.
   38        * @throws ConfigurationException
   39        */
   40       public static List<InterceptorMapping> constructInterceptorReference(InterceptorLocator interceptorLocator ,
   41               String refName, Map refParams, Location location, ObjectFactory objectFactory) throws ConfigurationException {
   42           Object referencedConfig = interceptorLocator.getInterceptorConfig(refName);
   43           List<InterceptorMapping> result = new ArrayList<InterceptorMapping>();
   44   
   45           if (referencedConfig == null) {
   46               throw new ConfigurationException("Unable to find interceptor class referenced by ref-name " + refName, location);
   47           } else {
   48               if (referencedConfig instanceof InterceptorConfig) {
   49                   InterceptorConfig config = (InterceptorConfig) referencedConfig;
   50                   Interceptor inter = null;
   51                   try {
   52                       
   53                       inter = objectFactory.buildInterceptor(config, refParams);
   54                       result.add(new InterceptorMapping(refName, inter));
   55                   } catch (ConfigurationException ex) {
   56                       LOG.warn("Unable to load config class "+config.getClassName()+" at "+
   57                               ex.getLocation()+" probably due to a missing jar, which might "+
   58                               "be fine if you never plan to use the "+config.getName()+" interceptor");
   59                       LOG.error("Actual exception", ex);
   60                   }
   61                   
   62               } else if (referencedConfig instanceof InterceptorStackConfig) {
   63                   InterceptorStackConfig stackConfig = (InterceptorStackConfig) referencedConfig;
   64   
   65                   if ((refParams != null) && (refParams.size() > 0)) {
   66                       result = constructParameterizedInterceptorReferences(interceptorLocator, stackConfig, refParams, objectFactory);
   67                   } else {
   68                       result.addAll(stackConfig.getInterceptors());
   69                   }
   70   
   71               } else {
   72                   LOG.error("Got unexpected type for interceptor " + refName + ". Got " + referencedConfig);
   73               }
   74           }
   75   
   76           return result;
   77       }
   78   
   79       /**
   80        * Builds a list of interceptors referenced by the refName in the supplied PackageConfig overriding the properties
   81        * of the referenced interceptor with refParams.
   82        *
   83        * @param interceptorLocator
   84        * @param stackConfig
   85        * @param refParams     The overridden interceptor properies
   86        * @return list of interceptors referenced by the refName in the supplied PackageConfig overridden with refParams.
   87        */
   88       private static List<InterceptorMapping> constructParameterizedInterceptorReferences(
   89               InterceptorLocator interceptorLocator, InterceptorStackConfig stackConfig, Map refParams,
   90               ObjectFactory objectFactory) {
   91           List<InterceptorMapping> result;
   92           Map<String, Map<Object, String>> params = new LinkedHashMap<String, Map<Object, String>>();
   93   
   94           for (Iterator iter = refParams.keySet().iterator(); iter.hasNext();) {
   95               String key = (String) iter.next();
   96               String value = (String) refParams.get(key);
   97   
   98               try {
   99                   String name = key.substring(0, key.indexOf('.'));
  100                   key = key.substring(key.indexOf('.') + 1);
  101   
  102                   Map<Object, String> map;
  103                   if (params.containsKey(name)) {
  104                       map = params.get(name);
  105                   } else {
  106                       map = new LinkedHashMap<Object, String>();
  107                   }
  108   
  109                   map.put(key, value);
  110                   params.put(name, map);
  111   
  112               } catch (Exception e) {
  113                   LOG.warn("No interceptor found for name = " + key);
  114               }
  115           }
  116   
  117           result = new ArrayList<InterceptorMapping>(stackConfig.getInterceptors());
  118   
  119           for (String key : params.keySet()) {
  120   
  121               Map<Object, String> map = params.get(key);
  122   
  123               InterceptorConfig cfg = (InterceptorConfig) interceptorLocator.getInterceptorConfig(key);
  124               Interceptor interceptor = objectFactory.buildInterceptor(cfg, map);
  125   
  126               InterceptorMapping mapping = new InterceptorMapping(key, interceptor);
  127               if (result != null && result.contains(mapping)) {
  128                   int index = result.indexOf(mapping);
  129                   result.set(index, mapping);
  130               } else {
  131                   result.add(mapping);
  132               }
  133           }
  134   
  135           return result;
  136       }
  137   }

Save This Page
Home » xwork-2.1.1-src » com.opensymphony.xwork2.config.providers » [javadoc | source]