1   /*
2    * CacheRegions.java
3    *
4    * $Id: CacheRegions.java,v 1.4 2003/07/02 12:00:51 baliuka Exp $
5    */
6   
7   package net.sf.voruta;
8   
9   import java.util.*;
10  import java.sql.*;
11  
12  /***
13   *
14   * @author  baliuka
15   */
16  final class CacheRegions {
17      
18       
19      
20      Map regions = Collections.synchronizedMap( new HashMap() ); 
21      int isolation;
22      private static Object [] STRINGS = new String[]{};
23      
24      /*** Creates a new instance of CacheRegions */
25      public CacheRegions(int isolation ) {
26          this.isolation = isolation;
27      }
28      
29      private Cache newCache(){
30          
31        switch(isolation){
32            
33            case Connection.TRANSACTION_NONE:
34            case Connection.TRANSACTION_READ_UNCOMMITTED:
35                return new ReadUncommitedCache();
36                
37            case Connection.TRANSACTION_READ_COMMITTED:
38                return new ReadCommitedCache();
39                
40            case Connection.TRANSACTION_REPEATABLE_READ:
41            case Connection.TRANSACTION_SERIALIZABLE:
42               return  new SnapshotCache(); 
43            default : throw new IllegalStateException("unknown isolation level " + isolation);
44                
45         }
46      }
47      
48      private Cache getRegion(String name){
49          
50         synchronized(regions){
51             
52          Cache cache =  (Cache)regions.get(name);
53          if(cache == null){
54            cache = newCache();
55            regions.put(name,cache);
56          }
57          return cache;
58          
59         }
60      }
61      
62      private  String[] getAll(){
63           return ((String[])regions.keySet().toArray( STRINGS ) );
64      }
65      
66      void clearAll(){
67         clearRegions(getAll());
68      }
69      
70      void afterCommit(){
71           String names[] = getAll();
72           for( int i = 0, len = names.length; i < len; i++ ){
73               Cache cache =  (Cache)regions.get(names[i]);
74               cache.afterCommit();
75           }
76      }
77      
78       void beforeBegin(){
79           String names[] = getAll();
80           for( int i = 0, len = names.length; i < len; i++ ){
81               Cache cache =  (Cache)regions.get(names[i]);
82               cache.beforeBegin();
83           }
84       }
85     
86      
87      void afterRollback(){
88           String names[] = getAll();
89           for( int i = 0, len = names.length; i < len; i++ ){
90               Cache cache =  (Cache)regions.get(names[i]);
91               cache.afterRollback();
92           }
93      }
94      
95      
96      public  void clearRegions(String names[]){
97          
98         for( int i = 0, len = names.length; i < len; i++ ){
99            Cache cache =  (Cache)regions.get(names[i]);
100            if(cache != null){
101                cache.clear();
102            }
103        }
104         
105     }
106     
107     public  void put(String name, Object key, Object value ){
108        getRegion(name).put( key, value );   
109     }
110     
111     public  Object get(String name, Object key ){
112         
113         Cache cache =  (Cache)regions.get(name);
114         if(cache != null){
115           return cache.get(key);
116         }else{
117           return null;
118         }
119     }
120     
121     
122     
123 }
This page was automatically generated by Maven