📄 baseserver.java.svn-base
字号:
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.cldc.communication.midp;import java.io.IOException;import java.net.MalformedURLException;import java.net.ServerSocket;import java.net.URL;import java.util.ResourceBundle;public abstract class BaseServer { protected boolean done; protected int handlerCount = 5; protected Thread[] runners; protected boolean verbose = true; protected ServerSocket socket; protected String host; protected int port; protected int retry; protected static final String RETRY_AFTER = "retry.after="; protected static final String VERBOSE = "verbose="; protected static final String HTTP_PORT = "http.port="; protected static final String HTTP_URL = "http.url="; protected static final String TEST_URL = "test.url="; /** * This method must be called from every extending class. */ public void processArg(String arg) { if (arg.startsWith(RETRY_AFTER) && arg.length() > RETRY_AFTER.length()) { try { retry = Integer.parseInt(arg.substring(RETRY_AFTER.length())); } catch (NumberFormatException nfe) { throw new IllegalArgumentException(i18n.getString("error.retry") + arg.substring(RETRY_AFTER.length())); } } else if (arg.startsWith(VERBOSE) && arg.length() > VERBOSE.length()) { verbose = new Boolean(arg.substring(VERBOSE.length())).booleanValue(); } else if (arg.startsWith(HTTP_URL) && arg.length() > HTTP_URL.length()) { try { URL url = new URL(arg.substring(HTTP_URL.length())); host = url.getHost(); port = url.getPort(); } catch (MalformedURLException e) { throw new IllegalArgumentException(i18n.getString("error.url") + arg.substring(HTTP_URL.length())); } } else if (arg.startsWith(HTTP_PORT) && arg.length() > HTTP_PORT.length()) { try { port = Integer.parseInt(arg.substring(HTTP_PORT.length())); } catch (NumberFormatException nfe) { throw new IllegalArgumentException(i18n.getString("error.port") + arg.substring(HTTP_PORT.length())); } } else { throw new IllegalArgumentException(i18n.getString("error.argument") + arg); } } public void verboseln(String s) { if (verbose) { System.out.println(s); } } public void verbosetrace(Throwable t) { if (verbose) { t.printStackTrace(); } } public boolean isDone() { return done; } /** * Process input arguments and initialize necessary parts of the server * @param args the string array of input arguments */ public abstract void init(String[] args); /** * Starts the server. */ public synchronized void start() { if (!done) throw new IllegalThreadStateException(i18n.getString("error.httpState")); try { socket = new ServerSocket(port, 50); socket.setSoTimeout(5000); } catch (IOException ioe) { throw new RuntimeException(i18n.getString("error.socket") + port); } done = false; for (int i = 0; i < handlerCount; i++) { ((BaseHttpServer)runners[i]).setSocket(socket); runners[i].start(); } } public synchronized void stop() { if (done) return; done = true; // wait for 5 sec to let the handlers exit gracefully long current = System.currentTimeMillis(); for (int i = 0; i < handlerCount; i++) { if (runners[i].isAlive()) { try { runners[i].join(5000); } catch (InterruptedException x) {} if (System.currentTimeMillis() >= current + 5000) break; // timeout } } // it didn't do it - try to force it for (int i = 0; i < handlerCount; i++) { if (runners[i].isAlive()) { runners[i].interrupt(); } } try {socket.close();} catch (IOException x) {} } private static final ResourceBundle i18n = ResourceBundle.getBundle("com.sun.cldc.communication.midp.i18n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -