Posts Tagged ‘java’

h1

Unifying your Web Applications with a common look through Apache Server Side Includes (SSI)

October 17, 2011

Most larger organizations have a set of web applications exposed to their customers. Some of those applications are grown in-house, some have been purchased. Each application has it’s own release cycle and therefore it becomes quite a bit of a problem to make common changes to all applications. Say your company has a new logo or is adding one more application to the top and bottom navigation of all sites, you end up having to touch each application and make the required modification in the header and footer, go through a testing cycle and redeploy all applications at the same time in order to make sure the site looks consistent. Read the rest of this entry ?

Advertisements
h1

Writing a Thick Client Java Application with EXTJS

October 20, 2010

update: example application

I have been writing thick client applications in Java for a while now. You can produce nice applications with Swing if you know how to and have the right tools and libraries at hand. Lately, however, some of our clients made me rethink the approach for thick client development. We were asked to develop a thick client application for Windows, Mac OSX and Linux. The customer also pointed out that in the future they would like the same application to be web based. This made me rethink the Swing strategy and bring back an older concept we developed for a customer back in 2000. The project is still online at http://www.tisca.com/tisca-tiara/handtuft/select/en/index.html. It’s an online carpet configurator allowing the customer to color and custom order a carpet. The project was initially delivered over the web but was also distributed to stores on a CD, allowing the store owners to run the application on a local computer without an internet connection. The approach taken was simple: an autostart executable on the CD runs a webserver as well as a web browser without navigation bars, etc, as one application. To the store, the application looks like a thick client, on the web, it’s just another AJAX style web application.

Back to 2010, we know have libraries such as EXTJS that bring an application look and feel to your web application. Out of the box, EXTJS supports more feature rich components than are supported by Java/Swing. Firefox is a great browser and the mozswing project allows you to bundle Firefox into your Java/Swing application. if you combine this with an embedded web server such as Jetty you can write and deliver a thick client application the same way you deliver a web application. The hybrid approach allows you to handle parts of your application such as file dialogs, etc in Swing. Once a user selects a file to load, you can make a javascript call from your Java/Swing application into the MozSwing browser window to communicate back to your local web server to load the file.

Using this technology stack allows us to deliver a rich thick client experience and at the same time reuse most of the code, apart from specific features such as file dialogs.

The following is a screenshot of the current application we’re building:

Please let me know what you think about this approach. If there is enough feedback I’ll follow up this post with an example application for you to play with.

An example implementation can be found here https://ruben42.wordpress.com/2011/06/01/example-thick-client-app-with-extjs-and-java/

h1

Catching all Runtime Exceptions in Swing

March 30, 2009

[note: see my post about writing a java thick client applications using extjs]

So you got some framework that you are using in your swing application and it throws a runtime exception. The Exception is thrown all the way out and ends up on the system.out – very nasty, not good looking and doesn’t help us determining that there is a problem in our application. We might just go on and do the same task again even though the exception was telling us that something went fatally wrong and that it probably would be a good idea to change the state of the application, go into recovery or terminate gracefully.

What can we do to prevent this? We can use try/catch clauses all over the place whenever we call some third party code but that is neither fun nor good code. And still there can be some exceptions slipping by – or are you really sure your code never throws a null pointer exception? Really?

There is an easier way to deal with this problem: All swing activity originates from the event queue and therefore all events are thrown to the event queue as well.  So in order to catch all runtime exceptions such as a null pointer exception we can just use the event queue.

We just need to extend EventQueue and overwrite the dispatchEvent method.

class EventQueueProxy extends EventQueue {

	protected void dispatchEvent(AWTEvent newEvent) {
		try {
			super.dispatchEvent(newEvent);
		} catch (Throwable t) {
			t.printStackTrace();
			String message = t.getMessage();

			if (message == null || message.length() == 0) {
				message = "Fatal: " + t.getClass();
			}

			JOptionPane.showMessageDialog(null, "General Error", message, JOptionPane.ERROR_MESSAGE);
		}
	}
}

The proxy class still calls it’s parents dispatchEvent method and catches anything throwable. When it does, it pops up a simple dialog.

Only thing remaining, we need to install our new event dispatcher on the event queue.

EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
queue.push(new EventQueueProxy());

Using this method one can also use exceptions to handle errors throughout a swing application – dealing with errors and exceptions ends up at one place in the application this way and can be used to transform the exceptions into human readable error messages.

For some other helpful links for building swing applications have a look at helpful-java-swing-articles

h1

Analyzing Open Source Projects as a Computer Science Class

January 25, 2009

Looking back at my time studying computer science I remember I learned a lot from the operating system class. The professor implemented his own variation of Xinu (http://en.wikipedia.org/wiki/Xinu) and then discussed parts of the implementation with us and left some things as an exercise for us to implement. It was one of the harder classes since there was a lot of lab work to actually complete the assignments but I still think about what we did in that class a lot and it gave me a good insight that I still use today.

Since I graduated a lot happened in our industry – as everybody knows, there are more open source projects than one can count. The lessons I learned from Xinu – looking at an architecture and analyzing an implementation – could also be learned from analyzing multiple open source projects and comparing their architecture and design, judging their quality, elasticity (how easy is it to extend and advance them and in what areas), appearance and complexity (complexity of code compared to functionality and overall learning curve complexity to start work on the project).

Analyzing existing code in such a way can be a good entry point into understanding projects and preventing the ‘can not fix because not written by me/it’s not good we have to redo this’ attitude a lot of developers have and would help giving a developer a better feel when a project can not be saved anymore.

I therefore suggest to add a class to computer science studies where the students take apart a set of open source applications and write a paper on their findings. It would be great if those findings should then also be published and available to everybody on the Internet in some public form.

h1

Running Java in .NET

August 13, 2008

Did you write a java library but now some of your customers would like to use the library in their .NET code? Or do you have a .NET library and you need to integrate with a java application? There is this great tool out there called IKVM (http://www.ikvm.net) – it allows you to cross compile the byte code to the .NET CLR and then run it on top of the .NET framework (swing excluded).

If you have not yet, you have to try this tool. We used it to bridge a java RMI client library to .NET code. So now the .NET application calls the Java RMI client that then calls it’s Java RMI server. Sure, we could have implemented a .NET client to call the server side but this was easy (a days work to play with the tool, get to know it and do the implementation).

Would love to hear from you what you think about IKVM and what you’re experience with it was.

h1

How many objects are too many?

August 11, 2008

Say we are writing a desktop application such as a simple spreadsheet. We could represent each cell as an object and if the cell contains a formula we could implement a listener infrastructure in order to get notifications when a referenced cell is changed.

If we take a 8 x 8 spreadsheet and put a formula in each cell that references 2 other cells we are at 64 cell objects, 128 listeners and probably 64 listener lists. That’s about 256 objects. If we start creating a larger spreadsheet with say 64 x 64 cells that have the same amount of references per cell we are already at 16’384 objects that will be lurking around in the permanent memory. And the worst of all, those objects won’t go away – they are in memory as long as we are using that spreadsheet.

Another design approach could be to store everything in a database. We create a cell table and a cell reference table. Now if we want to display a grid of cells we just query the database and get the data for all cells on the screen. If we cached the calculated value we are now done. If we did not cache the value we have to get each cell that is referenced in the formula and then the value from those cells (and of course continue calculating these cells as well). This can result in a lot of queries to find out what the actual value for a cell should be. In case we are caching the display value we have the same problem when updating a cell. We will have to recalculate all references and their references until we are done.

Both approaches have an issue with referential integrity (somebody has to check that). the first approach is a memory hog while the second approach uses to many queries to calculate the actual value of a cell.

Now what? We have two approaches that both have their drawbacks. Yet excel and open office calc have been around for a while and they both handle such a situation with ease and with a very fast calculation speed. How are they doing it? I played around with those two programs just a little to get a feel for how they may be solving this problem – I create a spreadsheet with a formula that touches as many cells as possible (each formula is dependent on the next one, etc) and then changed the initial value. Turns out open office calc is quite a bit slower than excel. Turns also out that excel guesses the final value and may change it after the calculation is done (create an exponential function, change your value from 1 to 2 where your result changes from say 1 to 4, now change your initial value from 2 to 3, excel will guess the result as 8 and then later correct it to 9).

I made the following two observations after playing with these applications:

  1. Memory usage is low even with larger spreadsheet. Using a file (virtual memory) to store all the data and the references is probably used
  2. If there are no circular references there are cells with 0, 1, 2 … references to other cells. The cells can be seen as a graph that can be processed. One should be able to order the cells in the order of their references and in order of their dependencies. One should then be able to run through all fields from top to bottom and calculate the result (should explain this one a bit more)

Approach taken: We opted for a NIO based file structure (memory mapped file) to store the cells and their references. This allowed us to have a faster access to any cell in our application. We can request a given cell from the NIO storage and get a wrapper object that actually only reads the data from the storage if it is really accessed. The lifet5ime of the wrapper objects was pretty short (get the object, render the value, release the object). These measures drove the overall memory usage down (the memory mapped file is not part of the regular memory limit of java).

We also created a differential calculation engine (our formulas were finite thus we could optimize the calculations and write code that can da a full recalculation of the spreadsheet as well as a differential calculation that would affect a small set of cells).

Love to hear your thoughts about this post

h1

Make changing your Application Easy

August 8, 2008

There we are – writing applications for a vertical market – we get some specs, we have to understand what the business people want from us and then we have to implement it. After a while we’re done with the application and then the business people find out it’s not what they wanted. Where did we go wrong? Sometimes it’s the business analysts/developers that did not get it right, sometimes the vision of the business people/process owners is not clear enough. Who knows. I think the question should be ‘what can we do so there is an easy way out of that situation’? The second problem I see is that once an application is deployed in the business world the requirements change – a new marketing strategy, new laws or just another company policy may require changes. We have to go back and change a running system, retest, redeploy, etc. My question again: What can we do so there is an easy way out of that situation?

This brings me to the following observations:

  • Business users like to change stuff around
  • Business users like to modify text and marketing information (especially if it’s customer facing)
  • Small to medium changes are required over the lifetime of an application to adapt to new situations

If we do not want to engineer ourselves into a corner and end up with work that we do not want to do in the long run we have to find a solution for the above requirements. Those requirements translate into the following for me:

  1. Make sure an application can easily be deployed
  2. Give the business users a tool to change the appearance of the application
  3. Use a DSL, XML structure or properties file to drive most of the application

Some of the projects we have done in the last two years are web based applications. I found by using a CMS or WCMS system such as magnolia we can build on a solid foundation that takes care of 1). We then store all properties used for the application in the content management system  as well sattisfying 2) and we added a form renderer and task engine to the CMS in order to sattisfy 3).  We also get full access management through the CMS and since magnolia is based on jackrabbit (JSR-170 compliant content repository) all content is stored in a way that other applications can access it as well.

Things that became easy to address because of magnolia as a base framework:

  • changing any text on the site
  • moving pages, form elements tab orders, validation
  • changing the look of the site
  • error handling
  • rewriting all form pages to support ajax
  • implementing the forms in a different framework
  • allowing form handling for different browsers in different technologies
  • staging to life deployment through an activation and review process

This list is not complete – by choosing a CMS and implementing the required functionality on top of it we are able to move a lot of the work that seems to come up because of misunderstandigns between the business users and the developers into the hand of the business users (they can take action themselves) or make the change easy for the developers (it’s just a property or two to change in a UI instead of having to change it in code, recompile and redeploy).

I’d love to hear from you what other approaches you take to solve this issue.