Domino Java Code for Creating Session

JAVA/CORBA CLASSES


Examples: Running a Java program
The following examples demonstrate the minimal code needed for a Java program that uses the Domino classes. The examples instantiate a Session object and print the String output of its getPlatform method (Platform property).

1. This is an application that makes local calls and extends the NotesThread class:

    import lotus.domino.*;
    public class platform1 extends NotesThread
    {
     public static void main(String argv[])
       {
           platform1 t = new platform1();
           t.start();
       }
     public void runNotes()
       {
       try
         {
           Session s = NotesFactory.createSession();
           // To bypass Readers fields restrictions
           // Session s = NotesFactory.createSessionWithFullAccess();
           String p = s.getPlatform();
           System.out.println("Platform = " + p);
         }
       catch (Exception e)
         {
           e.printStackTrace();
         }
       }
    }



2. This is an application that makes local calls and implements the Runnable interface:

    import lotus.domino.*;
    public class platform2 implements Runnable
    {
     public static void main(String argv[])
       {
           platform2 t = new platform2();
           NotesThread nt = new NotesThread((Runnable)t);
           nt.start();
       }
     public void run()
       {
       try
         {
           Session s = NotesFactory.createSession();
           String p = s.getPlatform();
           System.out.println("Platform = " + p);
         }
       catch (Exception e)
         {
           e.printStackTrace();
         }
       }
    }

3. This is an application that makes local calls and uses the static NotesThread methods:

    import lotus.domino.*;
    public class platform3
    {
       public static void main(String argv[])
       {
           try
           {
               NotesThread.sinitThread();
               Session s = NotesFactory.createSession();
               String p = s.getPlatform();
               System.out.println("Platform = " + p);
           }
           catch(Exception e)
           {
               e.printStackTrace();
           }
           finally
           {
               NotesThread.stermThread();
           }
       }
    }

4. This is an agent program where the code is entered through the Domino Designer UI. The UI generates everything in the example except the two lines following // (Your code goes here).

    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
     public void NotesMain() {
       try {
         Session session = getSession();
         AgentContext agentContext =
              session.getAgentContext();
         // (Your code goes here)
         String p = session.getPlatform();
         System.out.println("Platform = " + p);
       } catch(Exception e) {
         e.printStackTrace();
       }
     }
    }

5. This is an agent program where the code is imported into Domino Designer:

    import lotus.domino.*;
    public class platform4 extends AgentBase
    {
       public void NotesMain()
       {
       try
           {
               Session s = getSession();
               String p = s.getPlatform();
               System.out.println("Platform =" + p);
           }
       catch (Exception e)
           {
               e.printStackTrace();
           }
       }
    }

6. This is an agent program with print output that can be seen by a browser:

    import lotus.domino.*;
    import java.io.PrintWriter;
    public class platform5 extends AgentBase
    {
       public void NotesMain()
       {
           try
           {
               Session s = getSession();
               String p = s.getPlatform();
               PrintWriter pw = getAgentOutput();
               pw.println("Platform = " + p);
           }
           catch (Exception e)
           {
               e.printStackTrace();
           }
       }
    }

7. This example demonstrates an application that makes remote (IIOP) calls. The example requires the user to enter the name of the host Domino server and optionally a user name and password. If a user name and password are not supplied, the server must allow anonymous access.

    import lotus.domino.*;
    public class platform6 implements Runnable
    {
     String host=null, user="", pwd="";
     public static void main(String argv[])
       {
         if(argv.length<1)
         {
           System.out.println(
              "Need to supply Domino server name");
           return;
         }
         platform6 t = new platform6(argv);
         Thread nt = new Thread((Runnable)t);
         nt.start();
       }
     public platform6(String argv[])
     {
       host = argv[0];
       if(argv.length >= 2) user = argv[1];
       if(argv.length >= 3) pwd = argv[2];
     }
     public void run()
       {
       try
         {
           Session s = NotesFactory.createSession(
                       host, user, pwd);
           String p = s.getPlatform();
           System.out.println("Platform = " + p);
         }
       catch (Exception e)
         {
           e.printStackTrace();
         }
       }
    }

8. To enable SSL in the previous application example, replace:

            Session s = NotesFactory.createSession(
                       host, user, pwd);

    With:

            String args[] = new String[1];
           args[0] = "-ORBEnableSSLSecurity";
           Session s = NotesFactory.createSession(
                       host, args, user, pwd);

    TrustedCerts.class (typically in domino\java in the data directory of the server) must be on the classpath. This file is generated by the DIIOP task when it starts.

    In order to enable SSL for an applet, you should also enable the attribute “Applet uses CORBA SSL Security” in the “Java Applet Properties” dialog.

9. This example demonstrates an applet that makes Domino calls. AppletBase makes the calls locally if possible, remotely otherwise.

    import lotus.domino.*;
    public class foo extends AppletBase
    {
     java.awt.TextArea ta;
     public void notesAppletInit()
     {
       setLayout(null);
       setSize(100,100);
       ta = new java.awt.TextArea();
       ta.setBounds(0,0,98,98);
       add(ta);
       ta.setEditable(false);
       setVisible(true);
     }
     public void notesAppletStart()
     {
       Session s = null;
       try
       {
         // Can also do openSession(user, pwd)
         s = this.openSession();
         if (s == null) { //not able to make connection, warn user
           ta.append("Unable to create a session with the server");
           return;
         }
         String p = s.getPlatform();
         ta.append("Platform = " + p);
       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
       finally
       {
         try {this.closeSession(s);}
         catch(NotesException e) {e.printStackTrace();}
       }
     }
    }

10. This example demonstrates an applet that makes Domino calls in code that responds to an AWT event. Because the AWT event handler is a separate thread, you must explicitly initialize and terminate a Domino session if the code is running locally, and must not if the code is running remotely (IIOP).

    import lotus.domino.*;
    import java.awt.*;
    import java.awt.event.*;
    public class awt extends AppletBase implements ActionListener
    {
     private Button b;
     private String text = "";
     Graphics g;
     public java.awt.TextArea ta;
     public Session s;
     public void notesAppletInit()
     {
       g = getGraphics();
       b = new Button("Get user common name");
       add(b);
       ta = new java.awt.TextArea(15, 30);
       add(ta);
       setVisible(true);
     }
     public void paint(Graphics g)
     {
       b.setLocation(0, 0);
       ta.setLocation(200, 0);
     }
     public void notesAppletStart()
     {
       try
       {
         s = this.getSession();
         b.addActionListener(this);
       }
       catch(NotesException e)
       {
         text = e.id + " " + e.text;
       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
     }
     public void actionPerformed(ActionEvent e)
     {
       text = "";
       getTheName();
       ta.append(text + "\n");
     }
     public void getTheName()
     {
       try
       {
         if (isNotesLocal())
         {
           NotesThread.sinitThread();
         }
         text = "User " + s.getCommonUserName();
       }
       catch(NotesException e)
       {
         text = e.id + " " + e.text;
       }
       catch(Exception e)
       {
         e.printStackTrace();
       }
       finally
       {
         if (isNotesLocal())
         {
           NotesThread.stermThread();
         }
       }
     }
    }

11. This Domino agent gets a token for Single Sign-on and creates a remote (IIOP) session to another server based on the token. The agent runs on the server containing the token.

    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
     public void NotesMain() {
       try {
         Session session = getSession();
         AgentContext agentContext = session.getAgentContext();
         Session s2 = NotesFactory.createSession("test5.irish.com",
                      session.getSessionToken());
         System.out.println("remote session name = " + s2.getUserName());
       } catch(Exception e) {
         e.printStackTrace();
       }
     }
    }

12. This Domino agent gets a token for Single Sign-on and creates a remote (IIOP) session to a server based on the token. The agent runs a Notes client.

    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
     public void NotesMain() {
       try {
         Session session = getSession();
         AgentContext agentContext = session.getAgentContext();
         Session s2 = NotesFactory.createSession("test5.irish.com",
                      session.getSessionToken("test5.irish.com"));
         System.out.println("remote session name = " + s2.getUserName());
       } catch(Exception e) {
         e.printStackTrace();
       }
     }
    }

13. This servlet gets a token for Single Sign-on from the LtpaToken cookie through HttpServletRequest and creates a session based on the token.

    import java.lang.*;
    import java.lang.reflect.*;
    import java.util.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import lotus.domino.*;
    public class Cookies extends HttpServlet
    {
     private void respond(HttpServletResponse response, String entity)
       throws IOException
       {
         response.setContentType("text/plain");
         if (entity == null)
         { response.setContentLength(0);}
         else
         {
           response.setContentLength(entity.length() + 1);
           ServletOutputStream out = response.getOutputStream();
           out.println(entity);
         }
     }
     public void doGet (HttpServletRequest request,
       HttpServletResponse response)
       throws ServletException, IOException
       {
         String s1 = "";
         Cookie[] cookies = null;
         String sessionToken = null;
         try
         {
           cookies = request.getCookies();
         }
         catch (Exception e)
         {  
           respond(response,"Exception from request.getCookies(): " +
             e.toString());
           return;
         }
         if (cookies == null)
         {
           s1 = "No cookies received";
         }
         else
         {
           for (int i = 0; i < cookies.length; i++)
           {
             if (cookies[i].getName().equals("LtpaToken"))
             {
               sessionToken = cookies[i].getValue();
             }
           }
         }
         if (sessionToken != null)
         {
           try
             {
             NotesThread.sinitThread();
             Session session = NotesFactory.createSession(null, sessionToken);
             s1 += "\n" + "Server:           " + session.getServerName();
             s1 += "\n" + "IsOnServer:       " + session.isOnServer();
             s1 += "\n" + "CommonUserName:   " + session.getCommonUserName();
             s1 += "\n" + "UserName:         " + session.getUserName();
             s1 += "\n" + "NotesVersion:     " + session.getNotesVersion();
             s1 += "\n" + "Platform:         " + session.getPlatform();
             NotesThread.stermThread();
             }
             catch (NotesException e)
             {
                 s1 += "\n" + e.id + e.text;
                 e.printStackTrace();
             }
         }
         respond(response,s1);
     }
    }

14. This application snippet creates a session based on a Credentials object obtained from the WebSphere loginHelper.

        org.omg.SecurityLevel2.Credentials credentials =
         loginHelper.request_login("Jane Doe", "", "password",
         new org.omg.SecurityLevel2.CredentialsHolder(),
         new org.omg.Security.OpaqueHolder());
       Session s = NotesFactory.createSession("test5.iris.com", credentials);
       System.out.println("Got Session for " + s.getUserName());

15. This WebSphere Enterprise JavaBeans (ERB) application creates a session based on the current Credentials object in the WebSphere environment.

    import lotus.domino.*;
    public class HelloBean extends Object implements SessionBean {
    ... /* See HelloBean.java from Websphere for the complete class code */
     /**
       Returns the greeting. But has been modified to create a remote session to the
       Domino server.
       @return The greeting.
       @exception RemoteException Thrown if the remote method call fails.
     */
     public String getMessage () throws RemoteException
     {
     String result = "hello bean ";
     try {
       Session s = NotesFactory.createSession("test5.iris.com", null);
       result = result + " -- Got Session for " + s.getUserName();
     }
     catch (NotesException ne)
     {
       result = result + "-- " + ne.text;
       result = result + "-- failed to get session for user";
     }
     return (String) result + " -- done";
     }
    }

Glossary

Help on Help

All Help Contents

Glossary

Help on Help

All Help Contents

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

您正在使用您的 WordPress.com 账号评论。 注销 /  更改 )

Google photo

您正在使用您的 Google 账号评论。 注销 /  更改 )

Twitter picture

您正在使用您的 Twitter 账号评论。 注销 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 注销 /  更改 )

Connecting to %s


%d 博主赞过: