1 package org.sapia.regis.remote; 2 3 import java.util.Properties ; 4 5 import org.sapia.regis.Configurable; 6 import org.sapia.regis.Node; 7 import org.sapia.regis.Path; 8 import org.sapia.regis.RWNode; 9 import org.sapia.regis.RWSession; 10 import org.sapia.regis.RegisSession; 11 import org.sapia.regis.Registry; 12 import org.sapia.regis.RegistryContext; 13 import org.sapia.regis.loader.RegistryConfigLoader; 14 import org.sapia.regis.util.Utils; 15 import org.sapia.ubik.rmi.Consts; 16 import org.sapia.ubik.rmi.server.Hub; 17 18 131 public class RegistryServer implements RemoteConsts{ 132 133 private static RegistryExporter _exporter; 134 private static Properties _bootstrapProps = new Properties (); 135 public static boolean startThread = true; 136 137 138 public static void main(String [] args) { 139 if(args.length == 1 && ( 140 args[0].equals("--help") || 141 args[0].equals("-h") 142 )){ 143 help(); 144 return; 145 } 146 try{ 147 _bootstrapProps = new Properties (System.getProperties()); 148 if(args.length > 0){ 149 Utils.loadProps(RegistryServer.class, _bootstrapProps, args[0]); 150 } 151 152 if(_bootstrapProps.getProperty(Consts.IP_PATTERN_KEY) != null){ 153 System.setProperty(Consts.IP_PATTERN_KEY, _bootstrapProps.getProperty(Consts.IP_PATTERN_KEY)); 154 } 155 156 ServerDebug.enabled = _bootstrapProps.getProperty(DEBUG, "true").equalsIgnoreCase("true"); 157 158 RegistryContext ctx = new RegistryContext(_bootstrapProps); 159 Registry reg = ctx.connect(); 160 161 if(_bootstrapProps.getProperty(BOOTSTRAP) != null){ 162 try{ 163 loadBootStrap(reg, _bootstrapProps.getProperty(BOOTSTRAP), _bootstrapProps); 164 }catch(Exception e){ 165 System.out.println("Could not load bootstrap configuration."); 166 System.out.println("Got the following error:"); 167 e.printStackTrace(); 168 return; 169 } 170 } 171 172 _exporter = new RegistryExporter( 173 _bootstrapProps.getProperty(USERNAME, DEFAULT_USERNAME), 174 _bootstrapProps.getProperty(PASSWORD, DEFAULT_PASSWORD), 175 reg, 176 _bootstrapProps); 177 if(_bootstrapProps.getProperty(Consts.UBIK_DOMAIN_NAME) != null){ 178 _exporter.setDomain(_bootstrapProps.getProperty(Consts.UBIK_DOMAIN_NAME)); 179 } 180 _exporter.setPeerToPeer(new Boolean (_bootstrapProps.getProperty(PEER_TO_PEER, "false")).booleanValue()); 181 if(_bootstrapProps.getProperty(SERVER_JNDI_NAME) != null){ 182 _exporter.bind(_bootstrapProps.getProperty(SERVER_JNDI_NAME), _bootstrapProps); 183 } 184 else if(_bootstrapProps.getProperty(SERVER_PORT) != null){ 185 _exporter.bind(Integer.parseInt(_bootstrapProps.getProperty(SERVER_PORT))); 186 } 187 else{ 188 throw new IllegalStateException ("One of the following properties must be specified:" + 189 SERVER_JNDI_NAME + " or " + SERVER_PORT); 190 } 191 System.out.println("Registry server started - typce CTRL-C to abort cleanly."); 192 Runtime.getRuntime().addShutdownHook(new ShutdownHook()); 193 194 if(startThread){ 195 while(true){ 196 try{ 197 Thread.sleep(100000); 198 }catch(InterruptedException e){ 199 e.printStackTrace(); 200 break; 201 } 202 } 203 } 204 }catch(Exception e){ 205 e.printStackTrace(); 206 } 207 } 208 209 static final void help(){ 210 System.out.println("Excepted argument: <propfile> "); 211 } 212 213 static void loadBootStrap(Registry reg, String bootstrap, Properties props) throws Exception { 214 String [] resources = bootstrap.split(","); 215 RegisSession session = null; 216 try{ 217 session = reg.open(); 218 Node node = reg.getRoot(); 219 if(reg instanceof Configurable){ 220 loadConfigurable((Configurable)reg, resources); 221 } 222 else if(node instanceof Configurable){ 223 loadConfigurable((Configurable)node, resources); 224 } 225 else{ 226 RWSession rw = (RWSession)session; 227 rw.begin(); 228 try{ 229 RegistryConfigLoader loader = new RegistryConfigLoader((RWNode)reg.getRoot()); 230 for(int i = 0; i < resources.length; i++){ 231 loader.load(Utils.load(RegistryServer.class, resources[i].trim()), props); 232 } 233 rw.commit(); 234 }catch(RuntimeException e){ 235 rw.rollback(); 236 throw e; 237 } 238 } 239 }catch(ClassCastException e){ 240 throw new IllegalStateException ("Registry does not support write operations; cannot update"); 241 }finally{ 242 if(session != null){ 243 session.close(); 244 } 245 } 246 } 247 248 private static void loadConfigurable(Configurable conf, String [] resources) throws Exception { 249 Path path = Path.parse(Node.ROOT_NAME); 250 for(int i = 0; i < resources.length; i++){ 251 String xmlConf = Utils.loadAsString(Utils.load(RegistryServer.class, resources[i].trim())); 252 conf.load(path, null, null, xmlConf, _bootstrapProps); 253 } 254 } 255 256 public static class ShutdownHook extends Thread { 257 public void run() { 258 try{ 259 Hub.shutdown(30000); 260 if(_exporter != null) 261 _exporter.close(); 262 }catch(InterruptedException e){ 263 e.printStackTrace(); 264 } 265 System.out.println("Stopped registry server."); 266 } 267 } 268 269 } 270 | Popular Tags |