Wednesday, September 28, 2011

Re: Request for help with Request Factory and Spring MVC integration

>> http://jsinghfoss.wordpress.com/2011/08/10/gwt-2-2-0-requestfactory-spring-3-0-x-integration/ .

The solution presented in the above blog, suggests RF integration using DispatcherServlet, but there actually there is another way of integrating directly with your spring container. 

I'll put the solution here with as little code as possible. This will demonstrate

  1. BaseEntity (All entities extend this)
  2. Customer (Entity)
  3. CustomerProxy (EntityProxy/DTO)
  4. EntityLocator (Common to all Entities; a SpringBean)
  5. ServiceLocator (Common to all service; a SpringBean)
  6. AppServiceLayerDecorator (Extends ServiceLayerDecorator/enables RF to locate your EntityLocator which is a spring bean from the spring context)
  7. AppRequestFactoryServlet (To enable RF to use your AppServiceLayerDecorator)
  8. web.xml (To enable RF to use your AppRequestFactoryServlet instead of the default)
Note: I'm picking this from a larger project, which uses GWT 2.3/Hibernate/Postgres/Spring (without DispatcherServlet), if you are using 2.4 the CustomerProxy also could be made to extend BaseProxy on the client-side, I didn't do thtat at that time. Perhaps you can try..

BaseEntity


@MappedSuperclass
public abstract class Base implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;

@Version
private Integer version;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}
}


Customer

@Entity
public class Customer extends Base {

String name;

Integer age;

// ...

}


CustomerProxy (EntityProxy/DTO)


@ProxyFor(value = Customer.class, locator = EntityLocator.class)
public interface CustomerProxy extends EntityProxy {

public Long getId();

public void setId(Long id);

public Integer getVersion();

public void setVersion(Integer version);

public String getName();

public void setName(String name);

public Integer getAge();

public void setAge(Integer age);

// ..
}


EntityLocator

@Service("entityLocator")
public class EntityLocator extends Locator<Base, Long> {

@PersistenceContext
EntityManager entityManager;

@Override
public Base create(Class<? extends Base> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

@Override
public Base find(Class<? extends Base> clazz, Long id) {
return entityManager.find(clazz, id);
}

@Override
public Class<Base> getDomainType() {
throw new UnsupportedOperationException();
}

@Override
public Long getId(Base domainObject) {
return domainObject.getId();
}

@Override
public Class<Long> getIdType() {
return Long.class;
}

@Override
public Object getVersion(Base domainObject) {
return domainObject.getVersion();
}
}


ServiceLocator 


public class AppServiceLocator implements ServiceLocator {

@Override
public Object getInstance(Class<?> clazz) {

HttpServletRequest request = RequestFactoryServlet
.getThreadLocalRequest();
ApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(request.getSession()
.getServletContext());
return context.getBean(clazz);
}
}

AppServiceLayerDecorator 

public class AppServiceLayerDecorator extends ServiceLayerDecorator {

@Override
public <T extends Locator<?, ?>> T createLocator(Class<T> clazz) {

HttpServletRequest request = RequestFactoryServlet
.getThreadLocalRequest();
ApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(request.getSession()
.getServletContext());

log.info("createLocator: " + clazz.getName());

return context.getBean(clazz);
}
// ...
}

AppRequestFactoryServlet 

public class AppRequestFactoryServlet extends RequestFactoryServlet {

private static final long serialVersionUID = -3364570784675688621L;

public AppRequestFactoryServlet() {
this(new DefaultExceptionHandler(), new AppServiceLayerDecorator());
}

public AppRequestFactoryServlet(ExceptionHandler exceptionHandler,
ServiceLayerDecorator... serviceDecorators) {
super(exceptionHandler, serviceDecorators);
}

}

web.xml


<servlet>
<servlet-name>requestFactoryServlet</servlet-name>
<servlet-class>in.verse.calldesk.server.AppRequestFactoryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>requestFactoryServlet</servlet-name>
<url-pattern>/gwtRequest</url-pattern>
</servlet-mapping>

Note: Once you have got the above, you can create your DAO layer, and make them spring beans, and create corresponding RF RequestContext on the client, and use them using your AppServiceLocator!

This is a bigger concept, I've tried to put it all here. Hope this helps!

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/3Z_FbP03pYIJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment