AddEnclave.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.util.Map;
  18. import org.eclipse.cdt.core.templateengine.TemplateCore;
  19. import org.eclipse.cdt.core.templateengine.TemplateEngine;
  20. import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
  21. import org.eclipse.core.commands.ExecutionEvent;
  22. import org.eclipse.core.commands.ExecutionException;
  23. import org.eclipse.core.commands.IHandler;
  24. import org.eclipse.core.commands.IHandlerListener;
  25. import org.eclipse.core.resources.IProject;
  26. import org.eclipse.core.resources.IResource;
  27. import org.eclipse.core.runtime.CoreException;
  28. import org.eclipse.core.runtime.IConfigurationElement;
  29. import org.eclipse.core.runtime.IPath;
  30. import org.eclipse.core.runtime.IProgressMonitor;
  31. import org.eclipse.core.runtime.IStatus;
  32. import org.eclipse.core.runtime.NullProgressMonitor;
  33. import org.eclipse.core.runtime.Path;
  34. import org.eclipse.core.runtime.Platform;
  35. import org.eclipse.jface.dialogs.InputDialog;
  36. import org.eclipse.jface.viewers.ISelection;
  37. import org.eclipse.jface.viewers.IStructuredSelection;
  38. import org.eclipse.swt.widgets.Shell;
  39. import org.eclipse.ui.handlers.HandlerUtil;
  40. import com.intel.sgx.Activator;
  41. import com.intel.sgx.dialogs.AddEnclaveFileDialog;
  42. import com.intel.sgx.preferences.PreferenceConstants;
  43. public class AddEnclave extends ModuleCreationBaseHandler implements IHandler {
  44. public String edlFilename = "";
  45. public String linuxMakePath = "";
  46. @Override
  47. public void addHandlerListener(IHandlerListener handlerListener) {
  48. }
  49. @Override
  50. public void dispose() {
  51. }
  52. @Override
  53. public Object execute(ExecutionEvent event) throws ExecutionException {
  54. String edlBasename,linuxPath,enclaveBasename;
  55. IProject project = null;
  56. // Display display = Display.getCurrent();
  57. // Shell shell = new Shell(display);
  58. Shell shell = null;
  59. AddEnclaveFileDialog dialog = new AddEnclaveFileDialog(shell, this);
  60. if (dialog.open() != InputDialog.OK) {
  61. return null;
  62. }
  63. if((edlFilename.isEmpty())){
  64. System.err.println("No Enclave selected to Import.");
  65. return null;
  66. }
  67. edlBasename = edlFilename;
  68. ISelection selection = HandlerUtil.getCurrentSelection(event);
  69. Object element = null;
  70. if (selection instanceof IStructuredSelection) {
  71. element = ((IStructuredSelection) selection).getFirstElement();
  72. if (element instanceof IResource) {
  73. project = ((IResource) element).getProject();
  74. }
  75. }
  76. if (!project.exists()) {
  77. System.err.println("Error: Project not found");
  78. return null;
  79. }
  80. for (IConfigurationElement i : Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.core.resources.projectNature")){
  81. }
  82. IPath linuxMkRelPath = (Path.fromOSString(linuxMakePath)).makeRelativeTo(project.getLocation().append("sgx").append("enclave_"+edlBasename));
  83. if(linuxMkRelPath.removeLastSegments(1).lastSegment().toString().equalsIgnoreCase("sgx")){
  84. linuxPath = linuxMkRelPath.removeLastSegments(3).toOSString();
  85. enclaveBasename = linuxMkRelPath.removeLastSegments(2).lastSegment().toString();
  86. }
  87. else{
  88. linuxPath = linuxMkRelPath.removeLastSegments(2).toOSString();
  89. enclaveBasename = linuxMkRelPath.removeLastSegments(1).lastSegment().toString();
  90. }
  91. IProgressMonitor monitor = new NullProgressMonitor();
  92. TemplateCore template = null;
  93. if(isCPProject(project))
  94. if(dialog.generateApp())
  95. {
  96. template = TemplateEngine.getDefault().getTemplateById("SGXEnclaveC++WithSample");
  97. }
  98. else
  99. {
  100. template = TemplateEngine.getDefault().getTemplateById("SGXEnclaveC++Minimal");
  101. }
  102. else
  103. if(dialog.generateApp())
  104. {
  105. template = TemplateEngine.getDefault().getTemplateById("SGXEnclaveCWithSample");
  106. }
  107. else
  108. {
  109. template = TemplateEngine.getDefault().getTemplateById("SGXEnclaveCMinimal");
  110. }
  111. Map<String, String> valueStore = template.getValueStore();
  112. valueStore.put("projectName", project.getName());
  113. valueStore.put("workspacePath", linuxPath);
  114. valueStore.put("baseName", enclaveBasename);
  115. valueStore.put("enclaveName",edlFilename);
  116. valueStore.put("EnclaveName",capitalize(edlFilename));
  117. valueStore.put("ENCLAVENAME",edlFilename.toUpperCase());
  118. valueStore.put("SdkPathFromPlugin", Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.SDK_PATH));
  119. IStatus[] statuses = template.executeTemplateProcesses(monitor, false);
  120. for(IStatus e: statuses)
  121. {
  122. }
  123. ManagedBuildManager.saveBuildInfo(project, true);
  124. try {
  125. project.refreshLocal(IResource.DEPTH_INFINITE, null);
  126. } catch (CoreException e) {
  127. Activator.log(e);
  128. e.printStackTrace();
  129. }
  130. return null;
  131. }
  132. private String capitalize(final String line) {
  133. return Character.toUpperCase(line.charAt(0)) + line.substring(1);
  134. }
  135. @Override
  136. public boolean isEnabled() {
  137. return true;
  138. }
  139. @Override
  140. public boolean isHandled() {
  141. return true;
  142. }
  143. @Override
  144. public void removeHandlerListener(IHandlerListener handlerListener) {
  145. }
  146. public void setFilename(String filename) {
  147. edlFilename = filename;
  148. }
  149. public static void copyFile(File source, File dest) throws IOException {
  150. byte[] bytes = new byte[4092];
  151. if (source != null && dest != null) {
  152. if (source.isFile()) {
  153. FileInputStream in = null;
  154. FileOutputStream out = null;
  155. try {
  156. in = new FileInputStream(source);
  157. out = new FileOutputStream(dest);
  158. int len;
  159. while ((len = in.read(bytes)) != -1) {
  160. out.write(bytes, 0, len);
  161. }
  162. } catch (Exception e) {
  163. Activator.log(e);
  164. System.err.println("Error: " + e.toString());
  165. } finally {
  166. try {
  167. if (in != null)
  168. in.close();
  169. } finally {
  170. if (out != null)
  171. out.close();
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }