?? main.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 + -