Welcome!

Java IoT Authors: Yeshim Deniz, Liz McMillan, Mano Marks, Elizabeth White, Pat Romanski

Related Topics: Java IoT, Open Source Cloud

Java IoT: Article

j-Interop: An Open Source Library for COM Interoperability Without JNI

A search for pure, non-native, bi-directional interoperability with COM servers

I have spent a good part of the last year trying to "wrap" COM servers in Java for a content management organization. It had an array of syndication servers supported by an integrated messaging platform developed using COM. The purpose of this exercise was to increase the organization's market penetration by hooking on to the J2EE bandwagon across multiple platform configurations. With so many different complex COM servers to work with, some supporting automation and others not, I struggled with the all too familiar JNI cycle...code, crash, code some more, and then crash. Literally speaking, I must have brought down the JVM hundreds of times. To top it off, some syndication servers worked on a "pull" mechanism, they could pull the content out from the interfacing repositories. This meant bi-directional access and an event-based interoperation.

I had a look at some Open Source projects, but they were all using native libraries and didn't sufficiently meet the requirements in hand. Ultimately, we did complete a scaled-down version of the project. I'm quite sure that most of the requirements could have been handled if a non-native solution (in the Open Source community) existed to COM interoperability.

There are a number of reasons why I believe JNI shouldn't be used for accessing COM. It may work well for a small-sized project (two or three COM servers, no bi-directional access, etc.). But for any serious initiatives, I think it has certain drawbacks. Though JNI is Java, you still need to know a lot more about the native component and the target architecture before accessing its services. One has to be proficient in Java and in the native programming language as well (some Open Source projects are trying to address this complexity, i.e., trying to abstract JNI itself). With increasing demands to deliver on time - before time, and the delivery having quality, it's usually hard to find such cross-platform resources. The native and Java layers are so tightly coupled that having dedicated resources for each proves more of a headache than a solution.

I'm pretty sure that it's obvious to you that by linking with native code, JNI takes away one of the most powerful features of the language itself, "platform independence" (write once, run anywhere). It binds the application to the host platform. For example, when using JNI in a Windows environment, the Java application is forced to rely on the native DLLs for its functionality. The same application can't be ported without porting the native library to another platform. This also requires a proxy/stub approach when you want to access a COM server from Unix. This is an invasive procedure (you are deploying a potentially unknown code on the machine where the COM server is located, even if it's in the same intranet) and may not be allowed by the administrative policies governing the domain.

Another disadvantage, when linking with native code is the instability it may bring with it. A poorly written DLL (speaking of Windows) will bring down an entire JVM, taking with it some vital applications. And of course, try debugging that!

The architectures built on JNI look more or less like Figure 1.

Okay well...so what can be done about it? I sat down some months ago to develop an open source library that implements the DCOM protocol, thus allowing for pure, non-native, bi-directional interoperability with COM servers.

I'll try explaining my work in two steps. First I'll give you a primer on DCOM and its inner workings and then I'll talk about j-Interop. If you know how DCOM functions <i> under the hood</i>, you can skip to the section about j-Interop.

DCOM: What and How
Distributed COM or DCOM is a high-level network protocol developed to provide location transparency to COM-based components. The keywords being network protocol and location transparency. Traditional COM components can only perform inter-process communication across process boundaries on the same machine. DCOM uses an RPC mechanism to send and receive information transparently between COM components (clients and servers) on the same network. DCOM was first made available in 1995 with the initial release of Windows NT 4. Essentially, it serves the same purpose as CORBA or RMI. Please have a look at Figure 2.

In terms of an ISO OSI protocol stack, this is how it looks (see Figure 3).

As you can see, DCOM is an application level protocol. It leverages most of the functionality offered by DCE/RPC (see the side bar for a brief overview of DCE/RPC). Since DCE/RPC isn't naturally object-oriented, Microsoft's implementation enhanced the protocol by adding new constructs and providing different meanings to some of the packet fields. This enhanced protocol was christened the Object RPC (ORPC) or MS-RPC. Another significant change that Microsoft made to the protocol was the addition of an NTLM Security Service Provider. One disadvantage of this was the impossibility of any interoperability with other implementations of DCOM on platforms that don't have NTLM, since NTLMSSP is available only in MS-RPC.

Let's see how DCOM works.

DCOM (MS-RPC): Under the Hood
The best way to explain the inner workings of DCOM would be by creating a (COM) client/(COM) server application. I've used Visual Studio to create one. Since "how" to create a COM server (from now on referred to as a COM component) is outside the scope of this article, I won't be mentioning it here. It's best left for a tutorial.

Our COM component is an "out of process" server, i.e., an EXE. For the sake of brevity, it has a single interface "ITestCOMServer" that has a single API call, "Add," that adds two integers and returns the result. Also note, this example is void of any error checks.

HRESULT Add (int x, int y, [out] int* result);

The COM client will execute this API.

The first step is to obtain the "handle" to the COM class serving this interface. The following calls instantiate the COM server and also provide a pointer to its IUnknown interface.

IUnknown *ptrUnknown = NULL;
IITestCOMServer *ptrTestServer = NULL;
HRESULT hr = CoCreateInstance (CLSID_ITestCOMServer, NULL, CLSCTX_REMOTE_SERVER, IID_IUnknown,
(void**)&ptrUnknown);

The second step entails getting a pointer to the actual interface in which the "Add" method resides.

hr = ptrUnknown->QueryInterface(IID_IITestCOMServer, (void**)&ptrTestServer);

if (FAILED(hr))
{
cout << "Failed to get interface pointer, quitting";
}

The third step requires us to execute the "Add" API on the pointer obtained in step 2.

else
{
    int *result = new int;
    hr = ptrTestServer->Add(1,2,result);
    cout << "result of Add is " <<*result;
delete result;
}

The last step is to release all references to the COM server and let the COM runtime garbage collect it.

if (ptrTestServer)
{
ptrTestServer->Release();
}

ptrUnknown->Release();

Simple isn't it? Well, the COM runtime does a lot of work to orchestrate this cycle. Let me try to give you an overview of what happens behind the scenes. (Sources: www.opengroup.org, www.msj.com March 1998, DCOM specification)

1.  Each Windows machine on the network has a subsystem known as the Service Control Manager (not to be confused with the Windows "Services" system). It's a DCE/RPC server that listens at port 135 and runs inside rpcss.exe. The SCM makes sure that when a client request is made, the appropriate COM server is connected and ready to receive the request. It provides an RPC interface, known as IRemoteActivation, which has only a single operation, "RemoteActivation," designed to activate a COM server on a remote machine. This, by the way, is an important difference between DCOM and classic RPC where the server must be running before the client can connect to it. The SCM resides at well-known endpoints, one for each supported network protocol (135 for TCP/UDP).
2.  When the client gets a "CoCreateInstance" call with the execution context set as Remote (CLSCTX_REMOTE_SERVER specifies to the runtime that the COM server is located on a remote machine), the COM runtime consults the Windows registry for the "RemoteServerName" named-value. This value is located at [HKEY_CLASSES_ROOT\APPID\(CLSID of TestCOMServer)]. If a machine name is found under this key then the request to activate the COM server is forwarded to SCM on that remote machine. The remote SCM uses the IRemoteActivation interface to activate the object identified by CLSID_ITestCOMServer.
3.  What does it mean to "activate" a COM object? We will get to that after I talk about the IOxidResolver. I think it's important to clear these basics up otherwise things could become quite confusing.

Each machine that supports the COM network protocol supports a one-per-machine service known as the "OXID Resolver." Like the SCM, it also contains an RPC interface "IOxidResolver." Oxid Resolver performs many important operations, primarily maintaining the binding information necessary to connect to the COM components being exported. It also takes care of keeping the exported objects alive by receiving pings from the COM clients (otherwise they'd be garbage collected) and does lazy protocol registration for servers scoped by the Oxid Resolver. I'll explain this last point a bit more. Each COM server can decide to support a certain set of protocols over which it can be contacted. For example, a server may want to answer only on UDP or TCP or HTTP or all three. Instead of reserving ports for each protocol even before it's activated, a server delays this to the time it's actually activated on a requested protocol. This is quite useful in preventing the machine from running out of ports.
4.  Okay, coming back to activation. Activation should be seen as a set of activities that bring a COM server to a "ready to receive requests" state. In general, it means locating the COM server on the remote machine using the Windows registry, registering its connection information with the Oxid Resolver, marshaling the reference to its IUnknown (rather the "IRemUnknown") interface, and sending it back to the callee (explained in step 7).
5.  On the remote machine, when the server is started by its SCM, two activities take place.

  • The server is associated with an "object exporter" and assigned an object exporter identifier (OXID). An object exporter keeps track of all the interfaces (like in our case ITestCOMServer), which this COM server will export or import.
  • The COM runtime also associates an "Oxid Object" with the COM server, which implements the COM interface "IRemUnknown." It forms the remote proxy for the base "IUnknown" interface. Please note the standard IUnknown interface is never remoted in COM. In its place, the IRemUnknown interface is remoted and results in local calls to QueryInterface, AddRef, and Release on the server.

    At activation time, the RPC binding information of the OXID is also registered with the server-side OXID Resolver. These are full bindings carrying the "How-To-Connect" information (including the supported protocol/port combination) of the COM server. This information is used by the underlying RPC mechanism of the client system to initiate a session with the COM server. One more point worth mentioning is that during activation the server has the choice of being "ready" now or waiting for the first call to come (lazy activation). Usually all servers prefer to be lazy till an actual call comes (initially IRemUnknown) on a specific binding.

6.  I've talked about the SCM and the Oxid Resolver service. They are important infrastructure services. One provides for activation and the other for ```discovering the path and means to the activated object.
7.  Up until now, in executing "CoCreateInstance" we've been able to activate a COM object. We still need to return the interface pointer, which will uniquely identify this activated COM object and allow us further operations on it (like a QueryInterface). Microsoft extended the Network Data Representation (the presentation layer protocol responsible for packaging semantics of the DCE/RPC datatypes) to add the concept of a "Marshaled Interface Pointer" (MIP from hereon). The MIP symbolically represents an interface reference to an object. It consists of two elements, an array of bytes and a marker specifying how to interpret this array of bytes. There are three variations to the interpretation, but I'll stick to the STANDARD type.

The array of bytes representing the STANDARD interface pointer consists primarily of a 128-bit GUID known as IPID, short for interface identifier, that uniquely identifies an interface - it has a one-to-one mapping with each marshaled interface, i.e., ITestCOMServer will have a single IPID - an OXID and a 64-bit object ID (OID) that uniquely identifies the object on which the IPID is found. There's one-to-one mapping between an object instance (implementing one or more interfaces thus IPIDs) and the OID. This OID is quite useful during pinging. Along with all this the MIP also contains the full bindings for the OXID Resolver service running on the remote machine.
8.  When the marshaled interface pointer is returned to the client side through the server-side and client-side SCMs, the COM runtime extracts the OXID, addresses the remote OXID Resolver from MIP, and calls the ResolveOxid() method on its local OXID Resolver to get the bindings ("how to connect" information) identified by the OXID (it has to reach the COM server now for further operations).
9.  The clients-side OXID Resolver checks to se if it has a cached mapping for the OXID; if not, it invokes the ResolveOxid() method of the server-side OXID Resolver - it can since it has the address information from the MIP - which returns the registered RPC binding of the COM server.
10.  The client-side Resolver caches the mappings, and returns the RPC bindings of the COM server to the COM runtime. This lets the runtime create an RPC channel that's connected to the Object exporter of the COM server.
11.  The CoCreateInstance call is now complete.


More Stories By Vikram Roopchand

Vikram Roopchand is a Technical Architect working for Infosys Technologies Ltd. (www.infosys.com). He has about 8.5 years of experience and specializes in Cross Platform development across Content Management and Business Intelligence domains.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Peter 01/11/07 07:17:29 PM EST

Fantastic article! I wish I had this library back in 99!

@ThingsExpo Stories
Bert Loomis was a visionary. This general session will highlight how Bert Loomis and people like him inspire us to build great things with small inventions. In their general session at 19th Cloud Expo, Harold Hannon, Architect at IBM Bluemix, and Michael O'Neill, Strategic Business Development at Nvidia, discussed the accelerating pace of AI development and how IBM Cloud and NVIDIA are partnering to bring AI capabilities to "every day," on-demand. They also reviewed two "free infrastructure" pr...
In his keynote at @ThingsExpo, Chris Matthieu, Director of IoT Engineering at Citrix and co-founder and CTO of Octoblu, focused on building an IoT platform and company. He provided a behind-the-scenes look at Octoblu’s platform, business, and pivots along the way (including the Citrix acquisition of Octoblu).
Data is an unusual currency; it is not restricted by the same transactional limitations as money or people. In fact, the more that you leverage your data across multiple business use cases, the more valuable it becomes to the organization. And the same can be said about the organization’s analytics. In his session at 19th Cloud Expo, Bill Schmarzo, CTO for the Big Data Practice at Dell EMC, introduced a methodology for capturing, enriching and sharing data (and analytics) across the organization...
The explosion of new web/cloud/IoT-based applications and the data they generate are transforming our world right before our eyes. In this rush to adopt these new technologies, organizations are often ignoring fundamental questions concerning who owns the data and failing to ask for permission to conduct invasive surveillance of their customers. Organizations that are not transparent about how their systems gather data telemetry without offering shared data ownership risk product rejection, regu...
Grape Up is a software company, specialized in cloud native application development and professional services related to Cloud Foundry PaaS. With five expert teams that operate in various sectors of the market across the USA and Europe, we work with a variety of customers from emerging startups to Fortune 1000 companies.
Financial Technology has become a topic of intense interest throughout the cloud developer and enterprise IT communities. Accordingly, attendees at the upcoming 20th Cloud Expo at the Javits Center in New York, June 6-8, 2017, will find fresh new content in a new track called FinTech.
SYS-CON Events announced today that Interoute, owner-operator of one of Europe's largest networks and a global cloud services platform, has been named “Bronze Sponsor” of SYS-CON's 20th Cloud Expo, which will take place on June 6-8, 2017 at the Javits Center in New York, New York. Interoute is the owner-operator of one of Europe's largest networks and a global cloud services platform which encompasses 12 data centers, 14 virtual data centers and 31 colocation centers, with connections to 195 add...
Multiple data types are pouring into IoT deployments. Data is coming in small packages as well as enormous files and data streams of many sizes. Widespread use of mobile devices adds to the total. In this power panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists will look at the tools and environments that are being put to use in IoT deployments, as well as the team skills a modern enterprise IT shop needs to keep things running, get a handle on all this data, and deli...
The age of Digital Disruption is evolving into the next era – Digital Cohesion, an age in which applications securely self-assemble and deliver predictive services that continuously adapt to user behavior. Information from devices, sensors and applications around us will drive services seamlessly across mobile and fixed devices/infrastructure. This evolution is happening now in software defined services and secure networking. Four key drivers – Performance, Economics, Interoperability and Trust ...
The Internet of Things is clearly many things: data collection and analytics, wearables, Smart Grids and Smart Cities, the Industrial Internet, and more. Cool platforms like Arduino, Raspberry Pi, Intel's Galileo and Edison, and a diverse world of sensors are making the IoT a great toy box for developers in all these areas. In this Power Panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists discussed what things are the most important, which will have the most profound e...
@ThingsExpo has been named the Most Influential ‘Smart Cities - IIoT' Account and @BigDataExpo has been named fourteenth by Right Relevance (RR), which provides curated information and intelligence on approximately 50,000 topics. In addition, Right Relevance provides an Insights offering that combines the above Topics and Influencers information with real time conversations to provide actionable intelligence with visualizations to enable decision making. The Insights service is applicable to eve...
SYS-CON Events announced today that Grape Up will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct. 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Grape Up is a software company specializing in cloud native application development and professional services related to Cloud Foundry PaaS. With five expert teams that operate in various sectors of the market across the U.S. and Europe, Grape Up works with a variety of customers from emergi...
SYS-CON Events announced today that Hitachi, the leading provider the Internet of Things and Digital Transformation, will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Hitachi Data Systems, a wholly owned subsidiary of Hitachi, Ltd., offers an integrated portfolio of services and solutions that enable digital transformation through enhanced data management, governance, mobility and analytics. We help globa...
SYS-CON Events announced today that SoftLayer, an IBM Company, has been named “Gold Sponsor” of SYS-CON's 18th Cloud Expo, which will take place on June 7-9, 2016, at the Javits Center in New York, New York. SoftLayer, an IBM Company, provides cloud infrastructure as a service from a growing number of data centers and network points of presence around the world. SoftLayer’s customers range from Web startups to global enterprises.
20th Cloud Expo, taking place June 6-8, 2017, at the Javits Center in New York City, NY, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy.
SYS-CON Events announced today that Super Micro Computer, Inc., a global leader in compute, storage and networking technologies, will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Supermicro (NASDAQ: SMCI), the leading innovator in high-performance, high-efficiency server technology, is a premier provider of advanced server Building Block Solutions® for Data Center, Cloud Computing, Enterprise IT, Hadoop/...
Amazon has gradually rolled out parts of its IoT offerings in the last year, but these are just the tip of the iceberg. In addition to optimizing their back-end AWS offerings, Amazon is laying the ground work to be a major force in IoT – especially in the connected home and office. Amazon is extending its reach by building on its dominant Cloud IoT platform, its Dash Button strategy, recently announced Replenishment Services, the Echo/Alexa voice recognition control platform, the 6-7 strategic...
Judith Hurwitz is president and CEO of Hurwitz & Associates, a Needham, Mass., research and consulting firm focused on emerging technology, including big data, cognitive computing and governance. She is co-author of the book Cognitive Computing and Big Data Analytics, published in 2015. Her Cloud Expo session, "What Is the Business Imperative for Cognitive Computing?" is scheduled for Wednesday, June 8, at 8:40 a.m. In it, she puts cognitive computing into perspective with its value to the busin...
Cognitive Computing is becoming the foundation for a new generation of solutions that have the potential to transform business. Unlike traditional approaches to building solutions, a cognitive computing approach allows the data to help determine the way applications are designed. This contrasts with conventional software development that begins with defining logic based on the current way a business operates. In her session at 18th Cloud Expo, Judith S. Hurwitz, President and CEO of Hurwitz & ...
Cybersecurity is a critical component of software development in many industries including medical devices. However, code is not always written to be robust or secure from the unknown or the unexpected. This gap can make medical devices susceptible to cybersecurity attacks ranging from compromised personal health information to life-sustaining treatment. In his session at @ThingsExpo, Clark Fortney, Software Engineer at Battelle, will discuss how programming oversight using key methods can incre...