View Javadoc

1   /*
2   MadCaching is a Tile Caching solution
3   Copyright (C) 2005  Mac Vu
4   
5   This program is free software; you can redistribute it and/or
6   modify it under the terms of the GNU General Public License
7   as published by the Free Software Foundation; either version 2
8   of the License, or (at your option) any later version.
9   
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package com.macvu.tiles;
20  
21  import com.macvu.tiles.xmlDefinition.XmlCacheAttribute;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  
28  public class CacheInformation {
29      boolean cacheEnabled;
30      List cacheAttributes;
31      List possibleCacheAttributes;
32  
33      // Hook to caching repository
34      String repositoryName;
35      String repositoryFactory;
36      String keyFactory;
37      CacheComponentDefinition parent;
38  
39      public static final String DEFAULT_REPOSITORY_NAME = "DEFAULT_CACHE_TILE_REPOSITORY";
40      public static final String DEFAULT_REPOSITORY_FACTORY = "com.macvu.tiles.cache.SingleLRUCacheServiceFactory";
41      public static final String DEFAULT_KEY_FACTORY = "com.macvu.tiles.cache.SimpleCacheKeyFactory";
42  
43      public CacheInformation() {
44          init();
45      }
46  
47      public CacheInformation(CacheInformation info) {
48          init();
49          if (info != null) {
50              cacheEnabled = info.getCacheEnabled();
51              Iterator itr = info.getCacheAttributes().iterator();
52              while (itr.hasNext()) {
53                  XmlCacheAttribute xmlAtt = (XmlCacheAttribute) itr.next();
54                  CacheAttribute attr = new CacheAttribute(xmlAtt);
55                  cacheAttributes.add(attr);
56              }
57              repositoryFactory = info.getRepositoryFactory();
58              keyFactory = info.getKeyFactory();
59          }
60      }
61  
62      public CacheInformation(List cacheAttributes, boolean cacheEnabled) {
63          init();
64          this.cacheAttributes = cacheAttributes;
65          this.cacheEnabled = cacheEnabled;
66      }
67  
68      protected void init() {
69          cacheEnabled = false;
70          cacheAttributes = new ArrayList();
71          possibleCacheAttributes = new ArrayList();
72          repositoryFactory = DEFAULT_REPOSITORY_FACTORY;
73          keyFactory = DEFAULT_KEY_FACTORY;
74      }
75  
76      public boolean isCacheEnabled() {
77          return cacheEnabled;
78      }
79  
80      public void setCacheEnabled(boolean cacheEnabled) {
81          this.cacheEnabled = cacheEnabled;
82      }
83  
84      public boolean getCacheEnabled() {
85          return cacheEnabled;
86      }
87  
88      public List getCacheAttributes() {
89          return cacheAttributes;
90      }
91  
92      public void setCacheAttributes(List cacheAttributes) {
93          this.cacheAttributes = cacheAttributes;
94      }
95  
96      public boolean addCacheAttribute(CacheAttribute attribute) {
97          if (isInCacheAttributes(attribute.getScope(), attribute.getName())) {
98              return false;
99          }
100 
101         return cacheAttributes.add(attribute);
102     }
103 
104     public boolean removeCacheAttribute(CacheAttribute attribute) {
105         return cacheAttributes.remove(attribute);
106     }
107 
108     public void clearCacheAttribute() {
109         cacheAttributes.clear();
110     }
111 
112     public List getPossibleCacheAttributes() {
113         return possibleCacheAttributes;
114     }
115 
116     public void setPossibleCacheAttributes(List possibleCacheAttributes) {
117         this.possibleCacheAttributes = possibleCacheAttributes;
118     }
119 
120     public boolean addPossibleCacheAttribute(CacheAttribute attribute) {
121         if (isInPossibleCacheAttributes(attribute.getScope(), attribute.getName())) {
122             return false;
123         }
124 
125         return possibleCacheAttributes.add(attribute);
126     }
127 
128     public boolean removePossibleCacheAttribute(CacheAttribute attribute) {
129         return possibleCacheAttributes.remove(attribute);
130     }
131 
132     public void clearPossibleCacheAttribute() {
133         possibleCacheAttributes.clear();
134     }
135 
136     public String getRepositoryName() {
137         if (repositoryName == null) {
138             if (parent != null) {
139                 repositoryName = parent.getName();
140             } else {
141                 repositoryName = DEFAULT_REPOSITORY_NAME;
142             }
143         }
144         return repositoryName;
145     }
146 
147     public void setRepositoryName(String repositoryName) {
148         this.repositoryName = repositoryName;
149     }
150 
151     public String getRepositoryFactory() {
152         return repositoryFactory;
153     }
154 
155     public void setRepositoryFactory(String repositoryFactory) {
156         this.repositoryFactory = repositoryFactory;
157     }
158 
159     public String getKeyFactory() {
160         return keyFactory;
161     }
162 
163     public void setKeyFactory(String keyFactory) {
164         this.keyFactory = keyFactory;
165     }
166 
167     public boolean isInCacheAttributes(String scope, String name) {
168         boolean found = false;
169         Iterator itr = cacheAttributes.iterator();
170 
171         while (!found && itr.hasNext()) {
172             CacheAttribute attr = (CacheAttribute) itr.next();
173             if (attr.getName().equals(name) && attr.getScope().equals(scope)) {
174                 found = true;
175             }
176         }
177         return found;
178     }
179 
180     public boolean isInPossibleCacheAttributes(String scope, String name) {
181         boolean found = false;
182         Iterator itr = possibleCacheAttributes.iterator();
183 
184         while (!found && itr.hasNext()) {
185             CacheAttribute attr = (CacheAttribute) itr.next();
186             if (attr.getName().equals(name) && attr.getScope().equals(scope)) {
187                 found = true;
188             }
189         }
190         return found;
191     }
192 
193     public boolean isDefault() {
194         if (isCacheEnabled() == false &&
195                 cacheAttributes.isEmpty() &&
196                 DEFAULT_REPOSITORY_NAME.equals(repositoryName) &&
197                 DEFAULT_REPOSITORY_FACTORY.equals(repositoryFactory) &&
198                 DEFAULT_KEY_FACTORY.equals(keyFactory)) {
199             return true;
200         }
201 
202         return false;
203     }
204 
205     public String toString() {
206         return "{name=" + repositoryName +
207                 ", cacheFactory=" + repositoryFactory +
208                 ", keyFactory=" + keyFactory +
209                 ", enabled=" + cacheEnabled +
210                 ", cacheAttributes=" + cacheAttributes +
211                 ", possibleCacheAttributes=" + possibleCacheAttributes + "}\n";
212 
213     }
214 
215     public CacheComponentDefinition getParent() {
216         return parent;
217     }
218 
219     public void setParent(CacheComponentDefinition parent) {
220         this.parent = parent;
221     }
222 }