View Javadoc

1   /*
2    * $Id: PartitionedObjectStoreWrapper.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.store.ListableObjectStore;
15  import org.mule.api.store.ObjectAlreadyExistsException;
16  import org.mule.api.store.ObjectStoreException;
17  import org.mule.util.queue.QueueKey;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  public class PartitionedObjectStoreWrapper<T extends Serializable> implements ListableObjectStore<T>
24  {
25      String partitionName;
26      MuleContext context;
27      ListableObjectStore<T> baseStore;
28  
29      public PartitionedObjectStoreWrapper(String name, MuleContext context, ListableObjectStore<T> store)
30      {
31          partitionName = name;
32          this.context = context;
33          baseStore = store;
34      }
35  
36      @Override
37      public boolean contains(Serializable key) throws ObjectStoreException
38      {
39          return getStore().contains(new QueueKey(partitionName, key));
40      }
41  
42      @Override
43      public void store(Serializable key, T value) throws ObjectStoreException
44      {
45          // This required because QueuePersistenceObject store will NOT complain in
46          // cases where object already exists!
47          QueueKey qKey = new QueueKey(partitionName, key);
48          synchronized (this)
49          {
50              if (getStore().contains(qKey))
51              {
52                  throw new ObjectAlreadyExistsException();
53              }
54              getStore().store(qKey, value);
55          }
56      }
57  
58      @Override
59      public T retrieve(Serializable key) throws ObjectStoreException
60      {
61          return getStore().retrieve(new QueueKey(partitionName, key));
62      }
63  
64      @Override
65      public T remove(Serializable key) throws ObjectStoreException
66      {
67          return getStore().remove(new QueueKey(partitionName, key));
68      }
69  
70      @Override
71      public boolean isPersistent()
72      {
73          return getStore().isPersistent();
74      }
75  
76      @Override
77      public void open() throws ObjectStoreException
78      {
79          getStore().open();
80      }
81  
82      @Override
83      public void close() throws ObjectStoreException
84      {
85          getStore().close();
86      }
87  
88      @Override
89      public List<Serializable> allKeys() throws ObjectStoreException
90      {
91          // TODO this is NOT efficient!
92          List<Serializable> results = new ArrayList<Serializable>();
93          List<Serializable> keys = getStore().allKeys();
94          for (Serializable key : keys)
95          {
96              QueueKey qKey = (QueueKey) key;
97              if (qKey.queueName.equals(partitionName))
98              {
99                  results.add(qKey.id);
100             }
101         }
102         return results;
103     }
104 
105     private ListableObjectStore<T> getStore()
106     {
107         return baseStore;
108     }
109 
110     public ListableObjectStore<T> getBaseStore()
111     {
112         return getStore();
113     }
114 
115 }