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.relacs.gm; 19 20 import java.io.IOException; 21 import java.rmi.Remote; 22 import java.rmi.server.ExportException; 23 24 import jgroup.core.ExternalGMIService; 25 import jgroup.jini.GroupEntry; 26 import jgroup.relacs.config.AppConfig; 27 import jgroup.relacs.gmi.JeriGroupExporter; 28 import net.jini.core.entry.Entry; 29 import net.jini.core.lookup.ServiceID; 30 import net.jini.discovery.LookupDiscovery; 31 import net.jini.export.Exporter; 32 import net.jini.lookup.JoinManager; 33 import net.jini.lookup.ServiceIDListener; 34 35 import org.apache.log4j.Logger; 36 37 /** 38 * This layer is used to make the joining process into 39 * greg transparent, when creating an application. 40 * 41 * @author Rohnny Moland 42 * @since Jgroup 2.2 43 */ 44 public class GregRegistryLayer 45 implements GregRegistryService, ServiceIDListener 46 { 47 //////////////////////////////////////////////////////////////////////////////////////////// 48 // Logger 49 //////////////////////////////////////////////////////////////////////////////////////////// 50 51 /** Obtain logger for this class */ 52 private static final Logger log = Logger.getLogger(GregRegistryLayer.class); 53 54 55 //////////////////////////////////////////////////////////////////////////////////////////// 56 // Fields 57 //////////////////////////////////////////////////////////////////////////////////////////// 58 59 /** The service proxy */ 60 private ExternalGMIService egmis; 61 62 /** The server object using this layer. */ 63 private Object server; 64 65 /** The remote proxy */ 66 private Remote proxy; 67 68 69 //////////////////////////////////////////////////////////////////////////////////////////// 70 // Constructor / Static factory 71 //////////////////////////////////////////////////////////////////////////////////////////// 72 73 private GregRegistryLayer(ExternalGMIService egmis) 74 { 75 this.egmis = egmis; 76 } 77 78 public static GregRegistryLayer getLayer(ExternalGMIService egmis) 79 { 80 return new GregRegistryLayer(egmis); 81 } 82 83 84 //////////////////////////////////////////////////////////////////////////////////////////// 85 // Methods from Layer 86 //////////////////////////////////////////////////////////////////////////////////////////// 87 88 /* (non-Javadoc) 89 * @see jgroup.core.Layer#addListener(java.lang.Object) 90 */ 91 public void addListener(Object listener) 92 { 93 if (listener == null) 94 throw new NullPointerException("No replica specified for the registry layer."); 95 this.server = listener; 96 97 /** Get the application object for this replica */ 98 AppConfig thisApp = AppConfig.getApplication(listener); 99 100 String[] groups = new String[] {thisApp.getParam("lookup.group", "jgroup.sf.net")}; 101 if (!thisApp.getBooleanParam("GregRegistry.auto")) 102 return; 103 104 /** Joins the app into greg */ 105 join(thisApp.getRegistryName(), groups); 106 if (log.isDebugEnabled()) 107 log.debug("Successfully joined: " + thisApp.getRegistryName()); 108 } 109 110 111 //////////////////////////////////////////////////////////////////////////////////////////// 112 // Methods from GregRegistryService 113 //////////////////////////////////////////////////////////////////////////////////////////// 114 115 /* (non-Javadoc) 116 * @see jgroup.relacs.gm.GregRegistryService#join(java.lang.String) 117 */ 118 public void join(String serviceName, String[] groups) 119 { 120 Exporter exporter = new JeriGroupExporter(serviceName); 121 try { 122 proxy = exporter.export((Remote) server); 123 Entry[] attrs = new Entry[] { new GroupEntry(serviceName) }; 124 // FIXME: Register the lease time also in greg. 125 LookupDiscovery ld = new LookupDiscovery(groups); 126 JoinManager joinMgr = new JoinManager(proxy, attrs, this, ld, null); 127 } catch (ExportException e) { 128 log.error(e.getMessage()); 129 } catch (IOException e) { 130 log.error(e.getMessage()); 131 } 132 } 133 134 135 //////////////////////////////////////////////////////////////////////////////////////////// 136 // Methods from ServiceIDListener 137 //////////////////////////////////////////////////////////////////////////////////////////// 138 139 /* (non-Javadoc) 140 * @see net.jini.lookup.ServiceIDListener#serviceIDNotify(net.jini.core.lookup.ServiceID) 141 */ 142 public void serviceIDNotify(ServiceID serviceID) 143 { 144 if (server instanceof ServiceIDListener) { 145 ((ServiceIDListener) server).serviceIDNotify(serviceID); 146 } 147 if (log.isDebugEnabled()) 148 log.debug("Service ID (from layer): " + serviceID); 149 } 150 151 } // END GregRegistryLayer