Servlet Inter-communication with CORBA(来源:http://java.sun.com) with code concept contributed by Channasubbanna Prasad (April 2001)
A Common Object Request Broker Architecture (CORBA) object can be invoked from a Web browser using CGI scripts or applets. The CGI solution is slow and inconvenient for two reasons:
This article gives a brief overview of CORBA, then discusses servlets and demonstrates how servlets can communicate with CORBA servers. The example in this article use JavaIDL and Tomcat. If you want to see how a CORBA client can be implemented as a stand-alone application or as an applet, see Getting Started with JavaIDL.
The Common Object Request Broker Architecture (or CORBA) is an industry standard specification developed by
the Object Management Group (OMG) to aid in creating and using distributed objects. The software that implements the specification is called the Object Request Broker (or ORB), which is the heart of CORBA, as you can see from Figure 1.
Figure 1: A request from a client to an object implementation There are a couple of things to note about CORBA architecture and its computing model:
The interface between the client and server is simply a contract that specifies what kind of services are provided by the server and how they can be used by the client. CORBA interfaces are specified in a special definition language known as the Interface Definition Language (IDL). Through IDL, a particular object implementation tells its potential clients what operations are available and how they should be invoked. From IDL definitions, CORBA objects are mapped into different programming languages like C, C++, Java, and Smalltalk. This means once you define an interface to objects in IDL, you are free to implement the object using any suitable programming language that has IDL mapping. Consequently, if you want to use that object, you can use any programming language to make remote requests to the object. The development life cycle of CORBA-based systems is touched on throughout this article. The Servlet Programming ModelThe HTTP protocol is a request-response application protocol in which the parameters of the request must be set before the request is sent. For example, the input values in a fill-out form will be sent as part of the request when the form is submitted. Similar to CGI, servlets support a request and response programming model. When a client sends a request to the server, the server sends the request to the servlet. The servlet then constructs a response that the server sends back to the client. Unlike CGI scripts, however, servlets run within the same process as the HTTP server.
When a client request is made, the Invoking a CORBA object from a ServletOnce you understand basics of servlets and CORBA, you can look at an example of a simple calculator. The service:
Defining the IDL InterfaceThe first step in developing a CORBA service is defining an IDL interface that specifies the type of operations the server will support. In this case, the operations are of a mathematical nature -- adding numbers, subtracting numbers, multiplying numbers, and so on. For the sake of simplicity, this next example only considers the multiplication operation. As an exercise, you can modify the code by adding more operations of your own.
Code sample 1 shows the IDL interface for the
Code sample 1: Implementing the Interface
Implementing the
Code sample 2:
One important thing to note from Code sample 2 is the third argument of the
Each holder class has a default constructor and a public instance member. The default constructor
sets the To understand why holder classes are needed, consider the following operation:
In Java if you have To solve this problem, holder classes are used like this:
Developing the CORBA Server
The next step is implementing the CORBA server. The
The
Code sample 3: import org.omg.CosNaming.*; // naming service import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.util.*; import Arith.*; // the package containing the stubs and skeletons /** * Math Server * * @author Qusay H. Mahmoud [email protected]> */ public class MathServer { public static void main(String args[]) { try{ // Create and initialize the ORB ORB orb = ORB.init(args, null); // Create the servant and register // it with the ORB MultiplyServant mulRef = new MultiplyServant(); orb.connect(mulRef); // Get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // Bind the object reference in naming NameComponent nc = new NameComponent("Multiply", " "); NameComponent path[] = {nc}; ncRef.rebind(path, mulRef); System.out.println("server started...."); // Wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized(sync){ sync.wait(); } } catch(Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } }
Once the
The Developing the ClientInstead of developing a standalone client application to invoke the CORBA object, you can develop a Web application and a servlet that acts as a CORBA client. In this example, an HTML form is displayed in a browser with two fields and a button. The HTML source is shown in Code sample 4.
Code sample 4:
The request method used in this example is
Figure 2: HTML form loaded in a browser
Now it is time to develop a servlet that will act as a CORBA client. A sample implementation,
Code sample 5: import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; import Arith.*; // the package containing // the stubs and skeletons /** * Math servlet as a CORBA client * * @author Qusay H. Mahmoud [email protected]> */ public class MathServlet extends HttpServlet { Multiplication mulRef = null; NamingContext ncRef = null; NameComponent nc = null; public void init(ServletConfig config) throws ServletException { super.init(config); try { String args[] = null; // Create and initialize the ORB ORB orb = ORB.init(args, null); // Get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); ncRef = NamingContextHelper.narrow(objRef); // Resolve the object reference in naming nc = new NameComponent("Multiply", " "); } catch(Exception e) { System.out.println("ERROR : " + e); e.printStackTrace(System.out); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>MathServlet</title>"); out.println("</head>"); out.println("<body bgcolor=\"#ffffcc\" text=\"#006666\">"); String firstnumber = request.getParameter("firstnum"); String secondnumber = request.getParameter("secondnum"); double x = 0.0; double y = 0.0; if (firstnumber != null && secondnumber != null) { org.omg.CORBA.DoubleHolder z = new DoubleHolder(); try { x = (Double.valueOf(firstnumber)).doubleValue(); y = (Double.valueOf(secondnumber)).doubleValue(); } catch (NumberFormatException nfe) {} try { NameComponent path[] = {nc}; mulRef = MultiplicationHelper.narrow(ncRef.resolve(path)); mulRef.multiply(x, y, z); } catch (Exception e) {} out.println("<h3>The operation "+ x + " * "+y+ " = "+z.value+"</h3>"); } else { out.println("Null."); } out.println("</body>"); out.println("</html>"); } } Testing the applicationYou can compile and test the application as follows:
If everything goes well, you should see something similar to Figures 3 and 4.
Figure 3: Entering numbers in the form
Figure 4: CORBA object invoked by MathServlet Running the application on two machines
In the example above, you see how to run the application on a single machine. But in some cases
it may make more sense to run the application on two machines: one machine for the Web server and the servlet, and another machine for CORBA. There are several properties that can be used to
facilitate this, two of them are: If you like, you can start the naming service on a different port number as follows:
Now, when you start the CORBA server, you must inform it of the new port number for the naming service:
And, when you start the client, you must instruct it as to where to find the naming service and the server:
Alternatively these options can be specified at the code level using properties. So instead of initializing the ORB as:
It can be initialized specifying that the CORBA server machine (called turing) and the naming service's port number (to be 1500) as follows:
ConclusionIn Web-based CORBA applications, servlets can be key components in leveraging Java and other Internet technologies. This article has shown how to invoke a CORBA object from an HTML form using a servlet that acts as a CORBA client. Invoking a CORBA object from a servlet makes sense as your CORBA-based applications for the Web would be compatible with all browsers, as long as they support HTML forms. For more information
About the authorQusay H. Mahmoud provides Java consulting and training services. Qusay has published dozens of articles on Java, and is the author of Distributed Programming with Java (Manning Publications, 1999). |