⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainjini.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package smartworld.server;

import java.io.IOException;
import java.io.File;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.server.RemoteObject;
import java.rmi.server.UnicastRemoteObject;

import net.jini.core.entry.Entry;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.core.lookup.ServiceID;
import net.jini.lookup.JoinManager;
import net.jini.lookup.entry.Name;
import net.jini.lookup.ServiceIDListener;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryEvent;

import smartworld.interfaces.SmartWorld;
import smartproxy.proxies.DynamicStubHandler;
import smartproxy.proxies.LogProxy;
import smartproxy.proxies.JiniServiceProxy;
import smartproxy.proxies.RetryProxy;
import smartproxy.proxies.PerformanceProxy;

import com.dreambean.dynaserver.DynaServer;

/**
 *   Main server class. This creates a remote object, and registers it
 *   with the Jini LUS service.
 *
 *   @see SmartWorldImpl
 *   @author Rickard 謆erg (rickard@dreambean.com)
 *   @version $Revision:$
 */
public class MainJini
{
   // Attributes ----------------------------------------------------
   JoinManager joinManager;
   DynaServer webserver;
   
   // Static --------------------------------------------------------

   /**
    *   Run server as stand-alone application.
    *
    * @param   args  
    * @exception   Exception  
    */
   public static void main(String[] args)
      throws Exception
   {
      System.setSecurityManager(new SecurityManager()
      {
         public void checkPermission(java.security.Permission p)
         {
            
         }
      });
      
      System.getProperties().load(
         Thread.currentThread().
         getContextClassLoader().
         getResourceAsStream("system.properties"));
         
      new MainJini();
   }
   
   // Public --------------------------------------------------------

   /**
    *   Create a remote object and register it with the RMI-registry.
    *
    */
   public MainJini()
      throws Exception
   {
      startWebserver();
      startService();
   }
   
   protected void startService()
      throws Exception
   {
      // Set "java.protocol.handler.pkgs" to "smartproxy"
      DynamicStubHandler.init();
      
      // Create remote object
      SmartWorld server = (SmartWorld)new SmartWorldImpl();
      UnicastRemoteObject.exportObject(server);
      
      // Create smart proxy wrappers
      // Log on server
      server = (SmartWorld) Proxy.newProxyInstance(SmartWorld.class.getClassLoader(),
                                       new Class[] { SmartWorld.class },
                                       new LogProxy(server));
      UnicastRemoteObject.exportObject(server);
      
      // The proxies below are executed on client
      // Create service template
      Class[] types = new Class[] { SmartWorld.class };
      ServiceTemplate template = new ServiceTemplate(null, types, null);
   
      // Jini lookup for reconnect
      server = (SmartWorld) Proxy.newProxyInstance(SmartWorld.class.getClassLoader(),
                                       new Class[] { SmartWorld.class },
                                       new JiniServiceProxy(server, template));
                           
      // Retry
      server = (SmartWorld) Proxy.newProxyInstance(SmartWorld.class.getClassLoader(),
                                       new Class[] { SmartWorld.class },
                                       new RetryProxy(server));
                           
      // Performance logging
      server = (SmartWorld) Proxy.newProxyInstance(server.getClass().getClassLoader(),
                                       new Class[] { SmartWorld.class },
                                       new PerformanceProxy(server));

      // Log on client
      server = (SmartWorld) Proxy.newProxyInstance(SmartWorld.class.getClassLoader(),
                                       new Class[] { SmartWorld.class },
                                       new LogProxy(server));
                                       
      // Register server with Jini Lookup Service
      // Create attributes
      Entry[] attributes = new Entry[] 
      {
         new Name("SmartWorld service")
      };

      // Create a JoinManager
      // This will register our service with Jini lookup services
      ServiceIDListener sil = new ServiceIDListener()
      {
         public void serviceIDNotify(ServiceID serviceID)
         {
            System.out.println("Registered with LUS");
         }
      };
      joinManager = new JoinManager(server, 
                                    attributes, 
                                    (ServiceIDListener)sil, 
                                    null, 
                                    null);
      joinManager.getDiscoveryManager().addDiscoveryListener(new DiscoveryListener()
      {
         public void discovered(DiscoveryEvent e)
         {
            System.out.println("Discovered "+e);
         }
         
         public void discarded(DiscoveryEvent e)
         {
            System.out.println("Discarded "+e);
         }
      });
                                 
      // Register server with naming service - client will use this to do initial lookup (not really necessary)
      Naming.rebind(SmartWorld.NAME, server);
   
      System.out.println("Server has been started");
      new java.io.DataInputStream(System.in).readLine();
      joinManager.terminate();
      System.exit(0);
   }
   
   protected void startWebserver()
      throws IOException
   {
      // Create webserver for dynamic class downloading
      webserver = new DynaServer();
      webserver.setDebug(true);
      
      // Add the classloader for this application
      // This will allow any client to download classes/resources that
      // are in the classpath for this application
      webserver.addClassLoader(Thread.currentThread().getContextClassLoader());
      
      // Start the webserver
      webserver.start();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -