Monday, September 12, 2011

Html 5 upload and display image on canvas - Firefox and Chrome

Tried html5 to upload and display image on canvas. Mostly example only works on Firefox. After looking around tried the FileReader method. Both Firefox and Chrome supports the FileReader object. IE still not supporting this object at the moment. Maybe in the next IE version. Anyway below is the javascript to upload and display image on canvas. Tested on jboss 7 server. Won't work if not run on server though.




<script>
function handleFileSelect(files) {


for ( var i = 0, f; f = files[i]; i++) {


// Only process image files.
if (!f.type.match('image.*')) {
continue;
}


var reader = new FileReader();


// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
var img = document.createElement("img");
img.src = e.target.result;
img.onload = function() {
var canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
context.drawImage(img, 0, 0);


}
};
})(f);


// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
</script>



<body>
       <input type="file" id="files" name="files[]" multiple onchange="handleFileSelect(this.files)"/>
       <canvas id="myCanvas" width="778" height="600"></canvas>  
</body>



Tuesday, September 6, 2011

Getting data from json format using json tokener

I was asked to use extJs grid to display some info on the data grid component. Fine but the component uses json format to display its data on the grid. So deleting data from the grid need to pass in data as json format. Anyway here is the method to retrieve the id from the json data to perform deletion in the grid using JsonTokener. Need to download the net.sf.json jar files from the json website.

import net.sf.json.JSONArray;import net.sf.json.util.JSONTokener;   
public String delJson(String delData, int total, int start){
        ProjectService projService = (ProjectService) SpringApplicationContext.getBean("projectService");
         
        String returnJson = "";
        String delId = "";
        try {
            JSONArray ja = new JSONArray();
            ja = JSONArray.fromObject(delData);
            int jsize = ja.size();
            
            while(jsize!=0){
                String jstr = ja.getString(jsize - 1);
                JSONTokener jt = new JSONTokener(jstr);
                jt.skipPast("id\":");
                delId = jt.nextValue().toString();
                
                if(delId!=null && !"".equals(delId)){
                    try {
                        
                        projService.delete(delId);
                    } catch (NumberFormatException e) {
                        
                        logger.error("error ProjectBean.delJson",e);
                    } catch (Exception e) {
                        
                        logger.error("error ProjectBean.delJson",e);
                    }
                }
                jsize--;
            }
            
        } catch (Exception e) {
            
            logger.error("error ProjectBean.delJson",e);
        }
        return returnJson;
    }




Wednesday, August 31, 2011

Generating a number of tables dynamically in jsp

Recently got a requirement to do generation of dynamic tables based on data output. Used jsp to perform the generation of table from data format of list of multidimensional string arrays List<String[][]>. This will enable generation of multiple tables by looping number of String[][] with the first string array as number of rows and the second string array as number of columns. The jsp code is as below.

<%
        List<String[][]> processLst = new ArrayList<String[][]>();
processLst = //-- call method to get the list
String outstr = "";
int i = 0;
for (i = 0; i < processLst.size(); i++) {
String[][] outputData = processLst.get(i);
%>
<div>


<table border="1" width="450" >
<tr class="font-white">
<th bgcolor="#000000" colspan="<%=outputData[i].length %>"><%=outputData[0][0] %>
</th>
</tr>
<%
for (int rowNum = 1; rowNum < outputData.length; rowNum++) {
%>
<tr>
<% 
for (int cellNum = 0; cellNum < outputData[0].length; cellNum++) {
outstr="";
if(outputData[rowNum][cellNum]!=null)
outstr = outputData[rowNum][cellNum];
%>
<td bgcolor="white">
<%=outstr %>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
        </div>

Sunday, August 21, 2011

Hotdeploy in jboss 7.0.1

The new Jboss 7 server is blazing fast starting up and shutting down. To perform hot deploy or doing development on exploded war, there are setting in the  configuration folder that needs changing.
For standalone deployment, go to standalone/configuration directory and open standalone.xml. Look for the line below.


        <subsystem xmlns="urn:jboss:domain:deployment-scanner:1.0">
            <deployment-scanner name="default" path="deployments" scan-enabled="true" scan-interval="5000" relative-to="jboss.server.base.dir" deployment-timeout="60"/>
        </subsystem>

Add this param to enable hot deploy  auto-deploy-exploded="true" 
Changes are as below:

        <subsystem xmlns="urn:jboss:domain:deployment-scanner:1.0">
            <deployment-scanner name="default" path="deployments" scan-enabled="true" scan-interval="5000" relative-to="jboss.server.base.dir" auto-deploy-exploded="true" deployment-timeout="60"/>
        </subsystem>


Saturday, July 30, 2011

Creating a webservice in 5 minutes

One day the boss ask you to create a webservice for a method and have it deployed immeadiately. What do you do? Don't panic. The quick way to deploy a webserviceis just a few annotations away. Firstly download the latest version of jboss preferably version 6 from the Jboss website. 


Then you annotate the bean you need to expose as webservice.
Create an interface for the bean first. Then annotate with @Remote and @WebService on the class. 
eg.


import javax.ejb.Remote;
import javax.jws.WebService;
@Remote
@WebService
public interface TestWebService {
    public void getTestCodes(List<String> qualification, String local) throws Exception;
}



Put @Stateless and @WebService on the implementation class.
eg.


import javax.ejb.Stateless;
import javax.jws.WebService;
@Stateless
@WebService
public class TestWebServiceImpl implements TestWebService{
    public void getTestCodes(List<String> qualification, String local) throws Exception{
        try {
            //perform method implementation here
        } catch (Exception e) {
        }
    }
}


After compiling and build the codes, package it into a jar file. You can do it by clicking File menu -> export on eclipse.
Copy the jar file into jboss deploy folder at server/default/deploy.
Start jboss.
You can see your webservice is running by going to your localhost url  http://localhost:8080/jbossws/services
If you want to expose it to be accessible by other pc within same network for testing remember to run jboss with command run.bat -b <your ip address>. Can only access the webservice locally only using the default jboss run.bat. 
Testing the webservice, use soapui client available for download at http://www.soapui.org/