com.j_spaces.javax.cache
Class AbstractCacheLoader

java.lang.Object
  extended by com.j_spaces.javax.cache.AbstractCacheLoader
All Implemented Interfaces:
CacheLifeCycleManager, CacheLoader

Deprecated.

public abstract class AbstractCacheLoader
extends Object
implements CacheLoader, CacheLifeCycleManager

This class provides methods that convert the user's original object to GigaSpaces internal Entry representation - the IGSEntry. You should extend your custom CacheLoader or CacheStore from the AbstractCacheLoader and implement the relevant methods. When using the AbstractCacheLoader.getConverter().toObject(IGSEntry) your Entry must include getter and setter methods.

Since:
5.1

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.j_spaces.javax.cache.CacheLifeCycleManager
CacheLifeCycleManager.LifeCycleProperty
 
Constructor Summary
AbstractCacheLoader()
          Deprecated.  
 
Method Summary
 void dropTable(String className)
          Deprecated. drop table represented by this class name.
 Properties init(String dbURL, String user, String password, Properties properties)
          Deprecated. Called when space is started
abstract  Object load(Object key)
          Deprecated. Loads value from secondary data storage in case if it's not in space cache.
abstract  Map loadAll(Collection keys)
          Deprecated. Loads multiple values from secondary data storage in case they are not in cache.
 void shutdown()
          Deprecated. Called when space is shut down
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractCacheLoader

public AbstractCacheLoader()
Deprecated. 
Method Detail

init

public Properties init(String dbURL,
                       String user,
                       String password,
                       Properties properties)
Deprecated. 
Description copied from interface: CacheLifeCycleManager
Called when space is started

Specified by:
init in interface CacheLifeCycleManager
Parameters:
dbURL - The StorageAdapterURL space config/schema property value
user - The userName space config/schema property value
password - The password space config/schema property value
properties - Properties taken from property file
Returns:
property indicators CacheLifeCycleManager.LifeCycleProperty between the underlying driver and the cache adapter; can be null.

shutdown

public void shutdown()
Deprecated. 
Description copied from interface: CacheLifeCycleManager
Called when space is shut down

Specified by:
shutdown in interface CacheLifeCycleManager

dropTable

public void dropTable(String className)
Deprecated. 
Description copied from interface: CacheLifeCycleManager
drop table represented by this class name.

Specified by:
dropTable in interface CacheLifeCycleManager
Parameters:
className - representing the table to be dropped.

load

public abstract Object load(Object key)
Deprecated. 
Description copied from interface: CacheLoader
Loads value from secondary data storage in case if it's not in space cache.

