Esempio n. 1
0
 /**
  * Support for nested <param name="" value=""/> tags. Parameter named <i>cache</i>,
  * <i>algorithm</i>, <i>comparator</i> or <i>update</i> are mapped to the respective set-Method.
  * Parameter which names starts with <i>cache.</i> or <i>algorithm.</i> or <i>comparator.</i> are
  * tried to set on the appropriate object via its set-methods. Other parameters are invalid and an
  * BuildException will be thrown.
  *
  * @param parameter Key and value as parameter object
  */
 public void useParameter(Parameter parameter) {
   String key = parameter.getName();
   String value = parameter.getValue();
   if ("cache".equals(key)) {
     CacheName cn = new CacheName();
     cn.setValue(value);
     setCache(cn);
   } else if ("algorithm".equals(key)) {
     AlgorithmName an = new AlgorithmName();
     an.setValue(value);
     setAlgorithm(an);
   } else if ("comparator".equals(key)) {
     ComparatorName cn = new ComparatorName();
     cn.setValue(value);
     setComparator(cn);
   } else if ("update".equals(key)) {
     boolean updateValue = ("true".equalsIgnoreCase(value)) ? true : false;
     setUpdate(updateValue);
   } else if ("delayupdate".equals(key)) {
     boolean updateValue = ("true".equalsIgnoreCase(value)) ? true : false;
     setDelayUpdate(updateValue);
   } else if ("seldirs".equals(key)) {
     boolean sdValue = ("true".equalsIgnoreCase(value)) ? true : false;
     setSeldirs(sdValue);
   } else if (key.startsWith(CACHE_PREFIX)) {
     String name = key.substring(CACHE_PREFIX.length());
     tryToSetAParameter(cache, name, value);
   } else if (key.startsWith(ALGORITHM_PREFIX)) {
     String name = key.substring(ALGORITHM_PREFIX.length());
     tryToSetAParameter(algorithm, name, value);
   } else if (key.startsWith(COMPARATOR_PREFIX)) {
     String name = key.substring(COMPARATOR_PREFIX.length());
     tryToSetAParameter(comparator, name, value);
   } else {
     setError("Invalid parameter " + key);
   }
 }
 public static Cache getCache(CacheName cachename) {
   return mgr.getCache(cachename.name());
 }
Esempio n. 3
0
 // TODO 考虑与 EvictInterceptor 一样强制使用  @CacheName
 private String buildCacheName(Invocation inv, Controller controller) {
   CacheName cacheName = inv.getMethod().getAnnotation(CacheName.class);
   if (cacheName != null) return cacheName.value();
   cacheName = controller.getClass().getAnnotation(CacheName.class);
   return (cacheName != null) ? cacheName.value() : inv.getActionKey();
 }
Esempio n. 4
0
  /**
   * Configures this Selector. Does this work only once per Selector object.
   *
   * <p>Because some problems while configuring from <custom>Selector the configuration is done in
   * the following order:
   *
   * <ol>
   *   <li>collect the configuration data
   *   <li>wait for the first isSelected() call
   *   <li>set the default values
   *   <li>set values for name pattern '*': update, cache, algorithm, comparator
   *   <li>set values for name pattern '*.*: cache.cachefile, ...
   * </ol>
   *
   * <p>This configuration algorithm is needed because you don't know the order of arriving
   * config-data. E.g. if you first set the <i>cache.cachefilename</i> and after that the
   * <i>cache</i> itself, the default value for cachefilename is used, because setting the cache
   * implies creating a new Cache instance - with its defaults.
   */
  public void configure() {
    //
    // -----  The "Singleton"  -----
    //
    if (isConfigured) {
      return;
    }
    isConfigured = true;

    //
    // -----  Set default values  -----
    //
    Project p = getProject();
    String filename = "cache.properties";
    File cachefile = null;
    if (p != null) {
      // normal use inside Ant
      cachefile = new File(p.getBaseDir(), filename);

      // set self as a BuildListener to delay cachefile saves
      getProject().addBuildListener(this);
    } else {
      // no reference to project - e.g. during normal JUnit tests
      cachefile = new File(filename);
      setDelayUpdate(false);
    }
    Cache defaultCache = new PropertiesfileCache(cachefile);
    Algorithm defaultAlgorithm = new DigestAlgorithm();
    Comparator<? super String> defaultComparator = new EqualComparator();

    //
    // -----  Set the main attributes, pattern '*'  -----
    //
    for (Parameter parameter : configParameter) {
      if (parameter.getName().indexOf(".") > 0) {
        // this is a *.* parameter for later use
        specialParameter.add(parameter);
      } else {
        useParameter(parameter);
      }
    }
    configParameter = new Vector<Parameter>();

    // specify the algorithm classname
    if (algoName != null) {
      // use Algorithm defined via name
      if ("hashvalue".equals(algoName.getValue())) {
        algorithm = new HashvalueAlgorithm();
      } else if ("digest".equals(algoName.getValue())) {
        algorithm = new DigestAlgorithm();
      } else if ("checksum".equals(algoName.getValue())) {
        algorithm = new ChecksumAlgorithm();
      }
    } else {
      if (algorithmClass != null) {
        // use Algorithm specified by classname
        algorithm = (Algorithm) loadClass(algorithmClass, "is not an Algorithm.", Algorithm.class);
      } else {
        // nothing specified - use default
        algorithm = defaultAlgorithm;
      }
    }

    // specify the cache classname
    if (cacheName != null) {
      // use Cache defined via name
      if ("propertyfile".equals(cacheName.getValue())) {
        cache = new PropertiesfileCache();
      }
    } else {
      if (cacheClass != null) {
        // use Cache specified by classname
        cache = (Cache) loadClass(cacheClass, "is not a Cache.", Cache.class);
      } else {
        // nothing specified - use default
        cache = defaultCache;
      }
    }

    // specify the comparator classname
    if (compName != null) {
      // use Algorithm defined via name
      if ("equal".equals(compName.getValue())) {
        comparator = new EqualComparator();
      } else if ("rule".equals(compName.getValue())) {
        // TODO there is a problem with the constructor for the RBC.
        // you have to provide the rules in the constructors - no setters
        // available.
        throw new BuildException("RuleBasedCollator not yet supported.");
        // Have to think about lazy initialization here...  JHM
        // comparator = new java.text.RuleBasedCollator();
      }
    } else {
      if (comparatorClass != null) {
        // use Algorithm specified by classname
        @SuppressWarnings("unchecked")
        Comparator<? super String> localComparator =
            loadClass(comparatorClass, "is not a Comparator.", Comparator.class);
        comparator = localComparator;
      } else {
        // nothing specified - use default
        comparator = defaultComparator;
      }
    }

    //
    // -----  Set the special attributes, pattern '*.*'  -----
    //
    for (Iterator<Parameter> itSpecial = specialParameter.iterator(); itSpecial.hasNext(); ) {
      Parameter par = itSpecial.next();
      useParameter(par);
    }
    specialParameter = new Vector<Parameter>();
  }