Watching Mac OS X File Events from Windows: A Guide for .NET and Java Developers
Platform interoperability has long been the Holy Grail of software development. Although there has been a high degree of success creating code that will run on a variety of operating systems, such code has been used pretty much in high-level programming activity such as data access forms and browser-based web applications. For those of us who are concerned with lower-level, file system programming, writing cross-platform code can be a frustrating affair.
A while ago I was confronted with a problem, the solution of which is the focus of this article. I was writing a web-based .NET program that worked with enterprise-wide file systems. The newspaper publishing company where I work uses computers running both Windows and Macintosh OS X operating systems on the desktop and for file servers. One of my program's features required that it monitor the state of files on a Mac box running OS X Server. My application needed to be aware of when a file was added to a directory, when it was changed, and when it was removed. .NET ships with a FileSystemWatcher class that allows programs to monitor Windows-based file activity to a very high degree of granularity, but the class didn't work when it came to watching OS X–based files, so I needed to find an alternative way to solve this problem. The good news is that I found one—and, surprisingly, the solution required that I write only a little Java code and use a third-party product.
This article shows you how to write a Java-based Mac OS X file-monitoring program that communicates with an ASP.NET application via HTTP. I'll cover a bit of the impressive open source Eclipse development environment that I used to create the Mac application. In addition, I'll introduce you to the third-party component that I used to do the bulk of the file-monitoring work. I'll talk to you as an experienced .NET developer with fundamental knowledge of Java—and practically no knowledge of the innards of the Mac OS X operating system.
Architecture of the Application
As I mentioned earlier, I needed to be able to monitor OS X file system changes from a Windows environment. To achieve this goal, I decided to use a simple messaging architecture; when a file system change happened on an OS X computer, a message would be sent from the OS X machine to the Windows machine that was interested in the Mac's file system state. The transport mechanism that I used for sending messages from the OS X machine to the Windows machine was HTTP. It's a simple architecture, really—an Internet-aware Macintosh application watching the OS X file system and then communicating to a Windows-based web application via HTTP.
To monitor the OS X file system state, I created a Java application that uses an inexpensive third-class library, JNIWrapper. JNIWrapper ships with a FileSystemWatcher class that watches OS X file state; using associated classes, it sends a message when a file system state change occurs. On the Windows side, I created a simple ASP.NET application to receive and process HTTP-based notification messages from the OS X machine (see Figure 1). Using ASP.NET was incidental; I could have used PHP or any other Windows web application technology to receive the messages for the OS X machine.
Figure 1 The OS X to Windows architecture.