KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > service > ServiceRunner


1 package org.jgroups.service;
2
3 import org.jgroups.Channel;
4 import org.jgroups.JChannel;
5
6 import java.util.ResourceBundle JavaDoc;
7
8 /**
9  * <code>ServiceRunner</code> is utility class that runs services in standalone
10  * mode. Each service is described with resource file containing following
11  * properties:
12  * <p/>
13  * <ul>
14  * <li><code>serviceClass</code> - class name of service to run. Each service
15  * must define public constructor that takes two parameters, instances of
16  * {@link org.jgroups.Channel} class;
17  * <p/>
18  * <li><code>serviceChannel</code> - string description of protocol stack for
19  * inter-service communication channel;
20  * <li><code>serviceGroup</code> - group name of inter-service communication
21  * channel;
22  * <p/>
23  * <li><code>clientChannel</code> - protocol stack for client communication
24  * channel;
25  * <p/>
26  * <li><code>clientGroup</code> - group name of client communication channel.
27  * </ul>
28  * <p/>
29  * Class can be started from command line using:
30  * <pre>
31  * java org.jgroups.service.ServiceRunner -res <res_name>
32  * </pre>
33  * where <code>res_name</code> is name of the resource describing service to
34  * run in form acceptable by {@link java.util.ResourceBundle} class.
35  *
36  * @author Roman Rokytskyy (rrokytskyy@acm.org)
37  */

38 public class ServiceRunner {
39     public static final String JavaDoc SERVICE_CLASS="serviceClass";
40
41     public static final String JavaDoc SERVICE_CHANNEL_STACK="serviceChannel";
42
43     public static final String JavaDoc SERVICE_GROUP_NAME="serviceGroup";
44
45     public static final String JavaDoc CLIENT_CHANNEL_STACK="clientChannel";
46
47     public static final String JavaDoc CLIENT_GROUP_NAME="clientGroup";
48
49     public static final String JavaDoc HELP_SWITCH="-help";
50
51     public static final String JavaDoc RESOURCE_SWITCH="-res";
52
53     /**
54      * Method to start service. This method extracts parameters from
55      * specified resource, creates instance of service and starts it.
56      *
57      * @param res resource bundle containing information about resource.
58      */

59     public static void startService(ResourceBundle JavaDoc res) throws Exception JavaDoc {
60
61         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc clientChannelStack=res.getString(CLIENT_CHANNEL_STACK);
82
83         final JChannel clientChannel=new JChannel(clientChannelStack);
84
85         String JavaDoc svcGroup=res.getString(SERVICE_GROUP_NAME);
86         String JavaDoc clientGroup=res.getString(CLIENT_GROUP_NAME);
87
88         svcChannel.connect(svcGroup);
89         clientChannel.connect(clientGroup);
90
91         java.lang.reflect.Constructor JavaDoc serviceConstructor=
92                 serviceClass.getConstructor(new Class JavaDoc[]{Channel.class, Channel.class});
93
94         final AbstractService service=(AbstractService)
95                 serviceConstructor.newInstance(new Object JavaDoc[]{svcChannel, clientChannel});
96
97         service.start();
98
99         Runnable JavaDoc shutdownHook=new Runnable JavaDoc() {
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 JavaDoc shutdownThread=new Thread JavaDoc(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     /**
122      * Main entry to run this class.
123      */

124     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
125         if(args.length == 0) {
126             printUsage();
127             System.exit(0);
128         }
129
130         String JavaDoc 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 JavaDoc res=ResourceBundle.getBundle(resourceName);
149
150         startService(res);
151     }
152
153     /**
154      * Print usage of this class
155      */

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