diff options
author | tkellner <tkellner@174cde9d-5d70-4d2a-aa98-46368bc2aaf7> | 2013-04-10 19:18:04 +0000 |
---|---|---|
committer | tkellner <tkellner@174cde9d-5d70-4d2a-aa98-46368bc2aaf7> | 2013-04-10 19:18:04 +0000 |
commit | 77531710fb5bd3d01e97f70da4158109a4de3f82 (patch) | |
tree | a6eb576dab4a01a3f024980b15b1c9bc78c05cda /pdf-over-gui/src/main/java/at/asit/pdfover/gui | |
parent | 875155e11a8a854ae280f772f860a2a9556d44af (diff) | |
download | mocca-77531710fb5bd3d01e97f70da4158109a4de3f82.tar.gz mocca-77531710fb5bd3d01e97f70da4158109a4de3f82.tar.bz2 mocca-77531710fb5bd3d01e97f70da4158109a4de3f82.zip |
Only build 1 pkg per OS, load SWT libs @ runtime
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-over/trunk@244 174cde9d-5d70-4d2a-aa98-46368bc2aaf7
Diffstat (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui')
-rw-r--r-- | pdf-over-gui/src/main/java/at/asit/pdfover/gui/Main.java | 21 | ||||
-rw-r--r-- | pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTLoader.java | 89 |
2 files changed, 102 insertions, 8 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Main.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Main.java index e650cfc4..42a811be 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Main.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Main.java @@ -21,6 +21,8 @@ import java.io.File; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.asit.pdfover.gui.exceptions.InitializationException; +import at.asit.pdfover.gui.utils.SWTLoader; import at.asit.pdfover.gui.workflow.StateMachineImpl; /** @@ -37,20 +39,23 @@ public class Main { * @param args */ public static void main(String[] args) { - StateMachineImpl stateMachine = new StateMachineImpl(args); - + log.debug("Loading SWT libraries"); //$NON-NLS-1$ + try { + SWTLoader.loadSWT(); + } catch (InitializationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + File configDir = new File(System.getProperty("user.home")+"/.pdfover"); //$NON-NLS-1$//$NON-NLS-2$ - if(!configDir.exists()) { configDir.mkdir(); - } - + + StateMachineImpl stateMachine = new StateMachineImpl(args); + log.debug("Starting stateMachine ..."); //$NON-NLS-1$ - stateMachine.start(); - log.debug("Ended stateMachine ..."); //$NON-NLS-1$ } - } diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTLoader.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTLoader.java new file mode 100644 index 00000000..f966bb71 --- /dev/null +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTLoader.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.gui.utils; + +// Imports +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.gui.exceptions.InitializationException; + +/** + * + */ +@SuppressWarnings("nls") +public class SWTLoader { + /** + * SLF4J Logger instance + **/ + private static final Logger log = LoggerFactory.getLogger(SWTLoader.class); + + /** + * Load the SWT library for this OS + * @throws InitializationException Loading failed + */ + public static void loadSWT() throws InitializationException { + try { + log.debug("loading " + getSwtJarName()); + URLClassLoader cl = (URLClassLoader)SWTLoader.class.getClassLoader(); + Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); + addUrlMethod.setAccessible(true); + + String swtLibPath = "lib-swt/" + getSwtJarName(); + File swtLib = new File(swtLibPath); + if (!swtLib.isFile()) + throw new SWTLoadFailedException("Library " + swtLibPath + " not found"); + log.debug("Adding " + swtLib + " to ClassLoader..."); + addUrlMethod.invoke(cl, swtLib.toURI().toURL()); + log.debug("Success."); + } catch (Exception e) { + throw new InitializationException("SWT loading failed", e); + } + } + + private static int getArchBits() { + String arch = System.getProperty("os.arch"); + return arch.contains("64") ? 64 : 32; + } + + private static String getSwtJarName() throws SWTLoadFailedException { + String os = System.getProperty("os.name").toLowerCase(); + if (os.contains("win")) + os = "windows"; + else if (os.contains("mac")) + os = "mac"; + else if (os.contains("linux") || os.contains("nix")) + os = "linux"; + else { + log.error("Unknown OS: " + os); + throw new SWTLoadFailedException("Unknown OS: " + os); + } + return "swt-" + os + "-" + getArchBits() + ".jar"; + } + + private static class SWTLoadFailedException extends Exception { + private static final long serialVersionUID = 1L; + + SWTLoadFailedException(String msg) { + super(msg); + } + } +} |