com.j_spaces.javax.cache
Interface CacheIteratorFactory

All Known Implementing Classes:
HibernateCacheLoaderImpl, HibernateCacheStoreImpl

Deprecated.

public interface CacheIteratorFactory

This interface creates an iterator of matching candidates to be used by the following operations: read, readMultiple, take or takeMultiple using null template , non null template and SQLQuery - where several objects can match a given template. When the space is started the loadAll/CacheIteratorFactory.iterator() is called with IGSEntry that its class name is java.lang.Object. This allows you to load your data into the space before is it available for clients.

Implementation Example:

        public CacheIterator iterator(CacheQuery cacheQuery) {
                Object query = cacheQuery.getQuery();
                String classname = null;
                String querystr = null;
                // null template
                if (query instanceof IGSEntry) {
                        IGSEntry igsentry = (IGSEntry) query;
                        classname = igsentry.getClassName();

                        if (classname.equals("java.lang.Object")) {
                                System.out.println("HERE YOU CAN LOAD DATA INTO THE SPACE WHEN IT IS STARTED!");
                        }

                        if (!tableNames.containsKey(igsentry.getClassName())) {
                                System.out.println("Do not have mapping for class "
                                                + igsentry.getClassName());
                                return null;
                        }
                        querystr = "select * from "
                                        + tableNames.get(igsentry.getClassName());
                }

                //      null template or SQLQuery used
                else if (query instanceof SQLQuery) {
                        SQLQuery sqlquery = (SQLQuery) query;
                        classname = sqlquery.getClassName();
                        if (!tableNames.containsKey(sqlquery.getClassName())) {
                                System.out.println("Do not have mapping for class "
                                                + sqlquery.getClassName());
                                return null;
                        }
                        querystr = "select * from "
                                        + tableNames.get(sqlquery.getClassName());
                        querystr += " where " + sqlquery.getQuery();
                }
                PreparedStatement stP = null;
                ResultSet rs = null;
                Connection con =null; 
                try {
                        con = getConnection();
                        stP = con.prepareStatement(querystr);
                        rs = stP.executeQuery();
                } catch (SQLException e) {
                        throw new RuntimeException(e);
                }
                return new CacheLoaderIterator(con , rs, classname);
        };

        // The CacheLoaderIterator used by the CacheIteratorFactory.iterator
        public class CacheLoaderIterator implements CacheIterator {
                String className = null;
                ResultSet result = null;
                Connection con = null;

                public CacheLoaderIterator(Connection con , ResultSet result, String className) {
                        this.result = result;
                        this.className = className;
                        this.con = con;
                }

                public boolean hasNext() {
                        try {

                                return result.next();
                        } catch (SQLException e) {
                                e.printStackTrace();
                                return true;
                        }
                }

                public Object next() {
                        try {
                                Object obj = null;
                                if (className.equals(Person.class.getName())) {
                                        Integer id = new Integer(result.getInt(3));
                                        obj = new Person(result.getString(1), result.getString(2),
                                                        id);
                                }
                                IGSEntry value = getConvertor().toIGSEntry(obj);
                                return value;
                        } catch (Exception e) {
                                return null;
                        }
                }

                public void remove() {
                        throw new NotImplementedException();
                }

                public void close()
                {
                        try {
                                result.close();
                                con.close();
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
                }
        }

Since:
5.1

Method Summary
 CacheIterator iterator(CacheQuery query)
          Deprecated. Returns an instance of Iterator which holds a matching candidates.
 

Method Detail

iterator

CacheIterator iterator(CacheQuery query)
Deprecated. 
Returns an instance of Iterator which holds a matching candidates.

Parameters:
query - CacheQuery encapsulating the template or query data.
Returns:
CacheIterator holds the matching candidates.