core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/api/sdk/sbsv2/SBSv2QueryUtils.java
branchC3_BUILDER_WORK
changeset 1471 62024a5fa81d
child 1476 09e768e53db5
equal deleted inserted replaced
1469:5844e41d8bc7 1471:62024a5fa81d
       
     1 /*
       
     2 * Copyright (c) 2010 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 the License "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 * Test the BldInfViewPathHelper class.
       
    16 *
       
    17 */
       
    18 package com.nokia.carbide.cpp.internal.api.sdk.sbsv2;
       
    19 
       
    20 import java.io.BufferedReader;
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 import java.io.InputStreamReader;
       
    24 import java.io.StringReader;
       
    25 import java.text.MessageFormat;
       
    26 import java.util.ArrayList;
       
    27 import java.util.Enumeration;
       
    28 import java.util.HashMap;
       
    29 import java.util.List;
       
    30 import java.util.Properties;
       
    31 
       
    32 import javax.xml.parsers.DocumentBuilder;
       
    33 import javax.xml.parsers.DocumentBuilderFactory;
       
    34 
       
    35 import org.eclipse.cdt.utils.spawner.EnvironmentReader;
       
    36 import org.eclipse.core.runtime.IPath;
       
    37 import org.eclipse.core.runtime.IStatus;
       
    38 import org.eclipse.core.runtime.Path;
       
    39 import org.w3c.dom.Element;
       
    40 import org.w3c.dom.NamedNodeMap;
       
    41 import org.w3c.dom.Node;
       
    42 import org.w3c.dom.NodeList;
       
    43 import org.xml.sax.InputSource;
       
    44 import org.xml.sax.helpers.DefaultHandler;
       
    45 
       
    46 import com.nokia.carbide.cpp.internal.api.sdk.SBSv2Utils;
       
    47 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    48 import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin;
       
    49 import com.nokia.cpp.internal.api.utils.core.Logging;
       
    50 
       
    51 public class SBSv2QueryUtils {
       
    52 
       
    53 	public static final String QUERY_PRODUCTS_COMMAND = "--query=products";
       
    54 	public static final String QUERY_CONFIG_COMMAND = "--query=config";
       
    55 	public static final String QUERY_COMMAND = "--query=aliases";
       
    56 	
       
    57 	public static ISBSv2QueryData queryAliasAndProductVariants() {
       
    58 		List<String> argListConfigQuery = new ArrayList<String>();
       
    59 		List<String> argListProductQuery = new ArrayList<String>();
       
    60 		argListConfigQuery.add(QUERY_COMMAND);
       
    61 		SBSv2QueryData sbsQueryData = new SBSv2QueryData();
       
    62 		
       
    63 		/////// Invoke Raptor once with no EPOCROOT
       
    64 		Properties envVars = EnvironmentReader.getEnvVars();
       
    65 		envVars.setProperty("EPOCROOT", "FOOBAR");
       
    66 		String queryResult = getSBSQueryOutput(argListConfigQuery, createEnvStringList(envVars));
       
    67 	
       
    68 		HashMap<String, String> sbsAliasMap = parseQueryAliasResult(queryResult);
       
    69 		
       
    70 		for (String aliasKey : sbsAliasMap.keySet()){
       
    71 			String meaning = sbsAliasMap.get(aliasKey);
       
    72 			SBSv2ConfigData oneSBSConfig = new SBSv2ConfigData(aliasKey, meaning, null);
       
    73 			sbsQueryData.addConfigurationData(null, oneSBSConfig);
       
    74 		}
       
    75 		
       
    76 		/////// Do for each SDK to build up the alias list...
       
    77 		for (ISymbianSDK sdk : SDKCorePlugin.getSDKManager().getSDKList()){
       
    78 			IPath epocRoot = new Path(sdk.getEPOCROOT());
       
    79 			if ((sdk.getOSVersion().getMajor() <= 9 && sdk.getOSVersion().getMinor() <5)
       
    80 				|| !epocRoot.toFile().exists()){
       
    81 				
       
    82 				continue; // skip it, the sdk is not supported or broken
       
    83 			}
       
    84 			
       
    85 			envVars = EnvironmentReader.getEnvVars();
       
    86 			envVars.setProperty("EPOCROOT", sdk.getEPOCROOT());
       
    87 			
       
    88 			queryResult = getSBSQueryOutput(argListConfigQuery, createEnvStringList(envVars));
       
    89 			
       
    90 			sbsAliasMap = parseQueryAliasResult(queryResult);
       
    91 			
       
    92 			for (String aliasKey : sbsAliasMap.keySet()){
       
    93 				String meaning = sbsAliasMap.get(aliasKey);
       
    94 				SBSv2ConfigData oneSBSConfig = new SBSv2ConfigData(aliasKey, meaning, sdk);
       
    95 				sbsQueryData.addConfigurationData(sdk, oneSBSConfig);
       
    96 			}
       
    97 			
       
    98 			// Now get the products for each SDK
       
    99 			argListProductQuery.add(QUERY_PRODUCTS_COMMAND);
       
   100 			queryResult = getSBSQueryOutput(argListProductQuery, createEnvStringList(envVars));
       
   101 			List<String> productList = parseQueryProductsResults(queryResult);
       
   102 			sbsQueryData.addProductListForSDK(sdk, productList);
       
   103 		}
       
   104 		
       
   105 		return sbsQueryData;
       
   106 	}
       
   107 	
       
   108 	public static HashMap<String, String> queryConfigTargetInfo(List<String> aliasOrMeaningArray, ISymbianSDK sdk){
       
   109 		
       
   110 		List<String> argListConfigQuery = new ArrayList<String>();
       
   111 		
       
   112 		for (String alias : aliasOrMeaningArray){
       
   113 			argListConfigQuery.add(QUERY_CONFIG_COMMAND + "[" + alias + "]");
       
   114 		}
       
   115 		
       
   116 		Properties envVars = null;
       
   117 		if (sdk != null){
       
   118 			File epocRoot = new File(sdk.getEPOCROOT());
       
   119 			if (epocRoot.exists()){
       
   120 				envVars = EnvironmentReader.getEnvVars();
       
   121 				envVars.setProperty("EPOCROOT", sdk.getEPOCROOT());
       
   122 			}
       
   123 		}
       
   124 		String queryResult = getSBSQueryOutput(argListConfigQuery, createEnvStringList(envVars));
       
   125 		
       
   126 		return parseQueryConfigResults(queryResult);
       
   127 	}
       
   128 	
       
   129 	private static String[] createEnvStringList(Properties envProps) {
       
   130 		
       
   131 		if (envProps == null){
       
   132 			return null;
       
   133 		}
       
   134 		String[] env = null;
       
   135 		List<String> envList = new ArrayList<String>();
       
   136 		Enumeration<?> names = envProps.propertyNames();
       
   137 		if (names != null) {
       
   138 			while (names.hasMoreElements()) {
       
   139 				String key = (String) names.nextElement();
       
   140 				envList.add(key + "=" + envProps.getProperty(key));
       
   141 			}
       
   142 			env = (String[]) envList.toArray(new String[envList.size()]);
       
   143 		}
       
   144 		return env;
       
   145 	}
       
   146 	
       
   147 	private static String getSBSQueryOutput(List<String> queryCommandList, String[] env) {
       
   148 		String overallOutput = "";
       
   149 		
       
   150 		Runtime rt = Runtime.getRuntime();
       
   151 		IPath sbsPath = SBSv2Utils.getSBSPath();
       
   152 		Process p = null;
       
   153 		List<String> args = new ArrayList<String>();
       
   154 		args.add(sbsPath.toOSString());
       
   155 		args.addAll(queryCommandList);
       
   156 		try {
       
   157 			p = rt.exec(args.toArray(new String[args.size()]), env);
       
   158 		} catch (IOException e) {
       
   159 			// no such process, SBSv2 not available
       
   160 			Logging.log(
       
   161 					SDKCorePlugin.getDefault(),
       
   162 					Logging.newSimpleStatus(
       
   163 							0,
       
   164 							IStatus.WARNING,
       
   165 							MessageFormat
       
   166 									.format(
       
   167 											"Could not find or launch Raptor script ''{0}''; SBSv2 support will not be available",
       
   168 											sbsPath), e));
       
   169 		}
       
   170 		if (p != null) {
       
   171 			BufferedReader br = new BufferedReader(new InputStreamReader(p
       
   172 					.getInputStream()));
       
   173 			
       
   174 			String stdErrLine = null;
       
   175 			try {
       
   176 
       
   177 				// Only try for 30 seconds then bail in case Raptor hangs
       
   178 				int maxTries = 60;
       
   179 				int numTries = 0;
       
   180 				while (numTries < maxTries) {
       
   181 					try {
       
   182 						Thread.sleep(500);
       
   183 					} catch (InterruptedException e) {
       
   184 						// ignore
       
   185 					}
       
   186 					if (br.ready()) {
       
   187 						while ((stdErrLine = br.readLine()) != null) {
       
   188 							overallOutput += stdErrLine;
       
   189 							numTries = maxTries;
       
   190 						}
       
   191 
       
   192 					}
       
   193 					numTries++;
       
   194 				}
       
   195 			} catch (IOException e) {
       
   196 				e.printStackTrace();
       
   197 			}
       
   198 		}
       
   199 
       
   200 		return overallOutput;
       
   201 	}
       
   202 	
       
   203 	private static HashMap<String, String> parseQueryAliasResult(String queryResult) {
       
   204 		/* Alias to dotted name config */
       
   205 		HashMap<String, String> sbsAliasMap = new HashMap<String, String>();
       
   206 		
       
   207 		try {
       
   208     		Element root = null;
       
   209     		DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       
   210     		parser.setErrorHandler(new DefaultHandler());
       
   211     		
       
   212     		StringReader reader = new StringReader( queryResult );
       
   213     		InputSource inputSource = new InputSource( reader );
       
   214     		root = parser.parse(inputSource).getDocumentElement();
       
   215     		
       
   216     		NodeList children = root.getChildNodes();
       
   217     		for (int i=0; i< children.getLength(); i++) {
       
   218     			Node aliasNode = children.item(i);
       
   219     			if (aliasNode.getNodeName().equals("alias")){
       
   220     				NamedNodeMap meaning = aliasNode.getAttributes();
       
   221     				String dottedName = meaning.getNamedItem("meaning").getNodeValue();
       
   222     				String alias = meaning.getNamedItem("name").getNodeValue();
       
   223     				//System.out.println("ALIAS QUERY ==> " + dottedName + " <==> " + alias);
       
   224     				sbsAliasMap.put(alias, dottedName);
       
   225     			}
       
   226     		}
       
   227     		
       
   228     	} catch (Exception e) {
       
   229     		e.printStackTrace();
       
   230     		Logging.log(SDKCorePlugin.getDefault(), Logging.newStatus(SDKCorePlugin.getDefault(), e));
       
   231     	}
       
   232 		
       
   233 		
       
   234 		return sbsAliasMap;
       
   235 	}
       
   236 
       
   237 	private static HashMap<String, String> parseQueryConfigResults(String queryResult) {
       
   238 		/* Alias to output directory */
       
   239 		HashMap<String, String> sbsAliasMap = new HashMap<String, String>();
       
   240 		
       
   241 		try {
       
   242     		Element root = null;
       
   243     		DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       
   244     		parser.setErrorHandler(new DefaultHandler());
       
   245     		
       
   246     		StringReader reader = new StringReader( queryResult );
       
   247     		InputSource inputSource = new InputSource( reader );
       
   248     		root = parser.parse(inputSource).getDocumentElement();
       
   249     		
       
   250     		NodeList children = root.getChildNodes();
       
   251     		for (int i=0; i< children.getLength(); i++) {
       
   252     			Node aliasNode = children.item(i);
       
   253     			if (aliasNode.getNodeName().equals("config")){
       
   254     				NamedNodeMap meaning = aliasNode.getAttributes();
       
   255     				String outputpath = meaning.getNamedItem("outputpath").getNodeValue();
       
   256     				String fullName = meaning.getNamedItem("fullname").getNodeValue();
       
   257     				//System.out.println("ALIAS QUERY ==> " + dottedName + " <==> " + alias);
       
   258     				sbsAliasMap.put(fullName, outputpath);
       
   259     			}
       
   260     		}
       
   261     		
       
   262     	} catch (Exception e) {
       
   263     		e.printStackTrace();
       
   264     		Logging.log(SDKCorePlugin.getDefault(), Logging.newStatus(SDKCorePlugin.getDefault(), e));
       
   265     	}
       
   266 		
       
   267 		
       
   268 		return sbsAliasMap;
       
   269 	}
       
   270 	
       
   271 	private static List<String> parseQueryProductsResults(String queryResult) {
       
   272 		List<String> productList = new ArrayList<String>();
       
   273 		
       
   274 		try {
       
   275     		Element root = null;
       
   276     		DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       
   277     		parser.setErrorHandler(new DefaultHandler());
       
   278     		
       
   279     		StringReader reader = new StringReader( queryResult );
       
   280     		InputSource inputSource = new InputSource( reader );
       
   281     		root = parser.parse(inputSource).getDocumentElement();
       
   282     		
       
   283     		NodeList children = root.getChildNodes();
       
   284     		for (int i=0; i< children.getLength(); i++) {
       
   285     			Node aliasNode = children.item(i);
       
   286     			if (aliasNode.getNodeName().equals("product")){
       
   287     				NamedNodeMap productAttribs = aliasNode.getAttributes();
       
   288     				String name = productAttribs.getNamedItem("name").getNodeValue();
       
   289     				//System.out.println("ALIAS QUERY ==> " + dottedName + " <==> " + alias);
       
   290     				productList.add(name);
       
   291     			}
       
   292     		}
       
   293     		
       
   294     	} catch (Exception e) {
       
   295     		e.printStackTrace();
       
   296     		Logging.log(SDKCorePlugin.getDefault(), Logging.newStatus(SDKCorePlugin.getDefault(), e));
       
   297     	}
       
   298 		
       
   299 		return productList;
       
   300 	}
       
   301 
       
   302 	
       
   303 	
       
   304 	
       
   305 }