Sunday, July 4, 2010

mod_jk Vs mod_proxy_http Vs mod_proxy_ajp

It has always been confusing for me to understand the differences between - mod_jk Vs mod_proxy_http Vs mod_proxy_ajp.

The best link I have ever read that gives a summary on when to use what - http://www.tomcatexpert.com/blog/2010/06/16/deciding-between-modjk-modproxyhttp-and-modproxyajp

NJoy!

Thursday, March 19, 2009

Parent Child Relationship - Hibernate

Parent Child Relationship - Hibernate

Inserting values in both parent and child table in Hibernate in a single session.save() command. You need not insert value in the parent table first and then get the ID and set it in the child and then insert it. You can do both at the same time. Below are the sample code snippets of the files used.

hbm.xml file:

<class name="com.sample.pojos.UserInfoBean" table="USER_INFO" lazy="false">

... other properties configured
<property name="firstName" type="java.lang.String" column="FIRST_NAME" />
<property name="lastName" type="java.lang.String" column="LAST_NAME" />
<bag cascade="all-delete-orphan" inverse="true" lazy="true" name="userDetailsBean" table="USER_DETAILS">
<key column="USER_INFO_ID"/>
<one-to-many class="com.sample.pojos.UserDetailsBean"/>
</bag>
</class>

<class name="com.sample.pojos.UserDetailsBean" table="USER_DETAILS" lazy="false">

... other properties configured
<property name="address" type="java.lang.String" column="ADDRESS" />
<property name="city" type="java.lang.String" column="CITY" />
<many-to-one cascade="none" class="com.sample.pojos.UserInfoBean" column="USER_INFO_ID" insert="true" name="userInfoBean" outer-join="false" update="true"/>
</class>

UserInfoBean.java

private List userDetailsBean;

... other properties

/**
* @return Returns the userDetailsBean.
*/
public List getUserDetailsBean() {
return userDetailsBean;
}

/**
* @param userDetailsBean The userDetailsBean to set.
*/
public void setUserDetailsBean(List userDetailsBean) {
this.userDetailsBean = userDetailsBean;
}

... getters and setters for other properties
[/sourcecode]

UserDetailsBean.java

[sourcecode language='java']
private UserInfoBean userInfoBean;

... other properties

/**
* @return Returns the userInfoBean.
*/
public UserInfoBean getUserInfoBean() {
return userInfoBean;
}

/**
* @param userInfoBean The userInfoBean to set.
*/
public void setUserInfoBean(UserInfoBean userInfoBean) {
this.userInfoBean = userInfoBean;
}

... getters and setters for other properties

Business class:

Transaction transaction = null;

UserInfoBean userInfoBean = new UserInfoBean();

... set the required properties

UserDetailsBean userDetailsBean = new UserDetailsBean();

... set the required properties

// Setting the parent class in the child class
userDetailsBean.setUserInfoBean(userInfoBean);

// List for storing the User Details Beans
List userDetailsList = new ArrayList();

// Adding the UserDetailsBean to the list
userDetailsList.add(userDetailsBean);

// Setting the list in the UserInfoBean
userInfoBean.setUserDetailsBean(userDetailsList);

try {
Session session = SessionManager.currentSession();
transaction = session.beginTransaction();
session.save(userInfoBean);
session.flush();
session.clear();
transaction.commit();
createUserStatus = true;
} catch (Exception e) {
transaction.rollback();
} finally {
SessionManager.closeSession();
}

Now in a single session.save() command, values gets inserted in both the parent and child table.

Hope this helps.

Wednesday, December 31, 2008

Continuous Build Integration

In the Agile Software Development practice we would need to build and test the application frequently. For example we might need to build many times in a day. We could automate this process and have a cron job that builds and generates reports for the development team to consume.
There are tools that help us in this process of building and testing the application at frequent pre-defined intervals, such as Cruisecontrol, Hudson, Bamboo etc., Refer to this link for the feature matrix comparison of these Continuous Integration Tools.
As an example we shall look at Cruisecontrol which is a Java based, open source CI tool - released with BSD style licence.

How CruiseControl works
  • Developer checks work into version control

  • CC polls version control

  • CC triggers a build

  • CC captures logs and build artifacts

    • Examples: jar, war, javadoc, unit test report, code coverage report, code quality metrics

  • CC publishes results

    • Examples: send email, send instant message


