⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otaserver.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal.overtheair;

import java.io.File;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Preferences;
import org.mortbay.http.HttpContext;
import org.mortbay.http.HttpListener;
import org.mortbay.http.HttpServer;
import org.mortbay.http.SocketListener;
import org.mortbay.http.handler.NotFoundHandler;
import org.mortbay.util.Log;
import org.mortbay.util.MultiException;
import org.mortbay.util.OutputStreamLogSink;

import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;

/**
 * Singleton management of the Over the Air HTTP Server.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.3 $
 * <br>
 * $Date: 2005/07/07 02:37:17 $
 * <br>
 * @author Craig Setera
 */
public class OTAServer {
	/** Singleton instance of the OTAServer */
	public static final OTAServer instance = new OTAServer();
	
	/**
	 * Return the port to be listened on for OTA connections.
	 * 
	 * @return
	 */
	public static int getPort() {
		HttpListener listener = instance.getHttpServer().getListeners()[0];
		return listener.getPort();
	}

	/**
	 * 
	 * @uml.property name="httpServer"
	 * @uml.associationEnd 
	 * @uml.property name="httpServer" multiplicity="(1 1)"
	 */
	// The HttpServer instance in use for serving the content
	private HttpServer httpServer;

	
	/**
	 * Private constructor
	 */
	private OTAServer() {
		super();
	}
	
	/**
	 * Start the HttpServer if it has not already been started.
	 * 
	 * @throws MultiException
	 */
	public void start() 
		throws MultiException 
	{
		HttpServer server = getHttpServer();
		if (!server.isStarted()) {
			server.start();
		}
	}
	
	/**
	 * Stop the HttpServer if it is currently running.
	 * 
	 * @throws InterruptedException
	 */
	public void stop() 
		throws InterruptedException 
	{
		HttpServer server = getHttpServer();
		if (server.isStarted()) {
			server.stop();
		}
	}
	
	/**
	 * Configure the context(s) for the HttpServer.
	 * 
	 * @param server
	 */
	private void configureContext(HttpServer server) {
		// Configure the context
		HttpContext context = new HttpContext();
		context.setContextPath("/ota/*");
		server.addContext(context);
		
		// Configure the handlers in the context
		context.addHandler(new OTAHandler());
		context.addHandler(new NotFoundHandler());	
	}

	/**
	 * Configure the HttpServer listener.
	 * 
	 * @param server
	 */
	private void configureListener(HttpServer server) {
		SocketListener listener = new SocketListener();
		listener.setMinThreads(1);
		listener.setMaxThreads(2);
		listener.setMaxIdleTimeMs(10000);
		listener.setDaemon(true);

		Preferences prefs = EclipseMECorePlugin.getDefault().getPluginPreferences(); 
		if (prefs.getBoolean(IEclipseMECoreConstants.PREF_OTA_PORT_DEFINED)) {
			int specifiedPortNumber = prefs.getInt(IEclipseMECoreConstants.PREF_OTA_PORT);
			listener.setPort(specifiedPortNumber);		
		}
		
		server.addListener(listener);
	}

	/**
	 * Configure the Jetty logging support so that it writes
	 * out to our metadata directory.
	 */
	private void configureLogging() 
		throws CoreException 
	{
		// Set some System properties so that they are available
		// when creating the logging
		System.setProperty("LOG_FILE_RETAIN_DAYS", "5");
		
		// Calculate the name of the file to be used for logging
		IPath stateLocation = EclipseMECorePlugin.getDefault().getStateLocation();
		IPath logDirectory = stateLocation.append("jetty");
		
		File logDirectoryFile = logDirectory.toFile();
		if (!logDirectoryFile.exists()) {
			logDirectoryFile.mkdir();
		}

		File logFile = new File(logDirectoryFile, "logfile");
		String filename = logFile + "yyyy_mm_dd.txt";
		
		// Create the output sink and add it to the logging class
		OutputStreamLogSink sink = new OutputStreamLogSink(filename);
		sink.start();
		Log.instance().add(sink);
	}

	/**
	 * Create and configure the HTTP server instance.
	 * 
	 * @return
	 */
	private HttpServer createHttpServer() {
		HttpServer server = new HttpServer();

		configureListener(server);
		configureContext(server);
		
		return server;
	}

	/**
	 * Get the HttpServer instance.
	 * 
	 * @return
	 * 
	 * @uml.property name="httpServer"
	 */
	private HttpServer getHttpServer() {
		if (httpServer == null) {
			httpServer = createHttpServer();
			try {
				configureLogging();
			} catch (CoreException e) {
				EclipseMECorePlugin.log(IStatus.WARNING, "configureLogging", e);
			}
		}

		return httpServer;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -