A.J.

Someday somehow someone is going to steal your carbon.

Archive for the ‘java’ Category

Uploadify and Stripes

with 2 comments

Uploadify is a pretty cool plugin for jquery that uses flash for a bulk uploader. The only sucky thing is the examples are for php.

So I wrote one for stripes:

git://github.com/ayax79/stripes-uploadify.git

Written by Jack

July 1, 2009 at 2:42 pm

Posted in java

Tagged with , , , ,

JMock Map Matcher Example

leave a comment »

Quick example of how to use jmock matchers with maps.

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

// Static imports for the IsMapContaing matchers
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.collection.IsMapContaining.hasKey;

import org.hamcrest.collection.IsMapContaining;
import static org.hamcrest.core.AllOf.allOf;
import org.hamcrest.core.AllOf;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Map;
import java.util.HashMap;

@RunWith(JMock.class)
public class DefaultReportDaoUnitTest
{

    Mockery mockery = new JUnit4Mockery();

    @Test
    public void testFoo() {

        final MyStuff mine = mockery.mock(MyStuff.class, "mine");

        mockery.checking(new Expectations()
        {{

            // I usually seperate this from being inline
            // so that I can get the generic type right easier
            Matcher<Map<String, ?>> mapMatcher =
                AllOf.<Map<String, ?>>allOf(
                        hasEntry("foo", "one"),
                        hasEntry("bar", "two"),
                        hasKey("baz")
                );        

            // Use the map mapmatcher we defined
            // expect one call of 
            oneOf(mine).doSomething(with(mapMatcher));                                                        
        }};

        Map<String,String> map = new HashMap<String,String>(3);
        map.put("foo", "one");
        map.put("bar", "two");
        map.put("baz", "three");

        mine.doSometing(map);                

    }        

}        

Written by Jack

February 19, 2009 at 5:41 pm

Posted in java

Making Spring MVC and CXF play well together

with 4 comments

I have found the spring mvc and CXF really don’t play very well together. When using spring MVC is you are pretty much stuck using the spring DispatcherServlet which for some odd reason doesn’t well with the spring ContextLoaderListener and wants to initialize the config files itself.

The problem with this that if you use the DispatcherServlet to initialize the files then the CXFServlet isn’t able to find the context while it is initializing.

The only way I was able to get this to work out of the box was to list my config files in both places

<!-- inside the web.xml -->
<!-- don't do this, duplicates bean instances -->

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myconfig.xml</param-value>
</context-param>
	<listener>
	<listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <description>Spring Configuration Files</description>
<param-name>contextConfigLocation</param-name>
<param-value>
            /WEB-INF/myconfig.xml
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

The problem is that it will duplicate all your bean instances.

My solution is to just create a spring controller that handles the CXF requests. This was relatively easy to do using CXFServlet and AbstractCXFServlet as examples.

The benefit of doing this is that CXFController we create can be directly initialized with the ApplicationContext that it is created in.

package com.foo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.Bus;
import org.apache.cxf.BusException;
import org.apache.cxf.BusFactory;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.resource.ResourceManager;
import org.apache.cxf.transport.DestinationFactory;
import org.apache.cxf.transport.DestinationFactoryManager;
import org.apache.cxf.transport.servlet.ServletContextResourceResolver;
import org.apache.cxf.transport.servlet.ServletController;
import org.apache.cxf.transport.servlet.ServletTransportFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.util.Enumeration;

public class CXFController implements Controller, ApplicationContextAware
{

    private static final Log log = LogFactory.getLog(CXFController.class);

    protected Bus bus;
    protected ServletTransportFactory servletTransportFactory;
    protected ServletController controller;
    protected WebApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        this.applicationContext = (WebApplicationContext) applicationContext;
    }

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        try {
            BusFactory.setThreadDefaultBus(bus);
            controller.invoke(request, response);
        }
        catch (Exception e) {
            log.error(e.getMessage(),e);
            throw e;
        }
        finally {
            BusFactory.setThreadDefaultBus(null);
        }
        return null;
    }

    public void init()
    {
        bus = new SpringBusFactory(applicationContext).createBus();

        ResourceManager resourceManager = bus.getExtension(ResourceManager.class);
        resourceManager.addResourceResolver(new ServletContextResourceResolver(
                applicationContext.getServletContext()));

        replaceDestinationFactory();

        // Set up the ServletController
        controller = new ServletController(servletTransportFactory,
                SERVLET_CONFIG,
                applicationContext.getServletContext(),
                bus);
    }

    public void destroy() {
        bus.shutdown(true);
    }

    protected void replaceDestinationFactory()
    {

        DestinationFactoryManager dfm = bus.getExtension(DestinationFactoryManager.class);
        try
        {
            DestinationFactory df = dfm
                    .getDestinationFactory("http://cxf.apache.org/transports/http/configuration");
            if (df instanceof ServletTransportFactory)
            {
                servletTransportFactory = (ServletTransportFactory) df;
                log.info("DESTIONFACTORY_ALREADY_REGISTERED");
                return;
            }
        }
        catch (BusException e)
        {
            // why are we throwing a busexception if the DF isn't found?
        }

        DestinationFactory factory = createServletTransportFactory();

        for (String s : factory.getTransportIds())
        {
            registerTransport(factory, s);
        }
        log.info("REPLACED_HTTP_DESTIONFACTORY");
    }

    private void registerTransport(DestinationFactory factory, String namespace) {
        bus.getExtension(DestinationFactoryManager.class).registerDestinationFactory(namespace, factory);
    }

    protected DestinationFactory createServletTransportFactory() {
        if (servletTransportFactory == null) {
            servletTransportFactory = new ServletTransportFactory(bus);
        }
        return servletTransportFactory;
    }

    /**
     * Annoying hack...
     *
     * The ServletController class uses the servlet config to get other init parameters. This
     * mostly just prevents ServletController from throwing NullPointerExceptions
     *
     * We don't care about any of them but here is teh list if we decide we do:
     * hide-service-list-page
     * disable-address-updates
     * base-address
     * service-list-stylesheet
     */
    public static ServletConfig SERVLET_CONFIG = new ServletConfig()
    {

        public String getServletName()
        {
            return null;
        }

        public ServletContext getServletContext()
        {
            return null;
        }

        public String getInitParameter(String name)
        {
            return null;
        }

        public Enumeration getInitParameterNames()
        {
            return null;
        }
    };

}

There are three caveats to the examples above:
1. In CXFServlet it uses the ServletConfig to find specific init parameters, this can be done still with a little bit of effort by passing them in through the spring config and then use the SERVLET_CONFIG instance of ServletConfig in the example above to provide the values to the ServletController instance.

2. I don’t need the multiple cxf bus support so I didn’t port it over.

3. I didn’t add reloading support to my controller.

Finally you just need to configure spring to use this new controller:

<bean name="cxfController" class="com.foo.CXFController"
          init-method="init"
          destroy-method="destroy"
          lazy-init="true"/>

    <bean id="beanMappings"
          class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"
          p:order="0" />

    <bean id="urlMappings"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
          p:order="1">
<property name="urlMap">
            <util:map>
                <entry key="/cxf*/**" value-ref="cxfController"/>
            </util:map>
        </property>
    </bean>

It is very important to note that the cxfController is lazy initialized. This allows all services to be loaded before the CXFController does. This could probably also be done by making the CXFController an application listener and waiting until the spring context was fully initialized before initializing, but it was quicker to just make it lazy initialized 🙂

Written by Jack

February 19, 2009 at 4:32 pm

Posted in java

Tagged with , , , ,

Using custom hessian Serializers with Spring

with 2 comments

In my last post I showed an example of a custom hessian serializer and deserializer. In order to make this all work with spring some steps need to be performed.

Normally with hessian you add your custom SerializerFactory to the main hessian SerializerFactory via the addFactory method. This is particularly spring friendly. You will need to create a subclass of SerializerFactory like the following:

package com.foo;

import com.caucho.hessian.io.SerializerFactory;
import com.caucho.hessian.io.AbstractSerializerFactory;

import java.util.List;

public class SpringExtensibleSerializerFactory extends SerializerFactory
{

    public void setSerializerFactories(List<AbstractSerializerFactory> factories) {
        for (AbstractSerializerFactory factory : factories)
        {
            addFactory(factory);
        }
    }

}

This will allow you to add a list of custom SerializerFactory to the main hessian SerializerFactory. This SerializerFactory will need to be setup on both the client and server.

<bean id="hessianSerializerFactory"
          class="com.foo.SpringExtensibleSerializerFactory">
<property name="serializerFactories">
        <util:list>
             <bean class="com.foo.BigIntegerSerializerFactory"/>
        </util:list>
    </property>
 </bean>

Next on the server side I create an abstract hessian exporter bean.

<bean id="baseHessianService" class="org.springframework.remoting.caucho.HessianServiceExporter" abstract="true">
<property name="serializerFactory" ref="hessianSerializerFactory" />
</bean>

Services can then be exposed simply by extended the baseHessianService

<bean id="myHessianService" parent="baseHessianProxyFactory">
<property name="serviceUrl"
            value="http://foo.com/hessian/MyHessianService" />
<property name="serviceInterface"
            value="com.foo.MyHessianService" />
</bean>

On the client side create an abstract bean for HessianProxyFactoryBean with an instance of the hessianSerializerFactory described above.

<bean id="baseHessianProxyFactory" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"
          abstract="true" >
<property name="serializerFactory" ref="hessianSerializerFactory" />
</bean>

Services can then be consumed by extending this abstract bean

<bean id="myHessianServiceProxy" parent="baseHessianProxyFactory">
<property name="serviceUrl"
            value="http://foo.com/hessian/MyHessianService" />
<property name="serviceInterface"
            value="com.foo.MyHessianService" />
</bean>

Written by Jack

February 12, 2009 at 9:20 am

Posted in java

Tagged with , ,

BigInteger and hessian.

leave a comment »

Out of the box hessian does not have support for java.math.BigInteger.
It easy to add custom hessian serializers to hessian to extend it to handle cases like these.

The deserializer:


package com.foo.hessian;

import com.caucho.hessian.io.AbstractDeserializer;
import com.caucho.hessian.io.AbstractHessianInput;

import java.io.IOException;
import java.math.BigInteger;

public class BigIntegerDeSerializer extends AbstractDeserializer
{

    public Class getType()
    {
        return BigInteger.class;
    }

    public Object readMap(AbstractHessianInput in)
            throws IOException
    {
        int ref = in.addRef(null);

        String initValue = null;

        while (!in.isEnd())
        {
            String key = in.readString();

            if (key.equals("value"))
                initValue = in.readString();
            else
                in.readString();
        }

        in.readMapEnd();

        Object value = new BigInteger(initValue);

        in.setRef(ref, value);

        return value;
    }

    public Object readObject(AbstractHessianInput in, String[] fieldNames)
            throws IOException
    {
        int ref = in.addRef(null);

        String initValue = null;

        for (String key : fieldNames)
        {
            if (key.equals("value"))
                initValue = in.readString();
            else
                in.readObject();
        }

        Object value = new BigInteger(initValue);

        in.setRef(ref, value);

        return value;
    }

}

The serializer:

package com.foo.hessian;

import com.caucho.hessian.io.AbstractSerializer;
import com.caucho.hessian.io.AbstractHessianOutput;

import java.io.IOException;
import java.math.BigInteger;

public class BigIntegerSerializer extends AbstractSerializer
{
    public void writeObject(Object obj, AbstractHessianOutput out) throws IOException
    {

        if (obj == null)
            out.writeNull();
        else
        {
            Class cl = obj.getClass();

            if (out.addRef(obj))
                return;

            int ref = out.writeObjectBegin(cl.getName());

            BigInteger bi = (BigInteger) obj;

            if (ref < -1)             {                 out.writeString("value");                 out.writeString(bi.toString());                 out.writeMapEnd();             }             else             {                 if (ref == -1)                 {                     out.writeInt(1);                     out.writeString("value");                     out.writeObjectBegin(cl.getName());                 }                 out.writeString(bi.toString());             }         }     } } [/sourcecode] Next you will need a serializer factory for the BigInteger Serializer/Deserializer [sourcecode language='java'] public class BigIntegerSerializerFactory extends AbstractSerializerFactory { public Serializer getSerializer(Class cl) throws HessianProtocolException { if (BigInteger.class.isAssignableFrom(cl)) { return new BigIntegerSerializer(); } return null; } public Deserializer getDeserializer(Class cl) throws HessianProtocolException { if (BigInteger.class.isAssignableFrom(cl)) { return new BigIntegerDeSerializer(); } return null; } } [/sourcecode] Finally add this to the main via the SerializerFactory#addFactory method. In the next post I will show how to tie this all into spring.

Written by Jack

February 11, 2009 at 3:56 pm

Posted in java

Tagged with , ,

Spring configuration of DWR.

with 10 comments

I have found there really aren’t any great guides for getting DWR configured with spring. Here is my experiences.
Prerequisites:

  • Assuming some previous knowledge of DWR and spring.
  • Spring is configured and working.
  • DWR has been added the WEB-INF/lib.

1. Add DWR to the web.xml

Instead of using the DwrServlet, make sure you use the SpringDwrServlet:

<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <display-name>DWR Servlet</display-name>

        <description>Direct Web Remoter Servlet</description>
        <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
        <init-param>
<param-name>logLevel</param-name>
<param-value>INFO</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet></tt>

<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
       <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping><
&#91;/sourcecode&#93;
<h4>2. Next, add the dwr namespace and schema to your spring configuration file:</h4>
The namespace is xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
The schemaLocation is http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
       xsi:schemaLocation="

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd

http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd"

       default-autowire="no" default-lazy-init="false" default-init-method="init" default-destroy-method="destroy"></tt>

</beans>

3. Configure Spring creators.

There are two options for doing this. You can directly expose spring beans:

<bean id="dwrWatchesBean" class="com.jivesoftware.community.action.WatchesAction" >

    <dwr:remote javascript="Watches" />
<property name="communityManagerImpl" ref="communityManagerImpl" />
<property name="forumManagerImpl" ref="forumManagerImpl" />
<property name="watchManagerImpl" ref="watchManagerImpl" />
<property name="documentManagerImpl" ref="documentManagerImpl" />
<property name="blogManagerImpl" ref="blogManagerImpl" />
<property name="userManagerImpl" ref="userManagerImpl" />
</bean>

The dwr:remote element is the element that will expose the bean to dwr. In my opinion this is the best approach as it allows you to wire you dependencies like any other spring object.

Secondly you can configure them in a dwr:configuration much in the same as the standard dwr.xml file works:

<dwr:configuration>
    <dwr:create type="new" javascript="Watches"     class="com.jivesoftware.community.action.WatchesAction" />
</dwr:configuration>

4. Configure spring convertors

The spring configurators must be configured inside of a dwr:configuration element in spring.

<dwr:configuration>
    <dwr:convert type="bean" class="com.jivesoftware.community.Watch" />
</dwr:configuration>

5. Other important elements

dwr:include : Used to include specific methods. Can be used with dwr:convert, dwr:create, or dwr:remote element
dwr:exclude : Used to exclude specific methods. Can be used with dwr:convert, dwr:create, or dwr:remote element
dwr:init : Optional element that will allow you to specify custom creator types and converter types.
dwr:converter : Specifies a custom converter type, must be used within dwr:init.
dwr:create : Specifies a custom creator type, must be used within a dwr:init.
dwr:signature : Used to specify signatures much like the like element in the standard dwr.xml.

6. Full Example

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
       xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd"
       default-autowire="no" default-lazy-init="false" default-init-method="init" default-destroy-method="destroy"></tt>

    <bean id="dwrWatchesBean" class="com.jivesoftware.community.action.WatchesAction" >

        <dwr:remote javascript="Watches" />
<property name="communityManagerImpl" ref="communityManagerImpl" />
<property name="forumManagerImpl" ref="forumManagerImpl" />
<property name="watchManagerImpl" ref="watchManagerImpl" />
<property name="documentManagerImpl" ref="documentManagerImpl" />
<property name="blogManagerImpl" ref="blogManagerImpl" />
<property name="userManagerImpl" ref="userManagerImpl" />
         </bean>

        <dwr:configuration>
            <dwr:convert class="com.jivesoftware.community.DraftImpl" type="bean" />
        <dwr:create type="new" javascript="ImagePicker" class="com.jivesoftware.community.action.ImagePicker" >
            <dwr:include method="getImageByName" />
            <dwr:include method="getImageDetailsByName" />
            <dwr:include method="getImageDetailsByURL" />
            <dwr:include method="removeImage" />
        </dwr:create>

        <dwr:convert class="com.jivesoftware.base.User" type="bean" >
            <dwr:include method="username"/>
            <dwr:include method="name"/>
            <dwr:include method="ID"/>
        </dwr:convert>
        <dwr:convert class="com.jivesoftware.community.action.LDAPGroupBean" type="bean" />

        <dwr:signatures>
           <!&#91;CDATA&#91;
          import java.util.Map;
          import com.jivesoftware.community.DraftImpl;
          DraftImpl.setProperties(Map<String, String> properties);
          DraftImpl.setImageIDs(List<Long> imageIDs);
          &#93;&#93;>
        </dwr:signatures>

    </dwr:configuration>
</beans>

CAVEATS

  • Do not attempt to use DwrSpringServlet with org.springframework.web.servlet.mvc.ServletWrappingController. DwrSpringServlet expects the spring context to be fully initialized before it attempts to initialize.

Written by Jack

December 4, 2007 at 10:24 am

Posted in java

Tagged with ,

Adding Scrollable Result Set Support to JDBC Spring templates

with 9 comments

At jive there are many times we want to use scrollable result sets in clearspace. We use this functionality for things like pagination. Unfortunately Spring’s JDBC template support lacks this currently. However Spring is flexible enough to allow us to extend it fairly easily.

The first thing we want to do is to make our own template object, later we will add our custom methods to it:
// implement SimpleJdbcOperations to provide all the standard
// method available in SimpleJdbcOperations
public class JiveJdbcTemplate implements SimpleJdbcOperations {

	// For most operations we will delegate to the spring jdbc template
	private SimpleJdbcTemplate template;

	public JiveJdbcTemplate(SimpleJdbcTemplate template) {
		this.template = template;
	}

	// delegate SimpleJdbcOperation methods below....

}

Next we will need to make a custom PreparedStatementCreator that will created a scrollable
prepared statement:

public static class ScrollablePreparedStatementCreator implements PreparedStatementCreator {

        private String sql;
        private Object[] args;

        public ScrollablePreparedStatementCreator(String sql, Object... args) {
            this.args = args;
            this.sql = sql;
        }

        public PreparedStatement createPreparedStatement(Connection connection)
                throws SQLException
        {
            PreparedStatement ps = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            if (this.args != null) {
                for (int i = 0; i &lt; this.args.length; i++) {
                    Object arg = this.args[i];
                    if (arg instanceof SqlParameterValue) {
                        SqlParameterValue paramValue = (SqlParameterValue) arg;
                        StatementCreatorUtils
                                .setParameterValue(ps, i + 1, paramValue, paramValue.getValue());
                    }
                    else {
                        StatementCreatorUtils
                                .setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg);
                    }
                }
            }

            return ps;
        }
    }

Next we will need to create a ResultSetExtract that is able to take advantage of the ResultSet#FetchSize and scroll features.

public static class ScrollableResultSetExtractor implements ResultSetExtractor {

        private int startIndex;
        private int numResults;
        private RowMapper mapper;

        public ScrollableResultSetExtractor(int startIndex, int numResults, RowMapper mapper) {
            this.startIndex = startIndex;
            this.numResults = numResults;
            this.mapper = mapper;
        }

        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {

            // Set the fetch size and search results for the result set
 		   rs.setFetchSize(numResults);
            rs.setFetchDirection(ResultSet.FETCH_FORWARD);
            rs.absolute(startIndex);

            ArrayList&lt;Object&gt; list = new ArrayList&lt;Object&gt;();
            for (int i = 0; i &lt; numResults; i++) {
                if (rs.next()) {
                    Object o = mapper.mapRow(rs, i);
                    list.add(o);
                }
                else {
                    break;
                }
            }

            return list;
        }
    }

Finally we put it all together and add it all together by adding the following method to our custom template
created in the first step:

/**
 * Setups a srollable result set and only returns the speciied amount of results.
 *
 * @param sql The sql statement
 * @param startIndex the starting index of the result set
 * @param numResults the number of results to return.
 * @param mapper The row mapper to use.
 * @param args Arguments for the prepared statement.
 * @return The scrolled results.
 */
@SuppressWarnings({"UnnecessaryLocalVariable", "unchecked"})
public &lt;T&gt; List&lt;T&gt; queryScrollable(String sql, int startIndex, int numResults,
        ParameterizedRowMapper&lt;T&gt; mapper, Object... args)
{
    JdbcOperations jdbcOperations = template.getJdbcOperations();

    ScrollablePreparedStatementCreator creator = new ScrollablePreparedStatementCreator(sql,
            args);

    ScrollableResultSetExtractor extractor = new ScrollableResultSetExtractor(startIndex,
            numResults, mapper);

    List&lt;T&gt; list = (List&lt;T&gt;) jdbcOperations.query(creator, extractor);
    return list;
}

Written by Jack

September 21, 2007 at 5:29 pm

Posted in java

Tagged with , ,