Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
Like this article? We recommend

Creating the OS X File Watcher with JNIWrapper

The Java class library does all the work of monitoring the OS X file system state. Getting the JNIWrapper library to work with my EONFileMonUI.java code was simple. First, I had to import the required parts of the library into my application:

//The FileSystemWatcher interface definition
import com.jniwrapper.unix.system.io.FileSystemEventListener;
//Does the watching
import com.jniwrapper.unix.system.io.FileSystemWatcher;
//The events raised by the FileSystemWatcher
import com.jniwrapper.unix.system.io.FileSystemEvent;

The application had to know how to look for messages emitted by the FileSystemWatcher. This is done by forcing the application to implement the JNIWrapper FileSystemListener interface, by telling the dialog UI to implement the interface in its code. Notice the use of the implements keyword:

public class EONFileMonUI extends JDialog
    implements ActionListener, FileSystemEventListener
{

 //Class code here
}

I declare an instance of the JNIWrapper FileSystemWatcher class, like so:

private FileSystemWatcher fileSystemWatcher;

When the Start/Stop button is clicked, the JNIWrapper FileSystemWatcher needs to start and report any activity. I set this up in the action listener for the Start/Stop button, which I named cmdStart:

private void cmdStart_actionPerformed(ActionEvent e)
{
 //Clear the dialog box
 listModel.clear();
 //Initialize and start the file system monitor
 fileSystemWatcher = new
 FileSystemWatcher(txtFolderToMonitor.getText(),true);
 fileSystemWatcher.addFileSystemListener(this);
 fileSystemWatcher.start();

 //Send out a startup message
 listModel.addElement("Start at " + new EONUtils().GetDateTime(true));
 cmdStart.setText("Stop");
}

Finally, when a file system state changes, the interested Windows machine should be notified:

public void handle(FileSystemEvent event)
{
 String actionName = ACTION_NAMES[event.getAction()];
 EONCaller caller = new EONCaller();
 //get the time in milliseconds
 Long theTime = new Long(System.currentTimeMillis());
 String strTheTime =theTime.toString()
 listModel.addElement(caller.postChange(txtURLToNotify.getText(),
    event.getFileName(),actionName,strTheTime));
}

Notice this code:

listModel.addElement(caller.postChange(txtURLToNotify.getText(),
    event.getFileName(),actionName,strTheTime));

When a FileSystemEvent is raised, the postChange() method of the EONCaller class needs to be invoked. Let's take a look at the postChange() code:

public class EONCaller {
  public String postChange(String urlStr, String filName,
       StringfilMessage, String filDateString){
    StringBuffer buff = new StringBuffer();
    try
     {
     //Make the URL to the Windows machine
     String strURL =urlStr;
     strURL += "?FileName=" + URLEncoder.encode(filName);
     strURL += "&FileMessage=" + URLEncoder.encode(filMessage);
     strURL += "&FileDateStr=" + URLEncoder.encode(filDateString);

     URL url = new URL(strURL);
     URLConnection urlC = url.openConnection();
     //Call the windows machine URL
     InputStream is = url.openStream();
     int oneChar, count=0;
     DataInputStream in = new DataInputStream(is);
     byte ch;
     //Traverse the return bytes from the Windows machine
     while ((oneChar=is.read()) != -1)
       {
        //... and append it to the string buffer
        buff.append((char)oneChar);
       }
       is.close();
     }
     catch (MalformedURLException e)
       { return e.toString(); }
     catch (IOException e)
       { return e.toString(); }
     //pass the data returned from the Windows machine
     return new EONUtils().GetDateTime(true) + " " + buff.toString();
   }
}

The postChange() method, takes four parameters: the URL to call, the name of the file undergoing state change, the state change message, and the date/time that the change was made. The method creates a query string based on the values passed in the last three method parameters. The query string is appended to the URL indicating the receiver web site on the Windows machine that's interested in the OS X file system.

There's no magic here. The web site on the Windows machine is fully prepared to parse the query string data when it's called. The web site expects to see a FileName parameter, a FileMessage parameter, and a FileDateStr parameter in millisecond format.

Internally in the postChange() method, a connection to a web site is opened using the appended URL and URLConnection objects. These objects are part of the java.net.* package. All web access is handled by the classes in this package at no additional labor to you, the programmer. The Windows web site is called, passing the OS X file system state change data. The URL object returns the data passed back from the web site web as an InputStream object. The InputStream object is transformed into a DataInputStream object. The data in the DataInputStream is transformed to a String that is then passed out of the method. If an error is encountered, the error message is passed out the method.

That's it! This little method, postChange(), handles all communication between the OS X machine and the Windows machine.

  • + Share This
  • 🔖 Save To Your Account