1 28 package org.objectweb.carol.jtests.conform.basic.server; 29 30 import java.io.IOException ; 31 import java.net.ServerSocket ; 32 import java.net.Socket ; 33 import java.util.Arrays ; 34 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 39 import org.objectweb.carol.util.configuration.ConfigurationException; 40 import org.objectweb.carol.util.configuration.ConfigurationRepository; 41 42 46 public class BasicServer { 47 48 51 private static String basicObjectName = "basicname"; 52 53 56 private static String basicMultiObjectName = "basicmultiname"; 57 58 61 private static String basicObjectRefName = "basicrefname"; 62 63 66 private Context ic = null; 67 68 71 private BasicObjectItf ba; 72 73 76 private BasicMultiObjectItf bma; 77 78 81 private BasicObjectRef bref; 82 83 86 private int port; 87 88 91 private boolean startedSuccessfully; 92 93 97 public static void main(String [] args) { 98 if (args.length != 1) { 99 throw new IllegalArgumentException ("expected the port number, but got: " + Arrays.asList(args)); 100 } 101 new BasicServer(Integer.parseInt(args[0])).advertiseReadiness(); 102 } 103 104 108 private BasicServer(int port) { 109 this.port = port; 110 startedSuccessfully = true; 111 112 try { 113 ConfigurationRepository.init(); 114 } catch (ConfigurationException ex) { 115 System.err.println("carol is misconfigured"); 116 ex.printStackTrace(); 117 startedSuccessfully = false; 118 } 119 120 if (startedSuccessfully) { 121 try { 122 ba = new BasicObject(); 123 bma = new BasicMultiObject(); 124 bref = new BasicObjectRef("string"); 125 } catch (Exception ex) { 126 System.err.println("error creating basic objects"); 127 ex.printStackTrace(); 128 startedSuccessfully = false; 129 } 130 } 131 if (startedSuccessfully) { 132 try { 133 ic = new InitialContext (); 134 ic.rebind(basicObjectName, ba); 135 ic.rebind(basicMultiObjectName, bma); 136 ic.rebind(basicObjectRefName, bref); 137 } catch (NamingException ex) { 138 ex.printStackTrace(); 139 } 140 } 141 } 142 143 152 private void advertiseReadiness() { 153 ServerSocket serverSocket; 154 try { 155 serverSocket = new ServerSocket (port); 156 } catch (IOException ex) { 157 throw new RuntimeException ("Couldn't bind to " + port); 158 } 159 160 while (true) { 161 try { 162 Socket socket = serverSocket.accept(); 163 socket.close(); 164 } catch (IOException ex) { 165 throw new RuntimeException ("Error accepting a connection", ex); 166 } 167 } 168 } 169 } | Popular Tags |