Thursday, July 11, 2013

log4j hibernate show_sql to log file

Hibernate writes the SQL commands, which are logged via

<property name="hibernate.show_sql">true</property>
 
by default to console. In a tomcat envirement the default ist the catalina.out log file. To write your plain SQL logs to a seperate log file you have to configure a seperate logger for the SQL statements. You can do this by adding the following Logger in log4j.xml:
  <logger name="org.hibernate.SQL">
    <!-- level info logs -->
    <level  value="DEBUG" />
    <appender-ref ref="sqlAppender"/>    
  </logger>
 
And don’t forget to create a appender for it:

  <appender name="sqlAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd" />
    <param name="file" value="${log.path}/myproject-sql.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%d{ISO8601} %-5p [%t] %c: %m%n" />
    </layout>
  </appender>

Saturday, June 22, 2013

Java Image Scaling awt.Graphics2D vs. Scalr

The Target was very simple I had the last days. You will get a big image and have to scale it low for the web.
At the beginning I googled a little bit and found out the imagemagic is the best solution for quality scaling. I think it is, because it has a very big libary of commands and supportet image formats.Because I like to scale the images during a gwt server call I was not so happy to install a complete software on our hosting machines. So I checked other solution to Scale this image to a [x<=200 and y<=200] format. The first approach was using java awt image scaling stuff:
Unit Test Code


BufferedImage bsrc = ImageIO.read(new File(src));
for (String name : ImageIO.getWriterFormatNames()) {
System.out.println(name);
}
System.out.println("original size_" + bsrc.getWidth() + " height:" + bsrc.getHeight());
double scaleWith = (double) width / bsrc.getWidth();
double scaleheigth = (double) height / bsrc.getHeight();
double scalefactor = scaleheigth > scaleWith ? scaleWith : scaleheigth;
double finalWith = bsrc.getWidth() * scalefactor;
double finalHeight = bsrc.getHeight() * scalefactor;
System.out.println("Dest img size:" + finalWith + " height:" + finalHeight);
BufferedImage bdest = new BufferedImage((int) finalWith, (int) finalHeight, bsrc.getType());
Graphics2D g = bdest.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform at = AffineTransform.getScaleInstance(scalefactor, scalefactor);
g.drawRenderedImage(bsrc, at);
ImageIO.write(bdest, "JPG", new File(dest));

This was the result
the-hobbit-an-unexpected-journey-dvd-cover-68-out
For a Programmer it was ok. But not for an Artist, or people which don’t have a red-green handicap :-)
So I tried out imgscalr
here my Unit Test code:
public static void scaleNEW(final String src, final int width, final int height, final String dest) throws IOException {
BufferedImage bsrc = ImageIO.read(new File(src));
String extension = src.substring(src.lastIndexOf(".") + 1, src.length());
System.out.println("extension is:" + extension);
for (String name : ImageIO.getWriterFormatNames()) {
System.out.println(name);
}
System.out.println("original size_" + bsrc.getWidth() + " height:" + bsrc.getHeight());
double scaleWith = (double) width / bsrc.getWidth();
double scaleheigth = (double) height / bsrc.getHeight();
double scalefactor = scaleheigth > scaleWith ? scaleWith : scaleheigth;
double finalWith = bsrc.getWidth() * scalefactor;
double finalHeight = bsrc.getHeight() * scalefactor;
BufferedImage thumbnail = Scalr.resize(bsrc, (int) finalWith, (int) finalHeight, Scalr.OP_ANTIALIAS);
ImageIO.write(thumbnail, extension, new File(dest));
}

I was very supprised, that the api was so easy to use…. Here you see the result:
the-hobbit-an-unexpected-journey-dvd-cover-68-out2

Sunday, February 10, 2013

Abstraction ui elements with labels

You always have the issue, that you need next to ui elements a label to name the element.
There are many options to solve the problem. First the easy way:
ListBox box=new ListBox();
Label lbl=new Label("Countrys");

this.add(box);
this.add(lbl);
This code a lot of programmers have to do for hundrets of input fields, comboboxes and so on.I would recomment, to build up for your most needed UI Elements a class, which also contains the label and the data containing element.
public class MisTextAreaWithLabel extends MisVerticalPanel {
 private TextArea area;
 private Label lbl;

 public MisTextAreaWithLabel(String label) {
  this.lbl = new Label(label);
  this.area = new TextArea();

  this.add(area);
  this.add(lbl);
 }

 public void setValue(String newValue) {
  this.area.setValue(newValue);
 }

 public String getValue() {
  return this.area.getValue();
 }
}
This code is easyly useable with:
MisTextAreaWithLabel  area=new MisTextAreaWithLabel("firstname");
this.add(area);
By creating a new Panel where the element and it’s description is on, you can save a lot of code and you will be able to change the behavior of all your UI Elements easyly.

hibernate configure transaction Isolation Level

In the hibernate config file you can configure the default jdbc-transaction level. Hibernate uses the codes of the java.sql.Connection  class, which are listed above.
  •   public static final int TRANSACTION_NONE = 0;
  •   public static final int TRANSACTION_READ_UNCOMMITTED = 1;
  •   public static final int TRANSACTION_READ_COMMITTED = 2;
  •   public static final int TRANSACTION_REPEATABLE_READ = 4;
  •   public static final int TRANSACTION_SERIALIZABLE = 8;
To use Repeatable Read transactions you just have to type the number 4 in your isolation property.
<property name=”hibernate.connection.isolation”>4</property>
Here a short list, what each level means:
  • TRANSACTION_READ_UNCOMMITTED -> Allows dirty reads, non-repeatable reads, and phantom reads to occur.
  • TRANSACTION_READ_COMMITTED -> Ensures only committed data can be read.
  • TRANSACTION_REPEATABLE_READ -> Is close to being “serializable,” however, “phantom” reads are possible.
  • TRANSACTION_SERIALIZABLE -> Dirty reads, non-repeatable reads, and phantom reads are prevented. Serializable.
Performance of the software decreased by a higher transaction level. So you should check if you need more stability or performance. Depends always by the software you build up.

Monday, January 28, 2013

Tool of the day: Atlassian Confluence

Since many years I am working now with atlassian confluence as project documentation plattform. I am still suprised, how easy the integration with jira, greenhopper and co. works.4 months ago I introduced confluence in my current company with a lot of non technical employees.It was very cool, to see how easy they got confident in working on a day to day base with confluece.

We are very happy with it and I personally think that everybody should use it: freelancers, startups and big companys!Atlassian has a very nice license model. Until 10 User Accounts you just pay 10$  a moth to use confluence.That is not much money, for such a great documentation plattform. Also that setup with an MySQL and the deployment to a java Tomcat Server is pretty simple. The documentation of confluence is awesome.For me more or less one of the best tools I ever worked with. There is also a free trial version available.

Check the demonstration video to build yourself a picture!


CTRL+Z