Showing posts with label Lab programs. Show all posts
Showing posts with label Lab programs. Show all posts

An application that access CORBA service using JIDL





Aim: Write an CORBA application using JAVA IDL
Required programs:
  • IDL file(interface) for simple HelloWorld

  • A Server class

  • A Client class

Hello.idl
module HelloApp
{
  interface Hello
  {
  string sayHello();
  oneway void shutdown();
  };
};
Implementing the Server (HelloServer.java)
HelloServer.java


import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;


import java.util.Properties;


class HelloImpl extends HelloPOA{
  private ORB orb;


  public void setORB(ORB orb_val){
    orb = orb_val;
  }
 
  public String sayHello(){
    return "\nHello world !!\n";
  }
 
  public void shutdown(){
    orb.shutdown(false);
  }
}


public class HelloServer{


  public static void main(String args[]){
    try{
      // create and initialize the ORB
      ORB orb = ORB.init(args, null);


      // Get reference to rootpoa & activate the POAManager
      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
      rootpoa.the_POAManager().activate();


      // create servant and register it with the ORB
      HelloImpl helloImpl = new HelloImpl();
      helloImpl.setORB(orb);


      // create a tie, with servant being the delegate.
      HelloPOATie tie = new HelloPOATie(helloImpl, rootpoa);


      // obtain the objectRef for the tie
      // this step also implicitly activates the
      // the object
      Hello href = tie._this(orb);
           
      // get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
     
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);


      // bind the Object Reference in Naming
      String name = "Hello";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);


      System.out.println("HelloServer ready and waiting ...");


      // wait for invocations from clients
      orb.run();
      }
     
    catch (Exception e){
      System.err.println("ERROR: " + e);
      e.printStackTrace(System.out);
    }
   
    System.out.println("HelloServer Exiting ...");
       
  }
}


HelloClient.java




import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;


public class HelloClient{


  public static void main(String args[]){
 
    try{
      // 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");
         
      // Use NamingContextExt instead of NamingContext. This is
      // part of the Interoperable naming Service. 
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);


      // resolve the Object Reference in Naming
      String name = "Hello";
      Hello helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));


      System.out.println("Obtained a handle on server object: " + helloImpl);
      System.out.println(helloImpl.sayHello());
      helloImpl.shutdown();
      }
     
    catch (Exception e) {
      System.out.println("ERROR : " + e) ;
      e.printStackTrace(System.out);
    }
  }
}












To run this client-server application on your development machine:
  1. Change to the directory that contains the file Hello.idl.

  2. Run the IDL-to-Java compiler, idlj, twice on the IDL file to create stubs and skeletons. This step assumes that you have included the path to the java/bin directory in your path.

3.    idlj -fall  Hello.idl
4.    idlj -fallTie Hello.idl
    • It generates



    • HelloPOA.java

    • _HelloStub.java

    • Hello.java

    • HelloHelper.java

    • HelloHolder.java

    • HelloOperations.java



  1. Compile the .java files, including the stubs and skeletons (which are in the directory HelloApp). This step assumes the java/bin directory is included in your path.



6.     javac *.java HelloApp/*.java


  1. Start orbd.

  start orbd -ORBInitialPort 1050 -ORBInitialHost localhost
  1. Start the Hello server.

start java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost
  1. Run the client application:

 java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost


output:


Obtained a handle on server object: IOR:000000000000001749444c3a48656c6c6f417070
2f48656c6c6f3a312e300000000000010000000000000082000102000000000a3132372e302e302e
3100055a00000031afabcb00000000203a12163f00000001000000000000000100000008526f6f74
504f4100000000080000000100000000140000000000000200000001000000200000000000010001
00000002050100010001002000010109000000010001010000000026000000020002


Hello world !!
Read More...

A Program for Demonstrating JNDI (JAVAEE) using GLASSFISH

Aim: Write a program to demonstrate JNDI



program:



import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import java.util.Hashtable;

class MyLookupClass {

  public static void main(String[] args) {

    String myObject = " ";            // use your obj name

    Hashtable ep = new Hashtable(1);

    ep.put(Context.INITIAL_CONTEXT_FACTORY,

      "com.sun.jndi.fscontext.RefFSContextFactory");

    try {

      InitialContext ct = new InitialContext(ep);

Object obj = ct.lookup(myObject);

System.out.println("Object: " + obj);

ct.close();

    }

    catch (NamingException error) {

      System.err.println("Error:" + error.getMessage());

    }

  }

}





Execution steps:



step 1:

set the classpaths before the compilation and run.Here GLASSFISH is the Application server



a)set the foldername where we are executing the jndi

b)set the fscontext.jar

c)set the javaee.jar

d)set the jndi-properties.jar



step 2:



compile the code using "javac"

javac MyLookupClass.java



step 3:



Run the code using "java" at cmd prompt



java MyLookupClass



output:(Appears on the same cmd prompt)



Object: com.sun.jndi.fscontext.RefFSContext@1050e1f Read More...

A Program for Sending mail in java (JAVAEE)

Aim: Write a program to demonstrate java mail



program:



import java.util.*;

import java.io.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;



public class javamail_send extends Object {



  public static void main(String args[]){



    String smtpServer = null;

    String toEmail    = null;

    String fromEmail  = null;

    String body       = null;

  

    //--[ Parse the Command line parameters

    for ( int x=0; x < args.length-1; x++ ){

      if ( args[x].equalsIgnoreCase("-S") )

        smtpServer  = args[x+1];

      else if ( args[x].equalsIgnoreCase("-T") )

        toEmail     = args[x+1];

      if ( args[x].equalsIgnoreCase("-F") )

        fromEmail  = args[x+1];

      if ( args[x].equalsIgnoreCase("-B") )

        body  = args[x+1];

    }

  

 if ( smtpServer == null || toEmail == null || fromEmail == null || body == null ){

   System.out.println( "Usage: javamail_send -S -T -F -B " );

   System.exit(1);

 }



 //--[ Obtain a session

 try{

   //--[ Set up the default parameters

      Properties props = new Properties();

      props.put("mail.transport.protocol", "smtp" );

      props.put("mail.host", smtpServer );              

    



    //--[ Create the session and create a new mail message

      Session mailSession = Session.getInstance( props );

      Message msg = new MimeMessage( mailSession );



   //--[ Set the FROM, TO, DATE and SUBJECT fields

      msg.setFrom( new InternetAddress( fromEmail ) );

      msg.setRecipients( Message.RecipientType.TO,

                         InternetAddress.parse(toEmail) );

      msg.setSentDate( new Date() );

      msg.setSubject( "Test Mail" );



      //--[ Create the body of the mail

      msg.setText( body );



      Transport.send( msg );

    

      System.out.println( "The email below was sent successfully" );

      msg.writeTo( System.out );



 }catch(Exception E){

   System.out.println( E );

 }

  }

}







Execution Procedure:



step 1:  save this code with javamail_send.java and compile the code using javac.



  i.e.

  c:\>cd javamail  //this enters into the javamail folder

  c:\javamail> javac javamail_send.java



step 2: Set the classpath



  it can be done with the help of classpath setting in the environmental varialbles( taking GLASSFISH as app server )



 a) set the javaee.jar

 b) set the mail.jar

 c) the folder name where we are compiling our code

 d)make sure smtp server should be in our system(windows providing default smtp server as a optional component)



step 3: Run the code



c:\javamail>java javamail_send -s localhost -t localhost -f localhost -b "Hai, this is the program for jmail"



output:



The email below was sent successfully

Date: Wed, 22 Sep 2010 20:52:59 +0530 (IST)

From: localhost

To: localhost

Message-ID: <10748354.0.1285168979984.JavaMail.maxx@localhost>

Subject: Test Mail

MIME-Version: 1.0

Content-Type: text/plain; charset=us-ascii

Content-Transfer-Encoding: 7bit Read More...
Related Posts with Thumbnails
GiF Pictures, Images and Photos