View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  package jgroup.test.jini.reggie;
19  
20  import java.io.IOException;
21  import java.rmi.Remote;
22  import java.rmi.RemoteException;
23  
24  import jgroup.core.ConfigManager;
25  import jgroup.util.Network;
26  import net.jini.config.Configuration;
27  import net.jini.config.ConfigurationProvider;
28  import net.jini.core.discovery.LookupLocator;
29  import net.jini.core.entry.Entry;
30  import net.jini.core.lookup.ServiceID;
31  import net.jini.discovery.LookupDiscovery;
32  import net.jini.discovery.LookupDiscoveryManager;
33  import net.jini.export.Exporter;
34  import net.jini.jeri.BasicILFactory;
35  import net.jini.jeri.BasicJeriExporter;
36  import net.jini.jeri.tcp.TcpServerEndpoint;
37  import net.jini.lookup.JoinManager;
38  import net.jini.lookup.ServiceIDListener;
39  import net.jini.lookup.entry.Name;
40  
41  import org.apache.log4j.Logger;
42  
43  import com.sun.jini.config.Config;
44  import com.sun.jini.start.LifeCycle;
45  
46  
47  /**
48   * Simple server to test Reggie.
49   * 
50   * @author Rohnny Moland
51   */
52  public class ReggieHelloServer 
53    implements ServiceIDListener, ReggieHello
54  {  
55    ////////////////////////////////////////////////////////////////////////////////////////////
56    // Logger
57    ////////////////////////////////////////////////////////////////////////////////////////////
58  
59    /** Obtain logger for this class */
60    private static final Logger log = Logger.getLogger(ReggieHelloServer.class);
61  
62    ////////////////////////////////////////////////////////////////////////////////////////////
63    // Fields
64    ////////////////////////////////////////////////////////////////////////////////////////////
65  
66    /** Message returned by sayHello */ 
67    private String answer;
68    
69    /** The join manager instance */
70    private JoinManager joinManager;
71    
72    /** The exporter instance */
73    private Exporter exporter;
74    
75    /** The exported proxy instance */
76    private Remote proxy;
77    
78    
79    ////////////////////////////////////////////////////////////////////////////////////////////
80    // Constructor and main
81    ////////////////////////////////////////////////////////////////////////////////////////////
82  
83   
84    /**
85     * Constructor compatible with the jini service starter.
86     *
87     * @param configArgs <code>String</code> array whose elements are
88     *                   the arguments to use when creating the server.
89     * @param lifeCycle  instance of <code>LifeCycle</code> that, if
90     *                   non-<code>null</code>, will cause this object's
91     *                   <code>unregister</code> method to be invoked during
92     *                   shutdown to notify the service starter framework that
93     *                   the reference to this service's implementation can be
94     *                   'released' for garbage collection. A value of
95     *                   <code>null</code> for this argument is allowed.
96     *
97     * @throws Exception If there was a problem initializing the service.
98     */
99    public ReggieHelloServer(String[] configArgs, LifeCycle lifeCycle) 
100     throws Exception
101   { 
102     ConfigManager.init();
103     
104     final Configuration config = ConfigurationProvider.getInstance(configArgs, getClass()
105         .getClassLoader());
106  
107     answer = new String("Hello from " + Network.getLocalHostName());
108    
109     
110     /** Exports the remote object and joins it into reggie */ 
111     exporter = (Exporter) 
112       Config.getNonNullEntry(config, "jgroup.test.jini.reggie", 
113                       "serverExporter", Exporter.class, 
114                       new BasicJeriExporter(TcpServerEndpoint.getInstance(0), 
115                                             new BasicILFactory()));
116     proxy = exporter.export(this);
117 
118     Entry[] entries = (Entry[]) 
119       Config.getNonNullEntry(config, "jgroup.test.jini.reggie", "entries",
120                              Entry[].class, new Entry[] {new Name("ReggieHelloServer")});
121 
122     String[] groups = (String[]) 
123       Config.getNonNullEntry(config, "jgroup.test.jini.reggie", "initialLookupGroups", 
124           String[].class, new String[] {System.getProperty("reggiehello.lookup.group", "jgroup.sf.net")});
125     
126     try {
127       LookupDiscovery ld = new LookupDiscovery(groups);
128       joinManager = new JoinManager(proxy, entries, this, ld, null);
129     } catch (Exception e) {
130       log.error(e.getMessage());
131       System.exit(1);
132     }    
133   } 
134   
135   /**
136    * Helper method used to retrieve the LookupDiscoveryManager
137    * from a started service.
138    */
139   private LookupDiscoveryManager getDiscoveryManager()
140   {
141     return (LookupDiscoveryManager) joinManager.getDiscoveryManager();
142   }
143   
144   ////////////////////////////////////////////////////////////////////////////////////////////
145   // Methods from ReggieHello
146   ////////////////////////////////////////////////////////////////////////////////////////////
147 
148   /**
149    *  External method
150    */
151   public String sayHello() throws java.rmi.RemoteException
152   {    
153     log.debug("sayHello: returning " + answer);
154     return answer;
155   }
156 
157   
158   ////////////////////////////////////////////////////////////////////////////////////////////
159   // Methods from ServiceIDListener
160   ////////////////////////////////////////////////////////////////////////////////////////////
161 
162   public void serviceIDNotify(ServiceID serviceID)
163   {
164     log.debug("Service id: " + serviceID);
165   }
166 
167   
168   ////////////////////////////////////////////////////////////////////////////////////////////
169   // Methods from JoinAdmin
170   ////////////////////////////////////////////////////////////////////////////////////////////
171 
172   /* (non-Javadoc)
173    * @see net.jini.admin.JoinAdmin#getLookupAttributes()
174    */
175   public Entry[] getLookupAttributes() throws RemoteException
176   {
177     return joinManager.getAttributes();
178   }
179 
180 
181   /* (non-Javadoc)
182    * @see net.jini.admin.JoinAdmin#addLookupAttributes(net.jini.core.entry.Entry[])
183    */
184   public void addLookupAttributes(Entry[] attrSets) throws RemoteException
185   {
186     joinManager.addAttributes(attrSets);
187   }
188 
189 
190   /* (non-Javadoc)
191    * @see net.jini.admin.JoinAdmin#modifyLookupAttributes(net.jini.core.entry.Entry[], net.jini.core.entry.Entry[])
192    */
193   public void modifyLookupAttributes(Entry[] attrSetTemplates, Entry[] attrSets) throws RemoteException
194   {
195     joinManager.modifyAttributes(attrSetTemplates, attrSets);
196   }
197 
198 
199   /* (non-Javadoc)
200    * @see net.jini.admin.JoinAdmin#getLookupGroups()
201    */
202   public String[] getLookupGroups() throws RemoteException
203   {
204     return getDiscoveryManager().getGroups();
205   }
206 
207 
208   /* (non-Javadoc)
209    * @see net.jini.admin.JoinAdmin#addLookupGroups(java.lang.String[])
210    */
211   public void addLookupGroups(String[] groups) throws RemoteException
212   {
213     try {
214       getDiscoveryManager().addGroups(groups);      
215     } catch (IOException e) {
216       log.error(e.getMessage());
217     }
218   }
219 
220 
221   /* (non-Javadoc)
222    * @see net.jini.admin.JoinAdmin#removeLookupGroups(java.lang.String[])
223    */
224   public void removeLookupGroups(String[] groups) throws RemoteException
225   {
226     getDiscoveryManager().removeGroups(groups);
227   }
228 
229 
230   /* (non-Javadoc)
231    * @see net.jini.admin.JoinAdmin#setLookupGroups(java.lang.String[])
232    */
233   public void setLookupGroups(String[] groups) throws RemoteException
234   { 
235     try {
236       getDiscoveryManager().setGroups(groups);
237     } catch (IOException e) {
238       log.error(e.getMessage());
239     }
240   }
241 
242 
243   /* (non-Javadoc)
244    * @see net.jini.admin.JoinAdmin#getLookupLocators()
245    */
246   public LookupLocator[] getLookupLocators() throws RemoteException
247   {
248     return getDiscoveryManager().getLocators();
249   }
250 
251 
252   /* (non-Javadoc)
253    * @see net.jini.admin.JoinAdmin#addLookupLocators(net.jini.core.discovery.LookupLocator[])
254    */
255   public void addLookupLocators(LookupLocator[] locators) throws RemoteException
256   {
257     getDiscoveryManager().addLocators(locators);
258   }
259 
260 
261   /* (non-Javadoc)
262    * @see net.jini.admin.JoinAdmin#removeLookupLocators(net.jini.core.discovery.LookupLocator[])
263    */
264   public void removeLookupLocators(LookupLocator[] locators) throws RemoteException
265   {
266     getDiscoveryManager().removeLocators(locators);
267   }
268 
269 
270   /* (non-Javadoc)
271    * @see net.jini.admin.JoinAdmin#setLookupLocators(net.jini.core.discovery.LookupLocator[])
272    */
273   public void setLookupLocators(LookupLocator[] locators) throws RemoteException
274   {
275     getDiscoveryManager().setLocators(locators);
276   }
277 
278   
279   ////////////////////////////////////////////////////////////////////////////////////////////
280   // Methods from DestroyAdmin
281   ////////////////////////////////////////////////////////////////////////////////////////////
282 
283   /* (non-Javadoc)
284    * @see com.sun.jini.admin.DestroyAdmin#destroy()
285    */
286   public void destroy() throws RemoteException
287   {
288     exporter.unexport(true);
289   }
290 
291   
292   ////////////////////////////////////////////////////////////////////////////////////////////
293   // Methods from Administrable
294   ////////////////////////////////////////////////////////////////////////////////////////////
295 
296   /* (non-Javadoc)
297    * @see net.jini.admin.Administrable#getAdmin()
298    */
299   public Object getAdmin() throws RemoteException
300   {
301     return proxy;
302   }
303 
304 }