A simple OpenWeatherMap example in Java I've had my JPhotoFrame project using the OpenWeatherMap API for some time now, at least since Yahoo messed up its own weather service. I thought it would be useful to share a bit of code on how to use the OWM API, so here it is.
jPhotoFrame version 0.3 released with a bunch of cool updates I've released jPhotoFrame version 0.3 today. It has more customisation options for weather forecast positioning, adds pause functionality and fixes a bunch of small bugs like out of memory error and spiky text. In this release I've migrated over to the 2.5 version of the OpenWeatherMap API too.
OpenWeatherMap 2.1 version API is now officially dead Not long ago I've written that OpenWeatherMap crippled its 2.1 version API by returning data for the same city no matter what the request was. Now it looks that OWM has completely killed off the 2.1 API endpoints in favour of its (paid) 2.5 version of the API.
Code Odium: Unnecessary String concatenation and splitting I've decided that I'm going to start a new line of posts on my blog that deals with especially bad examples of code that I come across. These are meant to demonstrate how NOT to write code, specifically in Java. I will do an analysis of the bad code and will also provide my solution to fix the bad coding practice shown.
Waiting on multiple Future workers on a ThreadPoolExecutor I've been using the Java Future interface extensively. It's simple to implement and all you need is a ThreadPoolExecutor to run your code in a Thread. There is no messy thread handling, it's all taken care of for you. So what happens if you are running multiple Future tasks and have to wait for all of them to complete before you continue processing their results? Read on to see my approach.
OpenWeatherMap cripples its 2.1 version API I've noticed that for the past few weeks my Photo Frame has been reporting odd weather. Instead of Brisbane, Australia it was giving me weather for Whitby, GB. I didn't pay much attention to it at first but then it got to me, I checked the configuration and that hasn't changed so I figured there must be something odd going on with OpenWeatherMap and turns out I was right.
Using Mockito and JUnit to test a SOAPHandler implementation While adding JUnit unit tests to one of the projects I've contributed to I've come across a SOAPHandler implementation that was completely untested. I couldn't let that be so decided that at least some test coverage was required. Unfortunately testing a SOAPHandler is not so easy, but with a bit of Mockito magic is very doable.
Java Mission Control requires GTK but which version? I've recently had one of the DBAs on a project ask me what GTK version is required to run Java Mission Control with JDK 7. I thought easy, the documentation should tell me this, but after searching for several hours I've come up with nothing, apart from 'GTK Libraries Required' as stated in the Oracle Java Mission Control 5.3 Certified System Configurations.
Latest JodaTime and WebLogic incompatibility, missing methods Joda-Time is a great library if you can't use Java 8 in your project. If you happen to be using version above 220.127.116.11 with your web service and deploying to WebLogic 12c (12.1) you will likely get java.lang.NoSuchMethodError exceptions. This is how to fix it.
Conserving the Log4J MDC context when using a thread pool I've been updating logging in the web services I look after so that the WS-A MessageID can be logged for each request and for all of the messages written to the log during that request. On the most part putting this value it into the MDC and configuring the appender to write it out to the log is all that's required. However, when you start using thread pools e.g. with ThreadPoolExecutor the MDC goes haywire and that's because and I quote..."The MDC is managed on a per thread basis."
Returning compressed MTOM data with JAX-WS DataHandler A while back I've shown an example MTOM/XOP enabled web service using DataHandler. Although MTOM is great for optimising the output of a web service, sometimes it's just better to compress these results before sending them.
Missing 30 minutes when parsing Java 7 ISO 8601 time zone format I've been doing quite a bit of work with time conversion recently and started to use the new 'X' pattern in Java's SimpleDateFormat class to parse ISO 8601 time zones. Initially I treated this exactly the same way as the old 'Z' pattern for RFC 822 time zones. That ended up giving me 30 minute rounding errors in some cases. Upon reading the JavaDoc closer, I found this sentece hidden among all the verboseness...
Maven WebLogic Plugin and the Java 5 source code bug plus how to fix it I've noted previously that the JWSC Ant Task fails to build Java sources above 1.5 source version due to a hardcoded "1.5" value in one of the Jar files. That was happening for the 12.1.2 version of the Ant task, it also appears to be a problem in the 12.1.2 Maven plugin as well as the 12.1.3 Maven plugin.
Java code hanging in JDeveloper while establishing a T3 connection over VPN with Proxy It's not often that I work on my company's VPN but on this occasion I did. One of the things that I've forgotten about was how proxies are automatically configured when the Cisco AnyConnect VPN Client establishes the VPN connection. The reason I need to be on the VPN was to access some internal servers and run some of my code that connects via the T3 protocol to a weblogic server and executed some remote methods. Every time I executed my code it just sat there as if frozen.
A tip to bypass the Java unreachable statement compile error when debugging Many a time when I'm debugging I feel quite lazy and often want to stop execution of code within a method before that method actually ends. The naive solution is to put in a return statement or throw an exception at the point where you want to stop running your code. Of course this doesn't work if you have lines of code after your return/throw clause.
Take a heap dump with the HotSpotDiagnosticMXBean using JConsole This is a nice trick I learned over the past week while doing JVM heap analysis. You can use JConsole to take a heap dump of your JVM. Open the MBeans tab and navigate to com.sun.management, then HotSpotDiagnosticMXBean. This will display all the methods that can be invoked on this bean. We're interested in the dumpHeap method.
Addressing the weblogic.application.ModuleException: Context path '/my_shared_lib' is already in use error I've experienced a strange error coming out of the Weblogic wldeploy tool recently. It was reporting that a context path used by my shared library is already in use when I was trying to deploy a new web service. This seemed confusing to me - I had two web services that were both referencing the same shared library, the first service would deploy without any issues while the second would fail deployment with the context root error. Shared libraries by their nature are meant to be shared so what was the issue?
How to get the WebLogic 12c JAX-WS runtime version number I needed to know the version of the JAX-WS runtime that my installation of WebLogic (12.1.2) was using. This information was nowhere to be found in the documentation, so I decided to do some digging around to see what I can find.
Hacking together a new digital photo frame with RaspberryPi, Minibian and Java I've received my new Raspberry Pi Model A+ last week to use to replace the old Model A inside my photo frame, so I spent some time on the weekend setting that up. Last time that I've set this photo frame up, I went with out-of-the-box software, which did the job but did feel quite clunky. This time around I decided to do it better and write some software for the photo frame instead.
HashMap keys and values passed as null with JAX-WS and WebLogic 12c I've noticed something curious while testing some web services recently. The keys and values in the HashMap object that's passed as an IN parameter to the web service were all being turned into nulls. If I passed one key/value pair, I'd get one key/value pair of nulls, if I passed two key/value pairs, I'd get a pair of null/null, and so on. I did eventually track down the problem and fixed it, but it has been an interesting issue to come across.
Java StringWriter vs CharArrayWriter which one to choose for performance I've been writing some code recently that manipulates lots of strings, specifically it does appends on them via a Writer. In Java there are two choices when it comes to a Writer that's oriented around Strings - CharArrayWriter and StringWriter. I got curious about which one would be best in terms of performance, these are my findings.
JWSC does not play nice with Java 8, works fine with Java 7 The more I work with JWSC, the more issues I seem to find. This new one is related to JavaCompiler and has the same smell as the other JWSC problem I've found. I've not had time to investigate this one in detail, but did find a workaround still.
How to package your Java code as a native app on OSX into a DMG file I've come across a nice tool in the Java 8 SDK (Java 7 has it too, but it has a different name) that lets you package your Java application as a native app for a range of platforms. This tool is called javapackager and it makes distribution of you Java apps so much easier than previously and best of all, it's part of the JDK itself.
How to fix the JWSC WsBuildException: Wrong number of class files So I've continued moving my web services over from wsimport way of building them to JWSC and in my quest I keep hitting issues to do with JWSC compilation. This latest problem came in the form of 'Wrong number of class files' error message. This stumbled me for a while, it was an hour before my vacation was about to start, I just wanted to throw the laptop off the balcony at this point, but I managed to find a fix for this error.
Java validation regex pattern for the OID root UID value I've been focusing on making sure that the input to my web services at work is correct. One of these inputs happens to be the root part of a HL7 OID, which is a dot-separated UID. The values look like this '1.2.3', '18.104.22.168.7774.1', etc. It's very similar to an IP address, but can have more than 4 parts.
JWSC from WebLogic 12.1.2 fails to build Java sources above 1.5 I've written a post previously showing an example build system that uses JWSC to build your JWS based web service and then the wldeploy task to deploy it to WebLogic. Recently I've been expanding on the use of JWSC at work and have come up against a serious obstacle. JWSC would not build any Java sources that were above the 1.5 language level.
JNativeHook 2.0.2 example code with logging and System.out disabled I've been playing around a little with JNativeHook Java library. "JNativeHook is a library to provide global keyboard and mouse listeners for Java. This will allow you to listen for global shortcuts or mouse motion that would otherwise be impossible using pure Java." What I really didn't like is it printed its licence details every time my code ran. I've dug into the JNI code and saw that it makes a callout to System.out to print this information. My solution was pretty simple, just disable the system output stream during initialisation.
How to determine the JAXB runtime version I've had to confirm which JAXB version I've been using for my runtime. In my search through various forums I've come across some code that more or less worked, but it wasn't very robust and wasn't explained well. I've put this together to answer the question properly.
Performance difference between JAXB and Velocity generating XML fragments One of the peculiarities we've noticed during our recent bout of performance testing at work was how much JVM heap space was being used and immediately garbage collected by one of our web services. The figures were quite staggering, with a peak use of 3Gb to generate the response message in some cases. This was not impacting the performance of the system overall, but still needed investigating as it gives lots of room for improvements.
Are guard statements like isDebugEnabled() necessary when using Log4J ? I've been going through a fair amount of code that makes excessive use of log4j guard statements like isDebugEnabled() and began mercilessly removing their use. In my mind, these statements make the code messy and are really not necessary in most situations. In terms of performance, a guard statement does not necessarily give you any kind of boost, except in some odd edge cases. To prove my point however, I wrote some code and collected data to graph, here are the results.
Change the JRE used for SoapUI on OSX I use SoapUI on a regular basis and today I wanted to change the JDK that it uses. I've noticed that by default the OS X installation of SoapUI 5.1.2 is using JRE 1.7. On my system, I've had the 1.8 JDK set as the default for quite some time, my ~/.profile file pointed to the 1.8 JDK bin directory for the PATH and the JAVA_HOME was set correctly, so it was puzzling to me why 1.7 was being picked up by SoapUI. I dug a little deeper and found the answer.
WebLogic 12.1.2 not respecting @PostConstruct annotated method exceptions I've been working on moving our web services at work to WebLogic 12.1.2 and noticed a curious issue. When I first moved one of the web services across, I knew for a fact that the method that's annotated with @PostConstruct would fail (due to changed data source names). However, when I deployed the service it was successful. Looking at the managed server logs, the service did indeed fail during initialisation, so subsequent calls to this service were failing for me. This seemed like a problem so I went out to verify it with a simple example.
Tricking the Java compiler into allowing unreachable statements in code I've been doing some tricky debugging recently where I had to provide one of my web services that required the environment it ran in configured a very particular way. The developer I was working with did not have such an environment and there was no time to set one up properly. This was a bit of a dilemma, however the issue we were trying to debug didn't require my service to fully execute, it was sufficient to get to a particular point in the execution and exit, this could be done on a vanilla system.
Workaround for JDeveloper 12c Refactor Rename bug I use JDeveloper on an almost daily basis and when something as basic and fundamental to the product as the rename functionality breaks, it makes you want to tear your hair out. I'm talking about the Refactor Rename feature that has been broken since the 12c release. Luckily there is a workaround.
A quick way to visually detect unused variables in code, and exterminate them Making changes and fixes to other people's code is never fun. It is especially not fun when naming conventions and basic programming principles like using getter/setter methods are not used. It gets worse when part of a naming convention was used to specify variable scope only to have someone come along and change this variable's scope and not update its name to match the convention. Then there is the issue of unused variables. Unfortunately the former set of issues is a little harder to deal with, but unused variables can be removed at zero cost and so are a quick win in terms of cleaning up bad code. The trick is configuring your IDE to let you do this easily.
Fix the javax.net.ssl.SSLException: Received fatal alert: illegal_parameter in SOAP UI I've started receiving the 'javax.net.ssl.SSLException: Received fatal alert: illegal_parameter' exception when sending requests to some web services. These services were all working fine previously and continue to work when invoked from other applications e.g. OSB. This could only mean that the issue was local. Turns out that the problem started occurring due to a JDK upgrade.
NetBeans 7.1.2 does not work with JDK 8 I've recently installed the JDK 8 on my rMBP and all of a sudden NetBeans stopped working for me. I've dug into this a little further and tried several things, none of which worked, in the end I ended up forcing NetBeans to use JDK 7 and everything was working again.
JDeveloper 11g and 12c do not support new Java 7 Syntax: Try With Resources Now that Java 8 has been released, you would think that the IDE that comes from the makers of Java would support all of the language syntax that dates back to the Java 7 days, but alas it does not. I tried both JDeveloper 11g (22.214.171.124.0) and 12c (126.96.36.199.0) and neither appear to support the try-with-resources syntax that was introduced as a part of JDK 7. However, compiling the code has no problems, both IDEs are able to compile source code that is using this new syntax.
OS X Mavericks breaks the Java Dock Name functionality I've been updating a Swing app recently and discovered that under OS X Mavericks the dock name setting does not work as expected. Previously it was possible to set this programatically, now it seems that the dock name can only be set via a java startup option.
How to get a null return from a Java constructor I was looking at some Java code recently that got me stuck for quite some time. It seemed to be doing the impossible, getting a null reference when creating a new object. I was starting to think that maybe there is something wrong with the JDK, then I've created a new object of a similar type that worked ok, but the original object was still being created as a null reference. This was very confusing, but the answer to why this was happening was a very simple mistake in the code.
JVisualVM 64 and 32 bit incompatibility and seg faults I've been trying to set up JVisualVM to monitor a number of JVMs that are running as a part of an application server install. These JVMs are on JDK5, which can be monitored with JVisualVM, but for some reason every time I started it up, it crashed with a seg fault.
XJCTask task failing on OSX with JDK 6 when using 3rd party extension for JAX-WS I've recently had to use a different JAX-WS library for some development work on a project, which worked great by placing all the jar files into the Java Extensions directory, however when I went back to trying to compile another project I had, everything started to break, I was getting exceptions I have never seen before. After a bit of investigation, it turned out that the NetBeans JAXB libraries do no like being mixed with the JAX-WS libraries. The fix is simple, remove the 3rd party libraries from the Extensions directory.
Stand alone CSF (Credential Store Framework) example I've been trying to figure out the Oracle CSF 11g documentation over the last couple of days without getting very far. The documentation is a bit incomplete and has errors in it (referencing classes that do not exist for example). So since I had to implement this for the project I am on, I had no choice by to figure it out myself.
Playing with varargs Varargs in Java are very handy and I've been using this feature quite a bit lately, but I did find some shortfalls that unfortunately mean I have to write some ugly code to get around the limitations.
Simplifying code with Exception throwing As I've been working on the server code for my game, I've been noticing that the complexity of it is increasing but also duplicating unnecessarily. After thinking about it for some time and seeing why my code was exhibiting these traits I concluded that it was due to how I was handling errors.
JSON-RPC and Java collections do not play nice I've been using the JSON-RPC library for some time now to write the server for my game, thus far I've only had a need to transport simple objects back to the client, but recently I've had a need send a collection of objects back. This is where I found that JSON-RPC falls short if you try to use the standard Java collections.
JDO, you're dumped, App Engine and bi-directional relationships After searching high and low to find out how to add a child object to a parent in JDO efficiently and getting ever more frustrated with the lack of good documentation, I've decided to just write some of my own tests to see what's possible and how well it works.
Storing sparse 2D map coordinates, proximity and data store indexes While working on my game, one of the things that I've been trying to work out is how to best store the map coordinate data for the buildings I place on the map and to tell if a map cell is occupied already. In a typical scenario where buildings are of homogenous dimensions all I would need is the coordinate of the building and it's dimensions, this would give me a bounding box of map cells that the building occupies. However, in my game the buildings cannot be described by simple bounding boxes.
Getting 100% JUnit Test Coverage for JDO objects in App Engine Once I started writing unit tests for the objects I was persisting into the HRD in App Engine via JDO, I've noticed that the coverage for all of my classes was below 100%. This didn't seem right to me because I was certain that I was testing everything.
Parents, Children and Ancestor queries in Google App Engine with JDO Fetching a child object by the ID of the parent using JDO had me stuck for a while. According to this article "reads, writes, and ancestor queries will always reflect the results of the commit" in the Google App Engine data store (HRD). After trying various things and reading lots of documentation and forum posts it all came down to how I modelled my objects. Now when I persist a parent object with an attached child, then immediately perform a query to fetch that child, the data returns as expected.
A simple JSON-RPC server with Google AppEngine I've started exploring the use of Google AppEngine for a project I'm working on. JSON was one of the technologies that was going to be used and JSON-RPC looked like an attractive way of exposing functionality without writing your own server network code.
Using Java HIDAPI on OS X to read temperature from the TEMPer1 sensor The TEMPer1 USB Thermometer finally arrived a few days ago, so I started to play around with it. It comes with a CD with software on it, which I promptly disregarded. After searching around the net, I could not find anyone that's written Java code to interface with the device, but I did find other useful information like javahidapi and this blog post: Controlling your TEMPer on Mac OS X. Now my Objective-C is quite bad, but I was able to follow the code to extract the required information to read the temperature from the TEMPer1.
Changing Java Swing Nimbus L&F default colours One of the projects I've been working on recently had quite an extensive UI written (by me) using Swing. The Nimbus L&F was used throughout to make it look a bit more modern. Nimbus is a nice looking UI L&F, however the choice of some colours within its theme is a little odd.