home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-04-29 | 3.8 KB | 133 lines |
- package com.jproxy.samples.ejb.test;
-
- /**
- * The class is a client for TestSession example
- * that shows how JProxy may be used simple ORB without any EJB or RMI Sever involved.
- * The request current time from server and prints it.
- * The class may be used as an Applet or stand-alone application.
- */
-
- /*
- Here are some usage examples.
- java -cp samples.jar;proxyclient.jar;j2api.jar \
- -Djava.naming.provider.url=localhost:8080 \
- com.jproxy.samples.ejb.test.SessionClientNoEJB
-
-
- For HTTPS test
- -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol \
- -Djavax.net.ssl.trustStore=your_keystore \
- -Djavax.net.ssl.trustStorePassword=123456 \
- -Djavax.net.debug=all
- */
-
- import javax.naming.*;
- import java.applet.*;
- import java.util.*;
-
- import com.jproxy.proxy.*;
- import com.jproxy.samples.interfaces.ITest;
-
- public class SessionClientNoEJB
- {
- public String hostUrl = "localhost";
- public ISession session = null;
- public InitialContext context = null;
- public Properties env = new Properties();
-
- long time = 0;
-
- //Construct the applet
- public SessionClientNoEJB()
- {
- }
-
- /**
- * Applet init method
- * JProxy may be used as simple ORB. No EJB or RMI containers are necessary.
- */
- public void init()
- throws Exception
- {
- hostUrl = env.getProperty(Context.PROVIDER_URL);
-
- if(hostUrl!=null && hostUrl.toLowerCase().startsWith("https://"))
- {
- // java.security.Provider provider = new com.sun.net.ssl.internal.ssl.Provider();
- java.security.Provider provider = (java.security.Provider)Class.forName("com.sun.net.ssl.internal.ssl.Provider").newInstance();
- java.security.Security.addProvider(provider);
- // Uncomment if you want to implement your HostnameVerifier for HTTPS
- /* com.sun.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
- new com.sun.net.ssl.HostnameVerifier()
- {
- public boolean verify(String urlHostname, String certHostname)
- {
- System.out.println("####### urlHostname: "+urlHostname+" certHostname: "+certHostname);
- return urlHostname.equals(certHostname);
- }
- }
- );
- */
- }
- try{
- com.jproxy.proxy.Tunnel tunnel = com.jproxy.proxy.Tunnel.createTunnel(Context.PROVIDER_URL);
- session = (ISession)tunnel.newInstance(
- SessionEJB.class.getName(),//the classname of your server implementation
- new String[]{ISession.class.getName()},//the araay of classnames of remote interfaces that you want to make remote
- new Class[]{/* here could be classes for non-default constructor of your server */},
- new Object[]{/* here could be objects for non-default constructor of your server */});
- }
- catch(Throwable e){
- e.printStackTrace();
- }
- }
-
- public void test()
- throws Exception
- {
- time = session.getServerTime();
-
- // long t = System.currentTimeMillis();
- // for(int i=0; i<1000; i++)
- // time = session.getServerTime();
- // System.out.println("Execution time: "+(System.currentTimeMillis()-t)/1000.0+" seconds");
-
- System.out.println("\r\n<<<<<< Server Time: "+(new Date(time))+" >>>>>\r\n");
- }
-
- public void destroy()
- {
- TunnelInvocationHandler handler = ((Tunnelizable)session).getTunnelInvocationHandler();
- Tunnel tunnel = handler.getTunnel();
- Marshalable data = tunnel.createDestructRequest(handler.getObjectId());
- try{
- tunnel.invokeRequest(data);
- }
- catch(Throwable e){
- System.out.println("Could not destroy object. "+e.getMessage());
- }
- }
-
-
- public static void main(String[] args)
- {
- SessionClientNoEJB client = new SessionClientNoEJB();
- client.env = System.getProperties();
-
- if(client.env.getProperty(Context.PROVIDER_URL)==null)
- client.env.setProperty(Context.PROVIDER_URL, "localhost");
-
- if(client.env.getProperty(Context.INITIAL_CONTEXT_FACTORY)==null)
- client.env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.jproxy.proxy.NamingContextFactory");
-
- try{
- client.init();
- client.test();
- client.destroy();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
-