Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Java Flex Spring JPA BlazeDS Integration

Saturday, May 14, 2011 Posted by Sanka Senevirathna 1 comments

This sample application was develop by me for ceylon electricity board(CEB).application alow user to

· Insert/enter data

· Retrive/view data

· Edit/upate data and

· upload bulk data such as excel sheet

· Reporting & graphs

· Loging system(spring security)

http://dl.dropbox.com/u/7375335/sample%20ceb/TestApplication.swf

Labels: , , , ,

Spring BlazeDS Integration

Tuesday, June 1, 2010 Posted by Sanka Senevirathna 0 comments

Spring BlazeDS Integration






1. Configuring and Using the BlazeDS


MessageBroker with Spring



MessageBroker is the heart of the Spring BlazeDS Integration.
When HTTP messages come from Flex client will be routed through the
Spring DispatcherServlet to the Spring-managed MessageBroker.

There is no need to configure the BlazeDS MessageBrokerServlet when using the
Spring-managed MessageBroker.





2. Configuring the Spring DispatcherServlet


web.xml is the heart of the j2ee web project.So we have to configure it because
each an every request is map to web.xml.

The DispatcherServlet must be configured in web.xml to bootstrap a Spring
WebApplicationContext. For example:





Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
/WEB-INF/config/web-application-config.xml

1



3. Configuring the MessageBroker in Spring


A simplified Spring XML config namespace is provided for configuring the MessageBroker in
your WebApplicationContext. To use the namespace support you must add the schema location
in your Spring XML config files. A typical config will look something like the following:




...



Following XML config namespace tags makes the Spring BlazeDS Integration configuration tags available under the flex
namespace in your configuration files. The above setup will be assumed for the rest of the
configuration examples to follow. For the full detail of every attribute and tag available in the



config namespace, be sure to refer to the spring-flex-1.0.xsd as every element and attribute is
fully documented there. Using an XSD-aware XML editor such as the one in Eclipse should
bring up the documentation automatically as you type.

At a minimum, the MessageBrokerFactoryBean must be configured as a bean in your
Spring WebApplicationContext in order to bootstrap the MessageBroker, along with a
MessageBrokerHandlerAdapter and an appropriate HandlerMapping (usually a
SimpleUrlHandlerMapping) to route incoming requests to the Spring-managed
MessageBroker.

These beans will be registered automatically by using the provided message-broker tag in
your bean definition file. For example, in its simplest form:






This will set up the MessageBroker and necessary supporting infrastructure using sensible
defaults. The defaults can be overriden using the provided attributes of the message-broker
tag and its associated child elements. For example, the default location of the BlazeDS XML
configuration file (/WEB-INF/flex/services-config.xml) can be overridden using the
services-config-path attribute. The MessageBrokerFactoryBean uses Spring's
ResourceLoader abstraction, so that typical Spring resource paths may be used. For
example, to load the configuration from the application's classpath:



The equivalent MessageBrokerFactoryBean definition using vanilla Spring configuration
would be:








Note especially that with the message-broker tag, it is not necessary to assign a custom id to
the MessageBroker, and it is in fact discouraged so that you won't have to continually reference
it later. The only reason you would ever need to provide a custom id is if you were bootstrapping
more than one MessageBroker in the same WebApplicationContext.
Labels: , , ,

SBI Simple Code Example

Friday, April 2, 2010 Posted by Sanka Senevirathna 0 comments
web-application-config.xml















































/WEB-INF/classes/META-INF/persistence.xml













web-security-config.xml
































flex-servlet.xml




















