Return-Path: X-Original-To: apmail-archiva-commits-archive@www.apache.org Delivered-To: apmail-archiva-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7D695D4CC for ; Wed, 29 Aug 2012 15:38:41 +0000 (UTC) Received: (qmail 7796 invoked by uid 500); 29 Aug 2012 15:38:41 -0000 Delivered-To: apmail-archiva-commits-archive@archiva.apache.org Received: (qmail 7766 invoked by uid 500); 29 Aug 2012 15:38:41 -0000 Mailing-List: contact commits-help@archiva.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@archiva.apache.org Delivered-To: mailing list commits@archiva.apache.org Received: (qmail 7759 invoked by uid 99); 29 Aug 2012 15:38:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Aug 2012 15:38:41 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Aug 2012 15:38:36 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 91D2A2388BCD for ; Wed, 29 Aug 2012 15:37:21 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1378611 [12/25] - in /archiva/site-content/redback/components: ./ expression-evaluator/ redback-components/ spring-apacheds/ spring-cache/ spring-jdo2/ spring-quartz/ spring-registry/ spring-taskqueue/ spring-utils/ xref-test/ xref-test/or... Date: Wed, 29 Aug 2012 15:37:11 -0000 To: commits@archiva.apache.org From: olamy@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120829153721.91D2A2388BCD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCache.html URL: http://svn.apache.org/viewvc/archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCache.html?rev=1378611&view=auto ============================================================================== --- archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCache.html (added) +++ archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCache.html Wed Aug 29 15:37:03 2012 @@ -0,0 +1,459 @@ + + + + +EhcacheCache xref + + + +
+
+1   package org.apache.archiva.redback.components.cache.ehcache;
+2   
+3   /*
+4    * Licensed to the Apache Software Foundation (ASF) under one
+5    * or more contributor license agreements.  See the NOTICE file
+6    * distributed with this work for additional information
+7    * regarding copyright ownership.  The ASF licenses this file
+8    * to you under the Apache License, Version 2.0 (the
+9    * "License"); you may not use this file except in compliance
+10   * with the License.  You may obtain a copy of the License at
+11   *
+12   *  http://www.apache.org/licenses/LICENSE-2.0
+13   *
+14   * Unless required by applicable law or agreed to in writing,
+15   * software distributed under the License is distributed on an
+16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+17   * KIND, either express or implied.  See the License for the
+18   * specific language governing permissions and limitations
+19   * under the License.
+20   */
+21  
+22  import net.sf.ehcache.Cache;
+23  import net.sf.ehcache.CacheManager;
+24  import net.sf.ehcache.Element;
+25  import net.sf.ehcache.Status;
+26  import net.sf.ehcache.config.CacheConfiguration;
+27  import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
+28  import org.apache.archiva.redback.components.cache.CacheStatistics;
+29  import org.slf4j.Logger;
+30  import org.slf4j.LoggerFactory;
+31  
+32  import javax.annotation.PostConstruct;
+33  
+34  /**
+35   * EhcacheCache
+36   * configuration document available <a href="http://www.ehcache.org/documentation/configuration/index">EhcacheUserGuide</a>
+37   *
+38   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+39   */
+40  public class EhcacheCache
+41      implements org.apache.archiva.redback.components.cache.Cache
+42  {
+43  
+44      private Logger log = LoggerFactory.getLogger( getClass() );
+45  
+46      class Stats
+47          implements CacheStatistics
+48      {
+49          public void clear()
+50          {
+51              ehcache.clearStatistics();
+52          }
+53  
+54          public double getCacheHitRate()
+55          {
+56              double hits = getCacheHits();
+57              double miss = getCacheMiss();
+58  
+59              if ( ( hits == 0 ) && ( hits == 0 ) )
+60              {
+61                  return 0.0;
+62              }
+63  
+64              return (double) hits / (double) ( hits + miss );
+65          }
+66  
+67          public long getCacheHits()
+68          {
+69              return ehcache.getStatistics().getCacheHits();
+70          }
+71  
+72          public long getCacheMiss()
+73          {
+74              return ehcache.getStatistics().getCacheMisses();
+75          }
+76  
+77          public long getSize()
+78          {
+79              return ehcache.getMemoryStoreSize() + ehcache.getDiskStoreSize();
+80          }
+81  
+82          public long getInMemorySize()
+83          {
+84              return ehcache.calculateInMemorySize();
+85          }
+86      }
+87  
+88      /**
+89       * how often to run the disk store expiry thread. A large number of 120 seconds plus is recommended
+90       */
+91      private long diskExpiryThreadIntervalSeconds = 600;
+92  
+93      /**
+94       * Whether to persist the cache to disk between JVM restarts.
+95       */
+96      private boolean diskPersistent = true;
+97  
+98      /**
+99       * Location on disk for the ehcache store.
+100      */
+101     private String diskStorePath = System.getProperty( "java.io.tmpdir" ) + "/ehcache";
+102 
+103     /**
+104      *
+105      */
+106     private boolean eternal = false;
+107 
+108     /**
+109      *
+110      */
+111     private int maxElementsInMemory = 1000;
+112 
+113     /**
+114      *
+115      */
+116     private String memoryEvictionPolicy = "LRU";
+117 
+118     /**
+119      *
+120      */
+121     private String name = "cache";
+122 
+123     /**
+124      * Flag indicating when to use the disk store.
+125      */
+126     private boolean overflowToDisk = false;
+127 
+128     /**
+129      *
+130      */
+131     private int timeToIdleSeconds = 600;
+132 
+133     /**
+134      *
+135      */
+136     private int timeToLiveSeconds = 300;
+137 
+138     /**
+139      * @since 2.0
+140      */
+141     private boolean overflowToOffHeap = false;
+142 
+143     /**
+144      * @since 2.0
+145      */
+146     private long maxBytesLocalHeap;
+147 
+148     /**
+149      * @since 2.0
+150      */
+151     private long maxBytesLocalOffHeap;
+152 
+153     /**
+154      *
+155      */
+156     private boolean failOnDuplicateCache = false;
+157 
+158     private boolean statisticsEnabled = true;
+159 
+160     private CacheManager cacheManager = CacheManager.getInstance();
+161 
+162     private net.sf.ehcache.Cache ehcache;
+163 
+164     private Stats stats;
+165 
+166     public void clear()
+167     {
+168         ehcache.removeAll();
+169         stats.clear();
+170     }
+171 
+172     @PostConstruct
+173     public void initialize()
+174     {
+175         stats = new Stats();
+176 
+177         boolean cacheExists = cacheManager.cacheExists( getName() );
+178 
+179         if ( cacheExists )
+180         {
+181             if ( failOnDuplicateCache )
+182             {
+183                 throw new RuntimeException( "A previous cache with name [" + getName() + "] exists." );
+184             }
+185             else
+186             {
+187                 log.warn( "skip duplicate cache " + getName() );
+188                 ehcache = cacheManager.getCache( getName() );
+189             }
+190         }
+191 
+192         if ( !cacheExists )
+193         {
+194             CacheConfiguration cacheConfiguration = new CacheConfiguration().name( getName() ).maxEntriesLocalHeap(
+195                 getMaxElementsInMemory() ).memoryStoreEvictionPolicy( getMemoryStoreEvictionPolicy() ).overflowToDisk(
+196                 isOverflowToDisk() ).diskStorePath( getDiskStorePath() ).eternal( isEternal() ).timeToLiveSeconds(
+197                 getTimeToLiveSeconds() ).timeToIdleSeconds( getTimeToIdleSeconds() ).diskPersistent(
+198                 isDiskPersistent() ).diskExpiryThreadIntervalSeconds(
+199                 getDiskExpiryThreadIntervalSeconds() ).overflowToOffHeap( isOverflowToOffHeap() );
+200 
+201             if ( getMaxBytesLocalHeap() > 0 )
+202             {
+203                 cacheConfiguration.setMaxBytesLocalHeap( getMaxBytesLocalHeap() );
+204             }
+205             if ( getMaxBytesLocalOffHeap() > 0 )
+206             {
+207                 cacheConfiguration.setMaxBytesLocalOffHeap( getMaxBytesLocalOffHeap() );
+208             }
+209 
+210             ehcache = new Cache( cacheConfiguration );
+211 
+212             cacheManager.addCache( ehcache );
+213             ehcache.setStatisticsEnabled( statisticsEnabled );
+214         }
+215     }
+216 
+217     public void dispose()
+218     {
+219         if ( cacheManager.getStatus().equals( Status.STATUS_ALIVE ) )
+220         {
+221             log.info( "Disposing cache: " + ehcache );
+222             if ( this.ehcache != null )
+223             {
+224                 cacheManager.removeCache( this.ehcache.getName() );
+225                 ehcache = null;
+226             }
+227         }
+228         else
+229         {
+230             log.debug( "Not disposing cache, because cacheManager is not alive: " + ehcache );
+231         }
+232     }
+233 
+234     public Object get( Object key )
+235     {
+236         if ( key == null )
+237         {
+238             return null;
+239         }
+240         Element elem = ehcache.get( key );
+241         if ( elem == null )
+242         {
+243             return null;
+244         }
+245         return elem.getObjectValue();
+246     }
+247 
+248     public long getDiskExpiryThreadIntervalSeconds()
+249     {
+250         return diskExpiryThreadIntervalSeconds;
+251     }
+252 
+253     public String getDiskStorePath()
+254     {
+255         return diskStorePath;
+256     }
+257 
+258     public int getMaxElementsInMemory()
+259     {
+260         return maxElementsInMemory;
+261     }
+262 
+263     public String getMemoryEvictionPolicy()
+264     {
+265         return memoryEvictionPolicy;
+266     }
+267 
+268     public MemoryStoreEvictionPolicy getMemoryStoreEvictionPolicy()
+269     {
+270         return MemoryStoreEvictionPolicy.fromString( memoryEvictionPolicy );
+271     }
+272 
+273     public String getName()
+274     {
+275         return name;
+276     }
+277 
+278     public CacheStatistics getStatistics()
+279     {
+280         return stats;
+281     }
+282 
+283     public int getTimeToIdleSeconds()
+284     {
+285         return timeToIdleSeconds;
+286     }
+287 
+288     public int getTimeToLiveSeconds()
+289     {
+290         return timeToLiveSeconds;
+291     }
+292 
+293     public boolean hasKey( Object key )
+294     {
+295         return ehcache.isKeyInCache( key );
+296     }
+297 
+298     public boolean isDiskPersistent()
+299     {
+300         return diskPersistent;
+301     }
+302 
+303     public boolean isEternal()
+304     {
+305         return eternal;
+306     }
+307 
+308     public boolean isOverflowToDisk()
+309     {
+310         return overflowToDisk;
+311     }
+312 
+313     public void register( Object key, Object value )
+314     {
+315         ehcache.put( new Element( key, value ) );
+316     }
+317 
+318     public Object put( Object key, Object value )
+319     {
+320         // Multiple steps done to satisfy Cache API requirement for Previous object return.
+321         Element elem = null;
+322         Object previous = null;
+323         elem = ehcache.get( key );
+324         if ( elem != null )
+325         {
+326             previous = elem.getObjectValue();
+327         }
+328         elem = new Element( key, value );
+329         ehcache.put( elem );
+330         return previous;
+331     }
+332 
+333     public Object remove( Object key )
+334     {
+335         Element elem = null;
+336         Object previous = null;
+337         elem = ehcache.get( key );
+338         if ( elem != null )
+339         {
+340             previous = elem.getObjectValue();
+341             ehcache.remove( key );
+342         }
+343 
+344         return previous;
+345     }
+346 
+347     public void setDiskExpiryThreadIntervalSeconds( long diskExpiryThreadIntervalSeconds )
+348     {
+349         this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
+350     }
+351 
+352     public void setDiskPersistent( boolean diskPersistent )
+353     {
+354         this.diskPersistent = diskPersistent;
+355     }
+356 
+357     public void setDiskStorePath( String diskStorePath )
+358     {
+359         this.diskStorePath = diskStorePath;
+360     }
+361 
+362     public void setEternal( boolean eternal )
+363     {
+364         this.eternal = eternal;
+365     }
+366 
+367     public void setMaxElementsInMemory( int maxElementsInMemory )
+368     {
+369         this.maxElementsInMemory = maxElementsInMemory;
+370     }
+371 
+372     public void setMemoryEvictionPolicy( String memoryEvictionPolicy )
+373     {
+374         this.memoryEvictionPolicy = memoryEvictionPolicy;
+375     }
+376 
+377     public void setName( String name )
+378     {
+379         this.name = name;
+380     }
+381 
+382     public void setOverflowToDisk( boolean overflowToDisk )
+383     {
+384         this.overflowToDisk = overflowToDisk;
+385     }
+386 
+387     public void setTimeToIdleSeconds( int timeToIdleSeconds )
+388     {
+389         this.timeToIdleSeconds = timeToIdleSeconds;
+390     }
+391 
+392     public void setTimeToLiveSeconds( int timeToLiveSeconds )
+393     {
+394         this.timeToLiveSeconds = timeToLiveSeconds;
+395     }
+396 
+397     public boolean isStatisticsEnabled()
+398     {
+399         return statisticsEnabled;
+400     }
+401 
+402     public void setStatisticsEnabled( boolean statisticsEnabled )
+403     {
+404         this.statisticsEnabled = statisticsEnabled;
+405     }
+406 
+407     public boolean isFailOnDuplicateCache()
+408     {
+409         return failOnDuplicateCache;
+410     }
+411 
+412     public void setFailOnDuplicateCache( boolean failOnDuplicateCache )
+413     {
+414         this.failOnDuplicateCache = failOnDuplicateCache;
+415     }
+416 
+417     public boolean isOverflowToOffHeap()
+418     {
+419         return overflowToOffHeap;
+420     }
+421 
+422     public void setOverflowToOffHeap( boolean overflowToOffHeap )
+423     {
+424         this.overflowToOffHeap = overflowToOffHeap;
+425     }
+426 
+427     public long getMaxBytesLocalHeap()
+428     {
+429         return maxBytesLocalHeap;
+430     }
+431 
+432     public void setMaxBytesLocalHeap( long maxBytesLocalHeap )
+433     {
+434         this.maxBytesLocalHeap = maxBytesLocalHeap;
+435     }
+436 
+437     public long getMaxBytesLocalOffHeap()
+438     {
+439         return maxBytesLocalOffHeap;
+440     }
+441 
+442     public void setMaxBytesLocalOffHeap( long maxBytesLocalOffHeap )
+443     {
+444         this.maxBytesLocalOffHeap = maxBytesLocalOffHeap;
+445     }
+446 }
+
+
+ Added: archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCreator.html URL: http://svn.apache.org/viewvc/archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCreator.html?rev=1378611&view=auto ============================================================================== --- archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCreator.html (added) +++ archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/EhcacheCreator.html Wed Aug 29 15:37:03 2012 @@ -0,0 +1,86 @@ + + + + +EhcacheCreator xref + + + +
+
+1   package org.apache.archiva.redback.components.cache.ehcache;
+2   
+3   /*
+4    * Licensed to the Apache Software Foundation (ASF) under one
+5    * or more contributor license agreements.  See the NOTICE file
+6    * distributed with this work for additional information
+7    * regarding copyright ownership.  The ASF licenses this file
+8    * to you under the Apache License, Version 2.0 (the
+9    * "License"); you may not use this file except in compliance
+10   * with the License.  You may obtain a copy of the License at
+11   *
+12   *  http://www.apache.org/licenses/LICENSE-2.0
+13   *
+14   * Unless required by applicable law or agreed to in writing,
+15   * software distributed under the License is distributed on an
+16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+17   * KIND, either express or implied.  See the License for the
+18   * specific language governing permissions and limitations
+19   * under the License.
+20   */
+21  
+22  import org.apache.archiva.redback.components.cache.Cache;
+23  import org.apache.commons.lang.SystemUtils;
+24  import org.apache.archiva.redback.components.cache.CacheException;
+25  import org.apache.archiva.redback.components.cache.CacheHints;
+26  import org.apache.archiva.redback.components.cache.factory.CacheCreator;
+27  
+28  import java.io.File;
+29  
+30  /**
+31   * EhcacheCreator - runtime creation of an ehcache.
+32   *
+33   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+34   *
+35   */
+36  public class EhcacheCreator
+37      implements CacheCreator
+38  {
+39  
+40      public Cache createCache( CacheHints hints )
+41          throws CacheException
+42      {
+43          EhcacheCache cache = new EhcacheCache();
+44  
+45          cache.setName( hints.getName() );
+46  
+47          cache.setDiskPersistent( hints.isOverflowToDisk() );
+48          if ( hints.isOverflowToDisk() )
+49          {
+50              File overflowPath = null;
+51  
+52              if ( hints.getDiskOverflowPath() != null )
+53              {
+54                  overflowPath = hints.getDiskOverflowPath();
+55              }
+56              else
+57              {
+58                  File tmpDir = SystemUtils.getJavaIoTmpDir();
+59                  overflowPath = new File( tmpDir, "ehcache/" + hints.getName() );
+60              }
+61  
+62              cache.setDiskStorePath( overflowPath.getAbsolutePath() );
+63          }
+64  
+65          cache.setMaxElementsInMemory( hints.getMaxElements() );
+66          cache.setTimeToLiveSeconds( hints.getMaxSecondsInCache() );
+67          cache.setTimeToIdleSeconds( hints.getIdleExpirationSeconds() );
+68  
+69          cache.initialize();
+70  
+71          return cache;
+72      }
+73  }
+
+
+ Added: archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-frame.html URL: http://svn.apache.org/viewvc/archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-frame.html?rev=1378611&view=auto ============================================================================== --- archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-frame.html (added) +++ archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-frame.html Wed Aug 29 15:37:03 2012 @@ -0,0 +1,30 @@ + + + + + + Apache Archiva Redback Components 2.0-SNAPSHOT Reference Package org.apache.archiva.redback.components.cache.ehcache + + + + +

+ org.apache.archiva.redback.components.cache.ehcache +

+ +

Classes

+ + + + + \ No newline at end of file Added: archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-summary.html URL: http://svn.apache.org/viewvc/archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-summary.html?rev=1378611&view=auto ============================================================================== --- archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-summary.html (added) +++ archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/ehcache/package-summary.html Wed Aug 29 15:37:03 2012 @@ -0,0 +1,77 @@ + + + + + + Apache Archiva Redback Components 2.0-SNAPSHOT Reference Package org.apache.archiva.redback.components.cache.ehcache + + + +
+ +
+
+ +
+ +

Package org.apache.archiva.redback.components.cache.ehcache

+ + + + + + + + + + + + + + + + + + +
Class Summary
+ EhcacheCache +
+ EhcacheCreator +
+ Stats +
+ +
+ +
+
+ +
+
+ Copyright © 2006-2012 The Apache Software Foundation. All Rights Reserved. + + \ No newline at end of file Added: archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheCreator.html URL: http://svn.apache.org/viewvc/archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheCreator.html?rev=1378611&view=auto ============================================================================== --- archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheCreator.html (added) +++ archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheCreator.html Wed Aug 29 15:37:03 2012 @@ -0,0 +1,55 @@ + + + + +CacheCreator xref + + + +
+
+1   package org.apache.archiva.redback.components.cache.factory;
+2   
+3   /*
+4    * Licensed to the Apache Software Foundation (ASF) under one
+5    * or more contributor license agreements.  See the NOTICE file
+6    * distributed with this work for additional information
+7    * regarding copyright ownership.  The ASF licenses this file
+8    * to you under the Apache License, Version 2.0 (the
+9    * "License"); you may not use this file except in compliance
+10   * with the License.  You may obtain a copy of the License at
+11   *
+12   *  http://www.apache.org/licenses/LICENSE-2.0
+13   *
+14   * Unless required by applicable law or agreed to in writing,
+15   * software distributed under the License is distributed on an
+16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+17   * KIND, either express or implied.  See the License for the
+18   * specific language governing permissions and limitations
+19   * under the License.
+20   */
+21  
+22  import org.apache.archiva.redback.components.cache.Cache;
+23  import org.apache.archiva.redback.components.cache.CacheException;
+24  import org.apache.archiva.redback.components.cache.CacheHints;
+25  
+26  /**
+27   * CacheCreator - an interface for CacheCreators
+28   *
+29   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+30   *
+31   */
+32  public interface CacheCreator
+33  {
+34      /**
+35       * Create a Cache, initialize it, and return it.
+36       * 
+37       * @param cacheHint the cache hints to use.
+38       * @return the created cache.
+39       * @throws CacheException if there was a cache creation error.
+40       */
+41      public Cache createCache( CacheHints hints ) throws CacheException;
+42  }
+
+
+ Added: archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheFactory.html URL: http://svn.apache.org/viewvc/archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheFactory.html?rev=1378611&view=auto ============================================================================== --- archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheFactory.html (added) +++ archiva/site-content/redback/components/xref/org/apache/archiva/redback/components/cache/factory/CacheFactory.html Wed Aug 29 15:37:03 2012 @@ -0,0 +1,153 @@ + + + + +CacheFactory xref + + + +
+
+1   package org.apache.archiva.redback.components.cache.factory;
+2   
+3   /*
+4    * Licensed to the Apache Software Foundation (ASF) under one
+5    * or more contributor license agreements.  See the NOTICE file
+6    * distributed with this work for additional information
+7    * regarding copyright ownership.  The ASF licenses this file
+8    * to you under the Apache License, Version 2.0 (the
+9    * "License"); you may not use this file except in compliance
+10   * with the License.  You may obtain a copy of the License at
+11   *
+12   *  http://www.apache.org/licenses/LICENSE-2.0
+13   *
+14   * Unless required by applicable law or agreed to in writing,
+15   * software distributed under the License is distributed on an
+16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+17   * KIND, either express or implied.  See the License for the
+18   * specific language governing permissions and limitations
+19   * under the License.
+20   */
+21  
+22  import org.apache.archiva.redback.components.cache.Cache;
+23  import org.apache.archiva.redback.components.cache.CacheException;
+24  import org.apache.archiva.redback.components.cache.CacheHints;
+25  import org.apache.archiva.redback.components.cache.impl.NoCacheCache;
+26  import org.slf4j.Logger;
+27  import org.slf4j.LoggerFactory;
+28  
+29  import java.io.IOException;
+30  import java.net.URL;
+31  import java.util.Enumeration;
+32  import java.util.HashMap;
+33  import java.util.Map;
+34  import java.util.Properties;
+35  
+36  /**
+37   * CacheFactory - dynamic cache creation (and tracking) facility for non-plexus objects to use.
+38   *
+39   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+40   */
+41  public class CacheFactory
+42  {
+43      static class CacheFactoryHolder
+44      {
+45          static CacheFactory instance = new CacheFactory();
+46      }
+47  
+48      private static Map caches;
+49  
+50      private static CacheCreator creator;
+51  
+52      private Logger logger = LoggerFactory.getLogger( getClass() );
+53  
+54      private CacheFactory()
+55      {
+56          caches = new HashMap();
+57  
+58          try
+59          {
+60              ClassLoader classLoader = this.getClass().getClassLoader();
+61  
+62              if ( classLoader == null )
+63              {
+64                  classLoader = ClassLoader.getSystemClassLoader();
+65              }
+66  
+67              Enumeration cachePropResources = classLoader.getResources( "META-INF/plexus-cache.properties" );
+68  
+69              if ( cachePropResources.hasMoreElements() )
+70              {
+71                  URL propURL = (URL) cachePropResources.nextElement();
+72                  Properties props = new Properties();
+73  
+74                  props.load( propURL.openStream() );
+75                  String creatorImpl = props.getProperty( "cache.creator" );
+76  
+77                  Class creatorClass = classLoader.loadClass( creatorImpl );
+78                  creator = (CacheCreator) creatorClass.newInstance();
+79              }
+80  
+81              if ( cachePropResources.hasMoreElements() )
+82              {
+83                  logger.error( "More than 1 CacheCreator provider exists in classpath. Using first one found [{}].",
+84                                creator.getClass().getName() );
+85              }
+86          }
+87          catch ( IOException e )
+88          {
+89              throw new ExceptionInInitializerError( e );
+90          }
+91          catch ( ClassNotFoundException e )
+92          {
+93              throw new ExceptionInInitializerError( e );
+94          }
+95          catch ( InstantiationException e )
+96          {
+97              throw new ExceptionInInitializerError( e );
+98          }
+99          catch ( IllegalAccessException e )
+100         {
+101             throw new ExceptionInInitializerError( e );
+102         }
+103     }
+104 
+105     public static CacheFactory getInstance()
+106     {
+107         return CacheFactoryHolder.instance;
+108     }
+109 
+110     public void setCacheCreatorFactory( CacheCreator creator )
+111     {
+112         CacheFactory.creator = creator;
+113     }
+114 
+115     public Cache getCache( String id, CacheHints hints )
+116         throws CacheException
+117     {
+118         if ( creator == null )
+119         {
+120             return new NoCacheCache();
+121         }
+122 
+123         if ( caches.containsKey( id ) )
+124         {
+125             return (Cache) caches.get( id );
+126         }
+127 
+128         if ( hints == null )
+129         {
+130             // Setup some defaults.
+131             hints = new CacheHints();
+132             hints.setName( id );
+133         }
+134 
+135         Cache cache = CacheFactory.creator.createCache( hints );
+136 
+137         caches.put( id, cache );
+138         return (Cache) cache;
+139     }
+140 }
+
+
+