Monday, 17 December 2007
Tuesday, 11 December 2007
Emacs Using 100% CPU
I did run into a problem with EMACS hogging the CPU which is fixed by:
(custom-set-variables
'(semantic-idle-scheduler-idle-time 200))
Emacs Using 100% CPU: "quick fix is to set the Emacs variable semantic-idle-scheduler-idle-time to a large number (by default it is 2 seconds) so that the idle scheduler does not kick in (which is what is using all the CPU)!"
Thursday, 6 December 2007
Mylyn User Guide - Eclipsepedia
Mylyn User Guide - Eclipsepedia: "If an external builder (e.g. Maven, pydev, or other Ant-based builders) is producing output files that are being automatically added to your context because they are not being marked 'derived' as with Eclipse-based builders. You may note that such files are always show as interesting when they are generated or updated and can not be filtered away, since Mylyn expects all files that have changed as part of the task context to have interest. In this case you can explicitly exclude these files from being added to the task context the Preferences -> Mylyn -> Resources page. For example, if the output folder of the builder is 'target', you could set this the following way. Similarly, you could add a filter for '*.pyc' to exclude all files generated with that extension."
Developer's Guide - Google Chart API - Google Code
Wednesday, 5 December 2007
You WILL experience the Day of the Ninja -- December 5
Tuesday, 4 December 2007
Springframework Presentations
Friday, 26 October 2007
Free XML editors
Google brings up these free ones:
Cooktop: http://www.xmlcooktop.com/
XML Notepad 2007: http://www.microsoft.com/downloads/details.aspx?familyid=72d6aa49-787d-4118-ba5f-4f30fe913628&displaylang=en
Thursday, 18 October 2007
visualvm: Introduction to VisualVM
Tuesday, 16 October 2007
Which class or resource is being lodaed?
The code to do this is easy:
System.out.println(this.getClass().getClassLoader().getResource("applicationContext.xml"));
You can also find class files. To find java.lang.Object use
System.out.println(this.getClass().getClassLoader().getResource("java/lang/Object.class"));
The result is something like:
jar:file:/C:/Program%20Files/Java/j2sdk1.4.2_14/jre/lib/rt.jar!/java/lang/Object.class
Notice how the JRE version is in the output.
Retrotranslator
I needed to do some unit testing of some code that uses JavaMail. Poking around I found mock-javamail that does just that. However, it is written in Java 5 and I am stuck in the prehistoric world of Java 1.4.
At first, I converted the code to 1.4 but that leaves 2 different code bases. Then I was pointed at retrotranslator that would give a 1.4 compatible jar from 1.5 source. One code base. Nice.
Monday, 8 October 2007
Using Maven to create a one-jar executable jar.
Has instructions on getting the onejar-maven-plugin to work. This is similar to what UberJar used to to do. It takes all maven dependencies and places those jars into the output jar. It does not explode the jars as the assembly plugin is wont to do. Instead, it used the one-jar package that has a classloader that knows how to look into the jar file for other jar files. It makes for a cleaner jar file that is easier to maintain later.
It does allow the filename to be specified, but does not seem to allow manifest entries, like Implementation-Version.
Friday, 28 September 2007
ASP.NET caching based on a cookie
<%@ OutputCache Duration="600" VaryByCustom="StyleSheet" %>
and you have declare the following method in which we access the Cookie and pass the value back
public override string GetVaryByCustomString(System.Web.HttpContext context, string custom) {
if (custom == "StyleSheet"){
return "StyleSheet=" + this.Request.Cookies["StyleSheet"];
}
}
Given this method, you should be able to cache based not only on cookies, but any other string value or anything that can be converted into a string.
Monday, 20 August 2007
feature
What have they done with com/sun/corba/se/connection/ORBSocketFactory?
I was using the weblogic.jar but it is way too big. replacing it with wlclient.jar results in:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/corba/se/connection/ORBSocketFactory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at weblogic.corba.j2ee.naming.ORBHelper.(ORBHelper.java:113)
at weblogic.corba.client.ClientORBInitializer.initialize(ClientORBInitializer.java:84)
at weblogic.jndi.WLInitialContextFactory.(WLInitialContextFactory.java:29)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.(InitialContext.java:197)
at org.springframework.jndi.JndiTemplate.createInitialContext(JndiTemplate.java:105)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:83)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:121)
at com.ihc.phc.monitor.JndiMonitor.getStatus(JndiMonitor.java:29)
at com.ihc.phc.monitor.Monitor.getAllStatus(Monitor.java:21)
at com.ihc.phc.monitor.Main.main(Main.java:12)
Oh how pleasant. This error has been report as bug 6340079 which states:
Reason for compilation errors:
------------------------------
The following packages are not there on jdk1.5.0 but they are in j2sdk1.4.2How was this problem resolved? Remove the offending unit test of course.
1.com.sun.corba.se.connection.*
2.com.sun.corba.se.internal.core.*
There is also another thread on the subject stating basically the same thing but without helpful suggestions.
The only helpful suggestion seems to have been to try the wlclient jar from a newer version of the WebLogic server (9 or 10). I tried that and now the ClassDefNotFoundError goes away. Now there is a runtime error:
[java.rmi.MarshalException: CORBA MARSHAL 0 Maybe; nested exception is:
org.omg.CORBA.MARSHAL: vmcid: 0x0 minor code: 0 completed: Maybe]
You know, indecision is bad enough in humans, I do not need it in computers. Maybe it worked? Slight chance I would say. Ok, looks like the newer (9.1) wlclient.jar is not compatible with older (8.1) weblogic instances.
Funny thing is, the weblogic.jar with all its bloat works. I wonder .....
Looks like the weblogic.jar has it's own version of the ORBSocketFactory and that is does not use the sun one. Oh bother! Guess it is back to the bloated weblogic.jar for me.
Thursday, 16 August 2007
Watch "Advanced Topics in Programming Languages: Java Puzzlers, Episode VI"
Advanced Topics in Programming Languages: Java Puzzlers, Episode VI
1 hr 13 min 49 sec - Jul 23, 2007
Average rating: (23 ratings)
Description: Google Tech Talks July 23, 2007 ABSTRACT Java Puzzlers, Episode VI: The Phantom-Reference Menace/Attack of the Clone/Revenge of the Shift. Josh Bloch and special guest star Bill Pugh present yet another installment in the continuing saga of Java Puzzlers, consisting of eight more programming puzzles for your entertainment and enlightenment. The game show format keeps you on your toes while the puzzles teach you about the subtleties of the Java programming language and its core libraries. Anyone with a working knowledge of the language will be able to understand the puzzles, but even the most seasoned veterans will be challenged. The lessons you take from this session are directly applicable to your programs and designs. Some of the jokes may even be funny. If you loathed Episodes I--V, you'll detest Episode VI. Come early, because overripe fruit will, as usual, be given to the first 50 attendees. This is a repeat of a talk given at Google in May and at JavaOne 2007.
Want to see more cool videos?
Go to video.google.com/
Think you have an even cooler video?
Add it at video.google.com/videouploadform
If you're having trouble watching the video, try copying the following URL into your browser:
http://video.google.com/videoplay?docid=9214177555401838409&pr=goog-sl
Monday, 13 August 2007
LISP Cycles
I admit it, I like LISP. There are a certain set of programming problems that it handles elegantly. I feel the same about AWK, BASH, SQL and even JAVA. Each language has a sweet spot, that domain of problems it is well suited to solve. While other languages can address the issue, some are more painful than others.
My "perfect world of programming": Allow the programmer to solve the problem using the correct tool. That is, remove the "If all you have is a hammer, everything looks like a nail" attitude. I am sick to death of OR mapping in Java, as an example. If there were some way to build pieces using the proper tool and then combine them into a complete solution. See SEWER.
Monday, 18 June 2007
Felipe Ortiz's Blog: The Last JavaOne 2007 Wrap-up
JavaOne Wrap-ups
JavaOne wrap-up, from Gosling
JavaOne 2007 Wrapup, from Javalooby
JavaOne 2007 Wrap-up, Final Thoughts, and Open Questions, from Wolf Paulus
JavaOne 2007 Wrap-up, from Netzooid
JavaOne 2007 Conference Notes, from Juixe"
Tuesday, 5 June 2007
Linux.com | GNU Emacs 22 finally released
Linux.com | GNU Emacs 22 finally released: "GNU Emacs 22 finally released
Monday June 04, 2007 (04:50 PM GMT)
By: Joe 'Zonker' Brockmeier
Emacs fans, limber up your fingers -- there's a new GNU in town. Almost six years after the release of the previous version, the Free Software Foundation has announced the release of GNU Emacs 22. (Actually, 22.1, but who's counting?) This release includes support for GTK , drag and drop support for X, a number of new modes, and a graphical interface to the GNU Debugger (GDB).
The FSF has come under some criticism for taking so long to release a stable version. The previous release, Emacs 21, came out in October 2001."
Friday, 1 June 2007
Open Source Technical Support by OpenLogic
Open Source Technical Support by OpenLogic: "OpenLogic provides comprehensive technical support for the OpenLogic Enterprise platform as well as for over 200 certified open source software packages. The OpenLogic technical support model combines a traditional 'one number to call' approach with the strength of the open source community. Whether you have questions regarding the use of an open source software package, need help troubleshooting a complex issue, or have encountered a bug in a particular package, you can rest assured that OpenLogic has the expertise necessary to assist you."
Thursday, 24 May 2007
JavaOne Online Technical Sessions - Java EE
This includes the PDFs. The labs are also available. The multimedia should be added over the next month or so.
Wednesday, 23 May 2007
JPC - Computer Virtualization in Java
JPC - Computer Virtualization in Java: "Welcome to the JPC Project
JPC is a pure Java emulation of an x86 PC with fully virtual peripherals. It runs anywhere you have a JVM, whether x86, RISC, mobile phone, set-top box, possibly even your refrigerator! All this, with the bulletproof security and stability of Java technology."
JavaOne 2007 BOF 9529: Hudson
Slides For JavaOne BOF-2958 Dynamic Scripting BOF — PushToTest - The Open-Source SOA Governance and Test Automation Company
Here is the abstract:
Dynamic Scripting Languages and Java
Java 6 introduces native support for dynamic scripting languages (JSR 223, scripting languages and Java technology.) This BOF session brings together leaders, users, and critics from the Jython, Groovy, PHP, Ruby, and many other scripting camps for an exchange of ideas to develop a common understanding of the state of the art and practical examples of using dynamic scripting languages to solve problems.
-Frank"
JHat update jar
For those of you who came to Frank and myself's BOF session at JavaOne, you can download my 'modified' jhat here jhat.jar. My modification of original jhat include:
* add exclude functionality on query results to make it easier to filter out hundreds of duplicate reference chains, this way it will be possible to find unique leaks (see Figure 1)
* add classloader browser to nicely display classloaders' parent-child relationship, as well as number of classes loaded in each classloader (see Figure 2)
To run jhat, simple do 'java -Xmx512m -jar jhat.jar
JavaOne 2007 - BOF-8847: Developing Components for Java Business Integration: Binding Components and Service Engines
Presented by Frank Kieviet, Alex Fung, Sherry Weng, and Srinivasan Chikkala
Attendance: about 100
You cannot cover how to write JBI components in just 45 minutes. We were also not sure about what the audience was interested in. That's why we assumed that the audience would consist mostly of people who have never written a JBI component before, and are relatively new to JBI. That's why we decided to talk mostly about general information on JBI and JBI components, and highlight the power of JBI and discuss how to go about developing one.
As an experiment I wanted to try a new format (at least new for me): rather than slicing up the session into four parts of 10 minutes, we cast the session into a 'discussion forum'. Of course the questions and answers (and even the jokes) were well rehearsed.
Unfortunately, the audio/visual people that control the meeting rooms, had forgotten to start the session timer. As a result the audio was cut unexpectedly just a minute before we could finish up.
Nevertheless, I think it was an interesting session.
Presentation JavaOne07-BOF8847 (pdf)
Audio JavaOne07-BOF8847 (mp3)"
JavaONE 2007 - BOF 8034 and BOF 8745
I along with my colleagues presented 2 BOFs, and here are the slides and audio cast.
The presentation slides for the BOF 8034 are @
http://blogs.sun.com/bhavani/resource/BOF-8034.pdf
The presentation slides & audio cast for BOF 8745 is available in Frank's blog - http://mediacast.sun.com/share/fkieviet/JavaOne07-BOF8745-EESE.pdf
http://mediacast.sun.com/share/fkieviet/JavaOne07-BOF8745-EESE.mp3"
JavaOne 2007 BOF 5000: Glassitrope - Java Desktop Widgets
Due to a deluge of requests (both of them) here are the slides for our Awesome BoF 5000.
You can see them as a PDF or a QuickTime movie. I recommend the QuickTime if you can as it contains the rad transitions and animated jokes.
* QuickTime 7.4MB
* PDF 2.6 MB"
JavaOne 2007 BOF 3908 Slides: JAVA 3D API
Here are the Java 3D BOF slides that we presented at JavaOne 2007. Thank you to all those who came and participated.
* View HTML slides
* Download PDF file"
Mandy Chung's Blog: JavaOne 2007 BOFs slides 9123 and 2816
BOF-9123: Visualize Runtime Problems: A New All-in-One JDK Troubleshooting Tool"
JavaOne 2007 BOF 4108 Slides: Interoperable Web Services Security between Java and .NET with WSIT: Ease of Development and Performance
Thursday, 17 May 2007
Bug ID: 5084229 javax.naming.NamingEnuermation should be closed when parent DirContext is closed
Bug ID: 5084229 javax.naming.NamingEnuermation should be closed when parent DirContext is closed: "Bug ID: 5084229
Votes 10
Synopsis javax.naming.NamingEnuermation should be closed when parent DirContext is closed
Category jndi:other
Reported Against 1.4.2
Release Fixed
State Closed, not a bug
Related Bugs
Submit Date 09-AUG-2004"
Monday, 14 May 2007
JavaOne 2007 in Review
FindBugs
It was rare that I attended a session that FindBugs was not mentioned. Many of the presenters had good things to say about the project. I only attended one session that dealt with the topic of static code analysis, but it kept getting mentioned in the context of good things everyone should be doing. Take a look if you haven't already. There is support for many IDEs, even Eclipse.
Developer Knowledge Base - A Java Developer's Guide to Google Technologies
Developer Knowledge Base - A Java Developer's Guide to Google Technologies: "A Java Developer's Guide to Google Technologies
Chris Schalk, Google Developer Programs
May 2007
Java developers may not necessarily think of Google immediately when it comes to Java, but rest assured, Java development is thriving within the Google developer community. Google provides a substantial set of Java-based technologies and libraries, and even experienced Java developers using Google's Web Toolkit might be surprised to hear about the depth of Google's Java support. This article serves as an overview of the Java development possibilities with Google APIs and technologies."
Tuesday, 8 May 2007
OpenJDK
Wednesday, 25 April 2007
Wicket and Guice
Are you sick of XML? I sure am. While I really like Spring Framework, I am sick of all the XML. Writing unit tests is hard enough, but how do you test XML? Some might argue that it isn't really code and doesn't need testing. Some might be wrong. What I want is a way to write code WITHOUT XML.
Two interesting projects have caught my attention:
* Wicket
* Guice
Wicket - Home: "Welcome to Wicket
With proper mark-up/logic separation, a POJO data model, and a refreshing lack of XML, Wicket makes developing web-apps in Java simple and enjoyable again. Swap the boilerplate, complex debugging and brittle code for powerful, reusable components written with plain Java and HTML.
* Check the feature list
* Read some Wicket buzz or some Wicket blogs
* Find out why you should use Wicket
* Check out some examples and see them in live action
* Download Wicket 1.2.6
Wicket is released under the Apache License, Version 2.0 ."
Guice wholly embraces annotations and generics, thereby enabling you to wire together and test objects with less effort than ever before. Annotations finally free you from error-prone, refactoring-adverse string identifiers.
In a nutshell:
* Guice empowers dependency injection.
* Guice cures tight coupling.
* Guice enables simpler and faster testing at all levels.
* Guice reduces boilerplate code.
* Guice is type safe.
* Guice externalizes configuration when appropriate.
* Guice lets you compose your application of components which are truly independent.
* Guice reports error messages as if they will be read by human beings.
* Guice is the anti-static.
* Guice is small and very fast.
Friday, 13 April 2007
Felipe Gaucho's Blog: The name is One, JavaONE !
Felipe Gaucho's Blog: The name is One, JavaONE !: "
# Talk to everyone you can. The people at J1 are for the most part friendly, outgoing and have great experience and tips to share. Just introduce yourself and ask what cool things they do. John Gage (on the years he's there), always makes it a point to remind attendees to talk to one another. If nothing else, you can say, “Dr. Gage told me to talk to you”, and go from there. Breakfast and lunch are great places to talk. Just find a table with some folks, plop down and fire away. Some folks won't be forthcoming, but most are great. Talk to folks in line too: there will be plenty of time for that.
# Have a plan of what sessions/BOFs you want to go to. For each, have a backup session/BOF you are also interested in. Sometimes rooms are full and you can't get in, or the speaker is dull or not talking about what you expected. Don't be afraid to just get up and leave for your second choice.
# The most comfortable seats are on the ends or front rows. The seats are pretty close together. Usually wall sockets for charging your laptop are on the sides of the room, but sometimes they're all taken. Depending on the room and/or number of attendees, wireless access may or may not be available/reliable.
# Unless you want to really immerse yourself in one topic, don't go to sessions that are all on the same track. You'll find them repeating themselves, even though each will have some new material. Diversify and learn about stuff you never even thought about using. J1 is for discovering new stuff and broadening your knowledge.
# Take something warm to wear. Some of the session rooms are cold enough to freeze hydrogen.
# Make sure you make it to Birds-of-a-Feather sessions as well. For those coming from Europe, the BOFs can run late, but you can really get up close and personal with the actual developers and ask questions you can't get to in the bigger sessions. Be aware, some of the presenters aren't comfortable with that and will try to hide behind a prepared set of slides, but be polite and enthusiastic about what they're presenting. Most love having folks that appreciate the work they're doing.
# Visit the pavilion to learn about specific products you're interested in, but also to learn about stuff you've never heard of that might make your job easier. Don't go just to get cool junk for your kids (something of which I've been guilty!). Some exhibitors have actual developers with the marketing folk. Be kind to them all. By the end, they've been standing and talking for three days and start getting ragged.
# Make sure you visit some of San Francisco. A truly unique, physically beautiful and captivating city. (Don't be surprised by the number of panhandlers, though) Be prepared for lots of walking, some steep hills and weather that can range from cold and misty to gloriously sunny in a short time. So have layered clothing and comfortable shoes.
# The conference food is okay, but don't expect the eating experience of your lifetime. Save that for the evening and the San Francisco restaurants. During lunch they'll have hot food, but also lunch bags with a sandwich (turkey, ham, beef or vegie), chips, cookie and a fruit, that you can grab and take to a session.
# Don't buy books unless you feel like lugging them around and paying a little bit more than Bookpool. That being said, the bookstore has an awesome selection of the classics and the latest. Sometimes you can get free books from various pavilions. They'll also have authors available for book signings and chatting with. Josh Bloch will have a new book this year and I'm sure will be signing it. Also, go to any session he gives.
# Most of all, just immerse yourself in the whole JavaOne experience. Be positive (even with the guards!), get as much sleep as you can, check out the vendor parties, the After Dark party, the beer, and most of all, learn all you can about what great and creative things people are doing and just enjoy the company of other Java enthusiasts and professionals. "
Matthew Smith: JDK Community Star

