View Javadoc

1   /*
2    * $Id: AbstractObjectStore.java 21657 2011-04-03 06:00:22Z dirk.olmes $
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.store.ObjectAlreadyExistsException;
14  import org.mule.api.store.ObjectDoesNotExistException;
15  import org.mule.api.store.ObjectStore;
16  import org.mule.api.store.ObjectStoreException;
17  import org.mule.config.i18n.CoreMessages;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * This is an abstract superclass for {@link ObjectStore} implementations that conforms to the
26   * contract defined in the interface's javadocs. Subclasses only need to implement storing the
27   * actual objects.
28   */
29  public abstract class AbstractObjectStore<T extends Serializable> implements ObjectStore<T>
30  {
31      protected final Log logger = LogFactory.getLog(getClass());
32  
33      public boolean contains(Serializable key) throws ObjectStoreException
34      {
35          if (key == null)
36          {
37              throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
38          }
39          return doContains(key);
40      }
41  
42      protected abstract boolean doContains(Serializable key) throws ObjectStoreException;
43  
44      public void store(Serializable key, T value) throws ObjectStoreException
45      {
46          if (key == null)
47          {
48              throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
49          }
50  
51          if (contains(key))
52          {
53              throw new ObjectAlreadyExistsException();
54          }
55  
56          doStore(key, value);
57      }
58  
59      protected  abstract void doStore(Serializable key, T value) throws ObjectStoreException;
60  
61      public T retrieve(Serializable key) throws ObjectStoreException
62      {
63          if (key == null)
64          {
65              throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
66          }
67  
68          if (contains(key) == false)
69          {
70              String message = "Key does not exist: " + key;
71              throw new ObjectDoesNotExistException(CoreMessages.createStaticMessage(message));
72          }
73  
74          return doRetrieve(key);
75      }
76  
77      protected abstract T doRetrieve(Serializable key) throws ObjectStoreException;
78  
79      public T remove(Serializable key) throws ObjectStoreException
80      {
81          if (key == null)
82          {
83              throw new ObjectStoreException(CoreMessages.objectIsNull("key"));
84          }
85  
86          if (contains(key) == false)
87          {
88              throw new ObjectDoesNotExistException();
89          }
90  
91          return doRemove(key);
92      }
93  
94      protected abstract T doRemove(Serializable key) throws ObjectStoreException;
95  }