1 18 19 package sync4j.syncclient.spap.launcher; 20 21 import java.lang.reflect.Method ; 22 23 24 import java.io.*; 25 import java.security.*; 26 import sync4j.syncclient.common.logging.Logger; 27 import sync4j.syncclient.spap.Asset; 28 import sync4j.syncclient.spap.AssetInstallationException; 29 import sync4j.syncclient.spap.installer.InstallationContext; 30 import java.lang.reflect.*; 31 32 40 41 public class JavaLauncher implements Launcher { 42 43 45 private SimpleSecurityManager simpleSecurityManager = null; 47 48 49 Logger logger = new Logger(); 50 51 public JavaLauncher() { 53 simpleSecurityManager = new SimpleSecurityManager(); 54 } 55 56 58 72 public int execute(String programName, boolean install, InstallationContext ctx) 73 throws AssetInstallationException { 74 Integer exitState = null; 75 SecurityManager originalSecurityManager = System.getSecurityManager(); 76 77 String workingDirectory = ctx.getWorkingDirectory(); 78 79 80 try { 81 int indexClass = programName.lastIndexOf(".class"); 82 83 programName = programName.substring(0, indexClass); 84 85 String msgLog = "JavaLauncher - Execute " + 86 programName + 87 " in " + 88 workingDirectory ; 89 90 if (logger.isLoggable(Logger.DEBUG)) { 91 logger.debug(msgLog); 92 } 93 94 String methodName = null; 95 96 ClassLoader cl = this.getClass().getClassLoader(); 98 99 Class programClass = cl.loadClass(programName); 100 101 if (install) { 102 methodName = "install"; 104 105 } else { 106 methodName = "uninstall"; 108 } 109 110 Class [] arg = { InstallationContext.class }; 112 Method method = programClass.getMethod(methodName, arg); 113 Object [] objArg = { ctx }; 114 115 System.setSecurityManager(simpleSecurityManager); 116 117 exitState = (Integer )method.invoke(null, objArg); 118 } catch (InvocationTargetException ex) { 119 120 Throwable cause = ex.getTargetException(); 121 122 if (cause instanceof SimpleSecurityManagerException) { 123 cause.printStackTrace(); 124 exitState = new Integer ( ((SimpleSecurityManagerException)cause).getExitCode() ); 126 } else if (cause instanceof AssetInstallationException) { 127 throw (AssetInstallationException)cause; 128 } else { 129 throw new AssetInstallationException("Error in launcher", cause); 131 } 132 133 } catch (Exception e) { 134 throw new AssetInstallationException("Error in launcher: " + e, e); 135 } finally { 136 System.setSecurityManager(originalSecurityManager); 138 } 139 140 if (exitState == null) { 142 143 if (logger.isLoggable(Logger.DEBUG)) { 144 logger.debug("Java class executed with error. Return state null (--> -1)!! "); 145 } 146 147 return -1; 148 } 149 150 if (logger.isLoggable(Logger.DEBUG)) { 151 logger.debug("Java class executed. Return state: " + exitState.intValue()); 152 } 153 return exitState.intValue(); 154 } 155 } 156 157 158 163 class SimpleSecurityManager extends SecurityManager { 164 165 private final SecurityManager parent = System.getSecurityManager(); 166 167 public void checkPermission(Permission perm) { 168 if (parent != null) { 169 parent.checkPermission(perm); 170 } 171 } 172 173 public void checkExit(int code) { 174 throw new SimpleSecurityManagerException("System.exit not allowed", code); 175 } 176 177 } 178 179 185 class SimpleSecurityManagerException extends SecurityException { 186 187 private int exitCode = -1; 188 189 public SimpleSecurityManagerException(String message, int code) { 190 super(message); 191 exitCode = code; 192 } 193 194 public int getExitCode() { 195 return exitCode; 196 } 197 198 } | Popular Tags |