Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/fetish/toolkit/Administrator.java


1   package com.fetish.toolkit;
2   
3   import java.rmi.*;
4   import java.io.*;
5   import com.fetish.toolkit.*;
6   import com.fetish.directory.*;
7   import net.jini.core.lookup.*;
8   import net.jini.lookup.*;
9   import net.jini.discovery.*;
10  import net.jini.core.discovery.*;
11  import net.jini.lease.*;
12  import net.jini.core.lease.*;
13  import net.jini.event.*;
14  import net.jini.core.event.*;
15  
16  /**
17   * FADA node model.
18   * <br>Class for getting information and manipulating a FADA node
19   */
20  
21  public class Administrator implements java.io.Serializable {
22  
23      LeaseRenewalManager lrm = null;
24      LUSManager lusm = null;
25      Directory dir = null;
26      String url = null;
27      LUSManager[] neighbors = null;
28      ServiceItem[] services = null;
29  
30      public Administrator() {
31      }
32  
33      //public Administrator( String url ) throws RemoteException, IOException, ClassNotFoundException {
34          //this.url = url;
35          //this.lusm = this.getLUSManager( url );
36          //this.dir = this.getDirectory( url );
37          //this.neighbors = this.getNeighbors( lusm );
38          //this.services = this.getServices( dir );
39      //}
40      
41      //public void refresh() throws RemoteException, IOException, ClassNotFoundException {
42          //this.neighbors = this.getNeighbors( lusm );
43          //this.services = this.getServices( dir );
44      //}
45  
46      /**
47       * Obtains the LUSManager specified by the url.
48       * <br>
49       * The url must be in the form &lt;host&gt[&lt;port&gt;]
50       */
51      public LUSManager getLUSManager( String url )
52      throws RemoteException {
53          try {
54              LookupLocator locator = new LookupLocator( "jini://" + url );
55              ServiceRegistrar registrar = locator.getRegistrar();
56              Class[] classes = { LUSManager.class };
57              ServiceTemplate template =  new ServiceTemplate(
58                                              null,
59                                              classes,
60                                              null
61                                          );
62              Object lusm = registrar.lookup( template );
63              return ( LUSManager )lusm;
64          } catch( Exception e ) {
65            return null;
66          }
67    }
68  
69    /**
70     * Obtains the Directory specified by the url.
71     * <br>
72     * The url must be in the form &lt;host&gt[&lt;port&gt;]
73     */
74      public Directory getDirectory( String url )
75      throws RemoteException {
76          try {
77              LookupLocator locator = new LookupLocator( "jini://" + url );
78              ServiceRegistrar registrar = locator.getRegistrar();
79              Class[] classes = { Directory.class };
80              ServiceTemplate template =  new ServiceTemplate(
81                                              null,
82                                              classes,
83                                              null
84                                          );
85              Object dir = registrar.lookup( template );
86              return ( Directory )dir;
87          } catch( Exception e ) {
88              return null;
89          }
90      }
91          
92  
93      /**
94       * Connects two FADA nodes.
95       * <br>
96       * @param url1 String identifying the first FADA node
97       * @param url2 String identifying the second FADA node
98       * @throws RemoteException There was a communication error with the nodes
99       */
100     public boolean connect( String url1, String url2 ) throws RemoteException {
101             LUSManager lusm1 = getLUSManager( url1 );
102             LUSManager lusm2 = getLUSManager( url2 );
103             return connect( lusm1, lusm2 );
104     }
105     
106     /**
107      * Connects this FADA node with another.
108      * <br>
109      * @param url String identifying the FADA node to connect this one with
110      * @throws RemoteException There was a communication error with the node
111      */
112     //public boolean connect( String url1 ) throws RemoteException {
113         //LUSManager lusm1 = getLUSManager( url1 );
114         //return lusm.connect( lusm1 );
115     //}
116 
117     /**
118      * Connects two FADA nodes.
119      * <br>
120      * @param lusm1 Remote reference to the first FADA node
121      * @param lusm2 Remote reference to the second FADA node
122      * @throws RemoteException There was a communication error with the nodes
123      */
124     public boolean connect( LUSManager lusm1, LUSManager lusm2 ) throws RemoteException {
125             return lusm1.connect( lusm2 );
126     }
127 
128     ///**
129      //* Disconnects two FADA nodes.
130      //* <br>
131      //* Deletes both nodes from each other's list of neighbors.
132      //* @param url1 String identifying the first FADA node
133      //* @param url2 String identifying the second FADA node
134      //* @throws RemoteException There was a communication error with the nodes
135      //*/
136     //public void disconnect( String url1, String url2 ) throws RemoteException {
137             //LUSManager lusm1 = getLUSManager( url1 );
138             //LUSManager lusm2 = getLUSManager( url2 );
139             //disconnect( lusm1, lusm2 );
140     //}
141 
142     /**
143      * Disconnects two FADA nodes.
144      * <br>
145      * @param lusm1 Remote reference to the first FADA node
146      * @param lusm2 Remote reference to the second FADA node
147      * @throws RemoteException There was a communication error with the nodes
148      */
149     public void disconnect( LUSManager lusm1, LUSManager lusm2 )
150     throws RemoteException {
151       lusm1.disconnect( lusm2 );
152     }
153 
154     /**
155      * Disconnects two FADA nodes.
156      * <br>
157      * This method is provided to delete a neighbor that cannot be contacted,
158      * but whose ServiceID is known to the first FADA node.
159      * @param lusm Remote reference to the first FADA node
160      * @param sid ServiceID of the node to disconnect
161      * @throws RemoteException There was a communication error with either
162      * node
163      */
164   public void disconnect( String url1, String url2 )
165   throws RemoteException {
166     LUSManager lusm1 = getLUSManager( url1 );
167     disconnect( lusm1, url2 );
168   }
169 
170     /**
171      * Disconnects two FADA nodes.
172      * <br>
173      * This method is provided to delete a neighbor that cannot be contacted,
174      * but whose ServiceID is known to the first FADA node.
175      * @param lusm Remote reference to the first FADA node
176      * @param sid ServiceID of the node to disconnect
177      * @throws RemoteException There was a communication error with either
178      * node
179      */
180   public void disconnect( LUSManager lusm1, String url )
181   throws RemoteException {
182     lusm1.disconnect( url );
183   }
184 
185     /**
186      * Deletes all references to known neighbors.
187      * <br>Removes all connections to neighbors, and all connections from
188      * neighbors, in the node specified by the url
189      * @param url String identifying the FADA node
190      * @throws RemoteException There was a communication error with the node
191      */
192     public void isolate( String url ) throws RemoteException {
193         LUSManager lusm = getLUSManager( url );
194         isolate( lusm );
195     }
196 
197     /**
198      * Deletes all references to known neighbors.
199      * <br>Removes all connections to neighbors, and all connections from
200      * neighbors, in the node specified by the url
201      * @param lusm Remote reference to the FADA node
202      * @throws RemoteException There was a communication error with the node
203      */
204     public void isolate( LUSManager lusm ) throws RemoteException {
205         lusm.isolate();
206     }
207     
208     //public void isolate() throws RemoteException {
209         //this.lusm.isolate();
210     //}
211 
212     public String getServiceSID( Remote service ) throws RemoteException {
213             return null;
214     }
215 
216     /**
217      * Obtains the real URL for the FADA identified by the String url.
218      * @throws RemoteException There was a communication error with the node
219      */
220     public String getUrl( String url ) throws RemoteException {
221             LUSManager lusm = getLUSManager( url );
222             return lusm.getUrl();
223     }
224     
225     //public String getUrl() {
226         //return this.url;
227     //}
228 
229     /**
230      * Obtains the complete list of neighbors for a given FADA node.
231      * <br>
232      * The url is a String in the form &lt;host&gt;:[&lt;port&gt;]
233      * @param url The String url
234      * @throws RemoteException There was a communication error with the node
235      */
236     public LUSManager[] getNeighbors( String url ) throws RemoteException {
237             LUSManager lusm = getLUSManager( url );
238             if( lusm == null ) {
239               return new LUSManager[0];
240             }
241             return getNeighbors( lusm );
242     }
243     
244     //public LUSManager[] getNeighbors() {
245         //return this.neighbors;
246     //}
247 
248     /**
249      * Obtains the complete list of neighbors for a given FADA node.
250      * <br>
251      * @param lusm The LUSManager remote reference
252      * @throws RemoteException There was a communication error with the node
253      */
254     public LUSManager[] getNeighbors( LUSManager lusm ) throws RemoteException {
255         Remote[] remoteNeighbors = lusm.getNeighbors();
256         LUSManager[] lusmNeighbors = null;
257         if( remoteNeighbors == null ) {
258             remoteNeighbors = new Remote[0];
259             lusmNeighbors = new LUSManager[0];
260         } else {
261             lusmNeighbors = new LUSManager[ remoteNeighbors.length ];
262         }
263         for( int i=0; i<remoteNeighbors.length; i++ ) {
264             lusmNeighbors[i] = ( LUSManager )remoteNeighbors[i];
265         }
266         return lusmNeighbors;
267     }
268 
269     /**
270      * Get the list of services registered in a FADA node.
271      * <br>
272      * @param url The String url
273      * @returns An array of ServiceItem containing all services registered in
274      * the FADA node.
275      * @throws RemoteException There was a communication error with the node
276      * @throws IOException ?
277      * @throws ClassNotFoundException ?
278      */
279     public ServiceItem[] getServices( String url ) throws RemoteException, IOException, ClassNotFoundException {
280             Directory dir = getDirectory( url );
281             return getServices( dir );
282     }
283     
284     public ServiceItem[] getServices( Directory dir ) throws RemoteException, IOException, ClassNotFoundException {
285             ServiceMatches sm = dir.getServices();
286             ServiceItem[] services = sm.items;
287             return services;
288     }
289     
290     //public ServiceItem[] getServices() {
291         //return this.services;
292     //}
293   
294 
295     /*public ServiceItem[] getServices( String url ) throws RemoteException, IOException, ClassNotFoundException {
296         LookupLocator locator = new LookupLocator( "jini://" + url );
297         ServiceRegistrar registrar = locator.getRegistrar();
298         Class[] classes = new Class[0];
299         ServiceTemplate template = new ServiceTemplate(
300                                                         null,
301                                                         classes,
302                                                         null
303                                                 );
304         ServiceMatches serviceObjects = registrar.lookup( template, Integer.MAX_VALUE );
305         LUSManager lusm = getLUSManager( url );
306         ServiceID lusmSid = lusm.getServiceID();
307         ServiceItem[] services =
308             new ServiceItem[ serviceObjects.totalMatches - 1 ];
309         for( int i = 0, j = 0; j < serviceObjects.totalMatches - 1; i++ ) {
310             services[ j ] = ( ServiceItem )serviceObjects.items[ i ];
311             if( lusmSid.equals( services[ j ].serviceID ) ) {
312                 services[ j ] = null;
313             } else {
314                 j++;
315             }
316         }
317         return services;
318     }
319     */
320 
321     /**
322      * Stop a FADA node.
323      * <br>
324      * Brings down a FADA node, given its password is known.
325      * This method is sensitive to brute force attacks and will be replaced.
326      * @param url The String with the url for the FADA node, in the form
327      * &lt;host&gt;:[&lt;port&gt;]
328      * @param password The String with the password for the FADA node
329      * @throws RemoteException There was a communication error with the node
330      */
331     public void stop( String url, String password ) throws RemoteException {
332         LUSManager lusm = getLUSManager( url );
333         stop( lusm, password );
334     }
335 
336     /**
337      * Stop a FADA node.
338      * <br>
339      * Brings down a FADA node, given its password is known.
340      * This method is sensitive to brute force attacks and will be replaced.
341      * @param url The remote reference to a node
342      * @param password The String with the password for the FADA node
343      * @throws RemoteException There was a communication error with the node
344      */
345     public void stop( LUSManager lusm, String password ) throws RemoteException {
346         lusm.stop( password );
347     }
348     
349     //public void stop( String password ) throws RemoteException {
350         //this.lusm.stop( password );
351     //}
352 
353     //public void inspect( String url, RemoteEventListener listener )
354     //throws Exception {
355         //LookupLocator locator = new LookupLocator( "jini://" + url );
356         //ServiceRegistrar registrar = locator.getRegistrar();
357         //EventRegistration evReg = registrar.notify( new ServiceTemplate(
358                 //null, null, null
359             //),
360             //ServiceRegistrar.TRANSITION_NOMATCH_MATCH |
361             //ServiceRegistrar.TRANSITION_MATCH_MATCH,
362             //listener,
363             //null,
364             //Lease.ANY );
365 /*
366 public EventRegistration notify(ServiceTemplate tmpl,
367                                 int transitions,
368                                 RemoteEventListener listener,
369                                 java.rmi.MarshalledObject handback,
370                                 long leaseDuration)
371                          throws java.rmi.RemoteException
372 
373 */
374         //Lease lease = evReg.getLease();
375         //if( lrm == null )
376             //lrm = new LeaseRenewalManager();
377         //lrm.renewFor( lease, Lease.FOREVER, null );
378     //}
379 }