Wednesday, October 6, 2010

Problem with Grails on Glassfish v3?

I almost gave up trying to run grails applications on Glassfish v3. Today I stumbled on a solution that was so easy that I should be ashamed I did not investigate a bit further. The problem is with some OSGi headers and is documented here.

Others have had the problem and the solution I found on nabble was pretty simple:
Add the following line in BuildConfig.groovy in your project that should run on a Glassfish v3.

grails.project.war.osgi.headers=false

This helps with the problem not being able to deploy a grails app on Glassfish v3 when you experience the following hints in the log on GF:

SEVERE: Module type not recognized
SEVERE: There is no installed container capable of handling this application com.sun.enterprise.deploy.shared.FileArchive@68237947

Friday, August 20, 2010

Mountain climbing as analogy to programming.

I just had vacation and I do not know why, but suddenly I got to think that walking/climbing a mountain (as that is one of my favorite things to do at vacation) could be used as an analogy for programming and learning programming.

As a starting point we have a geek that have no physical ability to talk about (because geeks seldom do sports or go outside the house).

Start with easy tracks

To be able to climb a mountain you need to be fit. If you start with a very steep path without being fit, you will get exhausted or fall down and die.

Also keep the distance short so you do not overuse your muscles. you may want to take a hike to morrow as well and with aching limbs it will not be that easy or fun.

That is to programming: start learning an easy programming language, do a little bit of learning every day, and eventually you will get it. Doing to much makes your brain tired and you may be so bored with the stuff that you may quit and do something else.

Increase the difficulty gradually

Having started the tracking experience, it may be boring to do the same path every time and after a while your fitness will tend to stagnate and adapt to what you are doing. That is, walk longer paths, and also more steep paths, the body tends to adapt to be able to handle the demands, but not more than necessary. We need to increase the work load to be able to handle more and more.When we are fitter, we can handle more difficult paths. However, even if it is good to challenge yourself a bit, do not overdo it (you may fall down and die). When you climb the mountain higher you will get a more panoramic view, it is beautiful and you can also start to see the path you took, and maybe also see different paths you can take to get to the same place.

That is to programming: Do not just learn the basics, they are good to have but can not help you in the long run, specially not if you want to have it as a profession. do the basics, repeat them, but when you feel comfortable with them, increase the amount of code you write, and also the difficulty of the code: that is, start doing design work, object oriented programming etc. Knowing the basics may help you write simple routines, but climbing higher will give you a better view of the surroundings and starting to put things together or how to do different designs to get to the same result.

Walk different mountains

No we may not have made it to the top of the first one yet, but does that really matter? Not really, the last part of getting to the top of a mountain may be easy, but it is usually not. Take Matterhorn, or Mount Everest for example. A lot of people climb them, but not all of them go for the top. you can go to the mountain and go as far as you have the time to do, or as high as you have the strength to do. this does not mean that you do not have a meaningful experience or doing a bad work. You doing as much as you can do or are willing to do, and even if its it not to the top, you may reach much higher than most people will do in their lifetime.

If you think that you did a mountain as good as you can or that you did it long enough to get bored. Try another one, it will give you another view, different tracks. It may have paths that is easier then the first mountain, but also paths that is harder. Maybe it will get you higher quicker and let you see more, maybe the paths are harder but the view is more rewarding than the first one.

That is to programming: Learn different programming languages, the first one you learn may not be a very good one to use professionally, but it may be a good language for learning basic programming. The new language may give you more opportunities to solve things quicker than the first one you learned. It may also be harder to do stuff in it, but when they are done, it may be better, quicker to do what you want it to do. for example Java may be easier to learn and use than C++ but performance wise, some C++ programs do better. On the other hand Java is more platform independent than C++, so it may be a better option for some companies. 
Different language can solve things easier than in other languages, or have better performance. the choice of language depends of the application you want to make. Learn more than one to widen your options and you will be better at your work.

So when do I get to the top?

Ok, lets be realistic. You may never get to the top. It depends on the mountain you decide to climb. Take Mount everest for example, it is the highest mountain we have, only a few made it to the top, and even fewer without injuries.

Take most mountains that is not a walk in the park, you may never reach the top, but you can get far enough to be satisfied with the experience and the rewards it give.

For programmers: not all programmers learn absolutely everything about one single programming language. they leave the top part for experts, that are fit to do the advanced stuff. 


Anyway programming is not really about doing the most advanced stuff or the most dangerous stuff. It is about doing stuff that other can follow. someone is coming after you and may have to maintain stuff you did. If it is to difficult they may not be able to go all the way, or understand how the heck you were able to get up to that cliff. programming is about leading the way for other to follow, you take a path that is fairly easy, sometimes not so easy but still manageable, you draw a map so that other can take the same path and continue from there.

So what are you waiting for, go out and climb a new mountain today!

Tuesday, May 4, 2010

Struts2 on Glassfish v3

OMG. It was not that trivial to get it going, at least not using Netbeans.

First trying to use maven since I need to have some EJBs, but I gave up after not being able to get the archetype for Struts2 do any project for me, first complaining about no commons-collections-3.2.1, downloading it and got a little bit longer, but then could not find anything for struts2.1.6.

So I went on creating a Struts2 project and got it to work by pointing out the jar files needed in Netbeans JavaEE webproject for Glassfish, YES there is a lot of jarfiles depending on each other since I also want to use TILES. Anyway, when I wanted the use the EJBs I had to create a EAR-project to hold everything together, what happens then?

Struts2 stops working, complaining of a lot of classes not loaded or cannot find. I spent almost all the afternoon before finally finding the problem.

Netbeans ant-script wants to copy every f...ing jar file that is not a TLD to the lib of the EAR, NOT into the Web applications WEB-INF/lib, which then makes sence for the errors in the log: when loading from that EAR/lib folder, it can not find struts-core classes since that jar file is in the Web-app because it includes TLDs,

So finally solved, hack the build-impl.xml and copy ALL jar files to the web-app so it works.Why Netbeans do this way I do not know, maybe someone can inform me?

Friday, March 26, 2010

Tutorials

I have started a Java tutorial for starting up with Java. It can be found at my Google-site. The tutorial is basic Java.

http://sites.google.com/site/ironicprogrammer/tutorials/ironic-programmer-java-tutorial

I will continuosly add more stuff to the site. Even if the first lessons may be a bit text to read. I will try to do programming by examples.

Sunday, March 7, 2010

Scary, but maybe true

A week ago I read the blogpost "The Non-Programming Programmer" by Jeff Atwood at Coding horror about that many applicants for programming work can not program at all.

During my 10 years in the business I experienced a few myself. I wish I then had the examples to give and exclude some that can not program.

Some examples:

What you can see as a teacher

At my first job as a teacher at university, part of my job was to lecture Pascal and C. Included was laborations in the language learned. Here I could see some serious laziness or lack of logic. One of the first tasks used to be to print out a matrix of the numbers 1..100 in a table of size 10x10. Of course this task is to learn some basic structures in the language learned like iterations as for-loop and conditionals like if. This should be a fairly simple task, but one guy actually just did a
and so on.
Another example is the laziness of students. Some did not want to put in some work to learn the language, they just wanted to pass the class. There is different ways to do this. One is to try to get a group to add your name to the report, another way is to copy someone elses work. Now the stupid part of this, a teacher notice more than you may think. Someone that is not able to manage simple task and then all of a sudden turn up with a solution that looks pretty similar to the brightest student or that of someone that the teacher helped and put in some pattern of text that was put there as an example but is failry recognizable since it was a value you do not just put in.

About applying programmers

Here we have programmers(? or not) that actually have a bigger mouth then ability to program. They do good at an interview and get the job. Later it turn out.

  • that they do not at all know anything about Java, and the excuse is that they hoped that they soon shuould catch up and learn.
  • the one I heard from the boss that applied (this was early 2000 when the market was hot). That they heard that the company was looking and maybe this person could be a candidate because he did som Basic 10 years ago and was thinking maybe he could do some programming.
  • I have noticed, at an interview, think really carefull when people start to talk about project they have been in and saying: "In this project WE used this and that language/database." or "The application I worked is build in this and that technique and this database". The problem with this is that some uses the knowledge of others in their CV, just because they were in a project that had a very nice arhitechture does not mean they did anything of it.

Programmers at work

  • One consultant I met at a task where we came in to look at why the clients web build in asp was so slow. Well the consultant they had there that have been working on the web did not know any SQL so in 4 different frames in a framset did a full lookup on the resultset of a table looped until the key was found and then used anothere key to do the same on another table. 
  • A programmer I worked with got the task to do some of the work when we were dividing an existing Session EJB into 3-4 smaller ones and at the same time change the medthods to return Collection of objects instead of a ResultSet. He actually did that, that is moved the code from the old one to the new one. What he missed was that the work is actually not finishe just because you move code, there is actually placed were you use the code, like a client that ALSO needs to be changed if you change the contract and move the methods it need. WHOOPS!
  • Another man I worked with said he knew Java EE after he went throug a book, downloading the examples and compiled and run throug them once. But, when he did some actual coding, he did not reuse the methods I already had done, he made new ones with other names.
  • Some programers seam to have the philosophy. "If it compiles and run it must be right". NO it is not. Making the compiler happy is easy, making the application work is not the same as keeping the compiler happy, it is logic not syntax.
So that post was very welcomed and will be used in the future.

While we are at it, try to solve this one with recursion:
You have a class called Org to represent a organization. The root organization is the starting point and every instance of the class holds its own suborganizations and a reference to its parent.


Org.java
Your task is to print the organization tree starting from the root. 

Please share your own stories in the comments.

Friday, March 5, 2010

File upload in JSF2 using base class

Surfing the net for JSF2 resources can be a tedious task. Actually for file upload I actually found a couple of blogs describing how to do it the firs I found actually only (FileUpload with JSF 2 and Servlet 3.0) made a taglib based on the second one. And it includes maven which I do not use.

I had to remake the example a bit since I got question to provide a sample application. When extracting the code I noticed it did not work anymore. The reason is probably the one mentioned at this blog: http://mcatr.blogspot.com/2010/01/multipart-requests-and-jsf.html.To make it work again, I did a solution that I am not happy with but it works. It takes the idea from this blog, joined with some from the other blogs mentioned. The former blog states that a multipart request to JSF 2 do not work, because it can not extract parameters from that kind of request.

The problem is that for what I actually wanted it was not right and one was a little bit overkill. So as usual I took another approach. I got some helf from Uploading files with JSF 2.0 and Servlet 3.0 but that solution includes making a custom component and renderer, and is dependant on classes found in Uploading files in Servlet 3.0. I actually copied all the code and tried it...but I got a failure when loading the page. So I wanted something with less code. and simpler to use, and I have a case when I am going to store vCards in a database so a solution to save the file on disk means I have to modify code anyway.

I took the following approach:

  • Use standard html <input type="file" />
  • Use @ManagedBean
  • I first had a base class to handle the file and extended the Managed bean with this one, but since this stopped working I had to add:
    • A Wrapper to wrap multipart requests
    • A Filter to catch multipart request and transform them to the Wrapper type.
  • I also have a FacesUtil class to simplify getting request and parameters
Here is the code:

In the view (vCardForm.xhtml)

Class to represent the uploaded file (UploadFile.java)

I created a class to store some information about the file being upload, I can later choose if I want to store to disk or in a database, for example sending this to an EJB and the EJB descides what to do.

The managed bean (VCardBean.java)

The interesting thing going on here is that i initialize the base class with the request for use of the request. Probably I could have done this in the constructor of the baseclass, but something you have to have to refactor.

MultipartRequestWrapper


MultipartRequestFilter



Session bean for handling the storage.

For the example this only stores it in the session.

Finally the class to inherit to make it all work

This is now obsolete, but I keep it in case JSF will handle the mutlipart form in the future. But even then it may not be needed.

The only thing needed in the Managed bean is to call findFile("vcardFile") on the multipart request to get the file that is being uploaded and take action on that.

A sample project (for netbeans) can be found here: http://sites.google.com/site/ironicprogrammer/home/jsf-file-upload-example

Sunday, January 10, 2010

Netbeans tutorial "Generating a JavaServer Faces 2.0 CRUD Application from a Database"

I went through this tutorial today. Nice example, but there was something I did not like. As a geek I sit on a sunday and try out a tutorial on my home computer. Lazy as I am I do not want to start my MySql instance, since adding more running components tend to slow down my almost four year old Toshiba laptop.

Since I was going to run Glassfish v3 anyway I was thinking a better idea is to have the database in the form of JavaDB, so I tweaked the script to fit into Derby instead, not much tweaking to do actually and doing it made me happy to not be forced to start MySql (My mysql is by the way a bit old and installed the same time as I installed InstantRails long time ago, and probably needs to be updated). The insert script in the tutorial work more or less as it is in JavaDB just remove the 3 SET-clauses in the top and the COMMIT in the end and it should work.

The script can be found at my google-site http://sites.google.com/site/ironicprogrammer/home/jsf-2-0-tutorial-db-script