[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[dsdp-mtj-commit] r1534 - in trunk/plugins/org.eclipse.mtj.core: META-INF src/org/eclipse/mtj/internal/core/launching/midp/ota

Author: gercan
Date: 2009-12-03 09:24:29 -0500 (Thu, 03 Dec 2009)
New Revision: 1534

Modified:
   trunk/plugins/org.eclipse.mtj.core/META-INF/MANIFEST.MF
   trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAHandler.java
   trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAServer.java
Log:
[281658] Move Jetty dependency to the platform level. OTAServer and OTAHandler are modified to use the Jetty6 level APIs. This removes the dependency to Jetty5.


Modified: trunk/plugins/org.eclipse.mtj.core/META-INF/MANIFEST.MF
===================================================================
--- trunk/plugins/org.eclipse.mtj.core/META-INF/MANIFEST.MF	2009-12-03 13:52:58 UTC (rev 1533)
+++ trunk/plugins/org.eclipse.mtj.core/META-INF/MANIFEST.MF	2009-12-03 14:24:29 UTC (rev 1534)
@@ -2,7 +2,7 @@
 Generated-from: 1120700250546;type=2
 Bundle-Name: %Bundle.Name
 Bundle-SymbolicName: org.eclipse.mtj.core;singleton:=true
-Bundle-Version: 1.2.0.qualifier
+Bundle-Version: 1.2.1.qualifier
 Bundle-ClassPath: .
 Bundle-Activator: org.eclipse.mtj.core.MTJCore
 Bundle-Vendor: %Bundle.Vendor
@@ -15,11 +15,13 @@
  org.eclipse.text,
  org.objectweb.asm,
  org.eclipse.mtj.core.hooks,
- org.mortbay.jetty;version="[5.1.11,5.1.14]",
  de.schlichtherle.truezip,
  antenna.preprocessor.v3,
  com.ibm.icu,
- org.eclipse.equinox.security
+ org.eclipse.equinox.security,
+ org.mortbay.jetty.server;bundle-version="[6.1.0,7.0.0)",
+ javax.servlet;bundle-version="[2.5.0,3.0.0)",
+ org.mortbay.jetty.util;bundle-version="[6.1.0,7.0.0)"
 Bundle-ActivationPolicy: lazy
 Bundle-ManifestVersion: 2
 Bundle-RequiredExecutionEnvironment: J2SE-1.5

Modified: trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAHandler.java
===================================================================
--- trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAHandler.java	2009-12-03 13:52:58 UTC (rev 1533)
+++ trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAHandler.java	2009-12-03 14:24:29 UTC (rev 1534)
@@ -1,19 +1,20 @@
 /**
  * Copyright (c) 2003, 2009 Craig Setera and others.
- * 
+ *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *     Craig Setera (EclipseME) - Initial implementation
  *     Diego Sandin (Motorola)  - Refactoring package name to follow
  *                                eclipse standards. Added serialVersionUID.
- *     Diego Sandin (Motorola)  - Adapting code to use Jetty version 5.1.11 
+ *     Diego Sandin (Motorola)  - Adapting code to use Jetty version 5.1.11
  *     Diego Sandin (Motorola)  - Fix problem with OTA deployment after multiple
  *                                MTJRuntime feature inclusion.
- *     Diego Sandin (Motorola)  - Fix problem with OTA deployment.                           
+ *     Diego Sandin (Motorola)  - Fix problem with OTA deployment.
+ *     Gorkem Ercan (Nokia)     - [281658]Move Jetty dependency to the latest
  */
 package org.eclipse.mtj.internal.core.launching.midp.ota;
 
@@ -25,6 +26,10 @@
 import java.util.Map;
 import java.util.StringTokenizer;
 
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
@@ -35,19 +40,20 @@
 import org.eclipse.mtj.core.MTJCore;
 import org.eclipse.mtj.internal.core.IMTJCoreConstants;
 import org.eclipse.mtj.internal.core.util.Utils;
-import org.mortbay.http.HttpException;
-import org.mortbay.http.HttpRequest;
-import org.mortbay.http.HttpResponse;
-import org.mortbay.http.handler.AbstractHttpHandler;
+import org.mortbay.jetty.MimeTypes;
+import org.mortbay.jetty.handler.AbstractHandler;
+import org.mortbay.jetty.handler.ContextHandler;
 
 /**
  * Jetty HttpHandler implementation for the OTA implementation.
- * 
+ *
  * @author Craig Setera
  */
-public class OTAHandler extends AbstractHttpHandler {
+public class OTAHandler extends AbstractHandler {
 
-    // Internal map of the known MIME types
+    private static final String OTA_CONTEXT_PATH = "/ota"; //$NON-NLS-1$
+
+	// Internal map of the known MIME types
     private static final String[] KNOWN_MIME_TYPES = new String[] { "jad", //$NON-NLS-1$
             "text/vnd.sun.j2me.app-descriptor", "jar", //$NON-NLS-1$ //$NON-NLS-2$
             "application/java-archive" }; //$NON-NLS-1$
@@ -81,27 +87,15 @@
      * Default constructor
      */
     public OTAHandler() {
-        super();
+    	super();
     }
 
-    /* (non-Javadoc)
-     * @see org.mortbay.http.handler.AbstractHttpHandler#getName()
-     */
-    @Override
-    public String getName() {
-        return "MTJ Over the Air Handler"; //$NON-NLS-1$
-    }
-
-    /* (non-Javadoc)
-     * @see org.mortbay.http.HttpHandler#handle(java.lang.String, java.lang.String, org.mortbay.http.HttpRequest, org.mortbay.http.HttpResponse)
-     */
-    public void handle(String pathInContext, String pathParams,
-            HttpRequest request, HttpResponse response) throws HttpException,
+    public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws ServletException,
             IOException {
         String project = null;
         String file = null;
 
-        StringTokenizer st = new StringTokenizer(pathInContext, "/"); //$NON-NLS-1$
+        StringTokenizer st = new StringTokenizer(target, "/"); //$NON-NLS-1$
         if (st.hasMoreTokens()) {
             project = st.nextToken();
         }
@@ -111,7 +105,7 @@
 
         if ((project != null) && (file != null)) {
             try {
-                handle(project, file, pathParams, request, response);
+                handle(project, file, target, request, response);
             } catch (CoreException e) {
                 throw new IOException(e.getMessage());
             }
@@ -120,7 +114,7 @@
 
     /**
      * Get the appropriate content encoding for the specified file.
-     * 
+     *
      * @param requestedFile
      * @return
      */
@@ -143,7 +137,7 @@
     /**
      * Return the IJavaProject for the specified name or <code>null</code> if no
      * such project is found.
-     * 
+     *
      * @param projectName
      * @return
      */
@@ -162,10 +156,10 @@
 
     /**
      * Handle sending the specified file in the specified Eclipse Java Project.
-     * 
+     *
      * This handle tries to find the jad file in the current active MTJRuntime
      * deploy folder.
-     * 
+     *
      * @param javaProject the project from where the jad file must be searched
      * @param file the jad file name
      * @param pathParams
@@ -175,7 +169,7 @@
      * @throws IOException
      */
     private void handle(IJavaProject javaProject, String file,
-            String pathParams, HttpRequest request, HttpResponse response)
+            String pathParams, HttpServletRequest request, HttpServletResponse response)
             throws CoreException, IOException {
         String deployableJadFolderName = IMTJCoreConstants.TEMP_FOLDER_NAME
                 + File.separator + IMTJCoreConstants.EMULATION_FOLDER_NAME;
@@ -193,7 +187,7 @@
 
     /**
      * Handle sending the specified file in the specified Eclipse project.
-     * 
+     *
      * @param project
      * @param file
      * @param pathParams
@@ -203,7 +197,7 @@
      * @throws IOException
      */
     private void handle(String project, String file, String pathParams,
-            HttpRequest request, HttpResponse response) throws CoreException,
+            HttpServletRequest request, HttpServletResponse response) throws CoreException,
             IOException {
         IJavaProject javaProject = getJavaProject(project);
         if (javaProject != null) {
@@ -213,20 +207,20 @@
 
     /**
      * Send the specified file back to the caller.
-     * 
+     *
      * @param requestedFile
      * @param response
      * @throws CoreException
      * @throws IOException
      */
-    private void sendFile(IFile requestedFile, HttpResponse response)
+    private void sendFile(IFile requestedFile, HttpServletResponse response)
             throws CoreException, IOException {
         String contentType = getContentType(requestedFile);
         String contentEncoding = getContentEncoding(requestedFile);
 
         File javaFile = requestedFile.getLocation().toFile();
 
-        response.setCharacterEncoding(contentEncoding, true);
+        response.setCharacterEncoding(contentEncoding);
         response.setContentType(contentType);
         response.setContentLength((int) javaFile.length());
 
@@ -236,6 +230,17 @@
         os.close();
         is.close();
 
-        response.commit();
+        response.flushBuffer();
     }
+
+    static ContextHandler getContextHandler(){
+        ContextHandler ctxHandler = new ContextHandler(OTAHandler.OTA_CONTEXT_PATH);
+        ctxHandler.setHandler(new OTAHandler());
+        MimeTypes types =new MimeTypes();
+        types.setMimeMap(getMimeTypeMap());
+        ctxHandler.setMimeTypes(types);
+        return ctxHandler;
+
+    }
+
 }

Modified: trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAServer.java
===================================================================
--- trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAServer.java	2009-12-03 13:52:58 UTC (rev 1533)
+++ trunk/plugins/org.eclipse.mtj.core/src/org/eclipse/mtj/internal/core/launching/midp/ota/OTAServer.java	2009-12-03 14:24:29 UTC (rev 1534)
@@ -1,39 +1,36 @@
 /**
  * Copyright (c) 2003,2009 Craig Setera and others.
- * 
+ *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *     Craig Setera (EclipseME) - Initial implementation
  *     Diego Sandin (Motorola)  - Refactoring package name to follow
  *                                eclipse standards
- *     Diego Sandin (Motorola)  - Adapting code to use Jetty version 5.1.11 
- *     Diego Sandin (Motorola)  - Fixed NPE [Bug 267668]                               
+ *     Diego Sandin (Motorola)  - Adapting code to use Jetty version 5.1.11
+ *     Diego Sandin (Motorola)  - Fixed NPE [Bug 267668]
+ *     Gorkem Ercan (Nokia)     - [281658] Move Jetty dependency to the latest
  */
 package org.eclipse.mtj.internal.core.launching.midp.ota;
 
 import java.io.File;
 
 import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Preferences;
+import org.eclipse.core.runtime.Platform;
 import org.eclipse.mtj.core.MTJCore;
 import org.eclipse.mtj.internal.core.IMTJCoreConstants;
-import org.eclipse.mtj.internal.core.util.log.MTJLogger;
-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.log.LogImpl;
-import org.mortbay.log.OutputStreamLogSink;
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.NCSARequestLog;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.bio.SocketConnector;
 
+
 /**
  * Singleton management of the Over the Air HTTP Server.
- * 
+ *
  * @author Craig Setera
  */
 public class OTAServer {
@@ -45,7 +42,7 @@
 
     /**
      * Return the unique instance of the OTAServer
-     * 
+     *
      * @return a unique OTAServer instance.
      */
     public static synchronized OTAServer getInstance() {
@@ -57,33 +54,27 @@
 
     /**
      * Return the port to be listened on for OTA connections.
-     * 
+     *
      * @return
      */
     public static synchronized int getPort() {
-        HttpListener listener = getInstance().getHttpServer().getListeners()[0];
-        return listener.getPort();
+       Connector connector = getInstance().getHttpServer().getConnectors()[0];
+        return connector.getLocalPort();
     }
 
     // The HttpServer instance in use for serving the content
-    private HttpServer httpServer;
+    private Server httpServer;
 
     /**
-     * Instance of Log
-     */
-    private LogImpl logImpl;
-
-    /**
      * The HTTP Server instance
      */
-    private HttpServer server;
+    private Server server;
 
     /**
      * Creates a new instance of OTAServer.
      */
     private OTAServer() {
         super();
-        logImpl = new LogImpl();
         server = getHttpServer();
     }
 
@@ -91,72 +82,25 @@
      * @throws Exception
      */
     public synchronized void start() throws Exception {
-        if (!server.isStarted()) {
+        if (server != null && !server.isStarted()) {
             server.start();
         }
     }
 
     /**
      * Stop the HttpServer if it is currently running.
-     * 
-     * @throws InterruptedException
+     * @throws Exception
      */
-    public synchronized void stop() throws InterruptedException {
-        HttpServer server = getHttpServer();
-        if (server.isStarted()) {
+    public synchronized void stop() throws Exception {
+        if (server!= null && server.isStarted()) {
             server.stop();
         }
     }
 
     /**
-     * Configure the context(s) for the HttpServer.
-     * 
-     * @param server
+     * Calculate a filename to direct Jetty logging
      */
-    private void configureContext(HttpServer server) {
-        // Configure the context
-        HttpContext context = new HttpContext();
-        context.setContextPath("/ota/*"); //$NON-NLS-1$
-        server.addContext(context);
-
-        // Configure the handlers in the context
-        context.addHandler(new OTAHandler());
-        context.addHandler(new NotFoundHandler());
-    }
-
-    /**
-     * Configure the HttpServer listener.
-     * 
-     * @param server the HTTP Server.
-     */
-    private void configureListener(HttpServer server) {
-        SocketListener listener = new SocketListener();
-        listener.setMinThreads(1);
-        listener.setMaxThreads(2);
-        listener.setMaxIdleTimeMs(10000);
-        listener.setDaemon(true);
-
-        Preferences prefs = MTJCore.getMTJCore().getPluginPreferences();
-        if (prefs.getBoolean(IMTJCoreConstants.PREF_OTA_PORT_DEFINED)) {
-            int specifiedPortNumber = prefs
-                    .getInt(IMTJCoreConstants.PREF_OTA_PORT);
-            listener.setPort(specifiedPortNumber);
-        }
-
-        server.addListener(listener);
-    }
-
-    /**
-     * Configure the Jetty logging support so that it writes out to our metadata
-     * directory.
-     * 
-     * @throws Exception
-     */
-    private void configureLogging() throws Exception {
-        // Set some System properties so that they are available
-        // when creating the logging
-        System.setProperty("LOG_FILE_RETAIN_DAYS", "5"); //$NON-NLS-1$ //$NON-NLS-2$
-
+    private String getLogFileName() {
         // Calculate the name of the file to be used for logging
         IPath stateLocation = MTJCore.getMTJCore().getStateLocation();
         IPath logDirectory = stateLocation.append("jetty"); //$NON-NLS-1$
@@ -168,40 +112,43 @@
 
         File logFile = new File(logDirectoryFile, "logfile"); //$NON-NLS-1$
         String filename = logFile + "yyyy_mm_dd.txt"; //$NON-NLS-1$
-
-        // Create the output sink and add it to the logging class
-        OutputStreamLogSink sink = new OutputStreamLogSink(filename);
-
-        sink.start();
-
-        logImpl.add(sink);
+        return filename;
     }
-
     /**
      * Create and configure the HTTP server instance.
-     * 
+     *
      * @return
      */
-    private HttpServer createHttpServer() {
-        HttpServer server = new HttpServer();
-        configureListener(server);
-        configureContext(server);
+    private Server createHttpServer() {
+        Server server = new Server();
+        server.setConnectors(getConnectors());
+        server.addHandler(OTAHandler.getContextHandler());
+        NCSARequestLog log = new NCSARequestLog(getLogFileName());
+        server.addLifeCycle(log);
         return server;
     }
 
+    private Connector[] getConnectors(){
+        Connector connector = new SocketConnector();
+        if(Platform.getPreferencesService().getBoolean(MTJCore.getPluginId(),
+        		IMTJCoreConstants.PREF_OTA_PORT_DEFINED, false, null))
+        {
+        	int specifiedPortNumber = Platform.getPreferencesService().getInt(MTJCore.getPluginId(),
+        			IMTJCoreConstants.PREF_OTA_PORT, 0, null);
+        	if (specifiedPortNumber != 0)
+        		connector.setPort(specifiedPortNumber);
+        }
+        return new Connector[]{connector};
+    }
+
     /**
      * Get the HttpServer instance.
-     * 
+     *
      * @return
      */
-    private HttpServer getHttpServer() {
+    private Server getHttpServer() {
         if (httpServer == null) {
             httpServer = createHttpServer();
-            try {
-                configureLogging();
-            } catch (Exception e) {
-                MTJLogger.log(IStatus.WARNING, "configureLogging", e); //$NON-NLS-1$
-            }
         }
 
         return httpServer;