Sun has been opening the Java platform for some time now. With the Mustang release, the source code was made available and a process was provided for submitting bug fixes.
About this same time we discovered an issue with the LDAP code that left connections open certain circumstances. Since I had access to the source code from Sun, I was able to find the problem and send a correction.
For this I was "honored" at JavaOne in 2006 as a JDK Community Star for contributing a bug fix to Java. Click on Matthew Smith.
Communciating between JSR-168 portlets and the portal containter
The JSR-168 spec does not address the need for portlets and the portlet container to communicate. Sometimes an action taken by a portlet needs to be reflected in the navigation which is controlled by the container. Conversely, an action taken in the navigation will need to change the state of the portlets. The following information applies specifically to Vignette Application Portal but should apply to other portals like Pluto, Jetspeed2 and Liferay.
Communicating from the portlets to the portal container.
Add a parameter to PortletURL. Then there can be something (grid, secondary page or my personal choice, a servlet filter) that is listening for that parameter. Of course, the portlet will add a lot of nonsense to the name of the parameter so the receiver needs to look for something ending with the parameter name. The servlet filter can then place the required information into the session.
Of course, you need to take reasonable security into account when deciding what to pass as a parameter.
Communicating from the portal to a a portlet.
Again, in a servlet filter, add the information that needs to be communicated to the portlets as a request attribute. The trick is that the name of the attribute must begin with "javax.servlet.". It will be visible in the RenderRequest and ActionRequest objects passed to the portlet. We are already doing this to pass things like Remote URL to the portlets.
Note: For this to work the object being passed needs to be in the classpath and not just loaded in the WAR file. The reason is that the object is loaded by one classloader and then passed to another. You end up with ClassCastException.
Wednesday, 11 April 2007
Fun with Java Enums
Way back in Java 1.5, enum types were added to the Java spec. The idea is simple enough; create an object with a fixed set of valid values. The values are constant, like coins. Coins come in a fixed number of types (penny, nickel, dime, etc) and each type has a fixed value (1, 5, 10, ... ). An enum type allows you to do just that in your code:
public class Coins {
enum Coin {
PENNY(1),NICKEL(5),DIME(10),QUARTER(25);
int value;
Coin( int v ) { value = v; }
}
public static void main(String[] args) {
System.out.println("A penny is worth: " + Coin.PENNY.value);
}
}
A Coin enum type is declared and each possible type of Coin is given it's respective value. And the code prints just as you would expect:
A penny is worth: 1
Part of the assumption is that a penny is always a penny and always worth 1 cent. However, if we add the following lines of code to the main method, let's see what happens.
Coin.PENNY.value = 1000;
System.out.println("A penny is now worth: " + Coin.PENNY.value);
When the code is compiled and run again, what is the output? In case you were wondering, it does compile quite nicely.
A penny is worth: 1
A penny is now worth: 1000
Now that's the way all my investments should grow! Seriously, though, what is the problem? The problem is that the
value, and any instance variables in an enum, are not magically immutable. The enum type does not do any special checking to ensure that the instances of the type (PENNY, NICKEL, ...) remain constant. Under the covers, the enum type is converted to a static class with static members for eachof the declared possbile values like this:
static final class Coins$Coin extends Enum
{
public static final Coins$Coin[] values()
{
return (Coins$Coin[])$VALUES.clone();
}
public static Coins$Coin valueOf(String s)
{
return (Coins$Coin)Enum.valueOf(Coins$Coin, s);
}
public static final Coins$Coin PENNY;
public static final Coins$Coin NICKEL;
public static final Coins$Coin DIME;
public static final Coins$Coin QUARTER;
int value;
private static final Coins$Coin $VALUES[];
static
{
PENNY = new Coins$Coin("PENNY", 0, 1);
NICKEL = new Coins$Coin("NICKEL", 1, 5);
DIME = new Coins$Coin("DIME", 2, 10);
QUARTER = new Coins$Coin("QUARTER", 3, 25);
$VALUES = (new Coins$Coin[] {
PENNY, NICKEL, DIME, QUARTER
});
}
private Coins$Coin(String s, int i, int j)
{
super(s, i);
value = j;
}
}
Our
value is given the same access modifiers in the generated class as it was given when we defined the enum. Meaning that the value is not able to be modified by any member of the same package. The fix
The fix is fairly simple. Make the
value instance variable final:
final int value;
Now when the code that tries to modify the value is compiled, this error:
Coins.java:28: cannot assign a value to final variable value
Coin.PENNY.value = 1000;
^
1 error
Problem solved. Right? Well, not so fast. For primitive types, yes. But for descendants of Object, maybe not. What if instead of an int, value was something like a Map. You could then do Coin.PENNY.value.put("bad key", "bad value") and the compiler would be perfectly happy with it.
To fix the problem, ensure that the Map is unmodifiable or immutable
public class Coins
{
enum Coin {
PENNY(1),NICKEL(5),DIME(10),QUARTER(25);
final Map value;
Coin( int v ) {
Map newValue = new HashMap();
newValue.put( "cents", v );
value = Collections.unmodifiableMap( newValue );
}
}
public static void main(String[] args) {
System.out.println("A penny is worth: " +
Coin.PENNY.value.get("cents"));
}
}
Summary
While the compiler will ensure that the enum types are immutable, it is the responsibility of the developer to ensure that any instance variables added to the enum are also immutable. This means the instance variable needs to be final AND the type of the instance variable must itself be immutable.Friday, 6 April 2007
Thursday, 5 April 2007
Elusive unit test error
I wish I knew why. If you have an idea, post in the comments.
UPDATE: The problem seems to be that Foglight/Performasure is installed on the server and is doing something with the JDBC getConnection response.
The options seems to be
- Put the client jar for Performasure in my test classpath. I do not know if the license allows for that.
- Remove it from the server.
java.lang.NoClassDefFoundError: com/sitraka/pas/agent/plugin/instrumentor/method/JdbcDriverInstrumentor$JdbcImplementation
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:480)
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:182)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClassLoader.java:224)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:217)
at weblogic.j2ee.ApplicationManager.loadFromNetwork(ApplicationManager.java:652)
at weblogic.j2ee.ApplicationManager.loadClass(ApplicationManager.java:322)
at weblogic.j2ee.ApplicationManager.loadClass(ApplicationManager.java:258)
at weblogic.j2ee.ApplicationManager.loadClass(ApplicationManager.java:253)
at weblogic.rmi.internal.ClientRuntimeDescriptor.computeInterfaces(ClientRuntimeDescriptor.java:254)
at weblogic.rmi.internal.ClientRuntimeDescriptor.intern(ClientRuntimeDescriptor.java:140)
at weblogic.rmi.internal.StubInfo.readObject(StubInfo.java:124)
at sun.reflect.GeneratedMethodAccessor20.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:824)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1746)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at weblogic.common.internal.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:120)
at weblogic.rjvm.MsgAbbrevInputStream.readObject(MsgAbbrevInputStream.java:120)
at weblogic.rmi.internal.ObjectIO.readObject(ObjectIO.java:56)
at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:164)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:293)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:247)
at weblogic.jdbc.common.internal.RmiDataSource_814_WLStub.getConnection(Unknown Source)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:462)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:528)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:561)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:579)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:589)
at com.ihc.provider.domain.ProviderImpl.findProviderForId(ProviderImpl.java:401)
at com.ihc.provider.domain.ProviderImpl.findProvider(ProviderImpl.java:383)
at com.ihc.phc.user.UserServiceImpl.addProviderInfo(UserServiceImpl.java:249)
at com.ihc.phc.user.UserServiceImpl.getMessagingDoctors(UserServiceImpl.java:231)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:335)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at com.ihc.phc.util.CacheInterceptor.invoke(CacheInterceptor.java:37)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy13.getMessagingDoctors(Unknown Source)
at com.ihc.phc.test.user.TestUserServiceImpl.testGetMessagingDoctors(TestUserServiceImpl.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Friday, 30 March 2007
Chapter�16.�Portlet MVC Framework
Chapter�16.�Portlet MVC Framework: "Portlet MVC Framework"
There is a blog posting over at O'Reilly that has some other useful links.
Wednesday, 21 March 2007
Coding Horror: The "Works on My Machine" Certification Program
Coding Horror: The "Works on My Machine" Certification Program: " 1. Compile your application code. Getting the latest version of any recent code changes from other developers is purely optional and not a requirement for certification.
2. Launch the application or website that has just been compiled.
3. Cause one code path in the code you're checking in to be executed. The preferred way to do this is with ad-hoc manual testing of the simplest possible case for the feature in question. Omit this step if the code change was less than five lines, or if, in the developer's professional opinion, the code change could not possibly result in an error.
4. Check the code changes into your version control system.
Congratulations! You're fully certified. Brand your app with your shiny new Works on My Machine badge. You'll certainly want to show it off to your fellow team members and key stakeholders. But please-- do try to keep your ego in check. Not everyone is capable of such an epic commitment to quality in software engineering."
Wednesday, 14 March 2007
Sun Certified Programmer for Java 5 Study Guid
At work I am leading the Java Certified Programmer study group and this is the book we have chosen after extensive research. OK, I asked 1 person, but he passed so it can't be all that bad.