1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.project.management;
\r
14 import java.io.File;
\r
15 import java.io.FileOutputStream;
\r
16 import java.io.IOException;
\r
17 import java.io.InputStream;
\r
18 import java.lang.reflect.InvocationTargetException;
\r
19 import java.lang.reflect.Method;
\r
20 import java.net.URL;
\r
21 import java.net.URLDecoder;
\r
22 import java.util.zip.ZipEntry;
\r
23 import java.util.zip.ZipInputStream;
\r
25 import org.apache.log4j.Logger;
\r
26 import org.simantics.db.DatabaseUserAgent;
\r
27 import org.simantics.db.Driver;
\r
28 import org.simantics.db.Manager;
\r
29 import org.simantics.db.exception.DatabaseException;
\r
30 import org.simantics.utils.FileUtils;
\r
32 public class ServerManagerFactory {
\r
33 public static ServerManager create(String databaseDriverId, String address) throws IOException, DatabaseException {
\r
34 Driver driver = Manager.getDriver(databaseDriverId);
\r
36 throw new IllegalArgumentException("Database driver for ID " + databaseDriverId + " Could not be found!");
\r
37 System.out.println("ServerManagerFactory.create called with databaseId=" + databaseDriverId + " and driver is " + driver.toString());
\r
38 DatabaseUserAgent agent = Manager.getUserAgent(databaseDriverId);
\r
40 driver.setDatabaseUserAgent(address, agent);
\r
41 return new ServerManager(driver);
\r
44 * Create a server manager in OSGi platform.
\r
47 * @throws IOException
\r
48 * @throws ClassNotFoundException
\r
49 * @throws RuntimeException unexpected problem with OSGi environment
\r
51 public static ServerManager createOSGI() throws IOException, RuntimeException {
\r
53 ClassLoader cl = ServerManager.class.getClassLoader();
\r
55 // Object bundle = Platform.getBundle("org.simantics.db.build");
\r
56 Class<?> Platform_class = cl.loadClass("org.eclipse.core.runtime.Platform");
\r
57 Method Platform_getBundle = Platform_class.getMethod("getBundle", String.class);
\r
58 Object bundle = Platform_getBundle.invoke(null, "org.simantics.db.build");
\r
59 if (bundle==null) throw new RuntimeException("Bundle org.simantics.db.build not found.");
\r
61 // URL db_build_url = bundle.getEntry("/");
\r
62 Class<?> Bundle_class = cl.loadClass("org.osgi.framework.Bundle");
\r
63 Method Bundle_getEntry = Bundle_class.getMethod("getEntry", String.class);
\r
64 URL db_build_url = (URL) Bundle_getEntry.invoke(bundle, "/");
\r
66 // URL db_build_file_url = FileLocator.toFileURL( db_build_url );
\r
67 Class<?> FileLocator_class = cl.loadClass("org.eclipse.core.runtime.FileLocator");
\r
68 Method FileLocator_toFileURL = FileLocator_class.getMethod("toFileURL", URL.class);
\r
69 URL db_build_file_url = (URL) FileLocator_toFileURL.invoke(null, db_build_url);
\r
71 @SuppressWarnings("unused")
\r
72 String buildFile = URLDecoder.decode(db_build_file_url.getPath(), "UTF-8");
\r
74 // File db_build_file = new File( buildFile );
\r
75 //return new ServerManager( db_build_file );
\r
76 throw new RuntimeException("ServerManager.createOSGI not implemented.");
\r
77 } catch( ClassNotFoundException e ) {
\r
78 throw new RuntimeException(e);
\r
79 } catch (SecurityException e) {
\r
80 throw new RuntimeException(e);
\r
81 } catch (NoSuchMethodException e) {
\r
82 throw new RuntimeException(e);
\r
83 } catch (IllegalArgumentException e) {
\r
84 throw new RuntimeException(e);
\r
85 } catch (IllegalAccessException e) {
\r
86 throw new RuntimeException(e);
\r
87 } catch (InvocationTargetException e) {
\r
88 throw new RuntimeException(e.getCause());
\r
93 * Create a server manager in an POJO environment.
\r
96 * @throws IOException
\r
98 public static ServerManager createPOJO() throws IOException {
\r
99 String tempPath = System.getenv("tmp");
\r
100 if (tempPath==null) tempPath = "c:/temp/";
\r
101 File driverDir = new File(tempPath+"/core_drivers");
\r
102 if (!driverDir.exists()) {
\r
103 System.out.println("Extracting Core drivers to "+driverDir);
\r
104 driverDir.mkdirs();
\r
105 ServerManagerFactory.extractDrivers(driverDir);
\r
107 System.out.println("Loading Core drivers from "+driverDir);
\r
109 //return new ServerManager(driverDir);
\r
110 throw new RuntimeException("ServerManager.createPOJO not implemented.");
\r
113 public static ServerManager createPOJO(File driverDir) throws IOException {
\r
114 //return new ServerManager(driverDir);
\r
115 throw new RuntimeException("ServerManager.createPOJO not implemented.");
\r
118 static ServerManager cached;
\r
120 public static boolean isOSGi() {
\r
122 ServerManager.class.getClassLoader().loadClass("org.osgi.framework.Bundle");
\r
124 } catch (ClassNotFoundException e) {
\r
129 public synchronized static ServerManager getServerManager() {
\r
130 if (cached!=null) return cached;
\r
133 return createOSGI();
\r
134 } catch(RuntimeException e) {
\r
135 return createPOJO();
\r
137 } catch (IOException e) {
\r
138 throw new RuntimeException(e);
\r
144 * Extracts drivers files to a location.
\r
146 * @param path location where to extract application files
\r
147 * @throws IOException
\r
149 public static void extractDrivers(File path)
\r
152 // Extract org.simantics.db.build.zip to workspace
\r
153 InputStream is = ServerManager.class.getResource("org.simantics.db.build.zip").openStream();
\r
155 extractZip(is, path);
\r
162 * Extract a zip file into a directory
\r
165 * @param dst directory
\r
166 * @throws IOException
\r
168 private static void extractZip(InputStream zipInput, File dst) throws IOException {
\r
169 Logger myLogger = Logger.getLogger(FileUtils.class);
\r
170 byte[] buf = new byte[8192];
\r
171 ZipInputStream zis = new ZipInputStream(zipInput);
\r
174 entry = zis.getNextEntry();
\r
175 while (entry != null) {
\r
176 // for each entry to be extracted
\r
177 String name = entry.getName();
\r
178 myLogger.debug("Extracting "+name);
\r
179 File file = new File(dst, name);
\r
181 if (entry.isDirectory())
\r
183 if ( !file.exists() ) file.mkdirs();
\r
185 File parent = file.getParentFile();
\r
186 if (!parent.exists()) parent.mkdirs();
\r
187 if (!file.exists()) file.createNewFile();
\r
189 FileOutputStream fileoutputstream = new FileOutputStream(file);
\r
192 while ((n = zis.read(buf, 0, buf.length)) > -1)
\r
193 fileoutputstream.write(buf, 0, n);
\r
195 fileoutputstream.close();
\r
200 entry = zis.getNextEntry();
\r