1 package org.jgroups.service; 2 3 import org.jgroups.Channel; 4 import org.jgroups.JChannel; 5 6 import java.util.ResourceBundle ; 7 8 38 public class ServiceRunner { 39 public static final String SERVICE_CLASS="serviceClass"; 40 41 public static final String SERVICE_CHANNEL_STACK="serviceChannel"; 42 43 public static final String SERVICE_GROUP_NAME="serviceGroup"; 44 45 public static final String CLIENT_CHANNEL_STACK="clientChannel"; 46 47 public static final String CLIENT_GROUP_NAME="clientGroup"; 48 49 public static final String HELP_SWITCH="-help"; 50 51 public static final String RESOURCE_SWITCH="-res"; 52 53 59 public static void startService(ResourceBundle res) throws Exception { 60 61 String className=res.getString(SERVICE_CLASS); 62 63 if(className == null || "".equals(className)) { 64 System.out.println("Specified resource does not contain service class name"); 65 System.exit(1); 66 } 67 68 Class serviceClass=Class.forName(className); 69 70 if(!AbstractService.class.isAssignableFrom(serviceClass)) { 71 System.out.println("Specified service class is not instance of " + 72 AbstractService.class.getName()); 73 74 System.exit(1); 75 } 76 77 String serviceChannelStack=res.getString(SERVICE_CHANNEL_STACK); 78 final JChannel svcChannel=new JChannel(serviceChannelStack); 79 svcChannel.setOpt(Channel.GET_STATE_EVENTS, Boolean.TRUE); 80 81 String clientChannelStack=res.getString(CLIENT_CHANNEL_STACK); 82 83 final JChannel clientChannel=new JChannel(clientChannelStack); 84 85 String svcGroup=res.getString(SERVICE_GROUP_NAME); 86 String clientGroup=res.getString(CLIENT_GROUP_NAME); 87 88 svcChannel.connect(svcGroup); 89 clientChannel.connect(clientGroup); 90 91 java.lang.reflect.Constructor serviceConstructor= 92 serviceClass.getConstructor(new Class []{Channel.class, Channel.class}); 93 94 final AbstractService service=(AbstractService) 95 serviceConstructor.newInstance(new Object []{svcChannel, clientChannel}); 96 97 service.start(); 98 99 Runnable shutdownHook=new Runnable () { 100 public void run() { 101 System.out.println("Shutting down service " + service.getName()); 102 103 service.stop(); 104 svcChannel.close(); 105 clientChannel.close(); 106 107 System.out.println("Done."); 108 109 } 110 }; 111 112 Thread shutdownThread=new Thread (shutdownHook, service.getName() + 113 " shutdown hook [" + service.getAddress() + ']'); 114 shutdownThread.setDaemon(true); 115 116 Runtime.getRuntime().addShutdownHook(shutdownThread); 117 118 System.out.println("Service '" + service.getName() + "' is up'n'running"); 119 } 120 121 124 public static void main(String [] args) throws Exception { 125 if(args.length == 0) { 126 printUsage(); 127 System.exit(0); 128 } 129 130 String resourceName=null; 131 132 for(int i=0; i < args.length; i++) { 133 if(HELP_SWITCH.equals(args[i])) { 134 printUsage(); 135 System.exit(0); 136 } 137 else 138 if(RESOURCE_SWITCH.equals(args[i])) { 139 resourceName=args[++i]; 140 } 141 } 142 143 if(resourceName == null) { 144 printUsage(); 145 System.exit(0); 146 } 147 148 ResourceBundle res=ResourceBundle.getBundle(resourceName); 149 150 startService(res); 151 } 152 153 156 private static void printUsage() { 157 System.out.println(); 158 System.out.println("Usage: java org.jgroups.service.ServiceRunner -res <service_desc_res>"); 159 160 System.out.println("<service_desc_res> -\tservice description resource,"); 161 System.out.println("\t\t\tstandard properties file containing information"); 162 System.out.println("\t\t\tabout service to run."); 163 } 164 } 165 | Popular Tags |