|
| 1 | +/** |
| 2 | + * Copyright (c) 2006-2010 MoVe - Laboratoire d'Informatique de Paris 6 (LIP6). |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Jean-Baptiste VORON (LIP6) - Project Head / Initial contributor |
| 10 | + * Clément DÉMOULINS (LIP6) - Project Manager |
| 11 | + * Yann THIERRY-MIEG (LIP6) |
| 12 | + * |
| 13 | + * Official contacts: |
| 14 | + * coloane@lip6.fr |
| 15 | + * http://coloane.lip6.fr |
| 16 | + */ |
| 17 | +package fr.lip6.petrispot.binaries; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URI; |
| 22 | +import java.net.URISyntaxException; |
| 23 | +import java.net.URL; |
| 24 | +import java.util.Enumeration; |
| 25 | +import java.util.logging.Logger; |
| 26 | + |
| 27 | +import org.eclipse.core.runtime.FileLocator; |
| 28 | +import org.eclipse.core.runtime.Plugin; |
| 29 | +import org.osgi.framework.BundleContext; |
| 30 | + |
| 31 | +/** |
| 32 | + * The activator class controls the plug-in life cycle |
| 33 | + */ |
| 34 | +public class BinaryToolsPlugin extends Plugin { |
| 35 | + |
| 36 | + // The plug-in ID |
| 37 | + public static final String PLUGIN_ID = "fr.lip6.petrispot.binaries"; //$NON-NLS-1$ |
| 38 | + |
| 39 | + // The shared instance |
| 40 | + private static BinaryToolsPlugin plugin; |
| 41 | + |
| 42 | + private static URI petriUri = null; |
| 43 | + |
| 44 | + /** |
| 45 | + * The constructor |
| 46 | + */ |
| 47 | + public BinaryToolsPlugin() { |
| 48 | + } |
| 49 | + |
| 50 | + /** {@inheritDoc} */ |
| 51 | + public final void start(BundleContext context) throws Exception { |
| 52 | + super.start(context); |
| 53 | + plugin = this; |
| 54 | + } |
| 55 | + |
| 56 | + /** {@inheritDoc} */ |
| 57 | + public final void stop(BundleContext context) throws Exception { |
| 58 | + plugin = null; |
| 59 | + super.stop(context); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the shared instance |
| 64 | + * |
| 65 | + * @return the shared instance |
| 66 | + */ |
| 67 | + public static BinaryToolsPlugin getDefault() { |
| 68 | + return plugin; |
| 69 | + } |
| 70 | + |
| 71 | + private static final Logger log = Logger.getLogger("fr.lip6.move.gal"); //$NON-NLS-1$ |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns the URI of the petri64 binary for the current platform. |
| 75 | + */ |
| 76 | + public static URI getPetriURI() throws IOException { |
| 77 | + if (petriUri == null) { |
| 78 | + String relativePath = "bin/" + getPetriExecutableName(); |
| 79 | + URL resource = getDefault().getBundle().getResource(relativePath); |
| 80 | + if (resource == null) { |
| 81 | + log.severe("unable to find PetriSpot binary in path " + relativePath); |
| 82 | + Enumeration<URL> e = getDefault().getBundle().findEntries("bin/", "*", true); |
| 83 | + log.fine("Listing URLs available in bin/"); |
| 84 | + while (e.hasMoreElements()) { |
| 85 | + log.finer(e.nextElement().toString()); |
| 86 | + } |
| 87 | + throw new IOException("unable to find the PetriSpot binary"); |
| 88 | + } |
| 89 | + URL fileUrl = FileLocator.toFileURL(resource); |
| 90 | + try { |
| 91 | + petriUri = new URI(fileUrl.getProtocol(), fileUrl.getPath(), null); |
| 92 | + } catch (URISyntaxException e) { |
| 93 | + throw new IOException("Could not create a URI to access the PetriSpot binary:", e); |
| 94 | + } |
| 95 | + log.fine("Location of PetriSpot binary: " + petriUri); |
| 96 | + |
| 97 | + File executable = new File(petriUri); |
| 98 | + if (!executable.setExecutable(true)) { |
| 99 | + log.severe("unable to make PetriSpot executable [" + petriUri + "]"); |
| 100 | + throw new IOException("unable to make PetriSpot executable"); |
| 101 | + } |
| 102 | + } |
| 103 | + return petriUri; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the platform-specific executable name for PetriSpot. |
| 108 | + */ |
| 109 | + private static String getPetriExecutableName() throws IOException { |
| 110 | + String osName = System.getProperty("os.name").toLowerCase(); |
| 111 | + if (osName.contains("windows")) { |
| 112 | + return "petri64.exe"; |
| 113 | + } else if (osName.contains("linux")) { |
| 114 | + return "petri64"; |
| 115 | + } else { |
| 116 | + throw new IOException("System platform not supported by PetriSpot: " + osName); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments