home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / JAVA Utilities / JProxy 1.1.0 / PROXY.EAR / samples.jar / com / jproxy / samples / ejb / test / SessionClientNoEJB.java < prev    next >
Encoding:
Java Source  |  2003-04-29  |  3.8 KB  |  133 lines

  1. package com.jproxy.samples.ejb.test;
  2.  
  3. /**
  4.  * The class is a client for TestSession example
  5.  * that shows how JProxy may be used simple ORB without any EJB or RMI Sever involved.
  6.  * The request current time from server and prints it.
  7.  * The class may be used as an Applet or stand-alone application.
  8.  */
  9.  
  10. /*
  11. Here are some usage examples.
  12. java -cp samples.jar;proxyclient.jar;j2api.jar \
  13.   -Djava.naming.provider.url=localhost:8080 \
  14.   com.jproxy.samples.ejb.test.SessionClientNoEJB
  15.  
  16.  
  17. For HTTPS test
  18.   -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol \
  19.   -Djavax.net.ssl.trustStore=your_keystore \
  20.   -Djavax.net.ssl.trustStorePassword=123456 \
  21.   -Djavax.net.debug=all
  22. */
  23.  
  24. import javax.naming.*;
  25. import java.applet.*;
  26. import java.util.*;
  27.  
  28. import com.jproxy.proxy.*;
  29. import com.jproxy.samples.interfaces.ITest;
  30.  
  31. public class SessionClientNoEJB
  32. {
  33.     public String hostUrl = "localhost";
  34.     public ISession session = null;
  35.     public InitialContext context = null;
  36.     public Properties env = new Properties();
  37.  
  38.     long time = 0;
  39.  
  40.     //Construct the applet
  41.     public SessionClientNoEJB()
  42.     {
  43.     }
  44.  
  45.     /**
  46.      * Applet init method
  47.      * JProxy may be used as simple ORB. No EJB or RMI containers are necessary.
  48.      */
  49.     public void init()
  50.         throws Exception
  51.     {
  52.         hostUrl = env.getProperty(Context.PROVIDER_URL);
  53.  
  54.         if(hostUrl!=null && hostUrl.toLowerCase().startsWith("https://"))
  55.         {
  56. //            java.security.Provider provider = new com.sun.net.ssl.internal.ssl.Provider();
  57.             java.security.Provider provider = (java.security.Provider)Class.forName("com.sun.net.ssl.internal.ssl.Provider").newInstance();
  58.             java.security.Security.addProvider(provider);
  59. // Uncomment if you want to implement your HostnameVerifier for HTTPS
  60. /*            com.sun.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
  61.                 new com.sun.net.ssl.HostnameVerifier()
  62.                 {
  63.                     public boolean verify(String urlHostname, String certHostname)
  64.                     {
  65.                         System.out.println("####### urlHostname: "+urlHostname+" certHostname: "+certHostname);
  66.                         return urlHostname.equals(certHostname);
  67.                     }
  68.                 }
  69.             );
  70. */
  71.         }
  72.         try{
  73.             com.jproxy.proxy.Tunnel tunnel = com.jproxy.proxy.Tunnel.createTunnel(Context.PROVIDER_URL);
  74.             session = (ISession)tunnel.newInstance(
  75.                 SessionEJB.class.getName(),//the classname of your server implementation
  76.                 new String[]{ISession.class.getName()},//the araay of classnames of remote interfaces that you want to make remote
  77.                 new Class[]{/* here could be classes for non-default constructor of your server */},
  78.                 new Object[]{/* here could be objects for non-default constructor of your server */});
  79.         }
  80.         catch(Throwable e){
  81.             e.printStackTrace();
  82.         }
  83.     }
  84.  
  85.     public void test()
  86.         throws Exception
  87.     {
  88.             time = session.getServerTime();
  89.  
  90. //            long t = System.currentTimeMillis();
  91. //            for(int i=0; i<1000; i++)
  92. //                time = session.getServerTime();
  93. //            System.out.println("Execution time: "+(System.currentTimeMillis()-t)/1000.0+" seconds");
  94.  
  95.             System.out.println("\r\n<<<<<< Server Time: "+(new Date(time))+" >>>>>\r\n");
  96.     }
  97.  
  98.     public void destroy()
  99.     {
  100.         TunnelInvocationHandler handler = ((Tunnelizable)session).getTunnelInvocationHandler();
  101.         Tunnel tunnel = handler.getTunnel();
  102.         Marshalable data = tunnel.createDestructRequest(handler.getObjectId());
  103.         try{
  104.             tunnel.invokeRequest(data);
  105.         }
  106.         catch(Throwable e){
  107.             System.out.println("Could not destroy object. "+e.getMessage());
  108.         }
  109.     }
  110.  
  111.  
  112.     public static void main(String[] args)
  113.     {
  114.         SessionClientNoEJB client = new SessionClientNoEJB();
  115.         client.env = System.getProperties();
  116.  
  117.         if(client.env.getProperty(Context.PROVIDER_URL)==null)
  118.             client.env.setProperty(Context.PROVIDER_URL, "localhost");
  119.  
  120.         if(client.env.getProperty(Context.INITIAL_CONTEXT_FACTORY)==null)
  121.             client.env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.jproxy.proxy.NamingContextFactory");
  122.  
  123.         try{
  124.             client.init();
  125.             client.test();
  126.             client.destroy();
  127.         }
  128.         catch(Exception e){
  129.             e.printStackTrace();
  130.         }
  131.     }
  132. }
  133.