PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/component/identity/src/main/java/org/exoplatform/services/organization/idm/IntegrationCache.java

https://github.com/thanhvc/exogtn
Java | 344 lines | 208 code | 78 blank | 58 comment | 34 complexity | 3e3da2706a8b443642bc03b7819a3242 MD5 | raw file
  1. package org.exoplatform.services.organization.idm;
  2. import org.exoplatform.commons.utils.LazyPageList;
  3. import org.exoplatform.services.organization.Query;
  4. import org.jboss.cache.Cache;
  5. import org.jboss.cache.CacheFactory;
  6. import org.jboss.cache.CacheStatus;
  7. import org.jboss.cache.DefaultCacheFactory;
  8. import org.jboss.cache.Fqn;
  9. import org.jboss.cache.Node;
  10. import org.jboss.cache.eviction.ExpirationAlgorithmConfig;
  11. import org.jboss.cache.eviction.ExpirationConfiguration;
  12. import org.picketlink.idm.api.Group;
  13. import org.picketlink.idm.api.User;
  14. import java.io.InputStream;
  15. import java.util.Collection;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. /*
  19. * JBoss, a division of Red Hat
  20. * Copyright 2010, Red Hat Middleware, LLC, and individual contributors as indicated
  21. * by the @authors tag. See the copyright.txt in the distribution for a
  22. * full listing of individual contributors.
  23. *
  24. * This is free software; you can redistribute it and/or modify it
  25. * under the terms of the GNU Lesser General Public License as
  26. * published by the Free Software Foundation; either version 2.1 of
  27. * the License, or (at your option) any later version.
  28. *
  29. * This software is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. * Lesser General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Lesser General Public
  35. * License along with this software; if not, write to the Free
  36. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  37. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  38. */
  39. /**
  40. * Provides cache for some data used in integration layer between PicketLink IDM and GateIn
  41. */
  42. public class IntegrationCache
  43. {
  44. private static Logger log = Logger.getLogger(IntegrationCache.class.getName());
  45. private Cache cache;
  46. public static final String NODE_GTN_GROUP_ID = "NODE_GTN_GROUP_ID";
  47. public static final String NODE_PLIDM_ROOT_GROUP = "NODE_PLIDM_ROOT_GROUP";
  48. public static final String NULL_NS_NODE = "GTN_IC_COMMON_NS";
  49. public static final String USER_QUERY_NODE = "GTN_USER_QUERY_LAZY_LIST";
  50. public static final String MAIN_ROOT = "NODE_GTN_ORG_SERVICE_INT_CACHE_MAIN_ROOT";
  51. public static final String NODE_OBJECT_KEY = "object";
  52. private int expiration = -1;
  53. private Fqn getRootNode()
  54. {
  55. return Fqn.fromString("/" + MAIN_ROOT);
  56. }
  57. private Fqn getNamespacedFqn(String ns)
  58. {
  59. String namespace = ns != null ? ns : NULL_NS_NODE;
  60. namespace = namespace.replaceAll("/", "_");
  61. return Fqn.fromString(getRootNode() + "/" + namespace);
  62. }
  63. private Fqn getFqn(String ns, String node, Object o)
  64. {
  65. return Fqn.fromString(getNamespacedFqn(ns) + "/" + node + "/" + o);
  66. }
  67. private Fqn getFqn(String ns, String node)
  68. {
  69. return Fqn.fromString(getNamespacedFqn(ns) + "/" + node);
  70. }
  71. public void initialize(InputStream jbossCacheConfiguration)
  72. {
  73. CacheFactory factory = new DefaultCacheFactory();
  74. if (jbossCacheConfiguration == null)
  75. {
  76. throw new IllegalArgumentException("JBoss Cache configuration InputStream is null");
  77. }
  78. this.cache = factory.createCache(jbossCacheConfiguration);
  79. this.cache.create();
  80. this.cache.start();
  81. }
  82. public void initialize(Cache cache)
  83. {
  84. this.cache = cache;
  85. CacheStatus status = cache.getCacheStatus();
  86. if (status.createAllowed())
  87. {
  88. this.cache.create();
  89. }
  90. if (status.startAllowed())
  91. {
  92. this.cache.start();
  93. }
  94. }
  95. Cache getCache()
  96. {
  97. return cache;
  98. }
  99. public void invalidate(String ns)
  100. {
  101. boolean success = cache.getRoot().removeChild(getNamespacedFqn(ns));
  102. if (log.isLoggable(Level.FINER))
  103. {
  104. log.finer(this.toString() + "Invalidating namespace:" + ns + "; success=" + success);
  105. }
  106. }
  107. public void invalidateAll()
  108. {
  109. boolean success = cache.getRoot().removeChild(getRootNode());
  110. if (log.isLoggable(Level.FINER))
  111. {
  112. log.finer(this.toString() + "Invalidating whole cache - success=" + success);
  113. }
  114. }
  115. /**
  116. * Store gatein group id
  117. * @param ns
  118. * @param pLIDMId
  119. * @param id
  120. */
  121. void putGtnGroupId(String ns, String pLIDMId, String id)
  122. {
  123. Fqn nodeFqn = getFqn(ns, NODE_GTN_GROUP_ID, pLIDMId);
  124. Node ioNode = getCache().getRoot().addChild(nodeFqn);
  125. if (ioNode != null)
  126. {
  127. ioNode.put(NODE_OBJECT_KEY, id);
  128. setExpiration(ioNode);
  129. if (log.isLoggable(Level.FINER))
  130. {
  131. log.finer(this.toString() + "GateIn group id cached. PLIDM group id: " + pLIDMId +
  132. "GateIn group id: " + id + ";namespace=" + ns);
  133. }
  134. }
  135. }
  136. /**
  137. * Retrieve gatein group id
  138. * @param ns
  139. * @param pLIDMId
  140. * @return
  141. */
  142. String getGtnGroupId(String ns, String pLIDMId)
  143. {
  144. Fqn nodeFqn = getFqn(ns, NODE_GTN_GROUP_ID, pLIDMId);
  145. Node node = getCache().getRoot().getChild(nodeFqn);
  146. if (node != null)
  147. {
  148. String id = (String)node.get(NODE_OBJECT_KEY);
  149. if (log.isLoggable(Level.FINER) && id != null)
  150. {
  151. log.finer(this.toString() + "GateIn group id found in cache. PLIDM group id: " + pLIDMId +
  152. "GateIn group id: " + id + ";namespace=" + ns);
  153. }
  154. return id;
  155. }
  156. return null;
  157. }
  158. /**
  159. * Store IDMUserListAccess
  160. * @param ns
  161. * @param query
  162. * @param list
  163. */
  164. void putGtnUserLazyPageList(String ns, Query query, IDMUserListAccess list)
  165. {
  166. Fqn nodeFqn = getFqn(ns, USER_QUERY_NODE, getQueryKey(query));
  167. Node ioNode = getCache().getRoot().addChild(nodeFqn);
  168. if (ioNode != null)
  169. {
  170. ioNode.put(NODE_OBJECT_KEY, list);
  171. setExpiration(ioNode);
  172. if (log.isLoggable(Level.FINER))
  173. {
  174. log.finer(this.toString() + "GateIn user query list cached. Query: " + getQueryKey(query) + ";namespace=" + ns);
  175. }
  176. }
  177. }
  178. /**
  179. * Retrieve IDMUserListAccess
  180. * @param ns
  181. * @param query
  182. * @return LazyPageList
  183. */
  184. IDMUserListAccess getGtnUserLazyPageList(String ns, Query query)
  185. {
  186. Fqn nodeFqn = getFqn(ns, USER_QUERY_NODE, getQueryKey(query));
  187. Node node = getCache().getRoot().getChild(nodeFqn);
  188. if (node != null)
  189. {
  190. IDMUserListAccess list = (IDMUserListAccess)node.get(NODE_OBJECT_KEY);
  191. if (log.isLoggable(Level.FINER) && list != null)
  192. {
  193. log.finer(this.toString() + "GateIn user query list found in cache. Query: " + getQueryKey(query) + ";namespace=" + ns);
  194. }
  195. return list;
  196. }
  197. return null;
  198. }
  199. /**
  200. * Store PLIDM root group
  201. * @param ns
  202. * @param rootGroup
  203. */
  204. void putRootGroup(String ns, Group rootGroup)
  205. {
  206. Fqn nodeFqn = getFqn(ns, NODE_PLIDM_ROOT_GROUP);
  207. Node ioNode = getCache().getRoot().addChild(nodeFqn);
  208. if (ioNode != null)
  209. {
  210. setExpiration(ioNode);
  211. ioNode.put(NODE_OBJECT_KEY, rootGroup);
  212. if (log.isLoggable(Level.FINER))
  213. {
  214. log.finer(this.toString() + "GateIn root group stored in cache" + ";namespace=" + ns);
  215. }
  216. }
  217. }
  218. /**
  219. * Retrieve PLIDM root group
  220. * @param ns
  221. * @return
  222. */
  223. Group getRootGroup(String ns)
  224. {
  225. Fqn nodeFqn = getFqn(ns, NODE_PLIDM_ROOT_GROUP);
  226. Node node = getCache().getRoot().getChild(nodeFqn);
  227. if (node != null)
  228. {
  229. Group rootGroup = (Group)node.get(NODE_OBJECT_KEY);
  230. if (log.isLoggable(Level.FINER) && rootGroup != null)
  231. {
  232. log.finer(this.toString() + "GateIn root group found in cache" + ";namespace=" + ns);
  233. }
  234. return rootGroup;
  235. }
  236. return null;
  237. }
  238. public void setExpiration(Node node)
  239. {
  240. if (expiration != -1 && expiration > 0)
  241. {
  242. Long future = new Long(System.currentTimeMillis() + expiration);
  243. node.put(ExpirationAlgorithmConfig.EXPIRATION_KEY, future);
  244. }
  245. }
  246. public int getExpiration()
  247. {
  248. return expiration;
  249. }
  250. public void setExpiration(int expiration)
  251. {
  252. this.expiration = expiration;
  253. }
  254. String getQueryKey(Query query)
  255. {
  256. StringBuilder sb = new StringBuilder();
  257. String SEP = ":::";
  258. sb.append(query.getEmail()).append(SEP)
  259. .append(query.getFirstName()).append(SEP)
  260. .append(query.getLastName()).append(SEP)
  261. .append(query.getUserName()).append(SEP)
  262. .append(query.getFromLoginDate()).append(SEP)
  263. .append(query.getToLoginDate()).append(SEP);
  264. return sb.toString();
  265. }
  266. }