This is not a tag line!
Posts tagged java
Beware of File.createTempFile
Sep 4th
By default, File.createTempFile (in Java, of course) creates a temporary file in the directory identified by the system property java.io.tmpdir. The problem is that on OS X, that directory can be quite weird. For example, on my system, it currently is:
scala> System.getProperties().getProperty("java.io.tmpdir")
res0: java.lang.String = /var/folders/ya/yaNATrKPGFu2HuSOWxSIu++++TI/-Tmp-/
Notice the ++++ in one of the directory names? Well, these little characters can later cause headaches! The reason, you ask? Simple, if that temporary file name is ever used in a URL, then those plus signs will be interpreted as spaces and whatever code is using a URL to find your temporary file will not find it! So beware!
Possible solutions include passing -Djava.io.tmpdir="whatever" when you launch your Java process, or use the createTempFile(String prefix, String suffix, File directory) version of File.createTempFile and pass it a directory that doesn’t contain an + sign in its name!
Accessing a JMX object programmatically from JBoss
Apr 15th
OK so here’s some geeky information… I’ve had several times the need to access a JMX service programmatically and each time, I had to hunt down the information so here it is so that I know where to find it next time.
It took me three years almost to the days to need that piece of code again and realize that it wouldn’t even compile… Should be fixed now!
import org.jboss.mx.util.MBeanProxy;
import org.jboss.mx.util.MBeanProxyCreationException;
import org.jboss.mx.util.MBeanServerLocator;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
ObjectName objectName;
String name;
try {
objectName = new ObjectName(name);
} catch (MalformedObjectNameException e) {
throw new IllegalArgumentException("'" + name + "' is not a valid ObjectName");
}
MBeanServer server = MBeanServerLocator.locateJBoss();
try {
return MBeanProxy.get(clazz, objectName, server);
} catch (MBeanProxyCreationException e) {
String message = "Couldn't retrieve '" + objectName.getCanonicalName() + "' MBean with class " + clazz.getName();
throw new RuntimeException(message, e);
}
