EnclaveConfigHandler.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2016 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 threadStackSize;
  42. public String globalHeapSize;
  43. public String tcsNum;
  44. public String tcsPolicy;
  45. public String disableDebug;
  46. private IFile configPath;
  47. @Override
  48. public void addHandlerListener(IHandlerListener arg0) {
  49. }
  50. @Override
  51. public void dispose() {
  52. }
  53. @Override
  54. public Object executeSGXStuff() throws CancelException, ErrorException {
  55. FilteredResourcesSelectionDialog d = SGXDialogBase
  56. .dialogForConfig(shell);
  57. d.setTitle("Select Config File");
  58. if (d.open() != Dialog.OK) {
  59. cancel();
  60. }
  61. configPath = ((IFile) d.getResult()[0]);
  62. readConfig(configPath.getLocation());
  63. EnclaveConfigDialog dialog = new EnclaveConfigDialog(shell, this);
  64. if (dialog.open() != InputDialog.OK) {
  65. return null;
  66. }
  67. writeConfig();
  68. refreshProject();
  69. return null;
  70. }
  71. protected void writeConfig() {
  72. IProgressMonitor monitor = new NullProgressMonitor();
  73. TemplateCore template = TemplateEngine.getDefault().getTemplateById("SGXEnclaveConfig");
  74. Map<String, String> valueStore = template.getValueStore();
  75. valueStore.put("projectName", project.getName());
  76. valueStore.put("configFile", configPath.getProjectRelativePath().toOSString());
  77. valueStore.put("ProdID", this.prodId);
  78. valueStore.put("IsvSvn", this.isvSvn);
  79. valueStore.put("ThreadStackSize", this.threadStackSize);
  80. valueStore.put("GlobalHeapSize", this.globalHeapSize);
  81. valueStore.put("TcsNumber", this.tcsNum);
  82. valueStore.put("TcsPolicy", this.tcsPolicy);
  83. valueStore.put("DisableDebug", this.disableDebug);
  84. IStatus[] result = template.executeTemplateProcesses(monitor, true);
  85. for (IStatus status: result) {
  86. }
  87. }
  88. protected void readConfig(IPath configPath) throws ErrorException {
  89. try {
  90. String xmlFile = configPath.toString();
  91. File configFile = new File(xmlFile);
  92. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  93. DocumentBuilder dBuilder;
  94. dBuilder = dbFactory.newDocumentBuilder();
  95. Document doc = dBuilder.parse(configFile);
  96. doc.getDocumentElement().normalize();
  97. NodeList nList = doc.getElementsByTagName("EnclaveConfiguration");
  98. Node nNode = nList.item(0);
  99. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  100. Element e = (Element) nNode;
  101. this.prodId = e.getElementsByTagName("ProdID").item(0)
  102. .getTextContent();
  103. this.isvSvn = e.getElementsByTagName("ISVSVN").item(0)
  104. .getTextContent();
  105. this.threadStackSize = e.getElementsByTagName("StackMaxSize")
  106. .item(0).getTextContent();
  107. this.globalHeapSize = e.getElementsByTagName("HeapMaxSize")
  108. .item(0).getTextContent();
  109. this.tcsNum = e.getElementsByTagName("TCSNum").item(0)
  110. .getTextContent();
  111. this.tcsPolicy = e.getElementsByTagName("TCSPolicy").item(0)
  112. .getTextContent();
  113. this.disableDebug = e.getElementsByTagName("DisableDebug")
  114. .item(0).getTextContent();
  115. }
  116. } catch (ParserConfigurationException e) {
  117. Activator.log(e);
  118. e.printStackTrace();
  119. quitWithError("Could not parse '"+configPath.toOSString()+"'");
  120. } catch (SAXException e) {
  121. Activator.log(e);
  122. e.printStackTrace();
  123. quitWithError("Could not parse '"+configPath.toOSString()+"'");
  124. } catch (IOException e) {
  125. Activator.log(e);
  126. e.printStackTrace();
  127. quitWithError("Could not read'"+configPath.toOSString()+"'");
  128. }
  129. }
  130. }