Specified by:
load in interface CacheLoader
Parameters:
key - Key (an IGSEntry) of the object to load.
Returns:
Object returned by load; can be null
Space API Implementation Example:

        public Object load(Object key) {

                Object values[] = null;
                Object loaded_object = null;

                try {
                        Object myObject = getObject((IGSEntry) key);
                        int keyValue = 0;
                        if (myObject instanceof Person) {
                                Person person = null;
                                // This is the template we got - must have ID
                                person = (Person) myObject;
                                // getting primary key value - another option could be via
                                // ((IGSEntry) key).getPrimaryKeyName()
                                keyValue = person.getId().intValue();
                                // Constructing the query
                                Connection con = getConnection();

                                PreparedStatement stP = con.prepareStatement(
                                                "select * from  "
                                                                + tableNames.get(((IGSEntry) key)
                                                                                .getClassName()) + " where ID = ? ");
                                stP.setInt(1, keyValue);

                                ResultSet rs = stP.executeQuery();
                                int sz = rs.getMetaData().getColumnCount();

                                values = new Object[sz];
                                while (rs.next()) {
                                        for (int i = 0; i < sz; i++) {
                                                values[i] = rs.getObject(i + 1);

                                        }
                                        // here we construct the loaded Person object
                                        loaded_object = new Person(String.valueOf(values[0]),
                                                        String.valueOf(values[1]), Integer.valueOf(String
                                                                        .valueOf(values[2])));
                                        break;
                                }
                                rs.close();
                                rs.close();
                                con.close();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
                return getConvertor().toIGSEntry(loaded_object);
        }


Map API Implementation Example:
          
        public Object load(Object key) {

                Object values[] = null;
                Person loaded_person = null;
                Connection con = null;
                try {
                        con = getConnection();
                        int keyValue = 0;
                        Map.Entry mEntry = ((IGSEntry) key).getMapEntry();

                        if (mEntry != null) {
                                keyValue = ((Integer) mEntry.getKey()).intValue();
                        } else {
                                keyValue = ((Integer) getId((IGSEntry) key)).intValue();
                        }

                        PreparedStatement stP = con.prepareStatement("select * from  " + tableNames.get(Person.class.getName())
                                                        + " where ID = ? ");
                        stP.setInt(1, keyValue);

                        ResultSet rs = stP.executeQuery();
                        int sz = rs.getMetaData().getColumnCount();

                        values = new Object[sz];
                        while (rs.next()) {
                                for (int i = 0; i < sz; i++) {
                                        values[i] = rs.getObject(i + 1);
                                }
                                // HERE WE MAP table row to Person object
                                loaded_person = new Person(String.valueOf(values[0]), String
                                                .valueOf(values[1]), Integer.valueOf(String
                                                .valueOf(values[2])));
                                break;
                        }
                        rs.close();
                        con.close();

                } catch (Exception e) {
                        try {
                                con.close();
                        } catch (SQLException e1) {
                                e1.printStackTrace();
                        }
                        e.printStackTrace();
                }
                return getConvertor().toIGSEntry(loaded_person);
        }

                

loadAll

public abstract Map loadAll(Collection keys)
Deprecated. 
Description copied from interface: CacheLoader
Loads multiple values from secondary data storage in case they are not in cache.

Specified by:
loadAll in interface CacheLoader
Parameters:
keys - Collection of (CacheQuery) Keys to load by.
Returns:
Map of key-value pairs loaded from data storage.
Space API Implementation Example

        public Map loadAll(Collection keys) {
                HashMap map = new HashMap();
                Iterator keyIter = keys.iterator();
                while (keyIter.hasNext()) {
                        CacheQuery exprationEntry = (CacheQuery) keyIter.next();
                        Object query = exprationEntry.getQuery();
                        String querystr = null;
                        // null template
                        if (query instanceof IGSEntry) {
                                IGSEntry igsentry = (IGSEntry) query;
                                if (!tableNames.containsKey(igsentry.getClassName())) {
                                        System.out.println("Do not have mapping for class "
                                                        + igsentry.getClassName());

                                        if (igsentry.getClassName().equals("java.lang.Object")) {
                                                System.out.println("HERE YOU CAN LOAD DATA INTO THE SPACE WHEN IT IS STARTED!");
                                        }
                                        // return empty map - indicates no objects loaded from database
                                        return map;
                                }
                                querystr = "select * from "
                                                + tableNames.get(igsentry.getClassName());
                        }
                        // non null template or SQLQuery used
                        if (query instanceof SQLQuery) {
                                SQLQuery sqlquery = (SQLQuery) query;
                                if (!tableNames.containsKey(sqlquery.getClassName())) {
                                        System.out.println("Do not have mapping for class "
                                                        + sqlquery.getClassName());
                                        // return empty map - indicates no objects loaded from database
                                        return map;
                                }
                                querystr = "select * from "
                                                + tableNames.get(sqlquery.getClassName());
                                querystr += " where " + sqlquery.getQuery();
                        }
                        try {
                                Connection con = getConnection();
                                PreparedStatement stP = con.prepareStatement(
                                                querystr);

                                ResultSet rs = stP.executeQuery();
                                IGSEntry value = null;
                                while (rs.next()) {
                                        Integer id = new Integer(rs.getInt(3));
                                        Person person = new Person(rs.getString(1),
                                                        rs.getString(2), id);

                                        value = getConvertor().toIGSEntry(person);
                                        map.put(value, value);
                                }
                                rs.close();
                                con.close();
                        } catch (Exception e) {
                                e.printStackTrace();
                                throw new RuntimeException(e);
                        }
                }

                return map;
        }


Map API Implementation Example

        public Map loadAll(Collection keys) {
                HashMap map = new HashMap();

                Iterator keyIter = keys.iterator();
                while (keyIter.hasNext()) {
                        CacheQuery cacheQuery = (CacheQuery) keyIter.next();
                        Object query = cacheQuery.getQuery();
                        String sqlstr = null;
                        sqlstr = "select * from Person";
                        Map.Entry mapentry = null;
                        if (query instanceof Map.Entry)
                        {
                                mapentry  = (Map.Entry)query;
                                sqlstr += " where id = " + Integer.valueOf(mapentry.getKey().toString()).intValue();
                        }
                        else if (query instanceof IGSEntry)
                        {
                                // All entries - for example IMap.values() call 
                                IGSEntry igsentry = (IGSEntry)query;
                                if (igsentry.getClassName().equals("java.lang.Object"))
                                {
                                        return map;
                                }
                        }
                        else if (query instanceof SQLQuery)
                        {
                                SQLQuery sqlquery = (SQLQuery)query;
                                if (sqlquery.getClassName().equals("java.lang.Object"))
                                {
                                        return map;
                                }
                                sqlstr += " where " + sqlquery.getQuery();
                        }
                        PreparedStatement stP =null; 
                        ResultSet rs = null;
                        Connection con = null;
                        try {
                                con = getConnection();
                                stP = con.prepareStatement(sqlstr);
                                rs = stP.executeQuery();
                                while (rs.next()) {
                                        Integer id = new Integer(rs.getInt(3));
                                        Person person = new Person(rs.getString(1),
                                                        rs.getString(2), id);
                                        IGSEntry  igsEntry = getConvertor().toIGSEntry(person);
                                        map.put( igsEntry ,  igsEntry );
                                }
                                rs.close();
                                con.close();
                        } catch (Exception e) {
                                try {
                                        rs.close();
                                } catch (SQLException e1) {
                                        e1.printStackTrace();
                                }
                                try {
                                        con.close();
                                } catch (SQLException e1) {
                                        e1.printStackTrace();
                                }
                                e.printStackTrace();
                                throw new RuntimeException(e);
                        }
                }
                return map;
        }