Progress on Java 6 on Macs
December 19, 2007 | In Java | No CommentsApple has released a new preview of Java 6. Only for Leopard and only for Macs with Core 2 Duo or Mac Pros.
This is a step in the right direction.
Service Locator vs. Dependency Injection
June 25, 2007 | In Java, Javaone | No CommentsLast year at Javaone I attended a session on API design held by some Netbeans developers. I remember I thought they used a strange definition on Dependency Injection (DI). Now when I see this years session on the same subject (Modular Programming with the NetBeans Platform, TS-3742) I still think it’s strange:

DI should use the “Hollywood Principle” - don’t call us, we’ll call you. Your Java class should get its dependencies set or wired in the constructor. It should not call out to its environment to resolve them.
The code sample above is using the Service Loader (SL) feature in Java 6. You specify somewhere under META-INF in the jar file what interface you implement. Then the client code can ask for an implementation of this interface in runtime. SL is “pull”, DI is “push”.
There is nothing wrong with this style, but it is not DI. It’s hard enough keeping up with the different idioms and patterns so please use well established names for things.
Javabeans, BeanInfo and cglib
March 2, 2007 | In Java | No CommentsHave: Pretty big Javabean object model with BeanInfo classes. No support for event listeners or firePropertyChange() in setters.
Want: Same, but with support for adding PropertyChangeListeners. Preferably without changing the object model code.
The object model is now supposed to be used in a Swing application based on Netbeans Platform. The platform has these nice wrappers from Javabeans with BeanInfo classes, showing the icons and configuring the Property Sheet for us. Nice.
When we add support for PropertyChangeListeners we also get instant update between the Explorer view, the editor window and the Property Sheet in Netbeans. Really needed to give a good UI.
I got inspired by Xavier Hanin to use cglib to generate support for firePropertyChange from my Javabeans, by letting cglib implement an Observable interface like this: Observable.java. It inserts all the code needed to register listeners on the properties of every Javabean.
It works really well, except that it breaks my use of BeanInfo classes. I guess this is because cglib decorate the Javabean by extending it. The new class is called something like MyBean$$EnhancerByCGLIBf989dfbb, and then the resolving of MyBeanBeanInfo fails.
But wait, if cglib implements Observable on the fly, why can’t it also implement the BeanInfo interface? It’s an undocumented feature that the Interceptor first checks if the class itself implements BeanInfo, before it looks for a MyBeanBeanInfo class (and if that doesn’t exist it fall back to Reflection).
So I let cglib add another interface to my proxy Javabean. Since I have access to the “real” bean in the proxy I just get the BeanInfo with the Introspector and delegate all BeanInfo methods to it.
It worked fine when I tested it:
BeanInfo proxyBean = (BeanInfo) BeanEnhancer.enhance(myBean);
proxyBean.getBeanDescriptor().getShortDescription()
It returned the description set in the BeanInfo class. Sweet.
But it fails when I use Introspector.getBeanInfo(proxyBean.getClass()). It fails somewhere inside Introspector.java. It’s something with proxyBean.getClass().newInstance().
java.lang.NoSuchMethodError: MyBean.getAdditionalBeanInfo()[Ljava/beans/BeanInfo;
at MyBean$$EnhancerByCGLIB$$dd36606c.getAdditionalBeanInfo()
at java.beans.Introspector.(Introspector.java:371);
I really liked the idea though…
Temporary in Netbeans land
February 18, 2007 | In Java | 2 CommentsI’m building a desktop client based on Netbeans Platform and therefore I have left Eclipse for a while. Now after three weeks with daily interaction with Netbeans IDE, I want to summarize my impressions.
Pros:
Matisse. Wonderful. Nuff’ said.
It’s nice to use Swing. It feels really snappy on my Mac. The look and feel is good. For me, SWT’s only advantage is Eclipse. I would not build a RCP on Eclipse/SWT.
Cons:
CVS integration. I miss the sync view in Eclipse every day. I constantly miss some file so that the build fails on my other machine, or my co-workers. We have also checked in binary files in ascii mode a few times. *.png was not recognized automatically as binary.
Error reporting. It always take a while to notice coding errors. I sometimes stop in the middle of typing, waiting for the errors to show and the small light bulb to appear. Eclipse is much better in that respect. Also the quick fixes in Netbeans are not as good as in Eclipse.
I’m mostly doing plugin projects and they behave different from Java projects. I can’t find a way to add more source directories. I can’t seem to run my unit tests in the plugin projects either.
Manage the classpath and source code attachments is also harder in Netbeans. This is especially true for plugin projects with the Library Wrapper concept. I know it’s there to help me to isolate my modules and their dependencies, but it’s not straight forward. My co-worker discovered that you could add more than one jar file to a library wrapper module, but that was not obvious.
After three weeks I would love to go back and continue working in Eclipse. I guess I could do that and use the Netbeans generated Ant script to build the project, but it would be a lot of work setting everything up in Eclipse, I think. I depend a lot on the Netbeans APIs.
Anyone out there who uses Eclipse to build apps on Netbeans Platform?
Program Blu-ray features in a free language
November 15, 2006 | In Java | No CommentsWhile some people say that Blu-ray (and HD-DVD) already are obsolete I still think they will be useful for a good number of years.
One possible killer feature in Blu-ray is the Java ME virtual machine that will be in every Blu-ray player. If a lot of companies and individuals start using this to add interactive and networked features to the disks, I think we will see a lot of cool things happen.
For Java developers this could be really exiting. Anyone seen any Java written for Blu-Ray disks yet?
And the Java platform used is free.
Links:
Who would use Mono now?
November 14, 2006 | In Java | 16 CommentsWith Java available as default in every Linux distribution (made possible by the move to GPL) I think Java has a great future on Linux. Better integration into the Gnome and KDE desktops would be nice, but I guess that is just a matter of time now that it’s Open Source.
Contrast this to Mono, the (sort of) free .NET implementation available on Linux and Mac OS X. The deal between Microsoft and Novell was aimed at assuring people that Mono was and will be free, and that Microsoft won’t sue anyone for implementing their patents. But the opposite happened. Everyone started looking really close on the problems with Mono. If you are a Novell employee or non-paid Mono developer you are not going to be sued. But what about another company investing resources in Mono? You can be sued. Is this really in the spirit of Open Source?
You can call it FUD, but for me it’s a question of investing my time and energy in technology that are and will be available on the platforms I use (Mac OS X and Linux). I don’t trust Microsoft and I don’t trust Novell anymore either. Who knows when Microsot will bring out its patents and kill Mono use outside of Novell? I think .NET is good if you want to develop for Windows only. For cross platform development I would not use it - I would go for Java.
Is OSGi something for us?
October 24, 2006 | In Java | No CommentsOur software is a Java web application. Administrators can upload XML files containing descriptions of user interfaces and logic. The XML describes something like small applications executed in a runtime environment that is the web application.
When the XML files get executed (by a user selecting a certain “sub application”) the runtime will instantiate the needed Java objects and load different kinds of templates and resources. They will mostly use code from the runtime, but sometimes an XML file calls for custom code, that needs to be provided separately. Ideally the administrators should be able to supply additional jar files containing logic and resources, without the need to restart the web application.
We let users outside of our organization develop extension and reference those from their XML files. Sometimes extensions has dependencies on third party libraries. We sometimes end up with a situation where two extensions require different versions of a dependecy. “Jar hell”.
An analogy would be a set of Excel workbooks requiring different DLL:s.
Will OSGi apply to our situation? Will we be able to add new classes and resources to a running application? Will we be able to handle conflicting dependencies?
Axis and .NET
October 23, 2006 | In Java | 1 CommentI have been banging my head against a SOAP service deployed on .NET.
First the service used SOAP headers for authentication, instead of simple Basic Authentication. Axis doesn’t seem to generate code to add the extra headers so I had to manually add it to the call:
SOAPHeaderElement auth = new SOAPHeaderElement("http://svc/","AuthenticationHeader");
auth.addChildElement("Username").addTextNode("foo");
auth.addChildElement("Passwd").addTextNode("bar");
But the .NET service did not accept this. The code generated from WSDL with Axis (wsdl2java) adds namespace attributes to all elements:
<mew:AuthenticationHeader>
<mew:Username>foo</mew:Username>
<mew:Passwd>bar</mew:Passwd>
</mew:AuthenticationHeader>
If I manually removed the namespace attributes, authentication works:
<mew:AuthenticationHeader>
<Username>foo</Username>
<Passwd>bar</Passwd>
</mew:AuthenticationHeader>
I took a look at the WSDL file and found this in the XML Schema part:
<s:schema elementFormDefault="qualified” targetNamespace=”http://MEW/”>
Qualified means that you need to add namespaces to the elements. It seems to me that .NET has messed this up. If I change the WSDL to
<s:schema elementFormDefault="unqualified” targetNamespace=”http://MEW/”>
The Axis client works fine and the generated SOAP request does not include the namespace for Username and Passwd is removed.
Who is to blame, Axis or .NET?
New books - good price
September 16, 2006 | In Java | No CommentsSome publishers allow reprints to be sold for a lower price in poorer countries. Since I live in one I can take advantage of that. Today I bought:
- Code Complete, second edition
- JavaServer Faces in Action
- Head First Design Patterns
- Pro Java Swing, third edition
- Java Development with Spring Framework
I paid around $65 in total.
In some book the print and paper is not as good as it would be in Sweden, but it’s totally readable. Some books are almost the same.
Profiling article wanted
September 14, 2006 | In Java | No CommentsWe are in the process of releasing a new application. During the development we have not spent a lot of time trying to optimize performance (I am not gonna quote you-know-who here…) and focused on clear and maintainable code, knowing that we will do a disciplined performance test in one of the later iterations.
So, now we are here. I haven’t used profilers that much, and when I have, the memory leak was spotted in the first snapshot. No need to dig deeper. This time there are no obvious suspects and the data doesn’t really speak to me.
Ok, so the recursive method in the Foo class is pretty expensive. Not a big surprise. So will I gain anything on the bottom line if I spend an hour making it faster? Or is the bottleneck really somewhere else? How can I tell?
So I thought I’d hit the Internet for help. I found loads of articles telling me how to profile with JProfiler, Netbeans, Eclipse, YourKit etc. But I can figure out that myself - it’s not hard to configure Tomcat and a profiler. The hard part is the methodology. How to go about finding the bottlenecks. Performance testing is hard.
So if you know a good online article that isn’t pushing a certain product but tells what questions I should ask the big amount of profiling data I have available in front of my eyes - please post a comment.