AddTrustedStaticLib.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.AddTrustedStaticLibFileDialog;
  42. import com.intel.sgx.preferences.PreferenceConstants;
  43. public class AddTrustedStaticLib 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. AddTrustedStaticLibFileDialog dialog = new AddTrustedStaticLibFileDialog(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. template = TemplateEngine.getDefault().getTemplateById("SGXTrustedStaticLibraryC++Minimal");
  95. else
  96. template = TemplateEngine.getDefault().getTemplateById("SGXTrustedStaticLibraryCMinimal");
  97. Map<String, String> valueStore = template.getValueStore();
  98. valueStore.put("projectName", project.getName());
  99. valueStore.put("workspacePath", linuxPath);
  100. valueStore.put("baseName", enclaveBasename);
  101. valueStore.put("enclaveName",edlFilename);
  102. valueStore.put("EnclaveName",capitalize(edlFilename));
  103. valueStore.put("ENCLAVENAME",edlFilename.toUpperCase());
  104. valueStore.put("SdkPathFromPlugin", Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.SDK_PATH));
  105. IStatus[] statuses = template.executeTemplateProcesses(monitor, false);
  106. ManagedBuildManager.saveBuildInfo(project, true);
  107. try {
  108. project.refreshLocal(IResource.DEPTH_INFINITE, null);
  109. } catch (CoreException e) {
  110. Activator.log(e);
  111. e.printStackTrace();
  112. }
  113. return null;
  114. }
  115. private String capitalize(final String line) {
  116. return Character.toUpperCase(line.charAt(0)) + line.substring(1);
  117. }
  118. @Override
  119. public boolean isEnabled() {
  120. return true;
  121. }
  122. @Override
  123. public boolean isHandled() {
  124. return true;
  125. }
  126. @Override
  127. public void removeHandlerListener(IHandlerListener handlerListener) {
  128. }
  129. public void setFilename(String filename) {
  130. edlFilename = filename;
  131. }
  132. public static void copyFile(File source, File dest) throws IOException {
  133. byte[] bytes = new byte[4092];
  134. if (source != null && dest != null) {
  135. if (source.isFile()) {
  136. FileInputStream in = null;
  137. FileOutputStream out = null;
  138. try {
  139. in = new FileInputStream(source);
  140. out = new FileOutputStream(dest);
  141. int len;
  142. while ((len = in.read(bytes)) != -1) {
  143. out.write(bytes, 0, len);
  144. }
  145. } catch (Exception e) {
  146. Activator.log(e);
  147. System.err.println("Error: " + e.toString());
  148. } finally {
  149. try {
  150. if (in != null)
  151. in.close();
  152. } finally {
  153. if (out != null)
  154. out.close();
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }