1 / 81

Chapter 22 - Jini

Chapter 22 - Jini. Outline

Download Presentation

Chapter 22 - Jini

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 22 - Jini Outline 22.1 Introduction22.2 Installation22.3 Set Up Run-Time Environment22.4 Start Required Services22.5 Running the Jini LookupBrowser22.6 Discovery 22.6.1 Unicast Discovery 22.6.2 Multicast Discovery22.7 Jini Service and Client Implementations 22.7.1 Service Interfaces and Supporting Classes 22.7.2 Service Proxy and Service Implementations 22.7.3 Registering the Service with Lookup Services 22.7.4 Jini Service Client22.8 Introduction to High-Level Helper Utilities 22.8.1 Discovery Utilities 22.8.2 Entry Utilities 22.8.3 Lease Utilities 22.8.4 JoinManager Utility 22.8.5 Service Discovery Utilities22.9 Internet and World Wide Web Resources

  2. 22.1 Introduction • Network services • Discover service • Know the interface of the service • Jini services • Discover services on the network dynamically • Dynamically download classes required to use those services • Dynamic class downloading is transparent to clients

  3. 22.2 Installation • Software requirements • Java 2 Standard Edition (J2SE) • Jini Technology Starter Kit • Jini Technology Starter Kit • Jini Technology Core Platform (JCP) • Jini Technology Extended Platform (JXP) • Jini Software Kit (JSK)

  4. 22.3 Set up Run-time Environment • CLASSPATH Environment Variable • jini-core.jar • jini-ext.jar • sun-util.jar

  5. 22.4 Start Required Services • Required services • Web server • RMI Activation Daemon (rmid) • Lookup service • StartService GUI • Load property file • Configure web server • Configure rmid • Configure lookup service

  6. 22.5 Running the Jini LookupBrowser • Start Jini LookupBrowser • LookupBrowser output

  7. 22.6 Lookup Service Discovery • Definition • Discover available lookup services • Types • Unicast Discovery • Multicast Discovery

  8. 22.6.2 Unicast Discovery • Unicast Discovery • Discover lookup services on a specific host • Uses class LookupLocator

  9. Class UnicastDiscovery Import packages Declarations 1 // UnicastDiscovery.java2 // UnicastDiscovery is an application that demonstrates Jini3 // lookup service discovery for a known host (unicast).4 package com.deitel.advjhtp1.jini.discovery;5 6 // Java core packages7 import java.rmi.*;8 import java.net.*;9 import java.io.*;10 import java.awt.*;11 import java.awt.event.*;12 13 // Java extension packages14 import javax.swing.*;15 16 // Jini core packages17 import net.jini.core.discovery.LookupLocator;18 import net.jini.core.lookup.ServiceRegistrar;19 20 publicclass UnicastDiscovery extends JFrame {21 22 private JTextArea outputArea = new JTextArea( 10, 20 );23 private JButton discoverButton;24 25 // hostname for discovering lookup services26 private String hostname; 27

  10. Discover lookup services when user clicks the button 3. Constructor 3.1 GUI 28 // UnicastDiscovery constructor29 public UnicastDiscovery( String host )30 {31 super( "UnicastDiscovery Output" );32 33 hostname = host; // set target hostname for discovery34 35 // create JButton to discover lookup services36 discoverButton = new JButton( "Discover Lookup Services" );37 discoverButton.addActionListener( 38 new ActionListener() {39 40 // discover lookup services on given host41 publicvoid actionPerformed( ActionEvent event )42 {43 discoverLookupServices();44 }45 } 46 );47 48 Container contentPane = getContentPane();49 contentPane.add( outputArea, BorderLayout.CENTER );50 contentPane.add( discoverButton, BorderLayout.NORTH );51 52 } // end UnicastDiscovery constructor53

  11. Jini URL Connect to lookup service Perform unicast discovery Get host name and port number of the lookup service Get group name of the lookup service 4. Discover lookup services 4.1 specify Jini URL 4.2 connect to lookup service 4.3 perform unicast discovery 4.4 append output 54 // discover lookup services on given host and get details55 // about each lookup service from ServiceRegistrar56 publicvoid discoverLookupServices()57 {58 // construct Jini URL59 String lookupURL = "jini://" + hostname + "/"; 60 61 // connect to the lookup service at lookupURL62 try {63 LookupLocator locator = new LookupLocator( lookupURL );64 outputArea.append( "Connecting to " + lookupURL + "\n" );65 66 // perform unicast discovery to get ServiceRegistrar67 ServiceRegistrar registrar = 68 locator.getRegistrar();69 70 // print lookup service information and71 outputArea.append( "Got ServiceRegistrar\n" +72 " Lookup Service Host: " + locator.getHost() + "\n" +73 " Lookup Service Port: " + locator.getPort() + "\n" );74 75 // get groups that lookup service supports76 String[] groups = registrar.getGroups();77 outputArea.append( "Lookup service supports " +78 + groups.length + " group(s):\n" );79 80 // get group names; if empty, write public81 for ( int i = 0; i < groups.length ; i++ ) {82 83 if ( groups[ i ].equals( "" ) )84 outputArea.append( " public\n" );85 86 else87 outputArea.append( " " + groups[i] + "\n" );88 }89 }

  12. Set security manager 4.5 handle exceptions 5. main 5.1 set security manager 90 91 // handle exception if URL is invalid92 catch ( MalformedURLException exception ) {93 exception.printStackTrace();94 outputArea.append( exception.getMessage() );95 }96 97 // handle exception communicating with ServiceRegistrar98 catch ( RemoteException exception ) {99 exception.printStackTrace();100 outputArea.append( exception.getMessage() );101 }102 103 // handle ClassNotFoundException obtaining ServiceRegistrar104 catch ( ClassNotFoundException exception ) {105 exception.printStackTrace();106 outputArea.append( exception.getMessage() );107 }108 109 // handle IOException obtaining ServiceRegistrar110 catch ( IOException exception ) {111 exception.printStackTrace();112 outputArea.append( exception.getMessage() );113 } 114 115 } // end method discoverLookupServices116 117 // launch UnicastDiscovery application118 publicstaticvoid main( String args[] )119 {120 // set SecurityManager121 if ( System.getSecurityManager() == null )122 System.setSecurityManager(123 new RMISecurityManager() );124

  13. Exit when window closes Grant AllPermission Policy file Program output 125 // check command-line arguments for hostname126 if ( args.length != 1 ) {127 System.err.println( 128 "Usage: java UnicastDiscovery hostname" );129 }130 131 // create UnicastDiscovery for given hostname132 else {133 UnicastDiscovery discovery = 134 new UnicastDiscovery( args[ 0 ] );135 136 discovery.setDefaultCloseOperation( EXIT_ON_CLOSE );137 discovery.pack();138 discovery.setVisible( true );139 }140 }141 } 1 // policy.all2 // grant AllPermission to all code (DANGEROUS!)3 grant {4 permission java.security.AllPermission "", "";5 };

  14. 22.6.2 Multicast Discovery • Multicast Discovery • Discover lookup services when host is not known • Multicast discovery request • Multicast announcements • Class LookupDiscovery • Uses class LookupDiscovery • Implements DiscoveryListener

  15. Implements DiscoveryListener to listen for discovery notifications Class MulticastDiscovery Import packages Declarations 1 // MulticastDiscovery.java2 // MulticastDiscovery is an application that demonstrates Jini3 // lookup service discovery using multicast.4 package com.deitel.advjhtp1.jini.discovery;5 6 // Java core packages7 import java.rmi.*;8 import java.io.*;9 import java.awt.*;10 import java.awt.event.*;11 12 // Java extension packages13 import javax.swing.*;14 15 // Jini core packages16 import net.jini.core.lookup.ServiceRegistrar;17 18 // Jini extension packages19 import net.jini.discovery.*;20 21 publicclass MulticastDiscovery extends JFrame 22 implements DiscoveryListener {23 24 // number of lookup services discovered through multicast25 privateint servicesFound = 0;26 27 private JTextArea outputArea = new JTextArea( 10, 20 );28

  16. Discover public lookup services Register to receive discovery notifications Receive notification when a valid lookup service is found 3. Constructor 3.1 multicast discover lookup service 3.2 add listener 4. Receive notification 29 // MulticastDiscovery constructor30 public MulticastDiscovery ()31 {32 super( "MulticastDiscovery" );33 34 // discover lookup services in public group using multicast35 try {36 LookupDiscovery lookupDiscovery = new LookupDiscovery( 37 new String[] { "" } );38 39 outputArea.append( "Finding lookup services for " +40 "public group ...\n" );41 42 // listen for DiscoveryEvents43 lookupDiscovery.addDiscoveryListener ( this );44 }45 46 // handle exception discovering lookup services47 catch ( IOException exception ) {48 exception.printStackTrace();49 }50 51 getContentPane().add( new JScrollPane( outputArea ), 52 BorderLayout.CENTER ); 53 }54 55 // receive notification of found lookup services56 publicvoid discovered ( DiscoveryEvent event )57 { 58 // get the ServiceRegistrars for found lookup services59 ServiceRegistrar[] registrars = event.getRegistrars();60 int order = 0;61

  17. Get lookup service hostname Get lookup service port number Get lookup service group name 4.1 get lookup service information 4.2 append output 62 // get the information for each lookup service found63 for ( int i = 0; i < registrars.length ; i++ ) {64 ServiceRegistrar registrar = registrars[ i ];65 66 if ( registrar != null ) {67 68 // append information about discovered services69 // to outputArea70 try {71 order = servicesFound + i + 1;72 73 // get the hostname and port number74 Runnable appender = new TextAppender(75 "Lookup Service " + order + ":\n" +76 " Host: " + 77 registrar.getLocator().getHost() + "\n" +78 "\n Port: " + 79 registrar.getLocator().getPort() + "\n" +80 " Group support: " );81 82 // append to outputArea on event-dispatch thread83 SwingUtilities.invokeLater( appender );84 85 // get the group(s) the lookup service served86 String[] groups = registrar.getGroups();87 88 StringBuffer names = new StringBuffer();89 90 // get the group names, if empty write public91 for ( int j = 0; j < groups.length ; j++ ) {92 93 if ( groups[ j ].equals( "" ) )94 names.append( "public\t" );95 else96 names.append( groups[ j ] + "\t" );97 }

  18. Receive notification when a lookup service becomes invalid 4.3 GUI 5 get invalid lookup service 5.1 GUI 98 99 // append group names to outputArea100 SwingUtilities.invokeLater( 101 new TextAppender( names + "\n" ) );102 }103 104 // handle exception communicating with ServiceRegistrar105 catch ( RemoteException exception ) {106 exception.printStackTrace();107 }108 }109 }110 111 // update number of services found112 servicesFound = order;113 114 } // end method discovered115 116 // receive notification of discarded lookup services that117 // are no longer valid118 publicvoid discarded( DiscoveryEvent event ) 119 {120 ServiceRegistrar[] discardedRegistrars = 121 event.getRegistrars();122 123 SwingUtilities.invokeLater( 124 new TextAppender( "Number of discarded registrars: " +125 discardedRegistrars.length + "\n" ) );126 }127

  19. Inner class for appending text to output area on the event-dispatching thread Set security manager 6. Inner class TextAppender 7. Main 7.1 set security manager 128 // TextAppender is a Runnable class for appending129 // text to outputArea on the event-dispatching thread.130 privateclass TextAppender implements Runnable {131 132 private String textToAppend; // text to append to outputArea133 134 // TextAppender constructor135 public TextAppender( String text )136 {137 textToAppend = text;138 }139 140 // append text to outputArea and scroll to bottom141 publicvoid run()142 {143 outputArea.append( textToAppend );144 outputArea.setCaretPosition( 145 outputArea.getText().length() );146 }147 148 } // end inner class TextAppender149 150 // launch MulticastDiscovery application151 publicstaticvoid main( String args[] ) 152 {153 // set SecurityManager154 if ( System.getSecurityManager() == null )155 System.setSecurityManager(156 new RMISecurityManager() );157 158 MulticastDiscovery discovery = new MulticastDiscovery();159 discovery.setDefaultCloseOperation( EXIT_ON_CLOSE );160 discovery.pack();161 discovery.setVisible( true );162 }163 }

  20. Program output

  21. 22.7 Jini Service and Client Implementations • Jini service • Service proxy • Backend interface • Service implementation • Jini client • Only knows service’s public interface

  22. 22.7.1 Service Interfaces and Supporting Classes • Service Interfaces • SeminarInterface (public interface) • BackendInterface (unknown to client) • Supporting classes • Seminar

  23. Explicitly specify the serial version UID Specify the title and location of a seminar Constructor Methods 1 // Seminar.java2 // Seminar represents a seminar, or lecture, including the 3 // Seminar title and location.4 package com.deitel.advjhtp1.jini.seminar;5 6 // Java core package7 import java.io.Serializable;8 9 publicclass Seminar implements Serializable10 {11 private String title;12 private String location;13 privatestaticfinallong serialVersionUID = 20010724L;14 15 // Seminar constructor16 public Seminar( String seminarTitle, String seminarLocation )17 {18 title = seminarTitle;19 location = seminarLocation;20 }21 22 // get String representation of Seminar object23 public String toString()24 {25 return"Seminar title: " + getTitle() + 26 "; location: " + getLocation();27 }28 29 // get Seminar title30 public String getTitle()31 {32 return title;33 }34 35 // get Seminar location36 public String getLocation()37 {38 return location;39 }40 }

  24. Public interface Service proxy’s interfaceunknown to client Interface SeminarInterface Interface BackendInterface 1 // SeminarInterface.java2 // SeminarInterface defines methods available from the SeminarInfo3 // Jini service.4 package com.deitel.advjhtp1.jini.seminar.service;5 6 // Java core packages7 import java.rmi.Remote;8 9 // Deitel packages10 import com.deitel.advjhtp1.jini.seminar.Seminar;11 12 publicinterface SeminarInterface {13 14 // get Seminar for given date15 public Seminar getSeminar( String date );16 } ------------------------------------------------------------------------1 // BackendInterface.java2 // BackendInterface defines the interface through which the 3 // service proxy communicates with the back-end service.4 package com.deitel.advjhtp1.jini.seminar.service;5 6 // Java core packages7 import java.rmi.Remote;8 9 // Deitel packages10 import com.deitel.advjhtp1.jini.seminar.Seminar;11 12 publicinterface BackendInterface extends Remote {13 14 // get Seminar for given day of the week15 public Seminar getSeminar( String date ) throws RemoteException;16 }

  25. 22.7.2 Service Proxy and Service Implementations • Service Proxy • SeminarProxy • Service Implementation • SeminarInfo

  26. Seminar service proxy implements the public interface SeminarInterface Communicates through interface BackendInterface using RMI Class SeminarProxy Constructor Methods 1 // SeminarProxy.java2 // SeminarProxy is a proxy for the SeminarInfo Jini service. 3 package com.deitel.advjhtp1.jini.seminar.service;4 5 // Java core packages6 import java.io.Serializable;7 import java.rmi.*;8 9 // Deitel packages10 import com.deitel.advjhtp1.jini.seminar.Seminar;11 12 publicclass SeminarProxy implements SeminarInterface, 13 Serializable {14 15 private BackendInterface backInterface;16 17 // SeminarProxy constructor18 public SeminarProxy( BackendInterface inputInterface )19 {20 backInterface = inputInterface;21 }22 23 // get Seminar for given date through BackendInterface24 public Seminar getSeminar( String date )25 {26 // get Seminar from service through BackendInterface27 try {28 return backInterface.getSeminar( date );29 }30 31 // handle exception communicating with back-end service32 catch ( RemoteException remoteException ) {33 remoteException.printStackTrace();34 }35 36 returnnull;37 38 } // end method getSeminar39 }

  27. RMI object Class SeminarInfo Import packages Declaration Constructor 1 // SeminarInfo.java2 // SeminarInfo is a Jini service that provides information3 // about Seminars offered throughout the week.4 package com.deitel.advjhtp1.jini.seminar.service;5 6 // Java core packages7 import java.io.*;8 import java.rmi.server.UnicastRemoteObject;9 import java.rmi.RemoteException;10 import java.util.StringTokenizer;11 12 // Deitel packages13 import com.deitel.advjhtp1.jini.seminar.Seminar;14 15 publicclass SeminarInfo extends UnicastRemoteObject 16 implements BackendInterface {17 18 // Strings that represent days of the week19 privatestaticfinal String MONDAY = "MONDAY";20 privatestaticfinal String TUESDAY = "TUESDAY";21 privatestaticfinal String WEDNESDAY = "WEDNESDAY";22 privatestaticfinal String THURSDAY = "THURSDAY";23 privatestaticfinal String FRIDAY = "FRIDAY";24 25 // SeminarInfo no-argument constructor26 public SeminarInfo() throws RemoteException {}27

  28. Load text file that contains seminar data Read seminar data 4. Get seminar information 4.1 load text file 4.2 read seminar info 28 // get Seminar information for given day 29 public Seminar getSeminar( String date ) 30 throws RemoteException 31 {32 String[] titles = new String[] { "", "", "", "", "" };33 String[] locations = new String[] { "", "", "", "", "" };34 35 // read seminar information from text file36 try {37 String fileName = SeminarInfo.class.getResource(38 "SeminarInfo.txt" ).toString();39 fileName = fileName.substring( 6 );40 41 FileInputStream inputStream = 42 new FileInputStream( fileName );43 44 BufferedReader reader = new BufferedReader(45 new InputStreamReader( inputStream ));46 47 String line = reader.readLine();48 49 // read seminar info from the file50 for ( int lineNo = 0; ( line != null ) 51 && ( lineNo < 5 ); lineNo++ ) { 52 StringTokenizer tokenizer = 53 new StringTokenizer( line, ";" ); 54 55 titles[ lineNo ] = tokenizer.nextToken();56 locations[ lineNo ] = tokenizer.nextToken();57 line = reader.readLine();58 }59 }60

  29. Return seminar information Content of SeminarInfo.txt 4.3 return result Seminar text file 61 // handle exception loading Seminar file62 catch ( FileNotFoundException fileException ) {63 fileException.printStackTrace();64 }65 66 // handle exception reading from Seminar file67 catch ( IOException ioException ) {68 ioException.printStackTrace();69 }70 71 // match given day of the week to available seminars72 if ( date.equalsIgnoreCase( MONDAY ) ) {73 returnnew Seminar( titles[ 0 ], locations[ 0 ] );74 } 75 elseif ( date.equalsIgnoreCase( TUESDAY ) ) {76 returnnew Seminar( titles[ 1 ], locations[ 1 ] );77 } 78 elseif ( date.equalsIgnoreCase( WEDNESDAY ) ) {79 returnnew Seminar( titles[ 2 ], locations[ 2 ] );80 } 81 elseif ( date.equalsIgnoreCase( THURSDAY ) ) {82 returnnew Seminar( titles[ 3 ], locations[ 3 ] );83 }84 elseif ( date.equalsIgnoreCase( FRIDAY ) ) {85 returnnew Seminar( titles[ 4 ], locations[ 4 ] );86 }87 else {88 returnnew Seminar( "Empty", "Not available" );89 }90 91 } // end method getSeminar92 }------------------------------------------------------------------1 Advanced Swing GUI Components; Deitel Seminar Room2 Model-View-Controller Architecture; Deitel Seminar Room3 Java 2 Enterprise Edition; Deitel Seminar Room4 Introduction to Jini; Deitel Seminar Room5 Java 2 Micro Edition; Deitel Seminar Room

  30. 22.7.3 Registering the Service with Lookup Service • SeminarInfoService • Multicast discovery for lookup services • Registers the SeminarInfo service with lookup services

  31. Implements DiscoveryListener to receive notifications of discovered services Search for public lookup services with multicast discovery Class SeminarInfoService Import packages Constructor 2.1 discover lookup services 1 // SeminarInfoService.java2 // SeminarInfoService discovers lookup services and registers3 // the SeminarInfo service with those lookup services.4 package com.deitel.advjhtp1.jini.seminar.service;5 6 // Java core packages7 import java.rmi.RMISecurityManager;8 import java.rmi.RemoteException;9 import java.io.IOException;10 11 // Jini core packages12 import net.jini.core.lookup.*;13 import net.jini.core.entry.Entry;14 15 // Jini extension packages16 import net.jini.discovery.*;17 import net.jini.lookup.entry.Name;18 19 publicclass SeminarInfoService implements DiscoveryListener {20 21 private ServiceItem serviceItem;22 privatefinalint LEASETIME = 10 * 60 * 1000;23 24 // SeminarInfoService constructor25 public SeminarInfoService()26 {27 // search for lookup services with public group28 try {29 LookupDiscovery discover = 30 new LookupDiscovery( new String[] { "" } );31 32 // add listener for DiscoveryEvents33 discover.addDiscoveryListener( this );34 }35 36 // handle exception creating LookupDiscovery

  32. NameEntry Specify service item Lookup service found Register service with lookup service 2.2 specify service item 3. Register service with lookup service 37 catch ( IOException exception ) {38 exception.printStackTrace();39 }40 41 // create an Entry for this service42 Entry[] entries = new Entry[ 1 ];43 entries[ 0 ] = new Name( "Seminar" );44 45 // set the service's proxy and Entry name46 serviceItem = new ServiceItem(47 null, createProxy(), entries );48 49 } // end SeminarInfoService constructor50 51 // receive lookup service discovery notifications52 publicvoid discovered( DiscoveryEvent event )53 {54 ServiceRegistrar[] registrars = event.getRegistrars();55 56 // register service with each lookup service57 for ( int i = 0; i < registrars.length; i++ ) {58 ServiceRegistrar registrar = registrars[ i ];59 60 // register service with discovered lookup service61 try {62 ServiceRegistration registration = 63 registrar.register( serviceItem, LEASETIME );64 } 65 66 // catch the remote exception67 catch ( RemoteException exception) {68 exception.printStackTrace();69 }70 71 } // end for72 73 } // end method discovered

  33. Return service proxy 4. Ignore invalid lookup service 5. Create service proxy 74 75 // ignore discarded lookup services76 publicvoid discarded( DiscoveryEvent event ) {}77 78 // create the seminar service proxy79 private SeminarInterface createProxy()80 {81 // get BackendInterface for service and create SeminarProxy82 try {83 returnnew SeminarProxy( new SeminarInfo() );84 }85 86 // handle exception creating SeminarProxy87 catch ( RemoteException exception ) {88 exception.printStackTrace();89 }90 91 returnnull;92 93 } // end method discovered94

  34. Keep application alive 6. main 6.1 set security manager 6.2 keep application alive 95 // method main keeps the application alive96 publicstaticvoid main( String args[] )97 {98 // set SecurityManager99 if ( System.getSecurityManager() == null )100 System.setSecurityManager( new RMISecurityManager() );101 102 new SeminarInfoService();103 104 Object keepAlive = new Object();105 106 synchronized ( keepAlive ) {107 108 // keep application alive 109 try {110 keepAlive.wait();111 }112 113 // handle exception if wait interrupted114 catch ( InterruptedException exception ) {115 exception.printStackTrace();116 }117 }118 119 } // end method main120 }

  35. 22.7.4 Jini Service Client • UnicastSeminarInfoClient • Discover lookup service with unicast discovery • Search for seminar service • Get seminar info

  36. Class UnicastSeminarInfoClient Import packages Declarations 1 // UnicastSeminarInfoClient.java2 // UnicastSeminarInfoClient uses unicast discovery to locate3 // lookup servides for the SeminarInfo service.4 package com.deitel.advjhtp1.jini.seminar.client;5 6 // Java core packages7 import java.awt.*;8 import java.awt.event.*;9 import java.io.*;10 import java.rmi.*;11 import java.net.*;12 import java.util.*;13 14 // Java extension packages15 import javax.swing.*;16 17 // Jini core packages18 import net.jini.core.discovery.LookupLocator;19 import net.jini.core.lookup.*;20 import net.jini.core.entry.Entry;21 22 // Jini extension packages23 import net.jini.lookup.entry.Name;24 25 // Deitel packages26 import com.deitel.advjhtp1.jini.seminar.Seminar;27 import com.deitel.advjhtp1.jini.seminar.service.SeminarInterface;28 29 publicclass UnicastSeminarInfoClient extends JFrame {30 31 // Strings representing the days of the week on which 32 // Seminars are offered33 privatestaticfinal String[] days = { "Monday", "Tuesday",34 "Wednesday", "Thursday", "Friday" };35 36 // hostname and ServiceRegistrar for lookup services37 private String hostname;38 private ServiceRegistrar registrar;39

  37. Get user-specified day 3. Constructor 3.1 GUI 40 // JButton for finding Seminars41 private JButton findSeminarButton;42 43 // UnicastSeminarInfoClient constructor44 public UnicastSeminarInfoClient( String host )45 {46 super( "UnicastSeminarInfoClient" );47 48 hostname = host; // set target hostname for discovery49 50 // create JButton for finding Seminars51 findSeminarButton = new JButton( "Find Seminar" );52 findSeminarButton.addActionListener(53 54 new ActionListener() {55 56 // discover lookup services, look up SeminarInfo57 // service, prompt user for desired day of the 58 // week and display available Seminars59 publicvoid actionPerformed( ActionEvent event )60 {61 discoverLookupServices();62 63 SeminarInterface seminarService = 64 lookupSeminarService();65 66 String day = ( String ) JOptionPane.showInputDialog( 67 UnicastSeminarInfoClient.this, "Select Day", 68 "Day Selection", JOptionPane.QUESTION_MESSAGE, 69 null, days, days[ 0 ] );70 71 showSeminars( seminarService, day ); 72 }73 }74 75 ); // end call to addActionListener

  38. Specify lookup URL Get lookup locator and registrar 4. Discover lookup service 4.1 specify lookup URL 4.2 get lookup locator and registrar 76 77 JPanel buttonPanel = new JPanel();78 buttonPanel.add( findSeminarButton );79 80 getContentPane().add( buttonPanel, BorderLayout.CENTER );81 82 } // end UnicastSeminarInfoClient constructor83 84 // discover lookup services using unicast discovery85 privatevoid discoverLookupServices()86 {87 String lookupURL = "jini://" + hostname + "/";88 89 // Get the lookup service locator at jini://hostname 90 // use default port91 try {92 LookupLocator locator = new LookupLocator( lookupURL );93 94 // return registrar95 registrar = locator.getRegistrar();96 } 97 98 // handle exceptions discovering lookup services99 catch ( Exception exception ) {100 exception.printStackTrace();101 }102 103 } // end method discoverLookupServices104

  39. Service requirement Lookup service 5. Search for service 5.1 specify service requirement 5.2 lookup service 6. Display result 105 // lookup SeminarInfo service in given ServiceRegistrar106 private SeminarInterface lookupSeminarService()107 {108 // specify the service requirement109 Class[] types = new Class[] { SeminarInterface.class };110 Entry[] attribute = new Entry[] { new Name( "Seminar" ) };111 ServiceTemplate template = 112 new ServiceTemplate( null, types, attribute );113 114 // find the service 115 try {116 SeminarInterface seminarInterface = 117 ( SeminarInterface ) registrar.lookup( template );118 return seminarInterface;119 } 120 121 // handle exception looking up SeminarInfo service122 catch ( RemoteException exception ) {123 exception.printStackTrace();124 }125 126 returnnull; 127 128 } // end method lookupSeminarService129 130 // show Seminar in given SeminarInfo service for given 131 // day of the week 132 privatevoid showSeminars( SeminarInterface seminarService, 133 String day )134 {135 StringBuffer buffer = new StringBuffer();136

  40. Get seminar title and location Display output 6.1 append output 6.2 display output 7. main 137 // get Seminar from SeminarInfo service 138 if ( seminarService != null ) {139 Seminar seminar = seminarService.getSeminar( day );140 141 // get subject and location from Seminar object142 buffer.append( "Seminar information: \n" );143 buffer.append( day + ":\n" );144 buffer.append( seminar.getTitle() + "\n" ); // title145 buffer.append( seminar.getLocation() ); // location146 }147 else // SeminarInfo service does not available148 buffer.append( 149 "SeminarInfo service does not available. \n" );150 151 // display Seminar information152 JOptionPane.showMessageDialog( this, buffer );153 154 } // end method showSeminars155 156 // launch UnicastSeminarInfoClient application157 publicstaticvoid main ( String args[] )158 {159 // check command-line arguments for hostname160 if ( args.length != 1 ) {161 System.err.println( 162 "Usage: java UnicastSeminarInfoClient hostname" );163 }164

  41. Program output 165 // create UnicastSeminarInfoClient for given hostname166 else {167 System.setSecurityManager( new RMISecurityManager() );168 169 UnicastSeminarInfoClient client = 170 new UnicastSeminarInfoClient( args[ 0 ] );171 172 client.setDefaultCloseOperation( EXIT_ON_CLOSE );173 client.pack();174 client.setSize( 250, 65 );175 client.setVisible( true );176 }177 178 } // end method main179 }

  42. 22.7.4 Jini Service Client • Preparing JAR files • Class files for the Jini service • SeminarService.jar • Class files for the Jini client • SeminarClient.jar • Downloadable Jini service class files. • SeminarServiceDownload.jar • Starting rmid, web server and lookup service • Launching SeminarInfoService • Launching UnicastSeminarInfoService

  43. 22.7.4 Jini Service Client

  44. 22.7.4 Jini Service Client

More Related