/*=_messageBroker

















web.xml






SBI template Configured by Sanka
SBI template Configured by Sanka




contextConfigLocation
/WEB-INF/config/*-config.xml


springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy



springSecurityFilterChain
/*





flex.messaging.HttpFlexSession



org.springframework.web.context.ContextLoaderListener



flex
org.springframework.web.servlet.DispatcherServlet
1





flex
/messagebroker/*




bin-debug/testApp.html





jdbc/catalogueDB
javax.sql.DataSource
Container








context.xml






















persistence.xml




com.abccompany.domain.Book







Services


package com.abccompany.service;

import java.util.List;

import com.abccompany.domain.Book;

public interface BookService {
List getListBooks();
}


////////


package com.abccompany.service;
import java.util.List;
import javax.annotation.Resource;
import com.abccompany.dao.BookDao;
import com.abccompany.domain.Book;

public class BookServiceImpl implements BookService {

@Resource(name="jpaBookDao")
BookDao bookDao;

@Override
public List getListBooks() {
return bookDao.getListBooks();
}

}





DAOs

package com.abccompany.dao;
import java.util.List;
import com.abccompany.domain.Book;

public interface BookDao {
List getListBooks();
}


////////////////////

package com.abccompany.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.abccompany.domain.Book;
@Repository
public class JpaBookDao implements BookDao {

@PersistenceContext
EntityManager em;

@Transactional(readOnly=true)
@Override
public List getListBooks() {
return(em.createNamedQuery("listBooks").getResultList());
}

}





Domain Objects(JPA Enities / SQL Tables)

package com.abccompany.domain;

import java.io.Serializable;
import java.lang.Float;
import java.lang.Long;
import java.lang.String;
import javax.persistence.*;

/**
* Entity implementation class for Entity: Book
*
*/
@Entity
@NamedQuery(name="listBooks", query="SELECT b FROM Book b")
public class Book implements Serializable {


@Id
@Column(name="Id")
private Long bookId;
private String title;
private Float price;
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
private static final long serialVersionUID = 1L;

public Book() {
super();
}
public Long getId() {
return this.bookId;
}
public void setId(Long bookId) {
this.bookId = bookId;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Float getPrice() {
return this.price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Integer getNbOfPage() {
return nbOfPage;
}
public void setNbOfPage(Integer nbOfPage) {
this.nbOfPage = nbOfPage;
}
public Boolean getIllustrations() {
return illustrations;
}
public void setIllustrations(Boolean illustrations) {
this.illustrations = illustrations;
}

}





Exception Translaters


package com.abccompany.flex.exception;

import org.springframework.flex.core.ExceptionTranslator;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.util.ClassUtils;

import flex.messaging.MessageException;

public class AccessException implements ExceptionTranslator {

//AccessDeniedException cannot use because BlazeDS handle it.
//JRException like exceptions can be handle.What have to do is throws new JRException
@Override
public boolean handles(Class clazz) {
return (ClassUtils.isAssignable(AccessDeniedException.class,clazz));
}

@Override
public MessageException translate(Throwable th) {
MessageException ex =new MessageException();
ex.setCode("Application.Service");
ex.setMessage("Access Denied");
ex.setRootCause(th);
return null;
}

}


////////////////

package com.abccompany.flex.exception;
import org.springframework.flex.core.ExceptionTranslator;
import org.springframework.util.ClassUtils;
import flex.messaging.MessageException;

public class GenaricNullPointerException implements ExceptionTranslator {

@Override
public boolean handles(Class clazz) {
return (ClassUtils.isAssignable(NullPointerException.class, clazz));
}

@Override
public MessageException translate(Throwable th) {
MessageException ex =new MessageException();
ex.setCode("Application.Service");
ex.setMessage("Empty Feild");
ex.setRootCause(th);
return ex;
}
}



/////////////////////



package com.abccompany.flex.exception;

import java.io.IOException;

import org.springframework.flex.core.ExceptionTranslator;
import org.springframework.util.ClassUtils;

import flex.messaging.MessageException;
import flex.messaging.messages.ErrorMessage;

public class GlobalGenericException implements ExceptionTranslator {

@Override
public boolean handles(Class clazz) {
return (ClassUtils.isAssignable(IOException.class, clazz));
}

@Override
public MessageException translate(Throwable th) {
MessageException ex =new MessageException();
ex.setCode("Application.Service");
ex.setMessage("Empty field found");
ex.setRootCause(th);
System.out.println("---------sanka's MessageException GlobalGenericException----------");
return ex;
}

}




Security

package com.abccompany.security;

import java.util.Map;

import org.springframework.flex.security3.AuthenticationResultUtils;

public class Security3Helper {

public Map getAuthentication() {
return AuthenticationResultUtils.getAuthenticationResult();
}
}
Labels: , , ,