1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.excel;
15 import java.io.IOException;
16 import java.io.PrintStream;
18 import java.net.URLDecoder;
19 import java.nio.charset.Charset;
20 import java.util.ArrayList;
21 import java.util.Enumeration;
22 import java.util.UUID;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
31 import org.eclipse.core.resources.IWorkspace;
32 import org.eclipse.core.resources.IWorkspaceRoot;
33 import org.eclipse.core.resources.ResourcesPlugin;
34 import org.eclipse.core.runtime.FileLocator;
35 import org.eclipse.core.runtime.IPath;
36 import org.eclipse.core.runtime.Path;
37 import org.eclipse.core.runtime.Platform;
38 import org.osgi.framework.Bundle;
39 import org.osgi.framework.BundleContext;
40 import org.simantics.excel.ExecEnvironment.ARCHType;
41 import org.simantics.excel.ExecEnvironment.OSType;
42 import org.simantics.utils.FileUtils;
46 private static Excel instance;
48 public static Excel getInstance(PrintStream out) throws ExcelException {
50 if(instance == null) {
51 if (Platform.inDevelopmentMode()) {
52 Bundle b = Activator.getDefault().getBundle();
53 URL url = FileLocator.find(b, new Path(""), null);
55 extractDir = new File(URLDecoder.decode(FileLocator.toFileURL(url).getPath(), "UTF-8"));
56 } catch (IOException e) {
57 e.printStackTrace(out);
58 throw new ExcelException(e);
63 } catch (IOException e) {
64 e.printStackTrace(out);
65 throw new ExcelException(e);
66 } catch (Throwable t) {
67 t.printStackTrace(out);
68 throw new ExcelException(t);
72 instance = new Excel(out);
80 public File getDirectory() throws IOException {
82 Bundle b = Platform.getBundle(Activator.PLUGIN_ID);
84 throw new AssertionError("Could not resolve bundle '" + Activator.PLUGIN_ID + "' although were running in its fragment. Should not happen.");
86 BundleContext context = b.getBundleContext();
88 throw new AssertionError("Could not get bundle context for bundle '" + Activator.PLUGIN_ID + "'. Bundle state is " + b.getState() + ".");
90 File extractDir = context.getDataFile("");
91 if (extractDir == null)
92 throw new IOException("Bundle '" + Activator.PLUGIN_ID + " context has no file system support. Cannot extract server executables.");
94 if (!extractDir.exists())
95 if (!extractDir.mkdir())
96 throw new IOException("Could not create directory '" + extractDir.getCanonicalPath() + "' for storing Balas solver data.");
102 public String getContainer() {
103 return UUID.randomUUID().toString();
106 public String getFile(String name) {
108 return getDirectory().getAbsolutePath() + "\\" + name;
109 } catch (IOException e) {
115 private Excel(PrintStream out) throws ExcelException {
118 Future<?> future = init_(extractDir + File.separator);
119 future.get(10, TimeUnit.SECONDS);
120 } catch (InterruptedException e) {
121 throw new ExcelException(e);
122 } catch (ExecutionException e) {
123 throw new ExcelException(e);
124 } catch (TimeoutException e) {
125 throw new ExcelException(e);
130 public static IPath getAbsolutePath(String inBundle, String fullpath) {
131 Bundle b = Platform.getBundle(inBundle);
134 return getAbsolutePath(b, fullpath);
137 public static IPath getAbsolutePath(Bundle inBundle, String fullpath) {
138 // System.out.println("getAbsolutePath: " + inBundle + ", " + fullpath);
139 IPath path = new Path(fullpath);
140 URL u = FileLocator.find(inBundle, path, null);
143 u = FileLocator.resolve(u);
144 // System.out.println(" PROTOCOL: " + u.getProtocol());
145 // System.out.println(" FILE: " + u.getFile());
146 // an absolute path is only available for the file protocol.
147 if ("file".equals(u.getProtocol())) {
148 IPath p = new Path(new File(u.getFile()).getAbsolutePath());
151 } catch (Exception e) {
157 private static final Charset ascii = Charset.forName("US-ASCII");
159 private static final String REQUIRED_FILES_DESCRIPTOR_FILE = "required_files.txt";
162 * List here all the files that are required from this bundle to be able to
163 * run the ProCoreServer executable. This is necessary for the starter core
164 * below to be able to extract all the needed files incase the bundle
165 * happens to be deployed as a JAR.
167 private static final String[] DEFAULT_REQUIRED_FILES = {
168 "SimanticsExcel.dll", "SimanticsExcel_64.dll"
172 * The extraction directory is stored as a static field so that it can be
173 * used to check whether the files have already been extracted.
175 static private File extractDir = null;
177 static private String[] requiredFiles = null;
179 static private boolean needExtraction = false;
181 private static IPath getAbsolutePath(String fullpath) {
182 Bundle b = Platform.getBundle(Activator.PLUGIN_ID);
185 // System.out.println("getAbsolutePath: " + inBundle + ", " + fullpath);
186 IPath path = new Path(fullpath);
187 URL u = FileLocator.find(b, path, null);
190 u = FileLocator.resolve(u);
191 // System.out.println(" PROTOCOL: " + u.getProtocol());
192 // System.out.println(" FILE: " + u.getFile());
193 // an absolute path is only available for the file protocol.
194 if ("file".equals(u.getProtocol())) {
195 IPath p = new Path(new File(u.getFile()).getAbsolutePath());
198 } catch (Exception e) {
204 static String[] getRequiredFiles() {
206 if (requiredFiles != null)
207 return requiredFiles;
209 Bundle b = Platform.getBundle(Activator.PLUGIN_ID);
213 ArrayList<Enumeration<?>> enu = new ArrayList<Enumeration<?>>();
215 enu.add(b.findEntries("/", "*.dll", true));
216 //enu.add(b.findEntries("/", "*.manifest", true));
218 ArrayList<String> filez = new ArrayList<String>();
220 for(Enumeration<?> e : enu) {
221 while(e.hasMoreElements()) {
222 URL url = (URL)e.nextElement();
223 filez.add(url.getFile());
224 // System.out.println(url.getFile());
228 requiredFiles = filez.toArray(new String[filez.size()]);
230 return requiredFiles;
234 private static File extractFiles() throws IOException {
235 Bundle b = Platform.getBundle(Activator.PLUGIN_ID);
237 throw new AssertionError("Could not resolve bundle '" + Activator.PLUGIN_ID + "' although were running in it. Should not happen.");
239 //System.out.println("bundle dir: " + baseDir);
240 for (String file : getRequiredFiles()) {
241 // FileLocator find files in fragments also, Bundle.getEntry won't do that.
242 URL url = FileLocator.find(b, new Path(file), null);
243 File fzz = new File(extractDir, file);
244 Path path = new Path(fzz.getAbsolutePath());
245 path.removeLastSegments(1).toFile().mkdirs();
246 FileUtils.copyResource(url, fzz, false);
251 public static final String EXCEL_FOLDER = "Excel"; //$NON-NLS-1$
253 public static void start(PrintStream out) throws IOException {
255 Bundle b = Platform.getBundle(Activator.PLUGIN_ID);
257 throw new AssertionError("Could not resolve bundle '" + Activator.PLUGIN_ID + "' although were running in its fragment. Should not happen.");
259 BundleContext context = b.getBundleContext();
261 throw new AssertionError("Could not get bundle context for bundle '" + Activator.PLUGIN_ID + "'. Bundle state is " + b.getState() + ".");
263 IWorkspace workspace = ResourcesPlugin.getWorkspace();
264 IWorkspaceRoot workspaceRoot = workspace.getRoot();
265 extractDir = new File(workspaceRoot.getLocation().toFile(), EXCEL_FOLDER);
267 if (!extractDir.exists())
268 if (!extractDir.mkdir())
269 throw new IOException("Could not create directory '" + extractDir.getCanonicalPath() + "' for storing Balas solver data.");
272 ExecEnvironment env = ExecEnvironment.calculate();
273 if (env.os == OSType.WINDOWS) {
274 if (env.arch == ARCHType.X86) {
275 asd = extractDir + "\\SimanticsExcel.dll";
276 } else if (env.arch == ARCHType.X86_64) {
277 asd = extractDir + "\\SimanticsExcel_64.dll";
282 File test = new File(asd);
284 needExtraction = false;
287 needExtraction = true;
290 // Resolve executable location
291 if (needExtraction) {
292 extractDir = extractFiles();
294 out.println("needExtraction=false");
300 private native int init();
301 private native String open(String fileName, String sheetName);
302 private native String getModifications(int handle);
303 private native int setDouble(int handle, int row, int column, double value);
304 private native int setString(int handle, int row, int column, String value);
305 private native int setName(int handle, int row, int column, String value);
306 private native int setVisible(int handle, boolean value);
307 private native int close(int handle);
309 // private native double getDouble(int handle, int row, int column);
310 // private native String getString(int handle, int row, int column);
312 final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
314 public Future<?> init_(final String path) {
315 return scheduler.submit(new Runnable() {
320 ExecEnvironment env = ExecEnvironment.calculate();
321 if (env.os == OSType.WINDOWS) {
322 if (env.arch == ARCHType.X86) {
323 asd = extractDir + "\\SimanticsExcel.dll";
324 } else if (env.arch == ARCHType.X86_64) {
325 asd = extractDir + "\\SimanticsExcel_64.dll";
334 public String open_(final String fileName, final String sheetName) {
336 return scheduler.submit(new Callable<String>() {
338 public String call() throws Exception {
339 return open(fileName, sheetName);
342 } catch (Exception e) {
348 public int setDouble_(final int handle, final int row, final int column, final double value) {
350 return scheduler.submit(new Callable<Integer>() {
352 public Integer call() throws Exception {
353 // System.out.println("Excel: setDouble at " + row + "-" + column);
354 return setDouble(handle, row, column, value);
357 } catch (Exception e) {
363 public int setString_(final int handle, final int row, final int column, final String value) {
365 return scheduler.submit(new Callable<Integer>() {
367 public Integer call() throws Exception {
368 // System.out.println("Excel: setString at " + row + "-" + column);
369 return setString(handle, row, column, value);
372 } catch (Exception e) {
378 public String getModifications_(final int handle) {
380 return scheduler.submit(new Callable<String>() {
382 public String call() throws Exception {
383 // System.out.println("Excel: setString at " + row + "-" + column);
384 return getModifications(handle);
387 } catch (Exception e) {
393 public int setName_(final int handle, final int row, final int column, final String value) {
395 return scheduler.submit(new Callable<Integer>() {
397 public Integer call() throws Exception {
398 // System.out.println("Excel: setString at " + row + "-" + column);
399 return setName(handle, row, column, value);
402 } catch (Exception e) {
408 public int setVisible_(final int handle, final Boolean value) {
410 return scheduler.submit(new Callable<Integer>() {
412 public Integer call() throws Exception {
413 // System.out.println("Excel: setString at " + row + "-" + column);
414 return setVisible(handle, value);
417 } catch (Exception e) {
423 public int close_(final int handle) {
425 return scheduler.submit(new Callable<Integer>() {
427 public Integer call() throws Exception {
428 // System.out.println("Excel: close " + handle);
429 int ret = close(handle);
430 // System.out.println("Excel: close = " + ret);
434 } catch (Exception e) {
440 // public double getDouble_(final int handle, final int row, final int column) {
442 // return scheduler.submit(new Callable<Double>() {
444 // public Double call() throws Exception {
445 // return getDouble(handle, row, column);
448 // } catch (Exception e) {
449 // e.printStackTrace();
450 // return Double.NaN;
454 // public String getString_(final int handle, final int row, final int column) {
456 // return scheduler.submit(new Callable<String>() {
458 // public String call() throws Exception {
459 // return getString(handle, row, column);
462 // } catch (Exception e) {
463 // e.printStackTrace();