From: <mwl...@us...> - 2008-07-08 18:51:35
|
Revision: 785 http://cishell.svn.sourceforge.net/cishell/?rev=785&view=rev Author: mwlinnem Date: 2008-07-08 11:50:58 -0700 (Tue, 08 Jul 2008) Log Message: ----------- A little refactoring. Modified Paths: -------------- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableAlgorithmFactory.java Modified: trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableAlgorithmFactory.java =================================================================== --- trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableAlgorithmFactory.java 2008-07-08 18:00:25 UTC (rev 784) +++ trunk/templates/org.cishell.templates/src/org/cishell/templates/staticexecutable/StaticExecutableAlgorithmFactory.java 2008-07-08 18:50:58 UTC (rev 785) @@ -13,29 +13,14 @@ * ***************************************************************************/ package org.cishell.templates.staticexecutable; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.URL; -import java.nio.channels.Channels; -import java.nio.channels.FileChannel; -import java.nio.channels.ReadableByteChannel; import java.util.Dictionary; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Properties; -import java.util.Set; import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; -import org.cishell.framework.algorithm.AlgorithmExecutionException; import org.cishell.framework.algorithm.AlgorithmFactory; -import org.cishell.framework.algorithm.ProgressMonitor; -import org.cishell.framework.algorithm.ProgressTrackable; import org.cishell.framework.data.Data; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; -import org.osgi.service.log.LogService; import org.osgi.service.metatype.MetaTypeProvider; import org.osgi.service.metatype.MetaTypeService; @@ -47,6 +32,7 @@ public StaticExecutableAlgorithmFactory() {} + //pretty sure this isn't used by anything, but I fear deleting it. public StaticExecutableAlgorithmFactory(String algName, BundleContext bContext) { this.algName = algName; this.bContext = bContext; @@ -69,183 +55,10 @@ } public Algorithm createAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) { - return new StaticExecutableAlgorithm(data, parameters, context); + return new StaticExecutableAlgorithm(data, parameters, context, this.bContext, this.algName); } public MetaTypeProvider createParameters(Data[] data) { return provider; } - - private class StaticExecutableAlgorithm implements Algorithm, ProgressTrackable { - private String ALGORITHM; - private String ALGORITHM_MACOSX_PPC; - private String MACOSX; - private String ALGORITHM_WIN32; - private String WIN32; - private String ALGORITHM_LINUX_X86; - private String LINUX; - private String ALGORITHM_DEFAULT; - Data[] data; - Dictionary parameters; - CIShellContext context; - LogService logger; - - private ProgressMonitor monitor; - - public StaticExecutableAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) - { - this.data = data; - this.parameters = parameters; - this.context = context; - logger = (LogService)context.getService(LogService.class.getName()); - - ALGORITHM = algName + "/"; - ALGORITHM_MACOSX_PPC = ALGORITHM + "macosx.ppc/"; - MACOSX = "macosx"; - ALGORITHM_WIN32 = ALGORITHM + "win32/"; - WIN32 = "win32"; - ALGORITHM_LINUX_X86 = ALGORITHM + "linux.x86/"; - LINUX = "linux"; - ALGORITHM_DEFAULT = ALGORITHM + "default/"; - } - - public Data[] execute() throws AlgorithmExecutionException { - try { - Properties serviceProps = getProperties("/"+algName+"/service.properties"); - Properties configProps = getProperties("/"+algName+"/config.properties"); - - serviceProps.putAll(configProps); - serviceProps.put("Algorithm-Directory", algName); - - StaticExecutableRunner runner = - new StaticExecutableRunner(bContext, context, serviceProps, parameters, data, monitor); - - copyFiles(runner.getTempDirectory()); - - return runner.execute(); - } catch (IOException e) { - throw new AlgorithmExecutionException(e.getMessage(), e); - } - } - - private void copyFiles(File dir) throws IOException, AlgorithmExecutionException { - Enumeration e = bContext.getBundle().getEntryPaths("/"+algName); - - Set entries = new HashSet(); - - while(e != null && e.hasMoreElements()) { - String entryPath = (String) e.nextElement(); - //logger.log(LogService.LOG_DEBUG, "entry: " + entryPath + "\n\n"); - if(entryPath.endsWith("/")) { - entries.add(entryPath); - } - } - - dir = new File(dir.getPath() + File.separator + algName); - dir.mkdirs(); - - String os = bContext.getProperty("osgi.os"); - String arch = bContext.getProperty("osgi.arch"); - - String path = null; - - //take the default, if there - if(entries.contains(ALGORITHM_DEFAULT)) { - String default_path = ALGORITHM_DEFAULT; - //logger.log(LogService.LOG_DEBUG, "base path: "+default_path+ - // "\n\t"+dir.getAbsolutePath() + "\n\n"); - copyDir(dir, default_path, 0); - } - - //but override with platform idiosyncracies - if(os.equals(WIN32) && entries.contains(ALGORITHM_WIN32)) { - path = ALGORITHM_WIN32; - } else if(os.equals(MACOSX) && entries.contains(ALGORITHM_MACOSX_PPC)) { - path = ALGORITHM_MACOSX_PPC; - } else if(os.equals(LINUX) && entries.contains(ALGORITHM_LINUX_X86)) { - path = ALGORITHM_LINUX_X86; - } - - String platform_path = ALGORITHM + os + "." + arch + "/"; - //and always override anything with an exact match - if(entries.contains(platform_path)) { - path = platform_path; - } - - if (path == null) { - throw new AlgorithmExecutionException("Unable to find compatible executable"); - } else { - //logger.log(LogService.LOG_DEBUG, "base path: "+path+ - // "\n\t"+dir.getAbsolutePath() + "\n\n"); - copyDir(dir, path, 0); - } - } - - private void copyDir(File dir, String dirPath, int depth) throws IOException { - Enumeration e = bContext.getBundle().getEntryPaths(dirPath); - - //dirPath = dirPath.replace('/', File.separatorChar); - - while (e != null && e.hasMoreElements()) { - String path = (String)e.nextElement(); - - if (path.endsWith("/")) { - String dirName = getName(path); - - File subDirectory = new File(dir.getPath() + File.separator + dirName); - subDirectory.mkdirs(); - //logger.log(LogService.LOG_DEBUG, "path: " + depth + " "+path+ - // "\n\t"+subDirectory.getAbsolutePath() + "\n\n"); - copyDir(subDirectory, path, depth + 1); - } else { - copyFile(dir, path); - } - } - } - - private void copyFile(File dir, String path) throws IOException { - URL entry = bContext.getBundle().getEntry(path); - - //path = path.replace('/', File.separatorChar); - String file = getName(path); - FileOutputStream outStream = new FileOutputStream(dir.getPath() + File.separator + file); - - ReadableByteChannel in = Channels.newChannel(entry.openStream()); - FileChannel out = outStream.getChannel(); - out.transferFrom(in, 0, Integer.MAX_VALUE); - - in.close(); - out.close(); - } - - private String getName(String path) { - if (path.lastIndexOf('/') == path.length()-1) { - path = path.substring(0, path.length()-1); - } - - path = path.substring(path.lastIndexOf('/')+1, - path.length()); - - return path; - } - - private Properties getProperties(String entry) throws IOException { - URL url = bContext.getBundle().getEntry(entry); - Properties props = null; - - if (url != null) { - props = new Properties(); - props.load(url.openStream()); - } - return props; - } - - public ProgressMonitor getProgressMonitor() { - return this.monitor; - } - - public void setProgressMonitor(ProgressMonitor monitor) { - this.monitor = monitor; - } - } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |