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

📄 main.java

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

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.MalformedURLException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.ServerException;
import java.rmi.AlreadyBoundException;
import java.rmi.NoSuchObjectException;
import java.rmi.server.RemoteObject;
import java.rmi.server.UnicastRemoteObject;
import java.util.Properties;
import java.util.HashMap;

import net.jini.core.entry.Entry;
import net.jini.lookup.JoinManager;
import net.jini.lookup.entry.Name;
import net.jini.lookup.ServiceIDListener;

import com.dreambean.dynaserver.DynaServer;

/**
 *   This is the main server class. It creates a webserver that can be used for 
 *   dynamic class downloading, creates a Jini service, and finally starts 
 *   a JoinManager in order to bind the service into Jini lookup services
 *
 *   Any clients of this server may download any required classes using 
 *   dynamic class downloading.
 *
 *   @see HelloWorldImpl
 *   @see masteringrmi.hellojini.client.HelloWorldImpl
 *   @author Rickard 謆erg (rickard@dreambean.com)
 *   @version 1.0
 */
public final class Main
{
   // Attributes ----------------------------------------------------
   private DynaServer webserver;
   private JoinManager joinManager;

   private HelloWorldImpl server;
   
   // Static --------------------------------------------------------
   public static void main(String[] args)
      throws Exception
   {
      // Load system properties from resource file
      // This file is located in /lib/resources, and is included in classpath
      // through the manifest in server.jar
      System.getProperties().load(
         Thread.currentThread().
         getContextClassLoader().
         getResourceAsStream("system.properties"));
      
      // Set policy to use
      System.setProperty("java.security.policy", Main.class.getResource("/server.policy").toString());
      
      // Set a security manager
      System.setSecurityManager(new SecurityManager());
      
      // Create server
      Main main = new Main();
      
      main.start();
      
      System.out.println("Server has been started");
      System.out.println("Press return to shutdown");

      // Wait for return to be hit
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      in.readLine();
      
      // Shutdown gracefully
      main.stop();
      
      // Exit
      System.exit(0);
      
   }
   
   // Public --------------------------------------------------------
   public void start()
      throws IOException
   {
      // Start all services in the right order
      startWebserver();
      startService();
      startJini();
   }
   
   public void stop()
   {
      // Stop  all services in the reverse order
      stopJini();
      stopService();
      stopWebserver();
   }
   
   // Protected -----------------------------------------------------
   protected void startWebserver()
      throws IOException
   {
      // Create webserver for dynamic class downloading
      webserver = new DynaServer();
      
      // 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();
   }

   protected void startService()
      throws RemoteException
   {
      // Create remote object
      server = new HelloWorldImpl();
      
      // Export object
      UnicastRemoteObject.exportObject(server);
   }
   
   protected void startJini()
      throws IOException
   {
      // Create attributes
      Entry[] attributes = new Entry[] 
      {
         new Name("My HelloWorld service")
      };
   
      // Create a JoinManager
      // This will register our service with Jini lookup services
      joinManager = new JoinManager(server, 
                                    attributes, 
                                    (ServiceIDListener)null, 
                                    null, 
                                    null);
   }
   
   protected void stopWebserver()
   {
      // Stop the internal webserver
      webserver.stop();
   }
   
   protected void stopService()
   {
      // Unexport the Jini service
      try
      {
         UnicastRemoteObject.unexportObject(server, false);
      } catch (NoSuchObjectException e)
      {
         // Ignore
         e.printStackTrace();
      }
   }
   
   protected void stopJini()
   {
      // Stop the JoinManager
      // This will terminate any leases it has to Jini lookup services
      joinManager.terminate();
   }
}

⌨️ 快捷键说明

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