All Posts in the ‘Software-Development’ Category

Hibernate Transaction Handling in DWR

Februar 22nd, 2010 | By Christoph in Software-Development | 1 Comment »

I am currently building a web-application using OSGI (HTTP Service) and Hibernate. I also use DWR for all the AJAX stuff in the application. DWR uses a special DWRServlet which handles the interaction of the browser with the server side. The other part of my application uses a global dispatcher servlet for all other servlet requests.  This global dispatcher servlet also does the Hibernate  transaction handling using a OpenSessionInView – Pattern. This means the dispatcher servlet begins, commits and rollback the transaction in case of an exception. This works well.

My problem until today was, that this kind of transaction handling was not working in my DWRServlet, because this is a servlet not under my control. My first approach was to create a new servlet which extens DWRServlet so that I could modify the behaviour , but that didn’t work completely, because DWR does not pass Exceptions to my servlet. DWR handles all exceptions, marshals them so that the Exception can be passed back to the browser to be displayed in your calling Javascript code.  That means I cannot catch the exception in my custom servlet  because it is already caught and handled. Damn!

So I debugged and digged through a complete DWR call and found the @GlobalFilter annotation. That seems to be exactly what I need. This annotation can be applied to a class and this class also needs to implement the AjaxFilter interface. DWR then applies all filters to each method call made through the DWRServlet. This class needs to be registered the same way as you register your other DWR annotated classes (e.g. the classes annotated with the @RemoteProxy or @RemoteMethod annotation)

So I came up with the following class which borrowed some code of the existing org.directwebremoting.hibernate.H3SessionAjaxFilter which does almost exactly what I need, except the rollback() of my transaction in case of an error.

@GlobalFilter
public class DWRFilter implements AjaxFilter {
 
	private static final Logger LOGGER = LoggerFactory.getLogger(DWRFilter.class.getName());
 
	public Object doFilter(Object obj, Method method, Object[] params,
			AjaxFilterChain chain) throws Exception {
 
        SessionFactory sessionFactory = Activator.getSessionFactory();
 
        Transaction transaction = null;
        if (sessionFactory != null)
        {
            Session session = sessionFactory.getCurrentSession();
            transaction = session.beginTransaction();
        }
        else
        {
            LOGGER.error("SessionFactory not initialized for this web application.");
        }
 
        Object reply = null;
		try {
			reply = chain.doFilter(obj, method, params);
 
			if (transaction != null)
			{
			    transaction.commit();
			}
		} catch (Exception e) {
			LOGGER.error("Error while committing transaction in DWRFilter. Transaction will be rolled back now...",e);
                        if(transaction != null){
			    transaction.rollback();
                        }
			throw new InvocationTargetException(e);

		}

        return reply;
	}

}

This basically does exactly what I want and is basically something like the Open Session In View pattern for DWR and Hibernate. For each DWR call it is the following flow:

  • begin transaction
  • do work
  • commit transaction
  • or rollback transaction in case of an exception

This way I have a central place for my DWR-Transaction handling.

This is my first solution to this problem and maybe it doesn’t catch every scenario, e.g. especially when you are using special features of DWR like batch calls etc. I haven’t used those features and for my current features using simple calls it works very well.

Java Code Snippet: Redirect System.out into a string

Januar 19th, 2010 | By Christoph in Software-Development | No Comments »

For a simple test application / prototype I just wanted to just dump everything from System.out into a String, which I can output somewhere e.g. in a JSP file so that I see what’s going on. And yes, I didn’t want to setup a Logging framework for this…I want a String. Here it is:


OutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.out.println(“Test”);
String sysout_content = out.toString();
System.out.println();
}

public static void main(String[] args) {

OutputStream out = new ByteArrayOutputStream();

System.setOut(new PrintStream(out));

System.out.println(“Test”);

String sysout_content = out.toString();

System.out.println();

}

A software system is only as good as easy it is to setup its development environment

Januar 8th, 2010 | By Christoph in Software-Development | No Comments »

… you get the idea.

After having spent almost an entire day to help our new developer to get setup his development environment containing the source code, eclipse, svn, appserver, database, user accounts I made a new resolution for every system I will build in the future:

The system about to be built:

  • has to have a development environment which can be setup within less than an hour from a single location / disc, automatically
  • to do this it has to use sensible defaults –> customization can happen later on a case by case basis
  • the developer has to start working with it right away
  • build and deployment on local environment has to happen automatically. required appservers etc. have to be part of the stuff installed ealier

I started developing with Ruby On Rails about 3 month ago and launched my first project a few days ago, I realized that the ROR already have taken those things into account. Everything is bundled inside the project folder and after checking it out from the SVN you are ready to go by just typing “script/server” in the command line. Same for setup “rails myProject”

I just started with Rails out of curiosity but I already love the way I can develop my ideas and web-projects. Besides that I love to develop in JAVA and currently in the field of OSGI on the server-side and there we basically need the ease of use and the spirit of Rails.

Ok, I switched a bit between topics, so I try to conclude my new years resolution:

“I want to build systems which solve complex tasks, and are easy to setup, fun to develop and fun to use.”

Stromverbrauch Macbook Arbeitsplatz

Oktober 28th, 2009 | By Christoph in Software-Development | 3 Comments »

Letzte Woche hatte ich mir rein aus Interesse mal ein Strommessgeraet von den Stadtwerken geholt, um mal meinen Arbeitsplatz im Arbeitszimmer durchzumessen.

Strommessgeraet1

Komponenten:

  • Macbook 13″ 2.16 GHz Intel Core 2 Duo (gekauft Oktober 2007),2 GB RAM
  • 24″ TFT Samsung SyncMaster 245B
  • Schreibtischlampe
  • DENON Verstaerker als Stereoanlage
Geraet Verbrauch minimal Verbrauch maximal
Macbook 13″ (gekauft Oktober 2007) 35 Watt 39 Watt
24″ TFT Samsung SyncMaster 245B 23 Watt 73 Watt
Schreibtischlampe Halogen 15 Watt 15 Watt
DENON Verstaerker als Stereoanlage 16 Watt 28 Watt
Summe 89 Watt 155 Watt

Durchschnittskosten pro 24h: 0,56 EUR (normale Freizeitarbeit, kein Freelancing)
Durchschnittskosten pro Jahr:  ca. 205 EUR

Minimal / Maximal:

Mit Minimal meine ich, dass der die Helligkeitseinstellungen beim Display bzw. die Lautstaerke der Stereoanlage minimal war. Maximal heisst beim Display volle Helligkeit und Lautstaerke etwas ueber Zimmerlautstaerke.

Fazit

Interessant was die Helligkeit allein ausmacht. Auch wenn alle Geraete ausgeschaltet sind, werden noch ca. 5 Watt gezogen. Das gilt auch fuer den Sleep-Modus des Macbooks, wo immer noch 9 Watt messbar waren, auch wenn alle anderen Geraet ausgeschaltet waren.

Aus diesem Grund schalte ich ab jetzt immer die komplette Steckdosenleiste ab, wenn ich das Haus verlasse.

Resolving Hibernate exceptions with MySQL on Linux because of case-sensitive table names

Oktober 16th, 2009 | By Christoph in Software-Development | No Comments »

Today I tried to deploy my application from my OSX dev machine to a server on Amazon EC2.

I was running into exceptions like this:

Caused by: org.hibernate.exception.SQLGrammarException: could not load an entity: [app.model.entities.Jobs#8]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1895)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:207)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:822)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:815)
at org.springframework.orm.hibernate3.HibernateTemplate$3.doInHibernate(HibernateTemplate.java:569)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
… 30 more
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table ‘database.Files’ doesn’t exist

Although all my table names are lowercase it seems that Hibernate tries to create case-sensitive SQL queries which relate to the name of my POJOs (e.g. MyPojo) .

While this was working on my OSX it didn’t on Fedora Linux on EC2.

Solution:

In my /etc/my.cnf I had to add the following configuration property:

lower_case_table_names=1

Thanks to the following articles which helped me:

http://www.parkroad.co.za/mysql-case-sensitivity-table-names-between-windows-and-linux-systems

http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html

https://forum.hibernate.org/viewtopic.php?p=2394017

Resolving trouble with Ruby On Rails with MySql on XAMPP on OSX Snow Leopard

Oktober 9th, 2009 | By Christoph in Software-Development | 3 Comments »

I was trying to work through the Get Started guide of Ruby On Rails but I had issues when I was trying to run the command “rake db:migrate” the first time.

It was complaining that the mysql gem was not up to date or something and I had to do a “gem install mysql”. But that resulted in errors as decribed in the following blog posts:
http://wonko.com/post/how-to-install-the-mysqlruby-gem-on-mac-os-x-leopard#comment-5544

http://boonedocks.net/mike/archives/175-MAMP-and-the-Ruby-MySQL-Gem.html

I had issues as I was using XAMPP and “gem install mysql” was complaining about missing mysql.h I had to upgrade my XAMPP and additionally download the XAMPP developer tools.

Go to http://sourceforge.net/projects/xampp/files/ and download:
xampp-macosx-1.7.2a.dmg
xampp-macosx-1.7.2a-dev.dmg

Of course first I had to remove/backup my existing XAMPP folder under /Applications/xampp Then I installed the new version and after that the xampp-macosx-1.7.2a-dev.dmg developer pack. this xampp-macosx-1.7.2a-dev.dmg contains the mysql.h header files and so on. After that dev tools installation the XAMPP folder has a new folder called “include” which has all the mysql header files like mysql.h Then the following command line worked for me:

sudo ARCHFLAGS=”-arch i386″ gem install mysql — –with-mysql-dir=/Applications/XAMPP/xamppfiles/

Note I was pointing to the xamppfiles base-dir as the mysql-dir, because then all the paths are relative as if this would be the mysql root dir.

Update: But as I am writing this, I was running into new trouble. It seems I need to update to the 64bit MySQL Version because of Snow Leopard, because now I am running into new problems:

Couldn’t create database for {”username”=>”root”, “adapter”=>”mysql”, “database”=>”mydatabase”, “password”=>nil, “socket”=>”/tmp/mysql.sock”}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)

According to http://www.techskater.com/ruby-on-rails/problems-with-mysql-gem-and-rake-on-snow-leopard/ this seems to be related to Snow Leopard and MySql 32bit vs. 64bit. On Snow Leopard the Gem Mysql needs to be build against a 64bit mysql or so.

I am currently reading http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard to resolve the issues and update MySQL. Let’s see…

Update 2: So I have finally installed MySQL 64Bit as suggested on the page above and I can start it. A “rake db:create” has also worked now as I could verify via MySQL Query browser. Let’s see what is the next thing which is not working…

Update 3: It really looks I am done and I am currently working through the Getting Started Guide.

Conclusion: 3 hours wasted to get it running on my OSX Snow Leopard. I did the same yesterday on my Windows machine at work and I didn’t have any such problems. It seems to be only related to Snow Leopard.  You might just read this page http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard and ignore everything above, but I leave it in this blog post for further reference. The difference is that you might not continue using the MySQL from XAMPP but a standalone mySQL. But you can still use phpMyAdmin from XAMPP or MySQL Query Browser to manage your database.


Eclipse PDT and XDebug finally working

Oktober 5th, 2009 | By Christoph in Software-Development | 1 Comment »

Yesterday I wanted to give myself a little update what’s going on in the PHP world as I was deeply involved in Java/J2EE in the last 4 years and haven’t done any PHP work besides maintaining some of my websites.
So I decided to give the Zend Framework a try and started going through the Quickstart guide.

Something what always enjoyed in the Java world was the availability of easy debugging. In PHP in the past I have used ugly “echo …” debugging statements and never really digged any deeper. Now the time has come and I read that it is possible too but maybe with some more setup efforts.

Environment

The following articles helped me to get setup:

Even though the articles are pretty good I had some minor problems, especially figuring out what exactly I have to add to my php.ini.

That’s why I give you what I have added to the very bottom of my php.ini which lives in the following path: /Applications/xampp/etc/php.ini

[xdebug]
zend_extension=/Applications/xampp/xamppfiles/lib/php/php5/extensions/no-debug-non-zts-20060613/xdebug.so
xdebug.auto_trace=1
xdebug.remote_enable=On
xdebug.remote_host=”localhost”
xdebug.remote_port=9000
xdebug.remote_handler=”dbgp”

The xdebug.so was mentioned in first tutorial above and I got it from here (Komodo IDE: http://aspn.activestate.com/ASPN/Downloads/Komodo/RemoteDebugging)

Here are some important infos and screenshots which might be helpful in understanding my setup:

  • php executable: /usr/bin/php
  • php.ini: /Applications/xampp/etc/php.ini

One of the most important things was the Path-Mapping, which maps your Eclipse Project structure to the URLs of your web-application.
URL of my app in the browser: http://localhost/zendtest/ZendFramework-1.9.3PL1-minimal/quickstart
Local-Path: /Applications/xampp/xamppfiles/htdocs/zendtest/ZendFramework-1.9.3PL1-minimal/quickstart

This mechanism is important, because it tells the debugger when to stop. If those settings are not correct (and they weren’t correct in my case) then the debugger will not recognize to which file the current URL in the browser belongs to and basically will not stop or will not stop at where you have placed your breakpoint.

Screen shot 2009-10-05 at 6.43.22 PM

Last but not least some screenshots which show the my setup and configuration:

Screen shot 2009-10-05 at 6.40.32 PM

Screen shot 2009-10-05 at 6.41.29 PM

Screen shot 2009-10-05 at 6.48.11 PM

Screen shot 2009-10-05 at 6.46.41 PM

7 tips how to write better emails that people will understand

August 4th, 2009 | By Christoph in Allgemein, Software-Development | 4 Comments »

We all know it: We have a complex problem which we need to solve but we need to explain the problem first to a group of people to look for a solution or give sign-off.
That means we first need to get the attention of those people, make them understand the problem. If the problem you need to explain becomes complex it can be quite hard to do so.
Here are a few tips I try to stick to when I write my emails:

1. Purpose and Goal

Make the recipients understand purpose and goal. You are writing the email for a reason and not for the sake of writing a mail. Make that as short as possible.
Just as you do for your meetings: Give each email an AGENDA. People should know what your email is about before they read it.

2. Structure is key!

Structure your email into sections:
- Introduction & problem description: Why are you writing this mail and what do you expect.
- Symptoms: Say what you did, what you see and believe is wrong and what you would have expected to see instead. Never say: “Software XYZ is not working!“, because nobody knows what and how you expect it to work.
- Solution proposal: If you believe you have an idea what could be the reason for the problem and how it can be solved, then say that. But outline that this is only a guess. If you are wrong, then no problem.
- Conclusion: This is most important. After you have written and explained a lot, people are usually more confused than before. Summarize your mail in a few short notes.

3. Be polite

Never think your problem is the most important problem. Better always expect that everybody couldn’t care less about your stupid problem. If you think like that, you spent more thoughts into writing the mail and making a point. Do this in polite words. Remember you are dealing with human beings just like you who all have their own little problems, maybe a bad day and at the end of the day have to do their jobs – just like you. So just behave like you would expect it from everybody else too. If your colleagues are all assholes, then stand out of the crowd and do NOT be one!

4. Use screenshots, screenshots and screenshots!

In the IT and Software Development industry, problems are usually about software which happens on your screen. Instead of writing 10 long sentences about a problem you see on e.g. a Web page, just take a screenshot, add some red arrows and some circles to it and send it. If one screenshot isn’t enough, send another one.
Don’t even think of the larger size of your email. That doesn’t matter. Hard disk space is for free these days and productivity and information is more important!. Time is money.
I use SnagIt! for Windows or Skitch on my Mac for taking screenshots and adding annotations and stuff. Some of my mails consist only of screenshots because that might explain the problem without any words and the recipient sees exactly what I see. Make sure you use short keys to make screenshots. I just press the “Print” button which brings up SnagIt. It is possible to write a mail with a screenshot within 5 seconds.

Example:
It makes clear that I want to point out that specific part of the text.

 

5. Do NOT use abbreviations

Instead of don’t, write do NOT.
Instead of can’t write cannot
Always write the word not in capital letters and maybe bold, if you want to highlight that this is important. When reading emails too fast that small word can be missed and maybe it turns the sense of your mail upside down.

6. Favor notes or bullet points over full sentences

If you are listing a couple of related thoughts, then just put it into bullet points or a numbered list instead of writing it into full sentences. That has the following advantages:
- people can refer to each bullet point / numbered item which is very exact. You immediately know what they are referring to.
- easier to see what belongs together. In long sentences it can get hard to distinguish between the different things
- they are shorter and people can read it faster. If something needs more explanation, people will ask you.

7. Summarize your mail

Always add a conclusion or summary to your mail. After you have written a lot of text, people are usually more confused because of information overload.
Thus summarize your mail at the end in 3-4 bullet points. It helps to get back to the main purpose and goal of your mail.

My 10 favorite links on Software architecture, scalability and design

Juli 30th, 2009 | By Christoph in Allgemein, Software-Development | No Comments »

1. The Internal Design of Force.com’s Multi-Tenant Architecture (Video)

2. Dan Pritchett on Architecture at eBay (Video)

3. Orbitz.com Architecture with Brian Zimmer (Video)

4. Scalability Principles

5. An Unorthodox Approach to Database Design : The Coming of the Shard

6. Domain Driven Design and Development In Practice

7. The Challenges of Latency

8. LinkedIn – A Professional Network built with Java Technologies and Agile Practices

9. LinkedIn Communication Architecture

10. JAX TV: Java-Programmierung im Multicore-Zeitalter (Java Programming in the age of multicore processors) by Angelika Langer

Freemarker Templates: Layout / Decorators with Shared Variables in Data Model

Juli 24th, 2009 | By Christoph in Software-Development | 1 Comment »

This tutorial is about the Freemarker template engine and it explains how to build a custom Layout / decorator mechanism similar to Tiles.
I have build my own Layout mechanism similar to Tiles, because it seemed the easiest to me so far and I didn’t want to include the Tiles lib in my app to keep things simple.

Here is what I want to do:

I have 2 templates.

1. A template layout.ftl which defines the basic layout (the decorator)

1
${screen_content}

Note the ${customPageTitle} page variable which is what I am talking about here. I will come to that later.

2.A second template content.ftl which defines my content.

This is my custom content, which should be displayed in the the screen_content variable of the layout.ftl template.

This template should be able to set the customPageTitle in the layout.ftl by doing a

${assign("customPageTitle", "This is custom test title rendered inside the layout template")}

The point in this post is the expression ${assign("customPageTitle", "This is custom test title rendered inside the layout template")} .

Usually in Freemarker you would have written:

<#assign customPageTitle="This is custom test title rendered inside the layout template">

But the problem is that this will not write that new *customPageTitle* variable to the underlying dataModel so that it can be used in the layout.ftl.

But it is possible. Continue with the serverside servlet code which renders the templates.

3. Render both templates in the servlet / java code
In my Servlet I am basically doing rendering of the 2 templates.
First I render the content.ftl and then the layout.ftl. The rendered output of the content.ftl is put into the data model so that it can be outputed by the layout.ftl

1
2
3
4
5
6
7
8
9
10
11
12
 
// 1. parse the content
Map dataModel = RequestContext.getInstance().getContext();
dataModel.put("assign", new DataModelModifier(dataModel)); // this is my own custom assign function.
 
String customContent = TemplateRenderer.getInstance().render("content.ftl", dataModel);
 
// 2. pass it in the model of the layout
dataModel.put("screen_content", customContent);
 
// 3. Render the complete layout
resp.getWriter().println(TemplateRenderer.getInstance().render("layouts/layout.ftl", dataModel));

Note: TemplateRenderer, RequestContext are *not* Freemarker classes. They are own helpers in my application.

You ask at this point what is actually the class DataModelModifier and why can the content.ftl do a ${assign("customtitle", "This is custom test title set inside a different template")} ?

The DataModelModifier which makes a method available in Freemarker. They are used like normal variables. see Method in the Freemarker Doc.

This class basically gets are reference to our data model which we want to modify and in the exec(List arguments) method we are just adding a value to our dataModel hashMap.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
protected static class DataModelModifier implements TemplateMethodModel{
 
private final Map dataModel;
 
public DataModelModifier(Map model) {
this.dataModel = model;
}
 
public Object exec(List arg0) throws TemplateModelException {
 
if (arg0.size() != 2) {
throw new TemplateModelException("Wrong arguments. key, value required");
}
 
this.dataModel.put((String) arg0.get(0), arg0.get(1));
return "";
}
 
}

Finally...

What I wanted to achieve is that the content.ftl template can assign a value to the variable "customPageTitle" which is rendered in the layout.ftl template which is a different template.
By default FTL does NEVER modify the dataModel according to the Freemarker Documentation under: http://freemarker.sourceforge.net/docs/pgui_misc_multithreading.html

From Freemarker doc:
It is impossible to modify the data-model object or a shared variable with FTL, unless you put methods (or other objects) into the data-model that do that. We discourage you from writing methods that modify the data-model object or the shared variables. Try to use variables that are stored in the environment object instead (this object is created for a single Template.process call to store the runtime state of processing), so you don't modify data that are possibly used by multiple threads. For more information read: Variables

But exactly this is what I am trying to do, and at the moment it sounds totally reasonable to me. I just don't know what the pitfalls are. I know that there are some risks too:
It can be kind of dangerous and error prone practise to let FTL modify the dataModel because it can lead to accidently overwriting of values which lead to strange errors.
But on the other hand it gives you great flexibility to build web applications. I will see when I will run into first problems with this approach.

Conclusion
Here is what we need in order to build our own custom template layout / decorator:

1. 2 templates. one for the layout and one for the actual content.
2. We need to render the content template first and make the output available in the layout template.
3. we need a custom class which allows the dataModel to be modified inside the templates via FTL.
4. we need to make this class available in the dataModel as a Freemarker Method. That means this class needs to implement the TemplateMethodModel interface
5. The content template uses this custom method (based on that class): ${assign("key","value")}

I hope that this tutorial helps somebody out there too.