Coverage Report - org.mule.transport.jdbc.store.JdbcObjectStore
 
Classes in this File Line Coverage Branch Coverage Complexity
JdbcObjectStore
0%
0/66
0%
0/8
0
JdbcObjectStore$1
0%
0/2
N/A
0
JdbcObjectStore$2
0%
0/2
N/A
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.jdbc.store;
 8  
 
 9  
 import java.io.Serializable;
 10  
 import java.sql.SQLException;
 11  
 
 12  
 import org.apache.commons.dbutils.ResultSetHandler;
 13  
 import org.apache.commons.dbutils.handlers.ArrayHandler;
 14  
 import org.mule.api.store.ObjectAlreadyExistsException;
 15  
 import org.mule.api.store.ObjectDoesNotExistException;
 16  
 import org.mule.api.store.ObjectStoreException;
 17  
 import org.mule.api.transaction.TransactionCallback;
 18  
 import org.mule.api.transaction.TransactionConfig;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 import org.mule.transaction.TransactionTemplate;
 21  
 import org.mule.transport.jdbc.JdbcConnector;
 22  
 import org.mule.util.store.AbstractMonitoredObjectStore;
 23  
 
 24  0
 public class JdbcObjectStore<T extends Serializable> extends AbstractMonitoredObjectStore<T>
 25  
 {
 26  
 
 27  
     private JdbcConnector jdbcConnector;
 28  
     private TransactionConfig transactionConfig;
 29  
     private String insertQueryKey;
 30  
     private String selectQueryKey;
 31  
     private String deleteQueryKey;
 32  
 
 33  
     private ArrayHandler arrayHandler;
 34  
 
 35  
     public JdbcObjectStore()
 36  0
     {
 37  0
         this.arrayHandler = new ArrayHandler();
 38  0
         this.maxEntries = -1;
 39  0
     }
 40  
 
 41  
     /**
 42  
      * {@inheritDoc}
 43  
      */
 44  
     protected void expire()
 45  
     {
 46  
         // DO NOTHING
 47  0
     }
 48  
 
 49  
     /**
 50  
      * {@inheritDoc}
 51  
      */
 52  
     public boolean contains(Serializable key) throws ObjectStoreException
 53  
     {
 54  0
         this.notNullKey(key);
 55  0
         Object[] result = (Object[]) this.query(this.getSelectQuery(), this.arrayHandler, key);
 56  0
         return result != null;
 57  
     }
 58  
 
 59  
     /**
 60  
      * {@inheritDoc}
 61  
      */
 62  
     public T remove(Serializable key) throws ObjectStoreException
 63  
     {
 64  0
         this.notNullKey(key);
 65  0
         T value = this.retrieve(key);
 66  0
         this.update(this.getDeleteQuery(), key);
 67  0
         return value;
 68  
     }
 69  
 
 70  
     /**
 71  
      * {@inheritDoc}
 72  
      */
 73  
     public T retrieve(Serializable key) throws ObjectStoreException
 74  
     {
 75  0
         Object[] row = (Object[]) this.query(this.getSelectQuery(), this.arrayHandler, key);
 76  0
         if (row == null)
 77  
         {
 78  0
             throw new ObjectDoesNotExistException(CoreMessages.objectNotFound(key));
 79  
         }
 80  
         else
 81  
         {
 82  0
             return (T) row[1];
 83  
         }
 84  
     }
 85  
 
 86  
     public void store(Serializable key, T value, String[] parameters) throws ObjectStoreException
 87  
     {
 88  0
         Object[] arguments = new Object[2 + parameters.length];
 89  0
         arguments[0] = key;
 90  0
         arguments[1] = value;
 91  
 
 92  0
         for (int i = 0; i < parameters.length; i++)
 93  
         {
 94  0
             String parameter = parameters[i];
 95  0
             arguments[2 + i] = parameter;
 96  
         }
 97  
 
 98  0
         this.update(this.getInsertQuery(), arguments);
 99  0
     }
 100  
 
 101  
     /**
 102  
      * {@inheritDoc}
 103  
      */
 104  
     public void store(Serializable key, T value) throws ObjectStoreException
 105  
     {
 106  0
         this.notNullKey(key);
 107  
         try
 108  
         {
 109  0
             this.update(this.getInsertQuery(), key, value);
 110  
         }
 111  0
         catch (ObjectStoreException e)
 112  
         {
 113  0
             throw new ObjectAlreadyExistsException(e);
 114  0
         }
 115  0
     }
 116  
 
 117  
     /**
 118  
      * Validates that the key is not null
 119  
      * 
 120  
      * @param key
 121  
      * @throws ObjectStoreException
 122  
      */
 123  
     private void notNullKey(Serializable key) throws ObjectStoreException
 124  
     {
 125  0
         if (key == null)
 126  
         {
 127  0
             throw new ObjectStoreException(CoreMessages.objectIsNull("id"));
 128  
         }
 129  0
     }
 130  
 
 131  
     /**
 132  
      * Executes the query in sql using the current transaction config
 133  
      * 
 134  
      * @param sql
 135  
      * @param handler
 136  
      * @param arguments
 137  
      * @return
 138  
      * @throws ObjectStoreException
 139  
      */
 140  
     private Object query(final String sql, final ResultSetHandler handler, final Object... arguments)
 141  
         throws ObjectStoreException
 142  
     {
 143  
         try
 144  
         {
 145  0
             TransactionTemplate<Object> tt = new TransactionTemplate<Object>(this.transactionConfig,
 146  
                 this.jdbcConnector.getMuleContext());
 147  
 
 148  0
             Object result = tt.execute(new TransactionCallback<Object>()
 149  0
             {
 150  
                 public Object doInTransaction() throws Exception
 151  
                 {
 152  0
                     return jdbcConnector.getQueryRunner().query(sql, handler, arguments);
 153  
                 }
 154  
             });
 155  
 
 156  0
             return result;
 157  
         }
 158  0
         catch (SQLException e)
 159  
         {
 160  0
             throw new ObjectStoreException(e);
 161  
         }
 162  0
         catch (Exception e)
 163  
         {
 164  0
             throw new ObjectStoreException(e);
 165  
         }
 166  
     }
 167  
 
 168  
     /**
 169  
      * Executes an update query using the current transaction config
 170  
      * 
 171  
      * @param sql
 172  
      * @param arguments
 173  
      * @return
 174  
      * @throws ObjectStoreException
 175  
      */
 176  
     private Object update(final String sql, final Object... arguments) throws ObjectStoreException
 177  
     {
 178  
         try
 179  
         {
 180  0
             TransactionTemplate<Object> tt = new TransactionTemplate<Object>(this.transactionConfig,
 181  
                 this.jdbcConnector.getMuleContext());
 182  
 
 183  0
             Object result = tt.execute(new TransactionCallback<Object>()
 184  0
             {
 185  
                 public Object doInTransaction() throws Exception
 186  
                 {
 187  0
                     return jdbcConnector.getQueryRunner().update(sql, arguments);
 188  
                 }
 189  
             });
 190  
 
 191  0
             return result;
 192  
         }
 193  0
         catch (SQLException e)
 194  
         {
 195  0
             throw new ObjectStoreException(e);
 196  
         }
 197  0
         catch (Exception e)
 198  
         {
 199  0
             throw new ObjectStoreException(e);
 200  
         }
 201  
     }
 202  
 
 203  
     public JdbcConnector getJdbcConnector()
 204  
     {
 205  0
         return jdbcConnector;
 206  
     }
 207  
 
 208  
     public void setJdbcConnector(JdbcConnector jdbcConnector)
 209  
     {
 210  0
         this.jdbcConnector = jdbcConnector;
 211  0
     }
 212  
 
 213  
     public TransactionConfig getTransactionConfig()
 214  
     {
 215  0
         return transactionConfig;
 216  
     }
 217  
 
 218  
     public void setTransactionConfig(TransactionConfig transactionConfig)
 219  
     {
 220  0
         this.transactionConfig = transactionConfig;
 221  0
     }
 222  
 
 223  
     public String getInsertQuery()
 224  
     {
 225  0
         return (String) this.jdbcConnector.getQueries().get(this.insertQueryKey);
 226  
     }
 227  
 
 228  
     public String getSelectQuery()
 229  
     {
 230  0
         return (String) this.jdbcConnector.getQueries().get(this.selectQueryKey);
 231  
     }
 232  
 
 233  
     public String getDeleteQuery()
 234  
     {
 235  0
         return (String) this.jdbcConnector.getQueries().get(this.deleteQueryKey);
 236  
     }
 237  
 
 238  
     public String getInsertQueryKey()
 239  
     {
 240  0
         return insertQueryKey;
 241  
     }
 242  
 
 243  
     public void setInsertQueryKey(String insertQueryKey)
 244  
     {
 245  0
         this.insertQueryKey = insertQueryKey;
 246  0
     }
 247  
 
 248  
     public String getSelectQueryKey()
 249  
     {
 250  0
         return selectQueryKey;
 251  
     }
 252  
 
 253  
     public void setSelectQueryKey(String selectQueryKey)
 254  
     {
 255  0
         this.selectQueryKey = selectQueryKey;
 256  0
     }
 257  
 
 258  
     public String getDeleteQueryKey()
 259  
     {
 260  0
         return deleteQueryKey;
 261  
     }
 262  
 
 263  
     public void setDeleteQueryKey(String deleteQueryKey)
 264  
     {
 265  0
         this.deleteQueryKey = deleteQueryKey;
 266  0
     }
 267  
 }