Linux.Com · Freshmeat · Slashdot · O S D N · Question Exchange  · Themes.Org My OSDN · AFFILIATES   

The Madcap Library


 


Summary |  Forums |  Bugs |  Support |  Patches |  Lists |  Tasks |  Docs |  Surveys |  News |  CVS |  Files

The Madcap (Machine and Device Capabilities) library is a package of functions that allows Java programmers to break out of the "lowest common denominator" approach and take advantage of certain platform specific capabilities that might be available on the runtime platform. Importantly though - this is done without the programmer being forced to tie their code to that platform and without writing any native code themselves. The approach used is to simply call an API in the Madcap library and if it is available on the current runtime it simply works and if not is merely throws an exception which can be handled appropriately.

The library is written using JNI because that's how one can access things that cannot be done in pure java. Examples of things that are currently working are get process ID (useful for writing a PID file), get and set of environment variables, get and set time, setuid/seteuid/setgid/setegid (very useful for starting servers on ports below 1024 on Unix, etc) and use of the tray icon in windows.

Here is a screenshot of a Windows tray icon with its associated tooltip and menu that was created by the Java test program that comes with the library.

Tray
		 Icon screenshot in png format

The code fragment to create this looks like:


....

// Minus sign creates a menu separator.
String menuArray [] = {"Stop", "Start", "Restart", "-", "Exit"};
	
// Create the Tray Icon with supplied icon, tooltip and menus
OSUtil tray = new OSUtil ();
tray.makeTrayIcon ("duke.ico", "Hello Duke!", menuArray, handler);

....
		

The library currently works with Java on Windows and on Linux but should be easy to port to other OSes. It is available under the BSD license.

It is a quick and dirty implementation BUT IT WORKS! The general design is to have a method signature that is as cross platform as possible that will be called in Java code. When the method is called it is doesnt exist on that platform then we simply throw an UnimplementedException. If not then the native code is executed. If an error occured we throw a JNIFailedException and return errno in the exception. Otherwise we return the requested data.


   
 OSDN - Open Source Development Network