EnclaveConfigHandler.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2018 Intel Corporation. //
  3. // //
  4. // All rights reserved. This program and the accompanying materials //
  5. // are made available under the terms of the Eclipse Public License v1.0 //
  6. // which accompanies this distribution, and is available at //
  7. // http://www.eclipse.org/legal/epl-v10.html //
  8. // //
  9. // Contributors: //
  10. // Intel Corporation - initial implementation and documentation //
  11. ///////////////////////////////////////////////////////////////////////////
  12. package com.intel.sgx.handlers;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Map;
  16. import javax.xml.parsers.DocumentBuilder;
  17. import javax.xml.parsers.DocumentBuilderFactory;
  18. import javax.xml.parsers.ParserConfigurationException;
  19. import org.eclipse.cdt.core.templateengine.TemplateCore;
  20. import org.eclipse.cdt.core.templateengine.TemplateEngine;
  21. import org.eclipse.core.commands.IHandlerListener;
  22. import org.eclipse.core.resources.IFile;
  23. import org.eclipse.core.runtime.IPath;
  24. import org.eclipse.core.runtime.IProgressMonitor;
  25. import org.eclipse.core.runtime.IStatus;
  26. import org.eclipse.core.runtime.NullProgressMonitor;
  27. import org.eclipse.jface.dialogs.Dialog;
  28. import org.eclipse.jface.dialogs.InputDialog;
  29. import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;
  30. import org.w3c.dom.Document;
  31. import org.w3c.dom.Element;
  32. import org.w3c.dom.Node;
  33. import org.w3c.dom.NodeList;
  34. import org.xml.sax.SAXException;
  35. import com.intel.sgx.Activator;
  36. import com.intel.sgx.dialogs.EnclaveConfigDialog;
  37. import com.intel.sgx.dialogs.SGXDialogBase;
  38. public class EnclaveConfigHandler extends SGXHandler {
  39. public String prodId;
  40. public String isvSvn;
  41. public String stackMinSize;
  42. public String stackMaxSize;
  43. public String heapMinSize;
  44. public String heapInitSize;
  45. public String heapMaxSize;
  46. public String tcsNum;
  47. public String tcsMaxNum;
  48. public String tcsPool;
  49. public String tcsPolicy;
  50. public String disableDebug;
  51. private IFile configPath;
  52. @Override
  53. public void addHandlerListener(IHandlerListener arg0) {
  54. }
  55. @Override
  56. public void dispose() {
  57. }
  58. @Override
  59. public Object executeSGXStuff() throws CancelException, ErrorException {
  60. FilteredResourcesSelectionDialog d = SGXDialogBase
  61. .dialogForConfig(shell);
  62. d.setTitle("Select Config File");
  63. if (d.open() != Dialog.OK) {
  64. cancel();
  65. }
  66. configPath = ((IFile) d.getResult()[0]);
  67. readConfig(configPath.getLocation());
  68. EnclaveConfigDialog dialog = new EnclaveConfigDialog(shell, this);
  69. if (dialog.open() != InputDialog.OK) {
  70. return null;
  71. }
  72. writeConfig();
  73. refreshProject();
  74. return null;
  75. }
  76. protected void writeConfig() {
  77. IProgressMonitor monitor = new NullProgressMonitor();
  78. TemplateCore template = TemplateEngine.getDefault().getTemplateById("SGXEnclaveConfig");
  79. Map<String, String> valueStore = template.getValueStore();
  80. valueStore.put("projectName", project.getName());
  81. valueStore.put("configFile", configPath.getProjectRelativePath().toOSString());
  82. valueStore.put("ProdID", this.prodId);
  83. valueStore.put("IsvSvn", this.isvSvn);
  84. valueStore.put("StackMinSize", this.stackMinSize);
  85. valueStore.put("StackMaxSize", this.stackMaxSize);
  86. valueStore.put("HeapMinSize", this.heapMinSize);
  87. valueStore.put("HeapInitSize", this.heapInitSize);
  88. valueStore.put("HeapMaxSize", this.heapMaxSize);
  89. valueStore.put("TcsNum", this.tcsNum);
  90. valueStore.put("TcsMaxNum", this.tcsMaxNum);
  91. valueStore.put("TcsMinPool", this.tcsPool);
  92. valueStore.put("TcsPolicy", this.tcsPolicy);
  93. valueStore.put("DisableDebug", this.disableDebug);
  94. IStatus[] result = template.executeTemplateProcesses(monitor, true);
  95. for (IStatus status: result) {
  96. }
  97. }
  98. protected void readConfig(IPath configPath) throws ErrorException {
  99. try {
  100. String xmlFile = configPath.toString();
  101. File configFile = new File(xmlFile);
  102. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  103. DocumentBuilder dBuilder;
  104. dBuilder = dbFactory.newDocumentBuilder();
  105. Document doc = dBuilder.parse(configFile);
  106. doc.getDocumentElement().normalize();
  107. NodeList nList = doc.getElementsByTagName("EnclaveConfiguration");
  108. Node nNode = nList.item(0);
  109. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  110. Element e = (Element) nNode;
  111. this.prodId = e.getElementsByTagName("ProdID").item(0)
  112. .getTextContent();
  113. this.isvSvn = e.getElementsByTagName("ISVSVN").item(0)
  114. .getTextContent();
  115. this.stackMinSize = e.getElementsByTagName("StackMinSize")
  116. .item(0).getTextContent();
  117. this.stackMaxSize = e.getElementsByTagName("StackMaxSize")
  118. .item(0).getTextContent();
  119. this.heapMinSize = e.getElementsByTagName("HeapMinSize")
  120. .item(0).getTextContent();
  121. this.heapInitSize = e.getElementsByTagName("HeapInitSize")
  122. .item(0).getTextContent();
  123. this.heapMaxSize = e.getElementsByTagName("HeapMaxSize")
  124. .item(0).getTextContent();
  125. this.tcsNum = e.getElementsByTagName("TCSNum").item(0)
  126. .getTextContent();
  127. this.tcsMaxNum = e.getElementsByTagName("TCSMaxNum").item(0)
  128. .getTextContent();
  129. this.tcsPool = e.getElementsByTagName("TCSMinPool").item(0)
  130. .getTextContent();
  131. this.tcsPolicy = e.getElementsByTagName("TCSPolicy").item(0)
  132. .getTextContent();
  133. this.disableDebug = e.getElementsByTagName("DisableDebug")
  134. .item(0).getTextContent();
  135. }
  136. } catch (ParserConfigurationException e) {
  137. Activator.log(e);
  138. e.printStackTrace();
  139. quitWithError("Could not parse '"+configPath.toOSString()+"'");
  140. } catch (SAXException e) {
  141. Activator.log(e);
  142. e.printStackTrace();
  143. quitWithError("Could not parse '"+configPath.toOSString()+"'");
  144. } catch (IOException e) {
  145. Activator.log(e);
  146. e.printStackTrace();
  147. quitWithError("Could not read'"+configPath.toOSString()+"'");
  148. }
  149. }
  150. }