View Javadoc

1   package org.apache.jcs;
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.jcs.access.GroupCacheAccess;
23  import org.apache.jcs.access.exception.CacheException;
24  import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
25  import org.apache.jcs.engine.control.CompositeCache;
26  import org.apache.jcs.engine.control.CompositeCacheManager;
27  
28  /***
29   * Simple class for using JCS. To use JCS in your application, you can use the static methods of
30   * this class to get access objects (instances of this class) for your cache regions. Ideally this
31   * class should be all you need to import to use JCS. One JCS should be created for each region you
32   * want to access. If you have several regions, then get instances for each. For best performance
33   * the getInstance call should be made in an initialization method.
34   */
35  public class JCS
36      extends GroupCacheAccess
37  {
38      /*** cache.ccf alternative. */
39      private static String configFilename = null;
40  
41      /*** The manager returns cache instances. */
42      private static CompositeCacheManager cacheMgr;
43  
44      /***
45       * Protected constructor for use by the static factory methods.
46       * <p>
47       * @param cacheControl Cache which the instance will provide access to
48       */
49      protected JCS( CompositeCache cacheControl )
50      {
51          super( cacheControl );
52      }
53  
54      /***
55       * Get a JCS which accesses the provided region.
56       * <p>
57       * @param region Region that return JCS will provide access to
58       * @return A JCS which provides access to a given region.
59       * @exception CacheException
60       */
61      public static JCS getInstance( String region )
62          throws CacheException
63      {
64          ensureCacheManager();
65  
66          return new JCS( cacheMgr.getCache( region ) );
67      }
68  
69      /***
70       * Get a JCS which accesses the provided region.
71       * <p>
72       * @param region Region that return JCS will provide access to
73       * @param icca CacheAttributes for region
74       * @return A JCS which provides access to a given region.
75       * @exception CacheException
76       */
77      public static JCS getInstance( String region, ICompositeCacheAttributes icca )
78          throws CacheException
79      {
80          ensureCacheManager();
81  
82          return new JCS( cacheMgr.getCache( region, icca ) );
83      }
84  
85      /***
86       * Gets an instance of CompositeCacheManager and stores it in the cacheMgr class field, if it is
87       * not already set. Unlike the implementation in CacheAccess, the cache manager is a
88       * CompositeCacheManager. NOTE: This can will be moved up into GroupCacheAccess.
89       */
90      protected static synchronized void ensureCacheManager()
91      {
92          if ( cacheMgr == null )
93          {
94              if ( configFilename == null )
95              {
96                  cacheMgr = CompositeCacheManager.getInstance();
97              }
98              else
99              {
100                 cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
101 
102                 cacheMgr.configure( configFilename );
103             }
104         }
105     }
106 
107     /***
108      * Set the filename that the cache manager will be initialized with. Only matters before the
109      * instance is initialized.
110      * <p>
111      * @param configFilename
112      */
113     public static void setConfigFilename( String configFilename )
114     {
115         JCS.configFilename = configFilename;
116     }
117 }