srcanaapps/apiquerytool/com.nokia.s60tools.apiquery/src/com/nokia/s60tools/apiquery/servlets/APIQueryWebServerConfigurator.java
changeset 0 a02c979e8dfd
equal deleted inserted replaced
-1:000000000000 0:a02c979e8dfd
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */ 
       
    17 package com.nokia.s60tools.apiquery.servlets;
       
    18 
       
    19 import java.io.IOException;
       
    20 import java.net.HttpURLConnection;
       
    21 import java.net.URL;
       
    22 import java.util.Hashtable;
       
    23 
       
    24 import org.eclipse.core.runtime.Platform;
       
    25 import org.eclipse.equinox.http.jetty.JettyConfigurator;
       
    26 import org.eclipse.equinox.http.jetty.JettyConstants;
       
    27 import org.eclipse.jface.dialogs.MessageDialog;
       
    28 import org.eclipse.swt.widgets.Display;
       
    29 import org.osgi.framework.Bundle;
       
    30 
       
    31 import com.nokia.s60tools.apiquery.shared.plugin.APIQueryPlugin;
       
    32 import com.nokia.s60tools.apiquery.shared.resources.Messages;
       
    33 import com.nokia.s60tools.apiquery.shared.util.console.APIQueryConsole;
       
    34 import com.nokia.s60tools.util.debug.DbgUtility;
       
    35 
       
    36 /**
       
    37  * Class for handling API Query Web server functionalities. E.g. start and stop Web Server.
       
    38  */
       
    39 public class APIQueryWebServerConfigurator {
       
    40 
       
    41 	/**
       
    42 	 * Host where report server is running (localhost)
       
    43 	 */
       
    44 	public static final String WEB_SERVER_HOST = "localhost"; //$NON-NLS-1$
       
    45 	
       
    46 	/**
       
    47 	 * HTTP port to be used with running HTTP server
       
    48 	 */
       
    49 	public static final int WEB_SERVER_HTTP_PORT = 4191;
       
    50 			
       
    51 	/**
       
    52 	 * Name of the API Query Web Server
       
    53 	 */
       
    54 	private static final String API_QUERY_WEB_SERVER_NAME = "API Query Server"; //$NON-NLS-1$
       
    55 	
       
    56 	/**
       
    57 	 * 
       
    58 	 */
       
    59 public static final int Active_Project_Start  = 0;
       
    60 
       
    61 public static final int Carbide_Instance_start  =1;
       
    62 
       
    63 static boolean activeProjectStartedServer = false;
       
    64 
       
    65 
       
    66 	/**
       
    67 	 * Starts API Query Web Server. Web Server is used for launch actions in HTML report. 
       
    68 	 * Reports are created with Export report functionality (API Query for Active project).
       
    69 	 * @see JettyConfigurator#startServer(String, java.util.Dictionary)
       
    70 	 */
       
    71 public static void startServer(int msgdialogueCode) {
       
    72 		try {
       
    73            
       
    74 		if (msgdialogueCode == Active_Project_Start) activeProjectStartedServer = true;
       
    75 			// Check if server is already running
       
    76 			if (allreadyRunning()) {
       
    77 				//Show the dialogue box
       
    78 				
       
    79 				if (msgdialogueCode==Carbide_Instance_start && APIQueryPlugin.isFirstLaunch&&!activeProjectStartedServer)
       
    80 				{
       
    81 					APIQueryPlugin.isFirstLaunch = false;					
       
    82 				MessageDialog.openInformation(APIQueryPlugin.getCurrentlyActiveWbWindowShell(), "APIQuery Plugin: Active Project Option Information", "Active project report links may not work. Close other Carbide.C++ instances and restart API Query plug-in.");
       
    83 				}
       
    84 				return;
       
    85 			}
       
    86 
       
    87 			Hashtable<String, Object> set = new Hashtable<String, Object>();
       
    88 			set.put(JettyConstants.HTTP_PORT, WEB_SERVER_HTTP_PORT);
       
    89 			JettyConfigurator.startServer(API_QUERY_WEB_SERVER_NAME, set);
       
    90 
       
    91 			Bundle bundle = Platform
       
    92 					.getBundle("org.eclipse.equinox.http.registry"); //$NON-NLS-1$
       
    93 
       
    94 			if (bundle.getState() == Bundle.RESOLVED) {
       
    95 				bundle.start(Bundle.START_TRANSIENT);
       
    96 			}
       
    97 			APIQueryConsole
       
    98 					.getInstance()
       
    99 					.println(
       
   100 							Messages
       
   101 									.getString("APIQueryWebServerConfigurator.StartServer_Msg_Part1")//$NON-NLS-1$ 
       
   102 									+ API_QUERY_WEB_SERVER_NAME
       
   103 									+ Messages
       
   104 											.getString("APIQueryWebServerConfigurator.StartServer_Msg_Part2")//$NON-NLS-1$ 
       
   105 									+ "http://" + WEB_SERVER_HOST + ":" + WEB_SERVER_HTTP_PORT //$NON-NLS-1$ //$NON-NLS-2$ 
       
   106 									+ Messages
       
   107 											.getString("APIQueryWebServerConfigurator.StartServer_Msg_Part3")); //$NON-NLS-1$
       
   108 
       
   109 		} catch (Exception e) {
       
   110 			e.printStackTrace();
       
   111 			APIQueryConsole
       
   112 					.getInstance()
       
   113 					.println(
       
   114 							Messages
       
   115 									.getString("APIQueryWebServerConfigurator.StartServer_ErrMsg_Part1") + API_QUERY_WEB_SERVER_NAME + Messages.getString("APIQueryWebServerConfigurator.StartServer_ErrMsg_Part2") + e, APIQueryConsole.MSG_ERROR); //$NON-NLS-1$ //$NON-NLS-2$
       
   116 
       
   117 		}
       
   118 	}	
       
   119 	
       
   120 	
       
   121 	
       
   122 	/**
       
   123 	 * Check if HTTP Server is already running
       
   124 	 * @return <code>true</code> if server is alreaydy running and <code>false</code> if not.
       
   125 	 */
       
   126 	private static boolean allreadyRunning() {
       
   127 		
       
   128 		
       
   129 		try {
       
   130 			//Servlet URL 
       
   131 			String servletURL = "http://" +WEB_SERVER_HOST  +":" +WEB_SERVER_HTTP_PORT + "/" + ReportActionConstants.SERVLET_NAME;
       
   132 			URL url = new URL(servletURL);
       
   133 			//Try to keep http connection alive
       
   134 			System.setProperty("http.keepAlive", "true");
       
   135 		
       
   136 			DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "-----> Reading URL: " + url.toString());
       
   137 			//Getting connection to check if server is running
       
   138 			HttpURLConnection con = (HttpURLConnection) url.openConnection();
       
   139 			if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
       
   140 				return true;
       
   141 			} 
       
   142 		
       
   143 		} catch (IOException e) {
       
   144 			// IOExeption occurs if HttpURLConnection is not alive 
       
   145 			//and when HttpURLConnection.getResponseCode() is called
       
   146 			//then we know that HTTP server is not up and running			
       
   147 		}
       
   148 		catch (Exception e) {
       
   149 			//No operation if something else happens, just returning false
       
   150 		}		
       
   151 		
       
   152 		return false;
       
   153 	}
       
   154 
       
   155 	/**
       
   156 	 * Stops API Query Web Server.
       
   157 	 * @see JettyConfigurator#stopServer(String)
       
   158 	 */
       
   159 	public static void stopServer(){
       
   160 		try {
       
   161 			JettyConfigurator.stopServer(API_QUERY_WEB_SERVER_NAME);
       
   162 			APIQueryConsole.getInstance().println(Messages.getString("APIQueryWebServerConfigurator.StoptServer_Msg_Part1") +API_QUERY_WEB_SERVER_NAME +Messages.getString("APIQueryWebServerConfigurator.StoptServer_Msg_Part2")); //$NON-NLS-1$ //$NON-NLS-2$
       
   163 		} catch (Exception e) {
       
   164 			e.printStackTrace();
       
   165 			APIQueryConsole.getInstance().println(Messages.getString("APIQueryWebServerConfigurator.StoptServer_ErrMsg_Part1") +API_QUERY_WEB_SERVER_NAME +Messages.getString("APIQueryWebServerConfigurator.StoptServer_ErrMsg_Part2") +e, APIQueryConsole.MSG_ERROR); //$NON-NLS-1$ //$NON-NLS-2$
       
   166 		}
       
   167 	}
       
   168 	
       
   169 }