1 16 17 package org.apache.axis.transport.http; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.components.threadpool.ThreadPool; 21 import org.apache.axis.server.AxisServer; 22 import org.apache.axis.session.Session; 23 import org.apache.axis.session.SimpleSession; 24 import org.apache.axis.utils.Messages; 25 import org.apache.axis.utils.Options; 26 import org.apache.axis.utils.NetworkUtils; 27 import org.apache.axis.collections.LRUMap; 28 import org.apache.axis.EngineConfiguration; 29 import org.apache.axis.management.ServiceAdmin; 30 import org.apache.axis.configuration.EngineConfigurationFactoryFinder; 31 import org.apache.commons.logging.Log; 32 33 import java.net.MalformedURLException ; 34 import java.net.ServerSocket ; 35 import java.net.Socket ; 36 import java.util.Map ; 37 import java.io.IOException ; 38 import java.io.File ; 39 40 53 public class SimpleAxisServer implements Runnable { 54 protected static Log log = 55 LogFactory.getLog(SimpleAxisServer.class.getName()); 56 57 private Map sessions; 63 private int maxSessions; 65 public static final int MAX_SESSIONS_DEFAULT = 100; 66 67 71 public static ThreadPool getPool() { 72 return pool; 73 } 74 75 78 private static ThreadPool pool; 79 80 82 private static boolean doThreads = true; 83 84 87 private static boolean doSessions = true; 88 89 92 public SimpleAxisServer() { 93 this(ThreadPool.DEFAULT_MAX_THREADS); 94 } 95 96 101 public SimpleAxisServer(int maxPoolSize) { 102 this(maxPoolSize, MAX_SESSIONS_DEFAULT); 103 } 104 105 110 public SimpleAxisServer(int maxPoolSize, int maxSessions) { 111 this.maxSessions = maxSessions; 112 sessions = new LRUMap(maxSessions); 113 pool = new ThreadPool(maxPoolSize); 114 } 115 116 117 121 protected void finalize() throws Throwable { 122 stop(); 123 super.finalize(); 124 } 125 126 130 public int getMaxSessions() { 131 return maxSessions; 132 } 133 134 138 public void setMaxSessions(int maxSessions) { 139 this.maxSessions = maxSessions; 140 ((LRUMap)sessions).setMaximumSize(maxSessions); 141 } 142 144 protected boolean isSessionUsed() { 145 return doSessions; 146 } 147 148 152 public void setDoThreads(boolean value) { 153 doThreads = value ; 154 } 155 156 public boolean getDoThreads() { 157 return doThreads ; 158 } 159 160 public EngineConfiguration getMyConfig() { 161 return myConfig; 162 } 163 164 public void setMyConfig(EngineConfiguration myConfig) { 165 this.myConfig = myConfig; 166 } 167 168 173 protected Session createSession(String cooky) { 174 175 Session session = null; 177 if (sessions.containsKey(cooky)) { 178 session = (Session) sessions.get(cooky); 179 } else { 180 session = new SimpleSession(); 182 183 sessions.put(cooky, session); 185 } 186 return session; 187 } 188 189 public static int sessionIndex = 0; 193 194 private static AxisServer myAxisServer = null; 196 197 private EngineConfiguration myConfig = null; 198 199 205 public synchronized AxisServer getAxisServer() { 206 if (myAxisServer == null) { 207 if (myConfig == null) { 208 myConfig = EngineConfigurationFactoryFinder.newFactory().getServerEngineConfig(); 209 } 210 myAxisServer = new AxisServer(myConfig); 211 ServiceAdmin.setEngine(myAxisServer, NetworkUtils.getLocalHostname() + "@" + serverSocket.getLocalPort()); 212 } 213 return myAxisServer; 214 } 215 216 220 private boolean stopped = false; 221 222 226 public void run() { 227 log.info(Messages.getMessage("start01", "SimpleAxisServer", 228 new Integer (getServerSocket().getLocalPort()).toString(),getCurrentDirectory())); 229 230 while (!stopped) { 232 Socket socket = null; 233 try { 234 socket = serverSocket.accept(); 235 } catch (java.io.InterruptedIOException iie) { 236 } catch (Exception e) { 237 log.debug(Messages.getMessage("exception00"), e); 238 break; 239 } 240 if (socket != null) { 241 SimpleAxisWorker worker = new SimpleAxisWorker(this, socket); 242 if (doThreads) { 243 pool.addWorker(worker); 244 } else { 245 worker.run(); 246 } 247 } 248 } 249 log.info(Messages.getMessage("quit00", "SimpleAxisServer")); 250 } 251 252 256 private String getCurrentDirectory() { 257 return System.getProperty("user.dir"); 258 } 259 260 263 264 private ServerSocket serverSocket; 265 266 269 public ServerSocket getServerSocket() { 270 return serverSocket; 271 } 272 273 278 public void setServerSocket(ServerSocket serverSocket) { 279 this.serverSocket = serverSocket; 280 } 281 282 289 public void start(boolean daemon) throws Exception { 290 stopped=false; 291 if (doThreads) { 292 Thread thread = new Thread (this); 293 thread.setDaemon(daemon); 294 thread.start(); 295 } else { 296 run(); 297 } 298 } 299 300 303 public void start() throws Exception { 304 start(false); 305 } 306 307 313 public void stop() { 314 if(stopped ) { 316 return; 317 } 318 322 stopped = true; 323 324 try { 325 if(serverSocket != null) { 326 serverSocket.close(); 327 } 328 } catch (IOException e) { 329 log.info(Messages.getMessage("exception00"), e); 330 } finally { 331 serverSocket=null; 332 } 333 334 log.info(Messages.getMessage("quit00", "SimpleAxisServer")); 335 336 pool.shutdown(); 338 } 339 340 343 public static void main(String args[]) { 344 345 Options opts = null; 346 try { 347 opts = new Options(args); 348 } catch (MalformedURLException e) { 349 log.error(Messages.getMessage("malformedURLException00"), e); 350 return; 351 } 352 353 String maxPoolSize = opts.isValueSet('t'); 354 if (maxPoolSize==null) maxPoolSize = ThreadPool.DEFAULT_MAX_THREADS + ""; 355 356 String maxSessions = opts.isValueSet('m'); 357 if (maxSessions==null) maxSessions = MAX_SESSIONS_DEFAULT + ""; 358 359 SimpleAxisServer sas = new SimpleAxisServer(Integer.parseInt(maxPoolSize), 360 Integer.parseInt(maxSessions)); 361 362 try { 363 doThreads = (opts.isFlagSet('t') > 0); 364 365 int port = opts.getPort(); 366 ServerSocket ss = null; 367 final int retries = 5; 369 for (int i = 0; i < retries; i++) { 370 try { 371 ss = new ServerSocket (port); 372 break; 373 } catch (java.net.BindException be){ 374 log.debug(Messages.getMessage("exception00"), be); 375 if (i < (retries-1)) { 376 Thread.sleep(3000); 378 } else { 379 throw new Exception (Messages.getMessage("unableToStartServer00", 380 Integer.toString(port))); 381 } 382 } 383 } 384 sas.setServerSocket(ss); 385 sas.start(); 386 } catch (Exception e) { 387 log.error(Messages.getMessage("exception00"), e); 388 return; 389 } 390 } 391 } 392 | Popular Tags |