Jacorb Configuration steps

Source: Internet
Author: User
Tags stub

Jacorb application development is generally divided into the following five steps:
1. Write IDL interface definition compile IDL

2. Interface definition generate Java class

3. Implement the interface generated in step 2

4. Write Server startup class and register to Orb

5. Write client to get service object reference download installation Jacorb2.2.4

1 Download Jacorb 2.2.4 from http://www.jacorb.org/download.html, unzip to disk, for example C:\JacORB

2 set in the system environment variable

Classpath=.; C:\JacORB\lib\idl.jar;

C:\JacORB\lib\jacorb.jar;

C:\JacORB\lib\classes;

Also, to facilitate the use of ant and command-line tools, add C:\JacORB\bin to the path

3) Then, copy the jacorb_properties.template in the C:\JacORB\etc directory to the C:\JacORB\classes directory and rename it to Jacorb.properties

4 Configure the Naming service, edit the Jacorb.properties file

Set the orbinitref.nameservice=file:/ D:/jacorb/ns_ref, this ns_ref file is generated when the naming service is started. The rest of the profile retains its default value. test whether the naming Service (ns,naming service) can start normally

1 in the C:\JacORB\bin directory to find the file Jac.bat.tpl, renamed to Jac.bat

2 Enter NS NS_REF under DOS window
If the following output indicates that it has started properly

[Jacorb.orb.print_ver] INFO:

Jacorb V 2.3.0, www.jacorb.org
<C> the Jacorbproject 17-feb_2007

[Jacorb.orb] INFO:P roperty "Jacorb.hashtable_class" is set to:java.util.Hashtable

......

......

[Jacorb.naming] Info:ns up

[Jacorb.orb] Info:orb Run

If there is no output, it may be that your jacorb.properties file is not placed in the directory where classpath resides.

public static void Main (String [] args)
{
Hashtable env = new Hashtable ();
Env.put (Context.initial_context_factory, "org.jnp.interfaces.NamingContextFactory");
Env.put (Context.provider_url, "localhost:1099");
Env.put ("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
Try
{
Context ctx = new InitialContext (env);
Object obj = ctx.lookup ("HelloWorld");
Helloworldhome home = (helloworldhome) Javax.rmi.PortableRemoteObject.narrow (
obj, Helloworldhome.class);
HelloWorld HelloWorld = Home.create ();
System.out.println (Helloworld.hello ());
Helloworld.remove ();
}
catch (Exception e)
{
E.printstacktrace ();
System.out.println ("Exception:" + e.getmessage ());
}
}

To reflect the operation of CORBA technology, starting from this HelloWorld-level CORBA, write in Java

1. Interface definition (HELLO.IDL)
Hello.idl
Module HelloApp
{
Interface Hello
{
String SayHello (in string message);
};
};

Installed Java on its own tool IDLJ (jdk1.3.0_01 above version, previous version is Idltojava or Idl2java), generate the client stub and server framework for interface definition files. The specific actions are as follows:
Idlj-oldimplbase-fall Hello.idl

The following six files will be formed in the HelloApp subdirectory after compilation:
_helloimplbase.java
The abstract class is a server skeleton that provides basic CORBA functionality for the server. It implements the Hello.java interface. Server Class Helloservant extended _helloimplbase.
_hellostub.java
This class is a client stub that provides CORBA functionality to the client. It implements the Hello.java interface.

Hello.java
This interface contains the Java version of the IDL interface. The Hello.java interface extends Org.omg.CORBA.Object and provides standard CORBA object functionality.

Hellohelper.java
This is a final state class that provides accessibility features, especially the narrow () method required to convert a CORBA object reference to the appropriate type.

Helloholder.java
This is a final state class that contains a public instance member of the Hello type. It can provide operations for "out" and "inout" variables. CORBA has these variables, but it's not easy to map to Java semantics.
Hellooperations.java
This is an interface class that contains the method SayHello ().

To complete the application, you simply provide the server and client implementations in file Helloserver.java and Helloclient.java.


2. Interface Implementation (Helloimpl.java)
Helloimpl.java is the implementation of the Hello IDL interface; Each hello instance is implemented by a Helloimpl instance. Helloimpl is a subclass of _helloimplbase, _helloimplbase is generated from the example IDL by the IDLJ compiler.


Import helloapp.*;
public class Helloimpl extends _helloimplbase {
/* Constructor/*
Public Helloimpl () {
Super ();
}
/* Implement Interface declaration method SayHello * *
public string SayHello (String message) {
System.out.println ("I'm on the server side of CORBA, the client is calling the ' SayHello ' method.") ");
System.out.println ("Hello" + message);
return message;
}
}

3. Server-side program (Helloserver.java)
Import org.omg.cosnaming.*;
Import org.omg.corba.*;
public class HelloServer {
public static void Main (String args[]) {
try {
/* Create and Initialize ORB * *
Orb orb = orb.init (args, null);
System.out.println ("Start ORB Server ...");
/* Create an instance and register it with the ORB * *
Helloimpl Helloimpl = new Helloimpl ();
Orb.connect (Helloimpl);
SYSTEM.OUT.PRINTLN ("Register an instance to Orb");
/* Get root Naming Context * *
Org.omg.CORBA.Object objRef =orb.resolve_initial_references ("Nameservice");
NamingContext ncref = Namingcontexthelper.narrow (OBJREF);
/* The object reference in the binding name/*
Namecomponent NC = new Namecomponent ("Hello", "");
Namecomponent path[] = {NC};
Ncref.rebind (path, Helloimpl);
/* Wait for calls from the client * *
Java.lang.Object sync = new Java.lang.Object ();
Synchronized (sync) {
Sync.wait ();
}
System.out.println ("Wait for CORBA client to invoke ...");
catch (Exception e) {
System.err.println ("error:" + E);
E.printstacktrace (System.out);
}
}
}


4. client program (Helloclient.java)
Import helloapp.*;
Import org.omg.cosnaming.*;
Import org.omg.corba.*;
public class Helloclient {
public static void Main (String args[]) {
try {
* * Create and Initialize orb */
Orb orb = Orb.init (args, null);
/* Get root Naming Context */
Org.omg.CORBA.Object objRef = orb.resolve_initial_references ("Nameservice");
NamingContext ncref = Namingcontexthelper.narrow (OBJREF);
/* Resolution of object references in naming */
Namecomponent NC = new Namecomponent ("Hello", "");
Namecomponent path[] = {NC};
Hello h = hellohelper.narrow (ncref.resolve (path));
/* Calls the Hello server object and prints the result */
System.out.println ("I started calling the ' SayHello ' method on the CORBA server side at the client);
System.out.println ("Welcome," + H.sayhello ("JAVAMXJ blog"));
} catch (Exception e) {
System.out.println ("error:" + e);
E.printstacktrace (System.out);
}
}
}

Compilation and running of the



CORBA server/client

• Copy the top 4 files to the D:\CorbaSample directory. Create client and server directories under this directory, assuming that they are clients and servers respectively.

• Compile hello.idl
D:\corbasample>idlj-oldimplbase-fall hello.idl
This will generate a HelloApp directory

· Compile all Java files:
D:\corbasample>javac *.java helloapp/*.java

• Create HelloApp subdirectories in the client and server directories, respectively, D:\
_hellostub.class
Hello.class
Hellohelper.class
Helloholder.class
in the Corbasample\helloapp directory Hellooperations.class
Copy to the D:\CORBASAMPLE\CLIENT\HELLOAPP directory, and then copy Helloclient.class from the D:\CorbaSample directory to D:\ The Corbasample\client directory.

Copies the
_helloimplbase.class
Hello.class
Hellooperations.class
in the D:\CorbaSample\HelloApp directory to D:\ Corbasample\server\helloapp directory, and then copy the Helloserver.class and Helloimpl.class in the D:\CorbaSample directory to D:\CorbaSample\ Server directory
(note: Of course, you can do this directly in the D:\CorbaSample directory without having to create server and client directories and above, and I do this primarily to differentiate between client and server)

:


· Make sure the name server is in a running state:
D:\corbasample\server>tnameserv-orbinitialport 1050


· Start Hello server in another DOS window:
D:\corbasample\server>java Helloserver-orbinitialport 1050

· And then start the CORBA client invoking the CORBA service in another DOS window:
D:\corbasample\client>java Helloclient-orbinitialport 1050
(Local invocation without the need to specify a remote IP address with the-orbinitialhost parameter)

D:\corbasample\server>java helloclient-orbinitialhost Localhost-orbinitialport 1050
(a remote call to the CORBA service can replace localhost with a remote IP address)

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.