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

📄 activatablelightbulbserver.java

📁 Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用安全
💻 JAVA
字号:
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.activation.*;
import java.net.URLEncoder;
import java.util.Properties;

// Chapter 11, Listing 9
public class ActivatableLightBulbServer
	// Extends the Activatable class
	extends Activatable
	// Implements a light bulb RMI interface
	implements RMILightBulb
{
	public ActivatableLightBulbServer(ActivationID activationID, MarshalledObject data) 
		throws RemoteException
	{
		// Call the constructor of the parent class (Activatable)
		// to export the object on any available port		
		super( activationID, 0);

		// Default to value of off
		setBulb(false);
	}

/*
	// Normally defined for UnicastRemoteObject subclasses... not for
	// an Activatable class
	public ActivatableLightBulbServer()
		throws RemoteException
	{
		// no code req'd
	}
*/
	public static void main(String args[])
	{
		System.out.println ("Loading Activatable RMI service");      
		System.out.println ();

		// Set a RMI security manager
		System.setSecurityManager(new RMISecurityManager());

		try
		{
			// Step one : create an ActivationGroupDesc instance
			ActivationGroupDesc groupDescriptor = new
				ActivationGroupDesc (new Properties(), null);

			// Step two : register that activation group descriptor
			//            with the activation system, and get groupID
			ActivationSystem system   = ActivationGroup.getSystem();
			// Register the group descriptor - without registering the group, no execution
			ActivationGroupID groupID = system.registerGroup(groupDescriptor);
			
			// Output the group ID to screen
			System.out.println ("Group ID : " + groupID);

			// Step three: create an activation group, passing the
			//             group ID and descriptor as parameters
			ActivationGroup.createGroup(groupID, groupDescriptor, 0);


			// Specify a location for the codebase of the activated
			// object. By default, the current directory will be used.
			// However, if dynamic class loading is used, a HTTP URL may
			// be specified instead
			java.io.File location = new java.io.File (".");

			// Encode URL, for Win32 systems. This may not be required 
			// on other operating systems that don't support spaces and
			// punctuation in filenames
			String strLoc = "file://" + URLEncoder.encode(location.getAbsolutePath());
			System.out.println ("Activation codebase : " + strLoc);

			// Step four : Create an activation descriptor, whose 
			//             constructor requires the class name,
			//             codebase, and an optional marshalled object
			ActivationDesc desc = new ActivationDesc 
				("ActivatableLightBulbServer", strLoc, null);

			// Step five : Register the object with the activation system
			//             Returns a stub, which may be registered with rmiregistry
			Remote stub = Activatable.register(desc); 

			// Check to see if a registry was specified
			String registry = "localhost";
			if (args.length >=1)
			{
				registry = args[0];
			}

			// Registration format //registry_hostname (optional):port /service
			String registration = "rmi://" + registry + "/RMILightBulb";


			// Step six :  Register the stub with the rmiregistry
			Naming.rebind(registration, stub);

			System.out.println("Service registered with rmid. Now terminating...");
			System.exit(0);
		}
		catch (RemoteException re)
		{
			System.err.println ("Remote Error - " + re);
		}
		catch (Exception e)
		{
			System.err.println ("Error - " + e);
		}
	}

	/* 
	   Remainder of code implements RMILightBulb interface,
	   and is not RMI specific
	 */
	// Boolean flag to maintain light bulb state information
	private boolean lightOn;

	// Remotely accessible "on" method - turns on the light
	public void on() throws java.rmi.RemoteException
	{
		// Turn bulb on
		setBulb (true);
	}

	// Remotely accessible "off" method - turns off the light
	public void off() throws java.rmi.RemoteException
	{
		// Turn bulb off
		setBulb (false);
	}

	// Remotely accessible "isOn" method, returns state of bulb
	public boolean isOn() throws java.rmi.RemoteException
	{
		return getBulb();
	}

	// Locally accessible "setBulb" method, changes state of bulb
	public void setBulb (boolean value)
	{
		lightOn = value;
	}

	// Locally accessible "getBulb" method, returns state of bulb
	public boolean getBulb ()
	{
		return lightOn;
	}
}

⌨️ 快捷键说明

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