Js| Object | Access combined with Java EE and CORBA, we can make full use of the advantages of both to build more powerful application systems, such as the CORBA object implemented by C + + as the business processing component, to form high-performance business logic layer, and to access CORBA component to build the presentation layer in JSP. Let's take a look at an example of accessing a CORBA service object in a JSP.
Software Selection:
CORBA service Provider, we choose the Software Business Middleware Co., Ltd. ( http://www.inforbus.comInforbus (a distributed object middleware that follows the CORBA specification) is developed based on C + +. The operating system chooses IBM AIX (of course, you can also choose HP UNIX, Linux, or Windows).
CORBA client, in order to demonstrate CORBA's interoperability capabilities, the orb contained in the JDK is used here. Of course, as a better choice, you can also use the Java version of Inforbus to implement CORBA's client side.
JSP operating environment, we choose to create software Business Middleware Co., Ltd. ( http://www.inforbus.comInforweb (an application server that follows the Java EE specification). The operating system chooses Windows (of course, you can also choose HP UNIX, Linux, or AIX).
Program Implementation
IDL
IDL is the protocol that the CORBA server and the client make the request call, and the client and the service can communicate seamlessly without any relation to the development language and operating system of the client and the service party, as long as the same IDL is used. You can even choose different CORBA middleware for both the client and the service provider (as long as they follow the CORBA specification). When implementing a CORBA client program, you do not need to care about any details of the service side, all you need to care about is idl!
Here we define the IDL (file name Apptest.idl) as shown below:
- module Example {
- Interface A
- Long Aoperation (in long ildata);
- };
- };
implementation of CORBA service-side program
The main tasks of CORBA Server implementation include: mapping IDL to C + +, implementing interfaces defined in IDL (that is, implementing processing logic), and writing a main program instantiation service object to provide services.
1. Mapping IDL to C + +
This only needs to invoke Inforbus IDL compiler IDL and execute the following command:
IDL Apptest.idl
This command will produce four files: apptest.h, Apptest.cpp, Apptest_skel.cpp, Apptest_skel.cpp, which contains skeleton.
2. Implementing the interfaces defined in IDL
Inherit skeleton, implement IDL interface definition, complete your business logic in implementation class, in this example we implement a simple logic, multiply the received parameter by 2, and return the result, the code is as follows:
- ////////////////////////////////////////
- //filename: apptest_impl.h
- #ifndef Apptest_impl_h
- #define Apptest_impl_h
- #include \ "Apptest_skel.h\"
- class A_impl: public poa_example::a, Portableserver::refcountservantbase {
- Public:
- Virtual CORBA::long aoperation (CORBA::long Ildata)
- Throw (CORBA::systemexception);
- };
- #endif
- //////////////////////////////////////////////////////
- //filename: apptest_impl.cpp
- //contains CORBA System class library
- #include <STARCORBA.h>
- using namespace Std;
- #include <apptest_impl.h>
- implementation of//Operation Aoperation
- CORBA::long a_impl::aoperation (CORBA::long Ildata)
- Throw (CORBA::systemexception) {
- CORBA::Long ret;
- RET = Ildata * 2;
- return ret;
- }
3. Write a main program instantiation service object Provision service
In the main program, establish a CORBA environment, instantiate the service object and register it with the name service, and then start waiting for the request, the code is as follows (for):
- ///////////////////////////////////////////
- //filename: Server.cpp
- #include <stdio.h>
- #include <STARCORBA.h>
- #include <STARCosNaming.h>
- #include <stdlib.h>
- #include <errno.h>
- #include \ "Apptest_impl.h\"
- using namespace Std;
- int Main (int argc, Char* argv[], char*[]) {
- Corba::orb_var ORB;
- Orb = Corba::orb_init (argc, argv);
- Corba::object_var nameobj =
- Orb-> resolve_initial_references (\ "Nameservice\");
- Cosnaming::namingcontext_var NC =
- Cosnaming::namingcontext:: _narrow (Nameobj.in ());
- Corba::object_var poaobj =
- Orb-> resolve_initial_references (\ "Rootpoa\");
- Portableserver::P Oa_var Rootpoa =
- Portableserver::POA:: _narrow (Poaobj);
- Portableserver::P Oamanager_var manager =
- Rootpoa-> The_poamanager ();
- a_impl* Aimpl = new A_impl ();
- Portableserver::servantbase_var Servanta = Aimpl;
- Portableserver::objectid_var Aid =
- Rootpoa-> Activate_object (Servanta);
- Corba::object_var A =
- Rootpoa-> servant_to_reference (Servanta);
- Cosnaming::Name aname;
- Aname. length (1);
- Aname[0].id = corba::string_dup (\ "A\");
- Aname[0].kind = corba::string_dup (\ "Operationa\");
- NC-> rebind (aname, a.in ());
- Manager-> activate ();
- Orb-> Run ();
- NC-> Unbind (aname);
- return exit_success;
- }
Web Application Implementation
In Web applications, CORBA service objects are accessed and displayed through a JSP, which serves as the client side of CORBA. Implementing a Web application requires two tasks: mapping IDL to Java, sending requests in a JSP, and displaying the results.
1. Mapping IDL to Java
This requires only the IDL compiler IDLJ to invoke the JDK, and execute the following command:
IDLJ Apptest.idl
This command will produce five files: A.java, Aoperations.java, Ahelper.java, Aholder.java, and _astub.java, which contains stubs.
2. Send the request in the JSP and show the result.
To avoid having too many script in the JSP, the code that sends the request is separated from the JSP into a simple Java class.
The code for the Java class is as follows (Aclient.java):
- /*
- * Aclient.java
- */
- Package example;
- import org.omg.corba.*;
- Import org.omg.CORBA.ORBPackage. Invalidname;
- import org.omg.cosnaming.*;
- Import org.omg.CosNaming.NamingContextPackage. Cannotproceed;
- Import org.omg.CosNaming.NamingContextPackage. NotFound;
- /**
- * @author the public fly
- */
- Public class aclient {
- Public Static int OpA (int num) throws Exception {
- String [] args = { \ "-orbinitref\",
- \ "Nameservice=corbaloc:iiop:192.168.60.158:900/nameservice\" };
- Orb orb = orb. Init (args, null);
- Org.omg.CORBA. Object ObjRef;
- ObjRef = orb.resolve_initial_references (\ "Nameservice\");
- namingcontext ncref = namingcontexthelper. Narrow (OBJREF);
- namecomponent NC = new namecomponent(\ "A\", \ "Operationa\");
- namecomponent path[] = {NC};
- Org.omg.CORBA. Object Obja;
- Obja = ncref.resolve (path);
- A A = Ahelper.narrow (Obja);
- int out;
- out = A.aoperation (num);
- return out;
- }
- }
The code for the JSP file is as follows (index.jsp):
- <%@ page contenttype=\ "Text/html;charset=gb2312\"%>
- <%@ page Import = \ "Example. Aclient\ " %>
-
-
- <title>hello JSP CORBA client</title>
-
- <body>
- 3*2=<%=aclient.opa (3)%>
- </body>
-
Well, compile your program, run the name service, run the CORBA service program, run the application server Inforweb, deploy the Web application, and then you can access it through the browser.