We have a config.xml file through which we could configure the tool.
We would specify the version control information(such as CVS host and credentials), the project details, the scheduling information(such as when to build the app), where to log the builds and save the artifacts and whom to inform when the build completes etc., in this config file.
Since plugins are available for cruisecontrol there is a lot of options that we can configure in this config.xml file.

Here's a sample config.xml file:
<cruisecontrol>
  <project name="connectfour">

<listeners>
<currentbuildstatuslistener file="logs/${project.name}/status.txt"/>
</listeners>

<bootstrappers>
<svnbootstrapper localWorkingCopy="projects/${project.name}" />
</bootstrappers>

<modificationset quietperiod="30">
<svn localWorkingCopy="projects/${project.name}"/>
</modificationset>

<schedule interval="300">
<ant anthome="apache-ant-1.7.0" buildfile="projects/${project.name}/build.xml"/>
</schedule>

<log>
<merge dir="projects/${project.name}/target/test-results"/>
</log>

<publishers>
<onsuccess>
<artifactspublisher dest="artifacts/${project.name}" file="projects/${project.name}/target/${project.name}.jar"/>
</onsuccess>
</publishers>

</project>
</cruisecontrol>
There is a cruisecontrol dashboard through which we can access the history of artifacts and build results and manage those.
Here is a sample Dashboard of cruisecontrol.




Monday, November 10, 2008

CSS and JS compression

A web project has lots CSS and JS files associated with it and lots of these files does affect the page load time based on the internet speed, its size etc.

It would be a wise option to compress the CSS and JS files to make download of these on the fly much faster.
1) GZIP compression
2) Remove unwanted space, line feed characters in JS.
3) Use smaller variable names
4) Remove comments

For (1) - Most of the application servers come with option of enabling compression for HTTP and HTTPS. For example in Tomcat you can do it by setting in the server.xml
...
<connector acceptcount="100" connectiontimeout="60000" disableuploadtimeout="true"
port="9014" redirectport="8944" scheme="https" proxyport="443" compression="on"
compressablemimetype="text/html,text/xml,text/plain,text/css,text/javascript">
...
Having 2, 3 and 4 done on the code base will make it ugly and not readable. There are lots of open source tool which can help as they does all these during build time when creating the achieves.

For example one such tool would be Yahoo UI compressor - it has a easy maven plugin as well - click here for more details on usage.

Thursday, May 10, 2007

Including HTML Tags/codes in Blog Post

This post is the outcome of my previous post. In my previous post,I had to include some HTML codes as an example, it contained Scripts, CSS and some Body code. I did that in a normal way, but Blogger Engine took that as HTML codes and dsiplayed the corresponding result as a browser does. I was thinking what to do for this, I tried commenting that part of the code, but it didnt work well.Finally we got a solution, instead of "<" open tag we shoudl use "amplt;" and instead of close tag ">" we should use "ampgt;". This works out well.
NOTE: Instead of "amp" use the "&" symbol, i have given " amp "here for reperesentation.

Applying Styles for File Button

Everyone who works in the front end will be aware of the input type=”file” , the function of this button is that, it will fetch you the file browser, so that you can chose the file for your Upload. Unfortunately, you can have images for these buttons, unlike we have for other Submit kind of buttons. So what can be done for this? There are two solutions,

1.You can find out the code behind of this File button and can implement that procedure in Onclick of this image button.
2.You can have an image Button over a File Button and you can invoke the Click event of the File button from the onclick event of this image buttton. For this you have to play with the z-index property.

we will have a look at the second method, as the first one is very simple.

How to do this?
Change the z-index of the File Button, make it as negative(or a lesser value than image button). I have included the code here, check it out here

<script type=”text/javascript”>
function clickBrowse()
{
document.getElementById(’openssme’).click()
}
</script<
<style type=”text/css”<
input.hide
{

position:relative;
left:-225px;
z-index: -1;
width:0px;
border-width:0px;
}

input.red
{
position:relative;
background-color:#cc0000;
font-weight:bold;
color:#ffffff;
z-index:100;
width:75px;
height:20px;
font-size:10px;
}
</style>

<!–[if IE]>
<style type=”text/css”>
input.hide
{
position:relative;
visibility:hidden;
left:-83px;
z-index: -1;
width:0px;
border-width:0px;
}
</style>
<![endif]–>

<img src=”btn_browse.gif” class=”red” id=”pseudobutton” onClick=”clickBrowse();” />
<input type=”file” class=”hide” id=”openssme”>

We have to handle IE and FF seperately, so a code for IE is placed seperately in this.
I thank Nirmal, for helping me out in this.