1 19 20 package com.sslexplorer.extensions; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.sslexplorer.boot.Util; 31 import com.sslexplorer.core.CoreUtil; 32 import com.sslexplorer.core.stringreplacement.ExtensionBundleReplacer; 33 import com.sslexplorer.extensions.store.ExtensionStore; 34 35 50 public class ExtensionInstaller { 51 52 final static Log log = LogFactory.getLog(ExtensionInstaller.class); 53 54 58 public static final String ON_ACTIVATE = "activate"; 59 60 64 public static final String ON_START = "start"; 65 66 68 private ExtensionBundle bundle; 69 private List <ExtensionInstallOp> ops; 70 71 76 public ExtensionInstaller(ExtensionBundle bundle) { 77 this.bundle = bundle; 78 ops = new ArrayList <ExtensionInstallOp>(); 79 } 80 81 86 public ExtensionBundle getBundle() { 87 return bundle; 88 } 89 90 95 public void addOp(ExtensionInstallOp op) { 96 ops.add(op); 97 } 98 99 106 public static File checkFile(File file) throws IOException { 107 111 return file; 112 } 113 114 121 public void doInstall(String phase) throws Exception { 122 if (ops.size() == 0) { 123 if (log.isInfoEnabled()) 124 log.info("Bundle " + bundle.getName() + " has no installer script."); 125 } else { 126 boolean started = false; 127 try { 128 for (ExtensionInstallOp op : ops) { 129 if(op.getPhase().equals(phase) || ( phase.equals(ExtensionInstaller.ON_ACTIVATE) && op.getPhase() == null) ) { 130 if(!started) { 131 if (log.isInfoEnabled()) 132 log.info("Starting installer for " + bundle.getName()); 133 started = true; 134 } 135 op.doOp(this); 136 } 137 } 138 if (log.isInfoEnabled()) 139 log.info("Completed installation for " + bundle.getName()); 140 } finally { 141 ExtensionStore.VERSION_PREFS.put(bundle.getId(), bundle.getVersion().toString()); 142 ExtensionStore.VERSION_PREFS.flush(); 143 } 144 } 145 } 146 147 152 public int getOpCount() { 153 return ops.size(); 154 } 155 156 161 public static interface ExtensionInstallOp { 162 163 171 public String getPhase(); 172 173 179 public void doOp(ExtensionInstaller install) throws Exception ; 180 } 181 182 186 public static abstract class AbstractExtensionInstallOp implements ExtensionInstallOp { 187 private String phase; 188 189 194 public AbstractExtensionInstallOp(String phase) { 195 this.phase = phase; 196 } 197 198 203 public String getPhase() { 204 return phase; 205 } 206 } 207 208 211 public static class MkdirInstallOp extends AbstractExtensionInstallOp { 212 private String path; 213 214 221 public MkdirInstallOp(String phase, String path) throws IllegalArgumentException { 222 super(phase); 223 this.path = path; 224 } 225 226 231 public void doOp(ExtensionInstaller install) throws Exception { 232 path = CoreUtil.platformPath(ExtensionBundleReplacer.replace(install.getBundle(), path)); 233 File f = checkFile(new File (path)); 234 if (log.isInfoEnabled()) 235 log.info("Creating directory " + f.getAbsolutePath()); 236 f.mkdirs(); 237 } 238 } 239 240 243 public static class RmInstallOp extends AbstractExtensionInstallOp { 244 private String path; 245 246 253 public RmInstallOp(String phase, String path) throws IllegalArgumentException { 254 super(phase); 255 this.path = path; 256 } 257 258 263 public void doOp(ExtensionInstaller install) throws Exception { 264 path = CoreUtil.platformPath(ExtensionBundleReplacer.replace(install.getBundle(), path)); 265 File f = checkFile(new File (path)); 266 Util.delTree(f); 267 } 268 } 269 270 273 public static class CpInstallOp extends AbstractExtensionInstallOp { 274 private String from; 275 private String to; 276 private String toDir; 277 private boolean overwrite; 278 279 290 public CpInstallOp(String phase, String from, String to, String toDir, boolean overwrite) throws IllegalArgumentException { 291 super(phase); 292 this.from = from; 293 this.to = to; 294 this.toDir = toDir; 295 this.overwrite = overwrite; 296 } 297 298 public void doOp(ExtensionInstaller install) throws Exception { 299 from = CoreUtil.platformPath(ExtensionBundleReplacer.replace(install.getBundle(), from)); 300 File f = checkFile(new File (from)); 301 if (to != null) { 302 File t = checkFile(new File (CoreUtil.platformPath(ExtensionBundleReplacer.replace(install.getBundle(), to)))); 303 if (f.isDirectory()) { 304 throw new Exception ("Cannot copy directory to a file"); 305 } 306 if (log.isInfoEnabled()) 307 log.info("Copying " + f.getAbsolutePath() + " to " + t.getAbsolutePath()); 308 if (t.exists() && !overwrite) { 309 log.error("Failed to copy to target because it already exists and overwrite attribute is not true"); 310 } else { 311 Util.copy(f, t); 312 } 313 } else if (toDir != null) { 314 File t = checkFile(new File (ExtensionBundleReplacer.replace(install.getBundle(), toDir))); 315 if (log.isInfoEnabled()) 316 log.info("Copying " + f.getAbsolutePath() + " to " + t.getAbsolutePath()); 317 if (((!t.isDirectory() && t.exists()) || (t.isDirectory() && new File (t, f.getName()).exists())) && !overwrite) { 318 log.error("Failed to copy to target because it already exists and overwrite attribute is not true"); 319 } else { 320 Util.copyToDir(f, t, false, true); 321 } 322 } 323 } 324 } 325 326 332 public static class CustomInstallOpWrapper extends AbstractExtensionInstallOp { 333 334 private String clazz; 335 336 342 public CustomInstallOpWrapper(String phase, String clazz) { 343 super(phase); 344 this.clazz = clazz; 345 } 346 347 352 public void doOp(ExtensionInstaller install) throws Exception { 353 if (log.isInfoEnabled()) 354 log.info("Running custom install op. " + clazz); 355 ExtensionInstallOp op = (ExtensionInstallOp) Class.forName(clazz, true, getClass().getClassLoader()).newInstance(); 356 op.doOp(install); 357 } 358 359 } 360 } 361 | Popular Tags |