Chapter 22 Jini Outline 22 1 22 2

  • Slides: 81
Download presentation
Chapter 22 - Jini Outline 22. 1 22. 2 22. 3 22. 4 22.

Chapter 22 - Jini Outline 22. 1 22. 2 22. 3 22. 4 22. 5 22. 6 22. 7 22. 8 22. 9 Introduction Installation Set Up Run-Time Environment Start Required Services Running the Jini Lookup. Browser Discovery 22. 6. 1 Unicast Discovery 22. 6. 2 Multicast Discovery 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 Client Introduction to High-Level Helper Utilities 22. 8. 1 Discovery Utilities 22. 8. 2 Entry Utilities 22. 8. 3 Lease Utilities 22. 8. 4 Join. Manager Utility 22. 8. 5 Service Discovery Utilities Internet and World Wide Web Resources 2001 Prentice Hall, Inc. All rights reserved.

22. 1 Introduction • Network services – Discover service – Know the interface of

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 2001 Prentice Hall, Inc. All rights reserved.

22. 2 Installation • Software requirements – Java 2 Standard Edition (J 2 SE)

22. 2 Installation • Software requirements – Java 2 Standard Edition (J 2 SE) – Jini Technology Starter Kit • Jini Technology Starter Kit – Jini Technology Core Platform (JCP) – Jini Technology Extended Platform (JXP) – Jini Software Kit (JSK) 2001 Prentice Hall, Inc. All rights reserved.

22. 3 Set up Run-time Environment • CLASSPATH Environment Variable – jini-core. jar –

22. 3 Set up Run-time Environment • CLASSPATH Environment Variable – jini-core. jar – jini-ext. jar – sun-util. jar 2001 Prentice Hall, Inc. All rights reserved.

22. 4 Start Required Services • Required services – Web server – RMI Activation

22. 4 Start Required Services • Required services – Web server – RMI Activation Daemon (rmid) – Lookup service • Start. Service GUI – – Load property file Configure web server Configure rmid Configure lookup service 2001 Prentice Hall, Inc. All rights reserved.

 2001 Prentice Hall, Inc. All rights reserved.

2001 Prentice Hall, Inc. All rights reserved.

 2001 Prentice Hall, Inc. All rights reserved.

2001 Prentice Hall, Inc. All rights reserved.

 2001 Prentice Hall, Inc. All rights reserved.

2001 Prentice Hall, Inc. All rights reserved.

 2001 Prentice Hall, Inc. All rights reserved.

2001 Prentice Hall, Inc. All rights reserved.

 2001 Prentice Hall, Inc. All rights reserved.

2001 Prentice Hall, Inc. All rights reserved.

22. 5 Running the Jini Lookup. Browser • Start Jini Lookup. Browser • Lookup.

22. 5 Running the Jini Lookup. Browser • Start Jini Lookup. Browser • Lookup. Browser output 2001 Prentice Hall, Inc. All rights reserved.

 2001 Prentice Hall, Inc. All rights reserved.

2001 Prentice Hall, Inc. All rights reserved.

22. 6 Lookup Service Discovery • Definition – Discover available lookup services • Types

22. 6 Lookup Service Discovery • Definition – Discover available lookup services • Types – Unicast Discovery – Multicast Discovery 2001 Prentice Hall, Inc. All rights reserved.

22. 6. 2 Unicast Discovery • Unicast Discovery – Discover lookup services on a

22. 6. 2 Unicast Discovery • Unicast Discovery – Discover lookup services on a specific host • Uses class Lookup. Locator 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Unicast. Discovery. java // Unicast. Discovery is an application that demonstrates Jini // lookup service discovery for a known host ( unicast). package com. deitel. advjhtp 1. jini. discovery; Class Unicast. Discovery // Java core packages import java. rmi. *; import java. net. *; import java. io. *; import java. awt. event. *; 2. Declarations 1. Import packages // Java extension packages import javax. swing. *; // Jini core packages import net. jini. core. discovery. Lookup. Locator; import net. jini. core. lookup. Service. Registrar; public class Unicast. Discovery extends JFrame { private JText. Area output. Area = new JText. Area( 10, 20 ); private JButton discover. Button; // hostname for discovering lookup services private String hostname; 2001 Prentice Hall, Inc. All rights reserved.

Outline 28 29 30 31 32 33 34 35 36 37 38 39 40

Outline 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 // Unicast. Discovery constructor public Unicast. Discovery( String host ) { super( "Unicast. Discovery Output" ); 3. Constructor 3. 1 GUI hostname = host; // set target hostname for discovery // create JButton to discover lookup services discover. Button = new JButton( "Discover Lookup Services" ); discover. Button. add. Action. Listener( new Action. Listener() { // discover lookup services on given host public void action. Performed( Action. Event event ) { discover. Lookup. Services(); Discover } } ); lookup services when user clicks the button Container content. Pane = get. Content. Pane(); content. Pane. add( output. Area, Border. Layout. CENTER ); content. Pane. add( discover. Button, Border. Layout. NORTH ); } // end Unicast. Discovery constructor 2001 Prentice Hall, Inc. All rights reserved.

54 55 56 57 58 59 60 61 62 63 64 65 66 67

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 // discover lookup services on given host and get details // about each lookup service from Service. Registrar public void discover. Lookup. Services() { // construct Jini URL Jini String lookup. URL = "jini: //" + hostname + "/"; Outline URL // connect to the lookup service at lookup. URL try { Lookup. Locator locator = new Lookup. Locator( lookup. URL ); output. Area. append( "Connecting to " + lookup. URL + "n" ); // perform unicast discovery to get Service. Registrar registrar = locator. get. Registrar(); Connect to lookup service 4. 1 specify Jini URL 4. 2 connect to lookup service 4. 3 perform unicast discovery Perform unicast discovery // print lookup service information and output. Area. append( "Got Service. Registrarn" + " Lookup Service Host: " + locator. get. Host() + "n" + " Lookup Service Port: " + locator. get. Port() + "n" ); // get groups that lookup service supports String[] groups = registrar. get. Groups(); output. Area. append( "Lookup service supports " + + groups. length + " group(s): n" ); 4. Discover lookup services 4. 4 append output Get host name and port Getofgroup name service of number the lookup service // get group names; if empty, write public for ( int i = 0; i < groups. length ; i++ ) { if ( groups[ i ]. equals( "" ) ) output. Area. append( " publicn" ); else output. Area. append( " } } " + groups[i] + "n" ); 2001 Prentice Hall, Inc. All rights reserved.

90 91 92 93 94 95 96 97 98 99 100 101 102 103

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 Outline // handle exception if URL is invalid catch ( Malformed. URLException exception ) { exception. print. Stack. Trace(); output. Area. append( exception. get. Message() ); } 4. 5 handle exceptions // handle exception communicating with Service. Registrar catch ( Remote. Exception exception ) { exception. print. Stack. Trace(); output. Area. append( exception. get. Message() ); } 5. main 5. 1 set security manager // handle Class. Not. Found. Exception obtaining Service. Registrar catch ( Class. Not. Found. Exception exception ) { exception. print. Stack. Trace(); output. Area. append( exception. get. Message() ); } // handle IOException obtaining Service. Registrar catch ( IOException exception ) { exception. print. Stack. Trace(); output. Area. append( exception. get. Message() ); } } // end method discover. Lookup. Services // launch Unicast. Discovery application public static void main( String args[] ) { // set Security. Manager if ( System. get. Security. Manager() == null ) System. set. Security. Manager( new RMISecurity. Manager() ); Set security manager 2001 Prentice Hall, Inc. All rights reserved.

125 126 127 128 129 130 131 132 133 134 135 136 137 138

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 // check command-line arguments for hostname if ( args. length != 1 ) { System. err. println( "Usage: java Unicast. Discovery hostname" ); } // create Unicast. Discovery for given hostname else { Unicast. Discovery discovery = new Unicast. Discovery( args[ 0 ] ); Outline Exit when window closes discovery. set. Default. Close. Operation ( EXIT_ON_CLOSE ); discovery. pack(); discovery. set. Visible( true ); } } } 1 2 3 4 5 // policy. all // grant All. Permission to all code (DANGEROUS!) grant { permission java. security. All. Permission "", ""; }; Policy file Grant All. Permission Program output 2001 Prentice Hall, Inc. All rights reserved.

22. 6. 2 Multicast Discovery • Multicast Discovery – – Discover lookup services when

22. 6. 2 Multicast Discovery • Multicast Discovery – – Discover lookup services when host is not known Multicast discovery request Multicast announcements Class Lookup. Discovery • Uses class Lookup. Discovery – Implements Discovery. Listener 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Multicast. Discovery. java // Multicast. Discovery is an application that demonstrates Jini // lookup service discovery using multicast. package com. deitel. advjhtp 1. jini. discovery; // Java core packages import java. rmi. *; import java. io. *; import java. awt. event. *; Class Multicast. Discovery 1. Import packages 2. Declarations // Java extension packages import javax. swing. *; // Jini core packages import net. jini. core. lookup. Service. Registrar; // Jini extension packages import net. jini. discovery. *; public class Multicast. Discovery extends JFrame implements Discovery. Listener { Implements Discovery. Listener to listen for discovery notifications // number of lookup services discovered through multicast private int services. Found = 0; private JText. Area output. Area = new JText. Area( 10, 20 ); 2001 Prentice Hall, Inc. All rights reserved.

29 30 31 32 33 34 35 36 37 38 39 40 41 42

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 Outline // Multicast. Discovery constructor public Multicast. Discovery () { super( "Multicast. Discovery" ); 3. Constructor // discover lookup services in public group using multicast try { Lookup. Discovery lookup. Discovery = new Lookup. Discovery( new String[] { "" } ); Discover output. Area. append( "Finding lookup services for " public + "public group. . . n" ); lookup services // listen for Discovery. Events lookup. Discovery. add. Discovery. Listener ( this ); 3. 1 multicast discover lookup service 3. 2 add listener 4. Receive notification Register to receive discovery notifications } // handle exception discovering lookup services catch ( IOException exception ) { exception. print. Stack. Trace(); } get. Content. Pane(). add( new JScroll. Pane( output. Area ), Border. Layout. CENTER ); } Receive notification when a valid lookup service is found // receive notification of found lookup services public void discovered ( Discovery. Event event ) { // get the Service. Registrars for found lookup services Service. Registrar[] registrars = event. get. Registrars(); int order = 0; 2001 Prentice Hall, Inc. All rights reserved.

62 63 64 65 66 67 68 69 70 71 72 73 74 75

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 // get the information for each lookup service found for ( int i = 0; i < registrars. length ; i++ ) { Service. Registrar registrar = registrars[ i ]; if ( registrar != null ) { // append information about discovered services // to output. Area try { order = services. Found + i + 1; Get lookup service port number // get the hostname and port number Runnable appender = new Text. Appender( "Lookup Service " + order + ": n" + " Host: " + registrar. get. Locator(). get. Host() + "n" + "n Port: " + registrar. get. Locator(). get. Port() + "n" + " Group support: " ); Outline 4. 1 get lookup service information 4. 2 append output Get lookup service hostname // append to output. Area on event-dispatch thread Swing. Utilities. invoke. Later( appender ); // get the group(s) the lookup service served String[] groups = registrar. get. Groups(); String. Buffer names = new String. Buffer(); // get the group names, if empty write public for ( int j = 0; j < groups. length ; j++ ) { if ( groups[ j ]. equals( "" ) ) names. append( "publict" ); else names. append( groups[ j ] + "t" ); } Get lookup service group name 2001 Prentice Hall, Inc. All rights reserved.

98 99 100 101 102 103 104 105 106 107 108 109 110 111

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 Outline // append group names to output. Area Swing. Utilities. invoke. Later( new Text. Appender( names + "n" ) ); 4. 3 GUI } // handle exception communicating with Service. Registrar catch ( Remote. Exception exception ) { exception. print. Stack. Trace(); } 5 get invalid lookup service 5. 1 GUI } } // update number of services found services. Found = order; } // end method discovered // receive notification of discarded lookup services that // are no longer valid public void discarded( Discovery. Event event ) { Service. Registrar[] discarded. Registrars = event. get. Registrars(); Swing. Utilities. invoke. Later( new Text. Appender( "Number of discarded registrars: " discarded. Registrars. length + "n" ) ); Receive notification when a lookup service becomes invalid + } 2001 Prentice Hall, Inc. All rights reserved.

128 129 130 131 132 133 134 135 136 137 138 139 140 141

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 // Text. Appender is a Runnable class for appending // text to output. Area on the event-dispatching thread. private class Text. Appender implements Runnable { Outline private String text. To. Append; // text to append to output. Area // Text. Appender constructor public Text. Appender( String text ) { text. To. Append = text; } 6. Inner class Text. Appender Inner class for appending 7. Main text to output area on the 7. 1 set event-dispatching thread manager security // append text to output. Area and scroll to bottom public void run() { output. Area. append( text. To. Append ); output. Area. set. Caret. Position( output. Area. get. Text(). length () ); } } // end inner class Text. Appender // launch Multicast. Discovery application public static void main( String args[] ) { // set Security. Manager if ( System. get. Security. Manager () == null ) System. set. Security. Manager ( new RMISecurity. Manager() ); Set security manager Multicast. Discovery discovery = new Multicast. Discovery(); discovery. set. Default. Close. Operation ( EXIT_ON_CLOSE ); discovery. pack(); discovery. set. Visible( true ); } } 2001 Prentice Hall, Inc. All rights reserved.

Outline Program output 2001 Prentice Hall, Inc. All rights reserved.

Outline Program output 2001 Prentice Hall, Inc. All rights reserved.

22. 7 Jini Service and Client Implementations • Jini service – Service proxy –

22. 7 Jini Service and Client Implementations • Jini service – Service proxy – Backend interface – Service implementation • Jini client – Only knows service’s public interface 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 1 Service Interfaces and Supporting Classes • Service Interfaces – Seminar. Interface

22. 7. 1 Service Interfaces and Supporting Classes • Service Interfaces – Seminar. Interface (public interface) – Backend. Interface (unknown to client) • Supporting classes – Seminar 2001 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // Seminar. java // Seminar represents a seminar, or lecture, including the // Seminar title and location. package com. deitel. advjhtp 1. jini. seminar; // Java core package import java. io. Serializable; public class Seminar implements Serializable { private String title; private String location; private static final long serial. Version. UID = 20010724 L; // Seminar constructor public Seminar( String seminar. Title, String seminar. Location ) { title = seminar. Title; location = seminar. Location; } // get String representation of Seminar object public String to. String() { return "Seminar title: " + get. Title() + "; location: " + get. Location(); } Outline 1. Constructor 2. Methods Explicitly specify the serial version UID Specify the title and location of a seminar // get Seminar title public String get. Title() { return title; } // get Seminar location public String get. Location() { return location; } } 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 // Seminar. Interface. java 2 // Seminar. Interface defines methods available from

Outline 1 // Seminar. Interface. java 2 // Seminar. Interface defines methods available from the Seminar. Info 3 // Jini service. 4 package com. deitel. advjhtp 1. jini. seminar. service; Interface 5 Seminar. Interface 6 // Java core packages 7 import java. rmi. Remote; 8 9 // Deitel packages 10 import com. deitel. advjhtp 1. jini. seminar. Seminar; 11 12 public interface Seminar. Interface { Public interface 13 14 // get Seminar for given date 15 public Seminar get. Seminar( String date ); 16 } ------------------------------------Interface 1 // Backend. Interface. java Backend. Interface 2 // Backend. Interface defines the interface through which the 3 // service proxy communicates with the back-end service. 4 package com. deitel. advjhtp 1. jini. seminar. service; 5 6 // Java core packages 7 import java. rmi. Remote; 8 9 // Deitel packages 10 import com. deitel. advjhtp 1. jini. seminar. Seminar; Service proxy’s interface 11 unknown to client 12 public interface Backend. Interface extends Remote { 13 14 // get Seminar for given day of the week 15 public Seminar get. Seminar( String date ) throws Remote. Exception; 16 } 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 2 Service Proxy and Service Implementations • Service Proxy – Seminar. Proxy

22. 7. 2 Service Proxy and Service Implementations • Service Proxy – Seminar. Proxy • Service Implementation – Seminar. Info 2001 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // Seminar. Proxy. java // Seminar. Proxy is a proxy for the Seminar. Info Jini service. package com. deitel. advjhtp 1. jini. seminar. service; // Java core packages import java. io. Serializable; import java. rmi. *; // Deitel packages import com. deitel. advjhtp 1. jini. seminar. Seminar; public class Seminar. Proxy implements Seminar. Interface, Serializable { private Backend. Interface back. Interface; // Seminar. Proxy constructor public Seminar. Proxy( Backend. Interface input. Interface ) { back. Interface = input. Interface; } // get Seminar for given date through Backend. Interface public Seminar get. Seminar( String date ) { // get Seminar from service through Backend. Interface try { return back. Interface. get. Seminar( date ); } Outline Class Seminar. Proxy 1. Constructor 2. Methods Seminar service proxy implements the public interface Seminar. Interface Communicates through interface Backend. Interface using RMI // handle exception communicating with back-end service catch ( Remote. Exception remote. Exception ) { remote. Exception. print. Stack. Trace(); } return null; } // end method get. Seminar } 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Seminar. Info. java // Seminar. Info is a Jini service that provides information // about Seminars offered throughout the week. package com. deitel. advjhtp 1. jini. seminar. service; Class Seminar. Info // Java core packages import java. io. *; import java. rmi. server. Unicast. Remote. Object; import java. rmi. Remote. Exception; import java. util. String. Tokenizer; 2. Declaration // Deitel packages import com. deitel. advjhtp 1. jini. seminar. Seminar; 1. Import packages 3. Constructor RMI object public class Seminar. Info extends Unicast. Remote. Object implements Backend. Interface { // Strings that represent days of the week private static final String MONDAY = "MONDAY"; private static final String TUESDAY = "TUESDAY"; private static final String WEDNESDAY = "WEDNESDAY"; private static final String THURSDAY = "THURSDAY"; private static final String FRIDAY = "FRIDAY"; // Seminar. Info no-argument constructor public Seminar. Info() throws Remote. Exception {} 2001 Prentice Hall, Inc. All rights reserved.

28 29 30 31 32 33 34 35 36 37 38 39 40 41

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 // get Seminar information for given day public Seminar get. Seminar( String date ) throws Remote. Exception { String[] titles = new String[] { "", "", "" }; String[] locations = new String[] { "", "", "" }; // read seminar information from text file try { String file. Name = Seminar. Info. class. get. Resource( "Seminar. Info. txt" ). to. String(); file. Name = file. Name. substring( 6 ); File. Input. Stream input. Stream = new File. Input. Stream( file. Name ); Buffered. Reader reader = new Buffered. Reader( new Input. Stream. Reader( input. Stream )); Outline 4. Get seminar information 4. 1 load text file 4. 2 read seminar info Load text file that contains seminar data String line = reader. read. Line(); // read seminar info from the file for ( int line. No = 0; ( line != null ) && ( line. No < 5 ); line. No++ ) { String. Tokenizer tokenizer = new String. Tokenizer( line, "; " ); Read seminar data titles[ line. No ] = tokenizer. next. Token(); locations[ line. No ] = tokenizer. next. Token(); line = reader. read. Line(); } } 2001 Prentice Hall, Inc. All rights reserved.

