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