KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > admin > server > ZeroconfJoramServer


1 package org.objectweb.joram.client.jms.admin.server;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6
7 import fr.dyade.aaa.agent.AgentServer;
8
9 import org.objectweb.joram.client.jms.admin.*;
10 import org.objectweb.joram.mom.proxies.tcp.TcpProxyService;
11
12 /**
13  * This class starts a Joram server without almost any configuration.
14  * It just needs to know an existing Joram server (host, port) and
15  * the root login. The existing server is usually "s0" from the base configuration.
16  * These data are dpecified by 4 environment properties: ADMIN_HOST_NAME,
17  * ADMIN_PORT, ROOT_USER_NAME and ROOT_USER_PWD.
18  *
19  * This server uses the current directory to store some data. You can specify another
20  * directory with the property BASE_DIR_PATH.
21  *
22  * This new server is added into the first domain found in the Joram platform.
23  * If no domain exists, a first domain D0 is created. Notice that this bootstrap
24  * mechanism has been designed for a single domain platform. If you need to build
25  * more complex configuration with several domains you must use the raw Joram administration API.
26  *
27  */

28 public class ZeroconfJoramServer {
29
30   public static final String JavaDoc BASE_DIR_PATH = "org.objectweb.joram.zeroconf.baseDirPath";
31   
32   public static final String JavaDoc ADMIN_HOST_NAME = "org.objectweb.joram.zeroconf.adminHostName";
33   
34   public static final String JavaDoc ADMIN_PORT = "org.objectweb.joram.zeroconf.adminPort";
35
36   public static final String JavaDoc ROOT_USER_NAME = "org.objectweb.joram.zeroconf.rootUserName";
37
38   public static final String JavaDoc ROOT_USER_PWD = "org.objectweb.joram.zeroconf.rootUserPwd";
39
40   public final static String JavaDoc SERVER_ID = "sid";
41
42   public final static String JavaDoc JORAM_SERVER_DATA = "joramServerData";
43
44   public final static String JavaDoc A3_SERVERS_XML = "a3servers.xml";
45
46   private static String JavaDoc baseDirPath;
47
48   private static File baseDir;
49
50   private static String JavaDoc hostName;
51
52   private static String JavaDoc serverName;
53
54   private static void init() throws Exception JavaDoc {
55     baseDirPath = System.getProperty(BASE_DIR_PATH, ".");
56     baseDir = new File(baseDirPath);
57
58     hostName = InetAddress.getLocalHost().getHostName();
59     serverName = hostName + '/' + baseDir.getCanonicalPath();
60   }
61
62   private static void adminConnect() throws Exception JavaDoc {
63     String JavaDoc adminHostName = System.getProperty(ADMIN_HOST_NAME, "localhost");
64     int adminPort = Integer.getInteger(ADMIN_PORT, 16010).intValue();
65     String JavaDoc rootUserName = System.getProperty(ROOT_USER_NAME, "root");
66     String JavaDoc rootUserPwd = System.getProperty(ROOT_USER_PWD, "root");
67     AdminModule.connect(
68       adminHostName,
69       adminPort,
70       rootUserName,
71       rootUserPwd, 60);
72   }
73
74   /**
75    * Starts a Joram server without any configuration.
76    */

77   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
78     init();
79
80     int serverId;
81     try {
82       serverId = loadServerId();
83     } catch (IOException exc) {
84       baseDir.mkdir();
85
86       adminConnect();
87
88       int adminServerId = AdminModule.getLocalServerId();
89       String JavaDoc[] domainNames = AdminModule.getDomainNames(adminServerId);
90
91       String JavaDoc domainName;
92       if (domainNames.length > 0) {
93         domainName = domainNames[0];
94       } else {
95         domainName = "D0";
96         int initialDomainPort = 17000;
97         int tryNb = 50;
98         int i = 0;
99         Random random = new Random();
100         while (i < tryNb) {
101           try {
102             AdminModule.addDomain(
103               "D0", adminServerId,
104               initialDomainPort + random.nextInt(tryNb) * 100);
105             break;
106           } catch (NameAlreadyUsedException exc1) {
107             throw exc1;
108           } catch (StartFailureException exc2) {
109             // Try again with a new port
110
i++;
111           }
112         }
113       }
114       
115       ServerSocket sock1 = new ServerSocket(0);
116       int port1 = sock1.getLocalPort();
117       ServerSocket sock2 = new ServerSocket(0);
118       int port2 = sock2.getLocalPort();
119
120       serverId = newServerId();
121       
122       AdminModule.addServer(
123         serverId,
124         hostName,
125         domainName,
126         port1,
127         serverName,
128         new String JavaDoc[]{"org.objectweb.joram.mom.proxies.tcp.TcpProxyService"},
129         new String JavaDoc[]{"" + port2});
130       
131       String JavaDoc configXml = AdminModule.getConfiguration();
132       File sconfig = new File(baseDir, A3_SERVERS_XML);
133       FileOutputStream fos = new FileOutputStream(sconfig);
134       PrintWriter pw = new PrintWriter(fos);
135       pw.println(configXml);
136       pw.flush();
137       fos.getFD().sync();
138       pw.close();
139       fos.close();
140
141       // Release the ports
142
sock1.close();
143       sock2.close();
144     }
145     
146     System.getProperties().put(AgentServer.CFG_DIR_PROPERTY,
147                                baseDirPath);
148     fr.dyade.aaa.agent.AgentServer.init(
149       (short)serverId,
150       new File(baseDir, JORAM_SERVER_DATA).getPath(),
151       null);
152     fr.dyade.aaa.agent.AgentServer.start();
153     
154     org.objectweb.joram.client.jms.admin.User user =
155       org.objectweb.joram.client.jms.admin.User.create(
156         "anonymous", "anonymous", serverId);
157
158     AdminModule.disconnect();
159   }
160
161   private static int newServerId() throws Exception JavaDoc {
162     List serverIds = AdminModule.getServersIds();
163     int newSid = 0;
164     for (int i = 0; i < serverIds.size(); i++) {
165       Integer JavaDoc sid = (Integer JavaDoc)serverIds.get(i);
166       if (newSid <= sid.intValue()) newSid = sid.intValue() + 1;
167     }
168     saveServerId(newSid);
169     return newSid;
170   }
171
172   private static void saveServerId(int sid)
173     throws IOException {
174     FileOutputStream fos = new FileOutputStream(
175       new File(baseDir, SERVER_ID));
176     ObjectOutputStream oos = new ObjectOutputStream(fos);
177     oos.writeInt(sid);
178     oos.flush();
179     fos.flush();
180     fos.getFD().sync();
181     fos.close();
182   }
183
184   private static int loadServerId()
185     throws IOException {
186     FileInputStream fis = new FileInputStream(
187       new File(baseDir, SERVER_ID));
188     ObjectInputStream ois = new ObjectInputStream(fis);
189     int res = ois.readInt();
190     fis.close();
191     return res;
192   }
193
194   public final static int getTcpEntryPointPort() {
195     return TcpProxyService.getListenPort();
196   }
197
198   public static void stop() {
199     AgentServer.stop();
200   }
201
202   public static void destroy() throws Exception JavaDoc {
203     init();
204     if (baseDir.exists()) {
205       int serverId;
206
207       try {
208         serverId = loadServerId();
209       } catch (IOException exc) {
210         // Nothing to do
211
serverId = -1;
212       }
213       if (serverId > 0) {
214         adminConnect();
215
216         // Check that this server is the one registered
217
// with the identifier 'serverId'.
218
Server[] servers = AdminModule.getServers();
219         for (int i = 0; i < servers.length; i++) {
220           if (servers[i].getId() == serverId &&
221               servers[i].getName() == serverName &&
222               servers[i].getHostName() == hostName) {
223             try {
224               AdminModule.removeServer(serverId);
225             } catch (UnknownServerException exc) {
226               // Nothing to do
227
}
228           }
229         }
230       }
231       new File(baseDir, SERVER_ID).delete();
232       File serverDataDir = new File(baseDir, JORAM_SERVER_DATA);
233       if (serverDataDir.exists()) {
234         File[] files = serverDataDir.listFiles();
235         for (int i = 0; i < files.length; i++) {
236           files[i].delete();
237         }
238         serverDataDir.delete();
239       }
240       new File(baseDir, A3_SERVERS_XML).delete();
241     }
242   }
243 }
244
Popular Tags