29 Aralık 2010 Çarşamba

How to construct image from byte array for gwt applications

Here is the scenario. You have images stored in database, and you want to use these images in your gwt application. As already known, the gwt image object constructor takes only String , this string should represent the url of the image.. You can  construct the image in the server, and you can save this image to your server's disk, and you can use the url of this image . But , this solution is not elegant, you have to save image to the server's disk..

As a second solution, you can write a servlet whose purpose is to fetch images from database and send them to the clients..  You can map this servlet ImageViewer , with  a  parameter. The request looks like this ;
   /imageViewer?imageId=1234 . You should create your gwt image widget like below;


  Image image = new Image( "/imageViewer?imageId=1234");



 ImageViewer servlet doGet method should be like that ;


public void doGet(HttpServletRequest request, HttpServletResponse response) {
        String imageId =  request.getParameter("imageId");
         byte[] imageData = readImageDataFromDB(imageId);
         response.setContentType("image/png");
         response.getOutputStream().write(imageData,0,imageData.length);
}




As a third solution , you can use the below code to convert image byte array data, to Base64 encoded string.. But you should know that this method does not work for IE7 and the previous versions. IE8 has 32 kb limit for encoded data.


public String getImageData(){

   byte[] imageByteArray = readImageDataFromDB(); //
   String base64 = com.google.gwt.user.server.Base64Utils.toBase64(imageByteArray);
    base64
= "data:image/png;base64,"+base64; 
   return base64;
}

And at gwt layer, you should use the below code ;


 
imageService.getImageData(new AsyncCallback() {
    @Override  
     public void onSuccess(String imageData) {    
       
Image image = new Image(imageData);    
        
RootPanel.get("image").add(image);  

      }
     @Override 
     public void onFailure(Throwable caught) {
      
Window.alert(caught.getMessage()); 

     }
}

17 Aralık 2010 Cuma

JPA 2.0 with GWT

Well, if you are using JPA 2.0 with GWT, and you are using your entity objects as DTO between GWT layer and web layer and you are using annotations for object mapping, you should do the following items ;
  • Download jpa2.0 sources . This file does not contain all source files of JPA 2.0 classes, only annotations and some interfaces are included. 
  • Put this jar file to your classpath
  • Add   <inherits name="javax.Persistence"/>  to  your [module name].gwt.xml file 
  • If your gwt version is below 2.1 , then you should add gwt-java-mathh to your project

9 Aralık 2010 Perşembe

How to write spring tests without spring transactional management facility

As you know, Spring 3.0 test framework does not allow transaction demercation . You should use beforeTransaction or afterTransaction method annotations to implement test logic after or before the transaction . But it means that you should write one class for every test method. As a result, the number of your test classes increase dramatically. Well, here is the solution , I have used AbstractJUnit4SpringContextTests instead of AbstractTransactionalJUnit4SpringContextTests.

@ContextConfiguration(value = "/applicationContext.xml") 
public class JPASpringTest extends AbstractJUnit4SpringContextTests { 
 @PersistenceContext(unitName="jpadenemelocal") 
 EntityManager entityManager; 
 
 @Autowired 
 protected PlatformTransactionManager transactionManager; 
 
 @Test 
 public void testInsertRolManyToMany() { 
 TransactionStatus status =  transactionManager.getTransaction(null); 
 // your code    
 
 transactionManager.commit(status); 
 } 
 
} 

15 Ekim 2010 Cuma

Maven class loading issues

Recently, I have tried to port one of our project to maven. When I try to use tomcat maven plugin,  I have got the below exception;

java.lang.ClassCastException: tr.com.belsis.common.web.utils.InitServlet cannot be cast to javax.servlet.Servlet at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1116) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)

Multiple javax.servlet.Servlet at class path causes this exception. The solution is to define the scope of servlet dependency as provided as below;

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>


27 Eylül 2010 Pazartesi

Mavenize existing eclipse project

It is very easy, you should just change .project file of your eclipse project..

You should add below xml snippet to under element of .project file.

<buildCommand>
   <name>org.maven.ide.eclipse.maven2Builder</name>
   <arguments>
   </arguments>
  </buildCommand>
   
  

And also you should add below xml snippet under element of .project file.

<nature>org.maven.ide.eclipse.maven2Nature</nature>

edit :

Below snipped should be included .classpath file.

<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER">
   <attributes>
    <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
   </attributes>
 </classpathentry>



13 Temmuz 2010 Salı

Toplink and Glassfish

If you want to use toplink natively with Glassfish application server , you should make some configuration. First of all, you should change platform class for Glassfish like below, because SunAS9ServerPlatform class constructor expects DatabaseSessionImpl as argument, but it is called with DatabaseSession.


public SunAS9ServerPlatform(DatabaseSession newDatabaseSession) {

       super(newDatabaseSession);
}



Secondly, you should add below ines to your session.xml file.



<server-platform xsi:type="custom-platform">

<server-class>oracle.toplink.platform.server.sunas.SunAS9ServerPlatform</server-class>
<external-transaction-controller-class>oracle.toplink.transaction.sunas.SunAS9TransactionController</external-transaction-controller-class>
</server-platform>
 
 
As a final note, Toplink version is 11.1.1.0.1, Glassfish version is 3.0.1