View Javadoc

1   /*
2    * Copyright 2007 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.portal.cms.cache;
17  
18  import java.util.Iterator;
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.TreeSet;
22  
23  import nl.hippo.client.api.content.DocumentPath;
24  
25  /***
26   * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
27   *
28   * @version $Id: PortalCacheKeyFactoryImpl.java 6521 2007-05-22 13:16:10Z ddam $
29   */
30  public class PortalCacheKeyFactoryImpl implements CacheKeyFactory {
31  
32      /* (non-Javadoc)
33       * @see nl.hippo.portal.cms.cache.CacheKeyFactory#createKey(java.lang.Object[])
34       */
35      public PortalCacheKey createKey(Object[] keyElements) {
36          Set keyElementSet = new TreeSet();
37          getElements(keyElementSet,keyElements);
38          StringBuffer key = new StringBuffer();
39          for (Iterator iter = keyElementSet.iterator(); iter.hasNext();) {
40              key.append(iter.next().toString());
41          }
42          return new PortalCacheKeyImpl(key.toString());
43      }
44      
45      private void getElements(Set currentSet, Object[] keyElements){
46          for (int i=0; i<keyElements.length; i++) {
47              if (keyElements[i] instanceof Object[]){
48                  getElements(currentSet,(Object[])keyElements[i]);
49              } else {
50                  getPart(currentSet,keyElements[i]);    
51              }            
52          }
53      }
54      
55      private void getPart(Set keyElementSet, Object keyElement){
56          StringBuffer buf = new StringBuffer();
57          getPartKey(buf, keyElement);
58          getPartValue(buf, keyElement);        
59          keyElementSet.add(buf.toString());
60      }
61  
62      private void getPartKey(StringBuffer buf, Object keyElement){
63          if (keyElement instanceof DocumentPath){
64              buf.append("_path[");
65          } else 
66          if (keyElement instanceof PortalCacheKey){
67              buf.append("_pkey[");
68          } else {
69              buf.append("_key");
70          }
71          buf.append(":");
72      }
73      
74      private void getPartValue(StringBuffer buf, Object keyElement){
75          if (keyElement instanceof DocumentPath){
76              buf.append(((DocumentPath)keyElement).getFullPath()); 
77              buf.append("]");
78          } else 
79          if (keyElement instanceof PortalCacheKey){
80              buf.append(((PortalCacheKey)keyElement).getKey());
81              buf.append("]");
82          } else
83          {
84              buf.append(keyElement.toString());
85          }
86          buf.append(";");
87      }
88      
89  }