MAC address lookup using Java
Posted 2007-07-09 in Java by Johann.
Code to extract the MAC address of the network card has been in UUID for a long time now. Because some people download UUID for just the MAC address part, here is a short explanation of how that code works and how you can use it in your projects.
First, download UUID and open com.eaio.uuid.UUIDGen
. The MAC address extraction is performed in the static initializer.
The code does a little bit of OS sniffing to find a suitable program which is then used to print the MAC address. The output of this program is read in and parsed.
Operating systems
HP-UX
On HP-UX, /usr/sbin/lanscan
is started.
Linux/MacOSX/possibly other Unices
The /sbin/ifconfig
command is used.
Windows
ipconfig /all
is used.
Solaris
On Solaris, the first line of the host name list is used to call /usr/sbin/arp
. The code is somewhat equivalent to /usr/sbin/arp `uname -n | head -1`
.
Ripping the MAC code
- Download the latest version of the UUID software and extract the archive.
- Copy the following source files from the
uuid-x.x.x/src/java
folder into your project:com/eaio/util/lang/Hex.java
com/eaio/uuid/MACAddressParser.java
com/eaio/uuid/UUIDGen.java
- Copy the static initializer up to and including
if (macAddress != null) { if (macAddress.indexOf(':') != -1) { clockSeqAndNode |= Hex.parseLong(macAddress); } else if (macAddress.startsWith("0x")) { clockSeqAndNode |= Hex.parseLong(macAddress.substring(2)); } }
into a new class. - Retrieve the MAC address as
clockSeqAndNode
variable.
That’s all.
Centralizing Resource Closing for Cleaner Code, Fun and Profit
Posted 2008-07-07 in Java by Johann.
Resources are openend and closed in every software. The opening is obviously different for different resources, but the closing isn’t.
99 % of the time, the closing code looks like this:
finally { if (xyz != null) { try { xyz.close(); } catch (XyzException ex) { // Log it, ignore it or flip it and reverse it. } } }
That’s a lot of code for such a simple task.
Centralizing Resource Closing
Since most resource classes have a close
method, we can simplify this by
- calling the
close
method via Reflection, - either logging or ignoring any exceptions,
- using
varargs
to close multiple objects at once and - using
static
imports to reduce the amount of boilerplate code.
Using these techniques, all of the above code essentially becomes
finally { close(xyz); }
Potential Problems
- Not all APIs use
close
. JSch for example usesdisconnect
. - More than just
close
needs to be called in some APIs. For example, the grotesque JMS API forces you to callstop
beforeclose
.
The Code
Download Resource.java
and the test case.
Resource
needs log4j in your classpath, the test case additionally needs EasyMock, the EasyMock Class Extension, the Code Generation Library and ASM.
Closing Resources
Import
Assuming you have downloaded the source file and put it in its com.eaio.util
package, continue by adding a static import to your classes.
package xyz; import static com.eaio.util.Resource.close;
Special close methods
Some classes have a special closing method. You can set them up by calling the insteadOfClose
method.
Resource.insteadOfClose(MySupahClass.class, "selfDestruct");
The method scanning is recursive and takes interfaces into account so MySupahClass
can be an interface or a common superclass.
Before-closing methods
Some classes require other methods to be called before they can be closed.
Resource.beforeClose(Keks.class, "sayGoodbye");
Any implementation of Keks
will now have its sayGoodbye
method called before close
is called.
After-closing methods
Special methods can also be called after a resource has been closed.
Resource.afterClose(Keks.class, "kThxPlzBye");
Varargs
Resource.close
will close all resources in the order they are given.
finally { close(inStream, outStream, rset, stmt, conn); }
Any of the arguments can be null
or throw an exception.
Logging
Resource
(perhaps incorrectly) assumes that nobody is interested in exceptions that happen when resources are closed. To see them, set the logger of com.eaio.util.Resource
to DEBUG
or TRACE
in log4j.properties
.
log4j.logger.com.eaio.util.Resource=DEBUG
If the log level is DEBUG
, any exceptions will be logged with WARN
level in a short format. If the log level is TRACE
, the full exception stack trace is logged.
Summary
It’s amazing how much code that does the same is replicated across larger projects. By relying on reflection, a lot of essentialy duplicate code can be removed.
Pages
Subscribe
RSS 2.0, Atom or subscribe by Email.
Top Posts
- DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
- 6 fast jQuery Tips: More basic Snippets
- xslt.js version 3.2 released
- xslt.js version 3.0 released XML XSLT now with jQuery plugin
- Forum Scanners - prevent forum abuse
- Automate JavaScript compression with YUI Compressor and /packer/