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 28 public class ZeroconfJoramServer { 29 30 public static final String BASE_DIR_PATH = "org.objectweb.joram.zeroconf.baseDirPath"; 31 32 public static final String ADMIN_HOST_NAME = "org.objectweb.joram.zeroconf.adminHostName"; 33 34 public static final String ADMIN_PORT = "org.objectweb.joram.zeroconf.adminPort"; 35 36 public static final String ROOT_USER_NAME = "org.objectweb.joram.zeroconf.rootUserName"; 37 38 public static final String ROOT_USER_PWD = "org.objectweb.joram.zeroconf.rootUserPwd"; 39 40 public final static String SERVER_ID = "sid"; 41 42 public final static String JORAM_SERVER_DATA = "joramServerData"; 43 44 public final static String A3_SERVERS_XML = "a3servers.xml"; 45 46 private static String baseDirPath; 47 48 private static File baseDir; 49 50 private static String hostName; 51 52 private static String serverName; 53 54 private static void init() throws Exception { 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 { 63 String adminHostName = System.getProperty(ADMIN_HOST_NAME, "localhost"); 64 int adminPort = Integer.getInteger(ADMIN_PORT, 16010).intValue(); 65 String rootUserName = System.getProperty(ROOT_USER_NAME, "root"); 66 String rootUserPwd = System.getProperty(ROOT_USER_PWD, "root"); 67 AdminModule.connect( 68 adminHostName, 69 adminPort, 70 rootUserName, 71 rootUserPwd, 60); 72 } 73 74 77 public static void main(String [] args) throws Exception { 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 [] domainNames = AdminModule.getDomainNames(adminServerId); 90 91 String 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 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 []{"org.objectweb.joram.mom.proxies.tcp.TcpProxyService"}, 129 new String []{"" + port2}); 130 131 String 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 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 { 162 List serverIds = AdminModule.getServersIds(); 163 int newSid = 0; 164 for (int i = 0; i < serverIds.size(); i++) { 165 Integer sid = (Integer )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 { 203 init(); 204 if (baseDir.exists()) { 205 int serverId; 206 207 try { 208 serverId = loadServerId(); 209 } catch (IOException exc) { 210 serverId = -1; 212 } 213 if (serverId > 0) { 214 adminConnect(); 215 216 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 } 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 |