View Javadoc
1   /*******************************************************************************
2    * Copyright 2015 Internet2
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" BASIS,
12   * 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 edu.internet2.middleware.changelogconsumer.googleapps.cache;
17  
18  import com.google.api.services.admin.directory.model.Group;
19  import com.google.api.services.admin.directory.model.User;
20  import edu.internet2.middleware.subject.Subject;
21  import org.joda.time.DateTime;
22  
23  import java.util.Hashtable;
24  import java.util.List;
25  import java.util.Set;
26  
27  /**
28   * CacheObject supports Google User, Google Group, Grouper Subject, and Grouper Group objects.
29   *
30   * * @author John Gasper, Unicon
31   */
32  public class Cache<T> {
33      private Hashtable<String, T> cache = new Hashtable<String, T>();
34      private DateTime cachePopulatedTime;
35      private int cacheValidity = 30;
36  
37      public T get(String id) {
38          return cache.get(id);
39      }
40  
41      public void clear() {
42          cache.clear();
43          cachePopulatedTime = null;
44      }
45  
46      public void put(T item) {
47          cache.put(getId(item), item);
48      }
49  
50      public void remove (String id) {
51          if (cache.containsKey(id)) {
52              cache.remove(id);
53          }
54      }
55  
56      public void seed(int size) {
57          cache = new Hashtable<String, T>(size);
58      }
59  
60      public void seed(List<T> items) {
61          cache = new Hashtable<String, T>(items.size() + 100);
62  
63          if (items == null) {
64              seed(100);
65          } else {
66              for (T item : items) {
67                  cache.put(getId(item), item);
68              }
69  
70              cachePopulatedTime = new DateTime();
71          }
72      }
73  
74      public int size() {
75          return cache == null ? 0 : cache.size();
76      }
77  
78      private String getId(T item) {
79          if (item.getClass().equals(User.class)) {
80              return ((User) item).getPrimaryEmail();
81          } else if (item.getClass().equals(Group.class)) {
82              return ((Group) item).getEmail();
83          } else if (item.getClass().equals(Subject.class)) {
84              return ((Subject) item).getSourceId() + "__" + ((Subject) item).getId();
85          } else if (item.getClass().equals(edu.internet2.middleware.grouper.Group.class)) {
86              return ((edu.internet2.middleware.grouper.Group) item).getName();
87          } else {
88              return item.toString();
89          }
90      }
91  
92      public void setCacheValidity(int minutes){
93          cacheValidity = minutes;
94      }
95  
96      public DateTime getExpiration() {
97          return cachePopulatedTime != null ? cachePopulatedTime.plusMinutes(cacheValidity) : null;
98      }
99  
100     public boolean isExpired() {
101         return cachePopulatedTime == null || getExpiration().isBeforeNow();
102     }
103 
104     public Set<String> getKeySet() {
105         return cache.keySet();
106     }
107 }