Social media company website lanched!

We've launched mockup of social media company that we started. Check the following url:

Human Readable Filesize

I'm sharing a utility library that I use to automatically convert filesize in bytes to human readable size. For example kb or mb.

Usage:
public String convertToHumanReadableSize(long sizeInBytes) {
        final FilesizeConverter fc = new FilesizeConverter(FilesizeConverter.UNIT_BYTES);
        return fc.convertToHumanReadable(sizeInBytes);
}
Download it here: http://dl.dropbox.com/u/1262817/blog/FilesizeConverter.java

Enabling JRebel Wicket Plugin

-Drebel.wicket_plugin=true

Nested Embeddable Object In Hibernate

One of my project needs to maintain the heirarchy of entity as elegant as possible while at the same time have a good
performance (maintaining 1 single table). I have the following class: ImageThumbnail, Image and ObjectThatUsesImage.

I could have use table per subclass but since I want the query performance to be fast, I decided to embed the Image to ObjectThatUsesImage. What I did is embed the ImageThumbnail to Image and then embed the Image to ObjectThatUsesImage.


Things should look like this now:
ImageThumbnail > Image > ObjectThatUsesImage
ImageThumbnail was embedded within Image since I believe it is logically correct for the Image to know about its thumbnail (not ObjectThatUsesImage) and for re-usability reasons.

In my first try, I simply used the following codes:
@Embeddable
public class ImageThumbnail {
    private String url;
    private Integer height;
    private Integer width;
}
@Embeddable
public class Image {
    private String url;
    private Integer height;
    private Integer width;


    @Embedded
    private ImageThumbnail thumbnail;
}
public class ObjectThatUsesImage {
    @Embedded
    private Image image;
}
Running the application will give you an error that you are trying to generate a column with duplicate name. In this case, you are trying to generate column names url, width and height for both ImageThumbnail and Thumbnail on the same table. To fix this, you need to override the attributes:
public class ObjectThatUsesImage implements Serializable {
    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="url", column = @Column(name="imageUrl")),
        @AttributeOverride(name="width", column = @Column(name="imageWidth")),
        @AttributeOverride(name="height", column = @Column(name="imageHeight")),
        @AttributeOverride(name="thumbnail.url", column = @Column(name="thumbnailImageUrl")),
        @AttributeOverride(name="thumbnail.width", column =  @Column(name="thumbnailImageWidth")),
        @AttributeOverride(name="thumbnail.height", column = @Column(name="thumbnailImageHeight")),
    })
    private Image image;
   // ...
}
Using overrides, you can customize the column names with whatever name you want.

JavaRebel, Maven and Jetty

First of all what is JavaRebel? JavaRebel is a tool that helps in decreasing time wasted on re-deploying the application by
automagically re-loading the changes from your workspace to the running application. But just like aspectj, you need to weave or
use a java agent when the application first start.


To setup Maven and Jetty to use JavaRebel, you can follow the following steps:

1. Add zero turnaround repo:
<pluginRepositories>
      <pluginRepository>
          <id>zt-repo</id>
          <name>Zero turnaround repo</name>
          <url>http://repos.zeroturnaround.com/maven2</url>
      </pluginRepository>
  </pluginRepositories>

2. Add the JavaRebel maven plugin. This will automatically generate the rebel.xml file:
 <plugin>
            <groupId>org.zeroturnaround</groupId>
              <artifactId>javarebel-maven-plugin</artifactId>
              <executions>
                <execution>
                      <id>generate-rebel-xml</id>
                      <phase>process-resources</phase>
                      <goals>
                        <goal>generate</goal>
                      </goals>
                    </execution>
              </executions>
        </plugin>

3. Generate rebel.xml using the following command:
mvn javarebel:generate -Drebel.xml.dir=src/main/resources/ 
4. Download JavaRebel and place the rebel.jar file on your project directory.

5. Run jetty with extra parameters:
mvn jetty:run -noverify -javaagent:jrebel.jar
 6. Try adding a new method and visiting the same page and updates will immediately take effect without restarting.


The example code for this tutorial can be found at http://dl.dropbox.com/u/1262817/javarebelsample.zip.

Try running the application first and then visit http://localhost:8080/javarebelsample/sample.

Edit SampleServlet.java, uncomment the commented methods or add a new method and then visit the same page without restarting.

Run ScheduleFactoryBean At Startup

ScheduleFactoryBean won't start after application starts when lazy init is set to true. To immediately start it, set lazy-init="false"
to either the bean class definition or the global beans declaration (on top of the application context xml file).

Custom Taglib With Body

To create custom taglib wth body, you need to:
  1. Extend the javax.servlet.jsp.tagext.BodyTagSupport class.
  2. Implement the doAfterBody() instead the doStartTag().
  3. Use getBodyContent() method to get the body.
  4. Add <body-content>JSP</body-content> to tld tag declaration.
For example a taglib that checks whether a given item exist within the given collection:

Taglib Class
public class ItemInCollectionTag extends BodyTagSupport {

private static final long serialVersionUID = -3130145552400188745L;
private Collection collection;
private Object item;

@Override
public int doAfterBody() throws JspException {
if(collection == null) {
return SKIP_BODY;
}

final HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
final JspWriter out = pageContext.getOut();

if(collection.contains(item)) {
final BodyContent bodyContent = getBodyContent();
try {
out.print(bodyContent.getString());
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
return SKIP_BODY;
}
// getters and setters here
}
Taglib Declaration (.tld file)
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname>util</shortname>
<info>util tag library</info>
<uri></uri>

<tag>
<name>itemInCollection</name>
<tagclass>com.reyjexter.core.taglib.util.ItemInCollectionTag</tagclass>
<body-content>JSP</body-content>
<info>Check whether an item exist within the collection given</info>
<attribute>
<name>collection</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

</taglib>

Using in JSP
<%@ taglib prefix="util" uri="/WEB-INF/util.tld"%>
<util:itemInCollection collection="${someCollection}" item="${someItem}">prints when true</util:itemInCollection>



top