?? helloclient.java
字號:
/*
* Copyright 1999 by dreamBean Software,
* All rights reserved.
*/
package masteringrmi.hellojini.client;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.core.lookup.ServiceItem;
import net.jini.lookup.LookupCache;
import net.jini.lookup.ServiceDiscoveryManager;
import net.jini.lookup.ServiceDiscoveryListener;
import net.jini.lookup.ServiceDiscoveryEvent;
import masteringrmi.hellojini.interfaces.HelloWorld;
import com.dreambean.dynaserver.DynaServer;
/**
* This is an application that will talk to the Jini service.
*
* When one or more HelloWorld service(s) are available it will
* show a button that when clicked will communicate with the service.
*
* If there are no services the button will be greyed out.
*
* This client uses the ServiceDiscoveryManager to keep track of any
* available instances of the HelloWorld Jini service.
*
* @see HelloWorld
* @author Rickard 謆erg (rickard@dreambean.com)
*/
public class HelloClient
extends Frame
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
/**
* The service lookup cache managed by the ServiceDiscoveryManager
*/
LookupCache serviceCache;
/**
* This label will show whether the service is available or not
*/
Label serviceAvailability;
/**
* The service usage response will shown in this label
*/
Label response;
/**
* Press this button to say hello to the service
*/
Button helloButton;
// Static -------------------------------------------------------
public static void main(String[] args)
throws IOException
{
new HelloClient();
}
// Constructors --------------------------------------------------
public HelloClient()
throws IOException
{
super("HelloJini client");
// Init security
System.setProperty("java.security.policy", getClass().getResource("/client.policy").toString());
System.setSecurityManager(new SecurityManager());
// Start client
startWebserver();
startGui();
startJini();
}
// Protected ----------------------------------------------------
protected void startWebserver()
throws IOException
{
// Create webserver for dynamic class downloading
DynaServer srv = 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
srv.addClassLoader(Thread.currentThread().getContextClassLoader());
// Start the webserver
srv.start();
}
protected void startGui()
{
// Create GUI
serviceAvailability = new Label("Service is not available");
response = new Label()
{
public java.awt.Dimension getPreferredSize()
{
return new java.awt.Dimension(200,20);
}
};
helloButton = new Button("Say hello!");
helloButton.setEnabled(false);
// Add listeners
helloButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
// Call server again and show response
sayHello();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
serviceCache.terminate();
System.exit(0);
}
});
// Layout GUI
add("North",serviceAvailability);
add("Center",response);
add("South",helloButton);
pack();
show();
}
protected void startJini()
throws IOException
{
// Create Jini service discovery manager
ServiceDiscoveryManager sdm = new ServiceDiscoveryManager(null, null);
// Create service template
Class[] types = new Class[] { HelloWorld.class };
ServiceTemplate template = new ServiceTemplate(null, types, null);
// Initialize service lookup
serviceCache = sdm.createLookupCache(template, null, new ServiceDiscoveryListenerHelper());
}
protected void sayHello()
{
ServiceItem si = serviceCache.lookup(null);
HelloWorld service = (HelloWorld)si.service;
try
{
String greeting = service.helloWorld("World");
response.setText(greeting);
} catch (RemoteException e)
{
serviceCache.discard(service);
}
}
// Inner classes -------------------------------------------------
class ServiceDiscoveryListenerHelper
implements ServiceDiscoveryListener
{
public void serviceAdded(ServiceDiscoveryEvent event)
{
System.out.println("Service added");
// Service is now available
helloButton.setEnabled(true);
serviceAvailability.setText("Service is available");
}
public void serviceRemoved(ServiceDiscoveryEvent event)
{
System.out.println("Service removed");
if (serviceCache.lookup(null) == null)
{
// Service is now not available
helloButton.setEnabled(false);
serviceAvailability.setText("Service is not available");
}
}
public void serviceChanged(ServiceDiscoveryEvent event)
{
// Ignore
System.out.println("Service changed");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -