Monday, August 17, 2009
Thursday, July 9, 2009
Using Dynamic model in Hibernate
When using Hibernate we are not forced to have POJOs alawsys for representing persistent entities. Hibernate also supports Dynamic models like Maps and DOM4J trees.
<hibernate-mapping>
<class entity-name="com.check.Employee" table="TEST_EMPLOYEE" polymorphism="implicit" lazy="false">
<id name="id" type="string">
<column name="id" null="true">
</column></id>
<property name="name" column="name" type="string">
<property name="emailId" column="emailId" type="string">
</property></property></class>
</hibernate-mapping>
Here "readEmpUsingObject" is used for reading object with composit id.
In plain english what this means is - we need to have only a mapping file (.hbm.xml) and no pojo class is required. However be careful when using this feature , as it may undergo change in the future releases.
Here we will see how to use the Hashmap for dynamic model.
Our table is
TEST_EMPLOYEE
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | varchar(255) | NO | PRI | | |
| name | varchar(255) | YES | | NULL | |
| emailId | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
Lets have a look at the curresponding mapping file -
<hibernate-mapping>
<class entity-name="com.check.Employee" table="TEST_EMPLOYEE" polymorphism="implicit" lazy="false">
<id name="id" type="string">
<column name="id" null="true">
</column></id>
<property name="name" column="name" type="string">
<property name="emailId" column="emailId" type="string">
</property></property></class>
</hibernate-mapping>
Check the use of "entity-name", this is required for dynamic model. We need not specify any class name here.
Sample code for CRUD operations.
Here HibernateHandler is a utility class used for managing sessionFactory.
|
Here "readEmpUsingObject" is used for reading object with composit id.
Curresponding mapping file will contain -
<composite-id >
<key-property name="id" type="string" column="id" />
<key-property name="name" type="string" column="name" />
</composite-id>
Tuesday, June 30, 2009
Apache Wicket - How to run and get response from JavaScript
If you dont know what is Wicket - you are realling missing something. It is a b'ful framework from Apache which will make our life very much easier when doing web application development.
display the GoogleEarth through loading JavaScript.
Curresponding html files are simple enough.
You can read more about Apache Wicket here
Here let us check how to invoke Javascript from a Wicket component and how receive/handle response. Assume the requirement is having a webpage where we will load some control using Javascript(For example - Google Earth ;) ) , and once the javascript execution is completed the page should do something.
01 public class MyPage extends WebPage {Here we are creating a webpage and adding a panel to that. The panel is the place where we will |
But before going to the panel let us check how to get a call back from the JavaScript.
For acheiving this we will add an AjaxBehavior to the panel
01 public MyPage() { |
Curresponding html files are simple enough.
Now lets look at the JSPanel. Here we need to call the javascrip onLoad of the page. This is acheived through RenderHead method.
|
Lets look at the relevant javascript now
function init(urlVal) {
url = urlVal;
//
//Create an instance of GE.
google.earth.createInstance("map3d", initCallback, failureCallback);
}
function initCallback(object) {
//
// Show GE and move camera to decired location.
ge = object;
ge.getWindow().setVisibility(true);
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
...
ge.getView().setAbstractView(lookAt);
...
//
// Make a call to the wicket callback url from here
var arg = "&key=" + value;
var wcall = wicketAjaxGet(url + arg , function() { }, function() { });
}
That is it. Let me know if you need the complete sample code for this.
Java code displayed as html here is using a tool HTML4Java, you can check it here http://www.toolbuddy.org/html4java.htm
Saturday, June 20, 2009
Capturing Image From Google Earth
Easiest way is - if you have a stand alone Google Earth application, start it and move to the decired location. This can be done through opening a KML file. Then use a screen capture program to take the screen shot (Or just use PrintScrn button)
What if there is no stand alone application available? Google static maps comes to the rescue.
Just load the url in a browser it will load the curresponding image.
Let us see how to do this from Java .
Use a url string.
String url = "http://maps.google.com/staticmap?center=%LAT%,%LON%&zoom=%ZOOM%&size=512x512&maptype=satellite&key=%KEY%";
Replace the place holders
%LAT% - Latitude
%LON% - Longitude
%ZOOM% - Zoom level
%KEY% - Google map key
//
// Load the image from url.
url = new URL(uri);
BufferedImage bi = ImageIO.read(url);
ImageIO.write(bi,"PNG",new File("image.PNG"));
ServiceMix MessageExchange memory leak
Are you observing memory leak in JBI components deployed in ServiceMix? First place to check will be whether the message exchanges are closed properly.
In the InOut message exchanges, after receiving message from provider , consumer should set the status as DONE in the exchange and again send it to provider.
Note - just setting the status to DONE is not enough , it must be send back to provider, otherwise the message exchanges will not be properly closed and will result in a memory leak.
E.g.-
inOutexchange.setStatus(ExchangeStatus.DONE); context.getDeliveryChannel().send(inOutexchange);
Memory leak in Java application
First thing to do is study the memory usage/ thread creation patterns of the application. Different profiler tools are available in market for this.
Two tools I commonly use are
1. JProfiler
2. YourKit
In JProfiler - if there are memory leaks , total memory of the objects (which are leaking) will be shown against the class which created the memory.
Sometimes this may be misleading as the object references may be held by someone else , and that class is the real reason for the leak.
In my experience , identifying those classes were bit easier in YourKit.
Taking ScreenShot (PrintScreen) in Java
It is pretty easy to take a screen shot from Java code
Try
Rectangle rect = new Rectangle(0, 0, 700, 500);
BufferedImage screencapture = new Robot()
.createScreenCapture(rect);
// Save as JPEG
File file = new File("FileName.Jpg");
ImageIO.write(screencapture, "jpg", file);
Interesting question is what will happen if this code is executed in an environment where display devices are not available? You will get an AWT exception indicating "headless environment".
Subscribe to:
Posts (Atom)