View Javadoc

1   /*
2    * $Id: ManagedObjectStore.java 22486 2011-07-21 09:11:49Z stephen.fenech $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util.store;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.context.MuleContextAware;
16  import org.mule.api.store.ListableObjectStore;
17  import org.mule.api.store.ObjectStoreException;
18  import org.mule.api.store.ObjectStoreManager;
19  
20  import java.io.Serializable;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  public class ManagedObjectStore<T extends Serializable> implements ListableObjectStore<T>, MuleContextAware
25  {
26      String storeName;
27      boolean isPersistent;
28      ListableObjectStore<T> store;
29      MuleContext context;
30      int maxEntries = 0;
31      int entryTTL;
32      int expirationInterval;
33  
34      public String getStoreName()
35      {
36          return storeName;
37      }
38  
39      public void setStoreName(String storeName)
40      {
41          this.storeName = storeName;
42      }
43  
44      public boolean isPersistent()
45      {
46          return isPersistent;
47      }
48  
49      public void setPersistent(boolean isPersistent)
50      {
51          this.isPersistent = isPersistent;
52      }
53  
54      public int getMaxEntries()
55      {
56          return maxEntries;
57      }
58  
59      public void setMaxEntries(int maxEntries)
60      {
61          this.maxEntries = maxEntries;
62      }
63  
64      public int getEntryTTL()
65      {
66          return entryTTL;
67      }
68  
69      public void setEntryTTL(int entryTTL)
70      {
71          this.entryTTL = entryTTL;
72      }
73  
74      public int getExpirationInterval()
75      {
76          return expirationInterval;
77      }
78  
79      public void setExpirationInterval(int expirationInterval)
80      {
81          this.expirationInterval = expirationInterval;
82      }
83  
84      @Override
85      public boolean contains(Serializable key) throws ObjectStoreException
86      {
87          return getStore().contains(key);
88      }
89  
90      @Override
91      public void store(Serializable key, T value) throws ObjectStoreException
92      {
93          getStore().store(key, value);
94      }
95  
96      @Override
97      public T retrieve(Serializable key) throws ObjectStoreException
98      {
99          return getStore().retrieve(key);
100     }
101 
102     @Override
103     public T remove(Serializable key) throws ObjectStoreException
104     {
105         return getStore().remove(key);
106     }
107 
108     @Override
109     public void open() throws ObjectStoreException
110     {
111         ListableObjectStore<T> store = getStore();
112         if (store != null)
113         {
114             store.open();
115         }
116     }
117 
118     @Override
119     public void close() throws ObjectStoreException
120     {
121         ListableObjectStore<T> store = getStore();
122         if (store != null)
123         {
124             getStore().close();
125         }
126     }
127 
128     @Override
129     public List<Serializable> allKeys() throws ObjectStoreException
130     {
131         ListableObjectStore<T> store = getStore();
132         if (store != null)
133         {
134             return store.allKeys();
135         }
136         return new ArrayList<Serializable>();
137     }
138 
139     private ListableObjectStore<T> getStore()
140     {
141         if (store == null)
142         {
143             ObjectStoreManager objectStoreManager = (ObjectStoreManager) context.getRegistry().lookupObject(
144                 MuleProperties.OBJECT_STORE_MANAGER);
145             if (objectStoreManager == null)
146             {
147                 return null;
148             }
149             if (maxEntries != 0)
150             {
151                 store = (ListableObjectStore<T>) objectStoreManager.getObjectStore(storeName, isPersistent,
152                     maxEntries, entryTTL, expirationInterval);
153             }
154             else
155             {
156                 store = (ListableObjectStore<T>) objectStoreManager.getObjectStore(storeName, isPersistent);
157             }
158         }
159         return store;
160     }
161 
162     @Override
163     public void setMuleContext(MuleContext context)
164     {
165         this.context = context;
166     }
167 
168 }