SGXHandler.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 javax.swing.JOptionPane;
  18. import org.eclipse.core.commands.ExecutionEvent;
  19. import org.eclipse.core.commands.ExecutionException;
  20. import org.eclipse.core.commands.IHandler;
  21. import org.eclipse.core.commands.IHandlerListener;
  22. import org.eclipse.core.resources.IProject;
  23. import org.eclipse.core.resources.IResource;
  24. import org.eclipse.core.runtime.CoreException;
  25. import org.eclipse.jface.viewers.ISelection;
  26. import org.eclipse.jface.viewers.IStructuredSelection;
  27. import org.eclipse.swt.widgets.Shell;
  28. import org.eclipse.ui.PlatformUI;
  29. import org.eclipse.ui.handlers.HandlerUtil;
  30. /**
  31. * Utility base class for all Handlers The derived classes must implement
  32. * executeSgxStuff() instead of execute(). The user may call methods cancel(),
  33. * quitWithError() & info()
  34. *
  35. * @author mlutescu
  36. *
  37. */
  38. public abstract class SGXHandler implements IHandler {
  39. public String projectPath = null;
  40. protected IProject project;
  41. protected Shell shell;
  42. /**
  43. * Throwing this IS an error. Means that the process can't continue
  44. *
  45. * @author mlutescu
  46. *
  47. */
  48. static protected class ErrorException extends Exception {
  49. public ErrorException(String message) {
  50. super(message);
  51. }
  52. }
  53. /**
  54. * Throwing this is not an error; just signals stop of execution because the
  55. * user cancels
  56. *
  57. * @author mlutescu
  58. *
  59. */
  60. static protected class CancelException extends Exception {
  61. public CancelException() {
  62. super();
  63. }
  64. }
  65. @Override
  66. public final Object execute(ExecutionEvent event) throws ExecutionException {
  67. try {
  68. initializeShell();
  69. initializeProject(event);
  70. return executeSGXStuff();
  71. } catch (ErrorException e) {
  72. e.printStackTrace();
  73. } catch (CancelException e) {
  74. // do nothing by design ; it's Ok to not handle this exception.
  75. }
  76. return null;
  77. }
  78. protected abstract Object executeSGXStuff() throws ErrorException,
  79. CancelException;
  80. public SGXHandler() {
  81. super();
  82. }
  83. public static void copyFile(File source, File dest) throws ErrorException {
  84. byte[] bytes = new byte[4092];
  85. if (source != null && dest != null) {
  86. if (source.isFile()) {
  87. FileInputStream in = null;
  88. FileOutputStream out = null;
  89. try {
  90. in = new FileInputStream(source);
  91. out = new FileOutputStream(dest);
  92. int len;
  93. while ((len = in.read(bytes)) != -1) {
  94. out.write(bytes, 0, len);
  95. }
  96. } catch (IOException e) {
  97. System.err.println("Error: " + e.toString());
  98. quitWithError("Could not copy from\n" + "'"
  99. + source.getAbsolutePath() + "'\n" + "to\n" + "'"
  100. + dest.getAbsolutePath());
  101. } finally {
  102. try {
  103. if (in != null) {
  104. in.close();
  105. }
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. } finally {
  109. if (out != null) {
  110. try {
  111. out.close();
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. protected static void quitWithError(String message) throws ErrorException {
  122. JOptionPane.showMessageDialog(null, message, "Two Step Enclave Sign",
  123. JOptionPane.ERROR_MESSAGE);
  124. throw new ErrorException(message);
  125. }
  126. static protected void cancel() throws CancelException {
  127. throw new CancelException();
  128. }
  129. protected void initializeProject(ExecutionEvent event)
  130. throws ErrorException {
  131. project = null;
  132. ISelection selection = HandlerUtil.getCurrentSelection(event);
  133. Object element = null;
  134. if (selection instanceof IStructuredSelection) {
  135. element = ((IStructuredSelection) selection).getFirstElement();
  136. if (element instanceof IResource) {
  137. project = ((IResource) element).getProject();
  138. }
  139. }
  140. if (!project.exists()) {
  141. quitWithError("Project not found");
  142. }
  143. }
  144. @Override
  145. public boolean isEnabled() {
  146. return true;
  147. }
  148. @Override
  149. public boolean isHandled() {
  150. return true;
  151. }
  152. @Override
  153. public void removeHandlerListener(IHandlerListener arg0) {
  154. }
  155. @Override
  156. public void addHandlerListener(IHandlerListener arg0) {
  157. }
  158. @Override
  159. public void dispose() {
  160. }
  161. protected void initializeShell() {
  162. shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
  163. }
  164. protected void refreshProject() throws ErrorException {
  165. try {
  166. project.refreshLocal(IResource.DEPTH_INFINITE, null);
  167. } catch (CoreException e1) {
  168. quitWithError(e1.getLocalizedMessage());
  169. }
  170. }
  171. protected void info(String windowName, String message) {
  172. JOptionPane.showMessageDialog(null, message,windowName,
  173. JOptionPane.INFORMATION_MESSAGE);
  174. }
  175. }