61 62 63 64 65 66 67 68 69 70 71 72 73 74

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 // handle exception loading Seminar file catch ( File. Not. Found. Exception file. Exception ) { file. Exception. print. Stack. Trace(); } Outline // handle exception reading from Seminar file catch ( IOException io. Exception ) { io. Exception. print. Stack. Trace(); } 4. 3 return result // match given day of the week to available seminars if ( date. equals. Ignore. Case( MONDAY ) ) { return new Seminar( titles[ 0 ], locations[ 0 ] ); } else if ( date. equals. Ignore. Case( TUESDAY ) ) { return new Seminar( titles[ 1 ], locations[ 1 ] ); } else if ( date. equals. Ignore. Case( WEDNESDAY ) ) { return new Seminar( titles[ 2 ], locations[ 2 ] ); } else if ( date. equals. Ignore. Case( THURSDAY ) ) { return new Seminar( titles[ 3 ], locations[ 3 ] ); } else if ( date. equals. Ignore. Case( FRIDAY ) ) { return new Seminar( titles[ 4 ], locations[ 4 ] ); } else { return new Seminar( "Empty", "Not available" ); } Return seminar information } // end method get. Seminar } ---------------------------------1 Advanced Swing GUI Components; Deitel Seminar Room 2 Model-View-Controller Architecture; Deitel Seminar Room 3 Java 2 Enterprise Edition; Deitel Seminar Room 4 Introduction to Jini; Deitel Seminar Room 5 Java 2 Micro Edition; Deitel Seminar Room Seminar Content of Seminar. Info. txt text file 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 3 Registering the Service with Lookup Service • Seminar. Info. Service –

22. 7. 3 Registering the Service with Lookup Service • Seminar. Info. Service – Multicast discovery for lookup services – Registers the Seminar. Info service with lookup services 2001 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 Outline // Seminar. Info. Service. java // Seminar. Info. Service discovers lookup services and registers // the Seminar. Info service with those lookup services. package com. deitel. advjhtp 1. jini. seminar. service; // Java core packages import java. rmi. RMISecurity. Manager; import java. rmi. Remote. Exception; import java. io. IOException; // Jini core packages import net. jini. core. lookup. *; import net. jini. core. entry. Entry; Class Seminar. Info. Service 1. Import packages Implements Discovery. Listener to receive notifications of discovered services 2. Constructor 3. 2. 1 discover lookup services // Jini extension packages import net. jini. discovery. *; import net. jini. lookup. entry. Name; public class Seminar. Info. Service implements Discovery. Listener { private Service. Item service. Item; private final int LEASETIME = 10 * 60 * 1000; // Seminar. Info. Service constructor public Seminar. Info. Service() { // search for lookup services with public group try { Lookup. Discovery discover = new Lookup. Discovery( new String[] { "" } ); Search for public lookup services with multicast discovery // add listener for Discovery. Events discover. add. Discovery. Listener ( this ); } // handle exception creating Lookup. Discovery 2001 Prentice Hall, Inc. All rights reserved.

37 38 39 40 41 42 43 44 45 46 47 48 49 50

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 catch ( IOException exception ) { exception. print. Stack. Trace(); } // create an Entry for this service Entry[] entries = new Entry[ 1 ]; entries[ 0 ] = new Name( "Seminar" ); // set the service's proxy and Entry name service. Item = new Service. Item( null, create. Proxy(), entries ); } // end Seminar. Info. Service constructor Outline Name Entry 2. 2 specify service item 3. Register service with lookup service Specify service item // receive lookup service discovery notifications public void discovered( Discovery. Event event ) { Service. Registrar[] registrars = event. get. Registrars(); Lookup service found // register service with each lookup service for ( int i = 0; i < registrars. length; i++ ) { Service. Registrar registrar = registrars[ i ]; // register service with discovered lookup service try { Service. Registration registration = registrar. register( service. Item, LEASETIME ); } Register service with lookup service // catch the remote exception catch ( Remote. Exception exception) { exception. print. Stack. Trace (); } } // end for } // end method discovered 2001 Prentice Hall, Inc. All rights reserved.

Outline 4. Ignore invalid lookup service 74 75 76 77 78 79 80 81

Outline 4. Ignore invalid lookup service 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 // ignore discarded lookup services public void discarded( Discovery. Event event ) {} 5. Create service proxy // create the seminar service proxy private Seminar. Interface create. Proxy() { // get Backend. Interface for service and create Seminar. Proxy try { return new Seminar. Proxy( new Seminar. Info() ); } Return service proxy // handle exception creating Seminar. Proxy catch ( Remote. Exception exception ) { exception. print. Stack. Trace(); } return null; } // end method discovered 2001 Prentice Hall, Inc. All rights reserved.

Outline 95 96 97 98 99 100 101 102 103 104 105 106 107

Outline 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 // method main keeps the application alive public static void main( String args[] ) { // set Security. Manager if ( System. get. Security. Manager() == null ) System. set. Security. Manager( new RMISecurity. Manager() ); 6. main 6. 1 set security manager 6. 2 keep application alive new Seminar. Info. Service(); Object keep. Alive = new Object(); synchronized ( keep. Alive ) { Keep application alive // keep application alive try { keep. Alive. wait(); } // handle exception if wait interrupted catch ( Interrupted. Exception exception ) { exception. print. Stack. Trace (); } } } // end method main } 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client • Unicast. Seminar. Info. Client – Discover lookup

22. 7. 4 Jini Service Client • Unicast. Seminar. Info. Client – Discover lookup service with unicast discovery – Search for seminar service – Get seminar info 2001 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // Unicast. Seminar. Info. Client. java // Unicast. Seminar. Info. Client uses unicast discovery to locate // lookup servides for the Seminar. Info service. package com. deitel. advjhtp 1. jini. seminar. client; // Java core packages import java. awt. *; import java. awt. event. *; import java. io. *; import java. rmi. *; import java. net. *; import java. util. *; Outline Class Unicast. Seminar. Info. Client 1. Import packages 2. Declarations // Java extension packages import javax. swing. *; // Jini core packages import net. jini. core. discovery. Lookup. Locator; import net. jini. core. lookup. *; import net. jini. core. entry. Entry; // Jini extension packages import net. jini. lookup. entry. Name; // Deitel packages import com. deitel. advjhtp 1. jini. seminar. Seminar; import com. deitel. advjhtp 1. jini. seminar. service. Seminar. Interface; public class Unicast. Seminar. Info. Client extends JFrame { // Strings representing the days of the week on which // Seminars are offered private static final String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; // hostname and Service. Registrar for lookup services private String hostname; private Service. Registrar registrar; 2001 Prentice Hall, Inc. All rights reserved.

40 41 42 43 44 45 46 47 48 49 50 51 52 53

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 // JButton for finding Seminars private JButton find. Seminar. Button; // Unicast. Seminar. Info. Client constructor public Unicast. Seminar. Info. Client ( String host ) { super( "Unicast. Seminar. Info. Client " ); Outline 3. Constructor 3. 1 GUI hostname = host; // set target hostname for discovery // create JButton for finding Seminars find. Seminar. Button = new JButton( "Find Seminar" ); find. Seminar. Button. add. Action. Listener( new Action. Listener() { // discover lookup services, look up Seminar. Info // service, prompt user for desired day of the // week and display available Seminars public void action. Performed( Action. Event event ) { discover. Lookup. Services(); Seminar. Interface seminar. Service = lookup. Seminar. Service(); Get userspecified day String day = ( String ) JOption. Pane. show. Input. Dialog ( Unicast. Seminar. Info. Client. this, "Select Day", "Day Selection", JOption. Pane. QUESTION_MESSAGE, null, days[ 0 ] ); show. Seminars( seminar. Service, day ); } } ); // end call to add. Action. Listener 2001 Prentice Hall, Inc. All rights reserved.

Outline 76 77 78 79 80 81 82 83 84 85 86 87 88

Outline 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 JPanel button. Panel = new JPanel(); button. Panel. add( find. Seminar. Button ); 4. Discover lookup service get. Content. Pane(). add( button. Panel, Border. Layout. CENTER ); 4. 1 specify lookup URL } // end Unicast. Seminar. Info. Client constructor 4. 2 get lookup locator and registrar // discover lookup services using unicast discovery private void discover. Lookup. Services() { String lookup. URL = "jini: //" + hostname + "/"; // Get the lookup service locator at jini: //hostname // use default port try { Lookup. Locator locator = new Lookup. Locator( lookup. URL ); // return registrar = locator. get. Registrar(); } Specify lookup URL Get lookup locator and registrar // handle exceptions discovering lookup services catch ( Exception exception ) { exception. print. Stack. Trace (); } } // end method discover. Lookup. Services 2001 Prentice Hall, Inc. All rights reserved.

Outline 105 106 107 108 109 110 111 112 113 114 115 116 117

Outline 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 // lookup Seminar. Info service in given Service. Registrar private Seminar. Interface lookup. Seminar. Service() { // specify the service requirement Class[] types = new Class[] { Seminar. Interface. class }; Entry[] attribute = new Entry[] { new Name( "Seminar" ) }; Service. Template template = new Service. Template( null, types, attribute ); // find the service try { Seminar. Interface seminar. Interface = ( Seminar. Interface ) registrar. lookup( template ); return seminar. Interface; } // handle exception looking up Seminar. Info service catch ( Remote. Exception exception ) { exception. print. Stack. Trace (); } 5. Search for service 5. 1 specify service requirement Service requirement 5. 2 lookup service 6. Display result Lookup service return null; } // end method lookup. Seminar. Service // show Seminar in given Seminar. Info service for given // day of the week private void show. Seminars( Seminar. Interface seminar. Service, String day ) { String. Buffer buffer = new String. Buffer(); 2001 Prentice Hall, Inc. All rights reserved.

Outline 137 138 139 140 141 142 143 144 145 146 147 148 149

Outline 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 // get Seminar from Seminar. Info service if ( seminar. Service != null ) { Seminar seminar = seminar. Service. get. Seminar( day ); // get subject buffer. append( 6. 1 append output 6. 2 display output and location from Seminar object "Seminar information: n" ); day + ": n" ); seminar. get. Title() + "n" ); // title seminar. get. Location() ); // location } else // Seminar. Info service does not available buffer. append( "Seminar. Info service does not available. n" ); 7. main Get seminar title and location Display output // display Seminar information JOption. Pane. show. Message. Dialog ( this, buffer ); } // end method show. Seminars // launch Unicast. Seminar. Info. Client application public static void main ( String args[] ) { // check command-line arguments for hostname if ( args. length != 1 ) { System. err. println( "Usage: java Unicast. Seminar. Info. Client hostname" ); } 2001 Prentice Hall, Inc. All rights reserved.

165 166 167 168 169 170 171 172 173 174 175 176 177 178

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 // create Unicast. Seminar. Info. Client for given hostname else { System. set. Security. Manager( new RMISecurity. Manager() ); Outline Unicast. Seminar. Info. Client client = new Unicast. Seminar. Info. Client ( args[ 0 ] ); client. set. Default. Close. Operation ( EXIT_ON_CLOSE ); client. pack(); client. set. Size( 250, 65 ); client. set. Visible( true ); } } // end method main } Program output 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client • Preparing JAR files – Class files for

22. 7. 4 Jini Service Client • Preparing JAR files – Class files for the Jini service • Seminar. Service. jar – Class files for the Jini client • Seminar. Client. jar – Downloadable Jini service class files. • Seminar. Service. Download. jar • Starting rmid, web server and lookup service • Launching Seminar. Info. Service • Launching Unicast. Seminar. Info. Service 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client 2001 Prentice Hall, Inc. All rights reserved.

22. 7. 4 Jini Service Client 2001 Prentice Hall, Inc. All rights reserved.

22. 8 Introduction to High-Level Helper Utilities • Helper Utilities – – – Discovery

22. 8 Introduction to High-Level Helper Utilities • Helper Utilities – – – Discovery Utilities Entry Utilities Lease Utilities Join. Manager Utilities Service Discovery Utilities 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 1 Discovery Utilities • Lookup. Locator. Discovery Utility – Discover lookup services

22. 8. 1 Discovery Utilities • Lookup. Locator. Discovery Utility – Discover lookup services on multiple known hosts • Lookup. Discovery. Manager Utility – Perform both unicast and multicast discovery 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Unicast. Discovery. Utility. java // Demonstrating how to locate multiple lookup services // using Lookup. Locator. Discovery utility package com. deitel. advjhtp 1. jini. utilities. discovery; // Java core packages import java. rmi. *; import java. io. *; import java. awt. event. *; import java. net. *; // Java swing package import javax. swing. *; // Jini core packages import net. jini. core. lookup. Service. Registrar; import net. jini. core. discovery. Lookup. Locator; Class Unicast. Discovery. Utility 1. Import packages // Jini extension packages import net. jini. discovery. Lookup. Locator. Discovery ; import net. jini. discovery. Discovery. Listener ; import net. jini. discovery. Discovery. Event ; public class Unicast. Discovery. Utility extends JFrame implements Discovery. Listener { private JText. Area output. Area = new JText. Area( 10, 20 ); 2001 Prentice Hall, Inc. All rights reserved.

30 31 32 33 34 35 36 37 38 39 40 41 42 43

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 Outline // Unicast. Discovery. Utility constructor public Unicast. Discovery. Utility( String urls[] ) { super( "Unicast. Discovery. Utility" ); get. Content. Pane(). add( new JScroll. Pane( output. Area ), Border. Layout. CENTER ); // discover lookup services using Lookup. Locator. Discovery try { 2. Constructor // create Lookup. Locator for each URL Lookup. Locator locators[] = new Lookup. Locator[ urls. length ]; for ( int i = 0; i < locators. length ; i++ ) locators[ i ] = new Lookup. Locator( urls[ i ] ); // create Lookup. Locator. Discovery object Lookup. Locator. Discovery locator. Discovery = new Lookup. Locator. Discovery( locators ); Create // register Discovery. Listener locator. Discovery. add. Discovery. Listener ( this ); } // end try Specify multiple 2. 1 locators GUI lookup for unicast 2. 2 specify multiple lookup locators discovery Lookup. Locator. Discovery 2. 3 discover lookup using Lookup. Locator. Discovery object services Add listener // handle invalid Jini URL catch ( Malformed. URLException exception ) { exception. print. Stack. Trace (); } } // end Unicast. Discovery. Utility constructor 2001 Prentice Hall, Inc. All rights reserved.

Outline 64 65 66 67 68 69 70 71 72 73 74 75 76

Outline 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 // receive notification of found lookup services public void discovered( Discovery. Event event ) { // get the proxy registrars for those services Service. Registrar[] registrars = event. get. Registrars(); // display information for each lookup service found for ( int i = 0; i < registrars. length ; i++ ) display. Service. Details( registrars[ i ] ); Found lookup services 3. Lookup services found } // end method discovered // display details of given Service. Registrar private void display. Service. Details( Service. Registrar registrar ) { try { final String. Buffer buffer = new String. Buffer(); // get the hostname and port number buffer. append( "Lookup Service: " ); buffer. append( "n Host: " + registrar. get. Locator(). get. Host () ); buffer. append( "n Port: " + registrar. get. Locator(). get. Port () ); buffer. append( "n Group support: " ); Lookup service host 4. Display lookup service info Lookup service port number // get lookup service groups String[] groups = registrar. get. Groups(); 2001 Prentice Hall, Inc. All rights reserved.

93 94 95 96 97 98 99 100 101 102 103 104 105 106

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 // get group names; if empty write public for ( int i = 0; i < groups. length ; i++ ) { if ( groups[ i ]. equals( "" ) ) buffer. append( "public, " ); else buffer. append( groups[ i ] + ", " ); Outline Lookup service group name } buffer. append( "nn" ); // append information to output. Area Swing. Utilities. invoke. Later( // create Runnable for appending text new Runnable() { // append text and update caret position public void run() { output. Area. append( buffer. to. String() ); output. Area. set. Caret. Position ( output. Area. get. Text(). length () ); } 4. 1 append output Append 4. 2 GUI output } ); // end call to invoke. Later } // end try // handle exception communicating with lookup service catch ( Remote. Exception exception ) { exception. print. Stack. Trace (); } } // end method display. Service. Details 2001 Prentice Hall, Inc. All rights reserved.

Outline 130 131 132 133 134 135 136 137 138 139 140 141 142

Outline 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 // ignore discarded lookup services public void discarded( Discovery. Event event ) {} // launch Unicast. Discovery. Utility application public static void main( String args[] ) { // set Security. Manager if ( System. get. Security. Manager() == null ) System. set. Security. Manager( new RMISecurity. Manager() ); // check command-line arguments for hostnames if ( args. length < 1 ) { System. err. println( "Usage: java Unicast. Discovery. Utility " + "jini: //hostname: port [ jini: //hostname: port ]. . . " ); } Set security manager 4. 3 ignore invalid lookup service 5. main // launch Unicast. Discovery. Utility for set of hostnames else { Unicast. Discovery. Utility unicast. Utility = new Unicast. Discovery. Utility( args ); unicast. Utility. set. Default. Close. Operation ( EXIT_ON_CLOSE ); unicast. Utility. set. Size( 300, 300 ); unicast. Utility. set. Visible ( true ); } } // end method main } 2001 Prentice Hall, Inc. All rights reserved.

Outline Program output 2001 Prentice Hall, Inc. All rights reserved.

Outline Program output 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // General. Discovery. Utility. java // General. Discovery. Utility demonstrates using class // Lookup. Discovery. Manager for performing multicast // and unicast discovery. package com. deitel. advjhtp 1. jini. utilities. discovery; // Java core packages import java. rmi. *; import java. io. *; import java. awt. event. *; import java. net. *; // Java standard extensions import javax. swing. *; import javax. swing. border. *; // Jini core packages import net. jini. core. lookup. Service. Registrar; import net. jini. core. discovery. Lookup. Locator; Class General. Discovery. Utility 1. Import packages 2. Declarations // Jini extension packages import net. jini. discovery. *; public class General. Discovery. Utility extends JFrame implements Discovery. Listener { private Lookup. Discovery. Manager lookup. Manager; private JText. Area multicast. Area = new JText. Area( 15, 20); private JText. Area unicast. Area = new JText. Area( 15, 20 ); 2001 Prentice Hall, Inc. All rights reserved.

32 33 34 35 36 37 38 39 40 41 42 43 44 45

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 Outline // General. Discovery. Utility constructor public General. Discovery. Utility( String urls[] ) { super( "General. Discovery. Utility" ); Output area for multicast discovery // lay out JText. Areas JPanel multicast. Panel = new JPanel(); multicast. Panel. set. Border( new Titled. Border( "Multicast (Group) Notifications" ) ); multicast. Panel. add( new JScroll. Pane( multicast. Area ) ); JPanel unicast. Panel = new JPanel(); unicast. Panel. set. Border( new Titled. Border( "Unicast (Locator) Notifications" ) ); unicast. Panel. add( new JScroll. Pane( unicast. Area ) ); Container content. Pane = get. Content. Pane(); content. Pane. set. Layout( new Flow. Layout() ); content. Pane. add( unicast. Panel ); content. Pane. add( multicast. Panel ); 3. 1 GUI Output area for unicast discovery // get Lookup. Locators and Lookup. Discovery. Manager try { Lookup. Locator locators[] = new Lookup. Locator[ urls. length ]; Multicast group 3. Constructor // get array of Lookup. Locators for ( int i = 0; i < urls. length ; i++ ) locators[ i ] = new Lookup. Locator( urls[ i ] ); 3. 2 lookup locators for unicast 3. 3 Lookup. Discovery. Manager Lookup locators for unicast Unicast Lookup. Locators // instantiate a Lookup. Discovery. Manager object lookup. Manager = new Lookup. Discovery. Manager( Discovery. Group. Management. ALL_GROUPS, locators, this ); } 2001 Prentice Hall, Inc. All rights reserved.

67 68 69 70 71 72 73 74 75 76 77 78 79 80

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 // handle invalid Jini URL catch ( Malformed. URLException exception ) { exception. print. Stack. Trace(); } Outline // handle exception creating Lookup. Discovery. Manager catch ( IOException exception ) { exception. print. Stack. Trace(); } } // end General. Discovery. Utility constructor // receive notifications of discovered lookup services. public void discovered( Discovery. Event event ) { // get the proxy registrars for those services Service. Registrar[] registrars = event. get. Registrars(); 3. 4 handle exceptions 4. Receive notifications 4. 1 get lookup service proxy registrar // display information for each lookup service found for ( int i = 0; i < registrars. length ; i++ ) { // display multicast results in multicast. Area if ( lookup. Manager. get. From( registrars[ i ] ) == Lookup. Discovery. Manager. FROM_GROUP ) { 4. 2 display lookup Display multicast service info discovered lookup services display. Service. Details( registrars[ i ], multicast. Area ); } Display unicast discovered lookup services // display unicast results in unicast. Area else display. Service. Details( registrars[ i ], unicast. Area ); } } // end method discovered 2001 Prentice Hall, Inc. All rights reserved.

Outline 103 104 105 106 107 108 109 110 111 112 113 114 115

Outline 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 // display details of given Service. Registrar private void display. Service. Details( Service. Registrar registrar, final JText. Area output. Area ) { try { final String. Buffer buffer = new String. Buffer(); // get hostname and port number buffer. append( "Lookup Service: " ); buffer. append( "n Host: " + registrar. get. Locator(). get. Host() ); buffer. append( "n Port: " + registrar. get. Locator(). get. Port() ); buffer. append( "n Group support: " ); // get lookup service groups String[] groups = registrar. get. Groups(); Lookup service host Lookup service port 5. Display details of lookup service 5. 1 append output // get group names; if empty write public for ( int i = 0; i < groups. length ; i++ ) { if ( groups[ i ]. equals( "" ) ) buffer. append( "public, " ); Lookup service groups else buffer. append( groups[ i ] + ", " ); } buffer. append( "nn" ); 2001 Prentice Hall, Inc. All rights reserved.

Outline 133 134 135 136 137 138 139 140 141 142 143 144 145

Outline 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 // append information to output. Area Swing. Utilities. invoke. Later( // create Runnable for appending text new Runnable() { // append text and update caret position public void run() { output. Area. append( buffer. to. String() ); output. Area. set. Caret. Position( output. Area. get. Text(). length() ); } } ); // end call to invoke. Later 5. 2 GUI 6. Ignore invalid lookup service } // end try // handle exception communicating with lookup service catch ( Remote. Exception exception ) { exception. print. Stack. Trace (); } } // end method display. Service. Details // receive discarded lookup service notifications public void discarded( Discovery. Event event ) {} 2001 Prentice Hall, Inc. All rights reserved.

162 163 164 165 166 167 168 169 170 171 172 173 174 175

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 Outline // launch General. Discovery. Utility application public static void main( String[] args ) { // set Security. Manager if ( System. get. Security. Manager() == null ) System. set. Security. Manager( new RMISecurity. Manager() ); // launch General. Discovery. Utility for set of hostnames General. Discovery. Utility utility = new General. Discovery. Utility( args ); utility. set. Default. Close. Operation ( EXIT_ON_CLOSE ); utility. pack(); utility. set. Visible( true ); } // end method main 7. Main Exit when window closes } program output 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 2 Entry Utilities • Standard Jini Entry attributes • Custom defined Entry

22. 8. 2 Entry Utilities • Standard Jini Entry attributes • Custom defined Entry attributes 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 2 Entry Utilities 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 2 Entry Utilities 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // Seminar. Provider. java // Seminar. Provider is an Entry object for the Seminar. Info service. package com. deitel. advjhtp 1. jini. utilities. entry; // Jini extension package import net. jini. entry. *; import net. jini. lookup. entry. *; public class Seminar. Provider extends Abstract. Entry implements Service. Controlled { Public public String provider. Name = ""; // no-argument constructor public Seminar. Provider() {} field No-argument constructor Class Seminar. Provider 1. No-argument constructor 2. Other constructor // Seminar. Provider constructor for specifying provider. Name public Seminar. Provider( String provider ) { provider. Name = provider; Other constructor } } 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 3 Lease Utilities • Why leasing? – Self-healing – Recover from commonplace

22. 8. 3 Lease Utilities • Why leasing? – Self-healing – Recover from commonplace problems • Leasing strategy – Renew leases until service terminates • Helper class – Lease. Renewal. Manager 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // Seminar. Info. Lease. Service. java // Seminar. Info. Lease. Service discovers lookup services, registers // the Seminar. Info service, and creates a Lease. Renewal. Manager // to maintain the Seminar. Info service lease. package com. deitel. advjhtp 1. jini. utilities. leasing; // Java core packages import java. rmi. RMISecurity. Manager; import java. rmi. Remote. Exception; import java. io. IOException; // Jini core packages import net. jini. core. lookup. *; import net. jini. core. entry. Entry; import net. jini. core. lease. Lease; // Jini extension packages import net. jini. discovery. *; import net. jini. lookup. entry. Name ; import net. jini. lease. Lease. Renewal. Manager ; Class Seminar. Info. Lease. Service 1. Import packages 2. Declarations // Deitel packages import com. deitel. advjhtp 1. jini. seminar. service. *; import com. deitel. advjhtp 1. jini. utilities. entry. Seminar. Provider; public class Seminar. Info. Lease. Service implements Discovery. Listener { private Lookup. Discovery discover; private Service. Item item; private static final int LEASETIME = 10 * 60 * 1000; 2001 Prentice Hall, Inc. All rights reserved.

Outline 32 33 34 35 36 37 38 39 40 41 42 43 44

Outline 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 // Seminar. Info. Lease. Service constructor public Seminar. Info. Lease. Service() { // search for lookup services with public group try { discover = new Lookup. Discovery( new String[] { "" } ); // register Discovery. Listener discover. add. Discovery. Listener( this ); } Multicast discovery 3. Constructor // handle exception creating Lookup. Discovery catch ( IOException exception ) { exception. print. Stack. Trace(); } // create and set Entry name for service Entry[] entries = new Entry[ 1 ]; entries[ 0 ] = new Seminar. Provider( "Deitel" ); 3. 1 multicast discovers lookup Specifyservices service 3. 2 specify service // specify the service's proxy and entry item = new Service. Item( null, create. Proxy(), entries ); } // end Seminar. Info. Lease. Service constructor 2001 Prentice Hall, Inc. All rights reserved.

57 58 59 60 61 62 63 64 65 66 67 68 69 70

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 // receive notifications of discovered lookup services public void discovered ( Discovery. Event event ) { Service. Registrar[] registrars = event. get. Registrars(); Outline // register the service with the lookup service for ( int i = 0; i < registrars. length; i++ ) { Service. Registrar registrar = registrars[ i ]; // register the service with the lookup service try { Service. Registration registration = registrar. register( item, LEASETIME ); Register service 4. Found lookup service // create Lease. Renewalmanager Lease. Renewal. Manager lease. Manager = new Lease. Renewal. Manager(); 4. 1 register service // renew Seminar. Info lease indefinitely lease. Manager. renew. Until( registration. get. Lease(), Lease. FOREVER, null ); } // end try 4. 2 renew service’s lease Renew service’s lease // handle exception registering Service. Item catch ( Remote. Exception exception ) { exception. print. Stack. Trace (); } } // end for } // end method discovered 2001 Prentice Hall, Inc. All rights reserved.

Outline 91 92 93 94 95 96 97 98 99 100 101 102 103

Outline 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 // ignore discarded lookup services public void discarded( Discovery. Event event ) {} // create seminar service proxy private Seminar. Interface create. Proxy() { // get Backend. Interface reference to Seminar. Info try { Backend. Interface back. Interface = new Seminar. Info(); return new Seminar. Proxy( back. Interface ); } // handle exception creating Seminar. Proxy catch ( Remote. Exception exception ) { exception. print. Stack. Trace(); } 5. Ignore invalid lookup service Return service proxy 6. Create service proxy return null; } // end method create. Proxy 2001 Prentice Hall, Inc. All rights reserved.

Outline 113 114 115 116 117 118 119 120 121 122 123 124 125

Outline 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 // launch Seminar. Info. Lease. Service public static void main( String args[] ) { // set Security. Manager if ( System. get. Security. Manager() == null ) { System. set. Security. Manager( new RMISecurity. Manager() ); } Seminar. Info. Lease. Service service = new Seminar. Info. Lease. Service(); Object keep. Alive = new Object(); // wait on keep. Alive Object to keep service running synchronized ( keep. Alive ) { // keep application alive try { keep. Alive. wait(); } Keep application alive 7. main 7. 1 set security manager 7. 2 keep application alive // handle exception if wait interrupted catch ( Interrupted. Exception exception ) { exception. print. Stack. Trace (); } } // end synchronized block } // end method main } 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 3 Lease Utilities 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 3 Lease Utilities 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 4 Join. Manager Utility • Deploy Jini service – Lookup discovery –

22. 8. 4 Join. Manager Utility • Deploy Jini service – Lookup discovery – Service registration – Lease management • Helper class – Join. Manager 2001 Prentice Hall, Inc. All rights reserved.

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13

Outline 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Seminar. Info. Join. Service. java // Seminar. Info. Join. Service uses a Join. Manager to find lookup // services, register the Seminar service with the lookup // services and manage lease renewal. package com. deitel. advjhtp 1. jini. utilities. join; // Java core packages import java. rmi. RMISecurity. Manager; import java. rmi. Remote. Exception; import java. io. IOException; // Jini core packages import net. jini. core. lookup. Service. ID; import net. jini. core. entry. Entry; // Jini extension packages import net. jini. lookup. entry. Name; import net. jini. lease. Lease. Renewal. Manager ; import net. jini. lookup. Join. Manager ; import net. jini. discovery. Lookup. Discovery. Manager ; import net. jini. lookup. Service. IDListener ; Class Seminar. Info. Join. Service 1. Import packages // Deitel packages import com. deitel. advjhtp 1. jini. seminar. service. *; import com. deitel. advjhtp 1. jini. utilities. entry. *; public class Seminar. Info. Join. Service implements Service. IDListener { 2001 Prentice Hall, Inc. All rights reserved.

Outline 29 30 31 32 33 34 35 36 37 38 39 40 41

Outline 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 // Seminar. Info. Join. Service constructor public Seminar. Info. Join. Service() { // use Join. Manager to register Seminar. Info service // and manage lease try { // create Lookup. Discovery. Manager for discovering // lookup services Lookup. Discovery. Manager lookup. Manager = new Lookup. Discovery. Manager( new String[] { "" }, null ); manager 2. 1 specify discovery manager // create and set Entry name for service Entry[] entries = new Entry[ 1 ]; entries[ 0 ] = new Seminar. Provider( "Deitel" ); Specify service 2. 2 specify // create Join. Manager manager = new Join. Manager( create. Proxy(), entries, this, lookup. Manager, new Lease. Renewal. Manager() ); } 2. Constructor Specify discovery service 2. 3 deploy service Deploy service // handle exception creating Join. Manager catch ( IOException exception ) { exception. print. Stack. Trace (); } } // end Seminar. Info. Join. Service constructor 2001 Prentice Hall, Inc. All rights reserved.

59 60 61 62 63 64 65 66 67 68 69 70 71 72

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 // create seminar service proxy private Seminar. Interface create. Proxy() { // get Seminar. Proxy for Seminar. Info service try { return new Seminar. Proxy( new Seminar. Info() ); } Outline Return service proxy // handle exception creating Seminar. Proxy catch ( Remote. Exception exception ) { exception. print. Stack. Trace(); } return null; } // end method create. Proxy // receive notification of Service. ID assignment public void service. IDNotify( Service. ID service. ID ) { System. err. println( "Service ID: " + service. ID ); } 3. Create service proxy 4. Get Service. ID Assigned 5. main Service ID // launch Seminar. Info. Join. Service public static void main( String args[] ) { // set Security. Manager if ( System. get. Security. Manager () == null ) { System. set. Security. Manager ( new RMISecurity. Manager() ); } // create Seminar. Info. Join. Service new Seminar. Info. Join. Service(); } } 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 4 Join. Manager Utility 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 4 Join. Manager Utility 2001 Prentice Hall, Inc. All rights reserved.

22. 8. 5 Service Discovery Utilities • Benefit Jini client • Helper class –

22. 8. 5 Service Discovery Utilities • Benefit Jini client • Helper class – Service. Discovery. Manager • Create a local cache of services • Receive event notifications • Perform searches not possible with Service. Templates 2001 Prentice Hall, Inc. All rights reserved.