JSP access CORBA service object as client side

Source: Internet
Author: User
Tags define object contains implement include interface access linux
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:
    1. module Example {
    2. Interface A
    3. Long Aoperation (in long ildata);
    4. };
    5. };


   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:
  1. ////////////////////////////////////////
  2. //filename: apptest_impl.h
  3. #ifndef Apptest_impl_h
  4. #define Apptest_impl_h
  5. #include \ "Apptest_skel.h\"
  6. class A_impl: public poa_example::a, Portableserver::refcountservantbase {
  7. Public:
  8. Virtual CORBA::long aoperation (CORBA::long Ildata)
  9. Throw (CORBA::systemexception);
  10. };
  11. #endif
  12. //////////////////////////////////////////////////////
  13. //filename: apptest_impl.cpp
  14. //contains CORBA System class library
  15. #include <STARCORBA.h>
  16. using namespace Std;
  17. #include <apptest_impl.h>
  18. implementation of//Operation Aoperation
  19. CORBA::long a_impl::aoperation (CORBA::long Ildata)
  20. Throw (CORBA::systemexception) {
  21. CORBA::Long ret;
  22. RET = Ildata * 2;
  23. return ret;
  24. }


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):
  1. ///////////////////////////////////////////
  2. //filename: Server.cpp
  3. #include <stdio.h>
  4. #include <STARCORBA.h>
  5. #include <STARCosNaming.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include \ "Apptest_impl.h\"
  9. using namespace Std;
  10. int Main (int argc, Char* argv[], char*[]) {
  11. Corba::orb_var ORB;
  12. Orb = Corba::orb_init (argc, argv);
  13. Corba::object_var nameobj =
  14. Orb-> resolve_initial_references (\ "Nameservice\");
  15. Cosnaming::namingcontext_var NC =
  16. Cosnaming::namingcontext:: _narrow (Nameobj.in ());
  17. Corba::object_var poaobj =
  18. Orb-> resolve_initial_references (\ "Rootpoa\");
  19. Portableserver::P Oa_var Rootpoa =
  20. Portableserver::POA:: _narrow (Poaobj);
  21. Portableserver::P Oamanager_var manager =
  22. Rootpoa-> The_poamanager ();
  23. a_impl* Aimpl = new A_impl ();
  24. Portableserver::servantbase_var Servanta = Aimpl;
  25. Portableserver::objectid_var Aid =
  26. Rootpoa-> Activate_object (Servanta);
  27. Corba::object_var A =
  28. Rootpoa-> servant_to_reference (Servanta);
  29. Cosnaming::Name aname;
  30. Aname. length (1);
  31. Aname[0].id = corba::string_dup (\ "A\");
  32. Aname[0].kind = corba::string_dup (\ "Operationa\");
  33. NC-> rebind (aname, a.in ());
  34. Manager-> activate ();
  35. Orb-> Run ();
  36. NC-> Unbind (aname);
  37. return exit_success;
  38. }


   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):
  1. /*
  2. * Aclient.java
  3.  */
  4. Package example;
  5. import org.omg.corba.*;
  6. Import org.omg.CORBA.ORBPackage. Invalidname;
  7. import org.omg.cosnaming.*;
  8. Import org.omg.CosNaming.NamingContextPackage. Cannotproceed;
  9. Import org.omg.CosNaming.NamingContextPackage. NotFound;
  10. /**
  11. * @author the public fly
  12.  */
  13. Public class aclient {
  14. Public Static int OpA (int num) throws Exception {
  15. String [] args = { \ "-orbinitref\",
  16. \ "Nameservice=corbaloc:iiop:192.168.60.158:900/nameservice\" };
  17. Orb orb = orb. Init (args, null);
  18. Org.omg.CORBA. Object ObjRef;
  19. ObjRef = orb.resolve_initial_references (\ "Nameservice\");
  20. namingcontext ncref = namingcontexthelper. Narrow (OBJREF);
  21. namecomponent NC = new namecomponent(\ "A\", \ "Operationa\");
  22. namecomponent path[] = {NC};
  23. Org.omg.CORBA. Object Obja;
  24. Obja = ncref.resolve (path);
  25. A A = Ahelper.narrow (Obja);
  26. int out;
  27. out = A.aoperation (num);
  28. return out;
  29. }
  30. }


The code for the JSP file is as follows (index.jsp):
    1. <%@ page contenttype=\ "Text/html;charset=gb2312\"%>
    2. <%@ page Import = \ "Example. Aclient\ " %>
    3. <title>hello JSP CORBA client</title>
    4. <body>
    5. 3*2=<%=aclient.opa (3)%>
    6. </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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.