]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/ServerManagerFactory.java
Include acorn db in db.client feature and make it the default db driver
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / ServerManagerFactory.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.project.management;\r
13 \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
24 \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
31 \r
32 public class ServerManagerFactory {\r
33     public static ServerManager create(String databaseId, String address) throws IOException, DatabaseException {\r
34         Driver driver = Manager.getDriver(databaseId);\r
35         if (driver == null)\r
36             throw new IllegalArgumentException("Database driver with ID " + databaseId + " could not be found!");\r
37         System.out.println("ServerManagerFactory.create called with id " + databaseId + ", driver is " + driver.toString());\r
38         DatabaseUserAgent agent = Manager.getUserAgent(databaseId);\r
39         if (agent != null)\r
40             driver.setDatabaseUserAgent(address, agent);\r
41         return new ServerManager(driver);\r
42     }\r
43         /**\r
44          * Create a server manager in OSGi platform.\r
45          *\r
46          * @return\r
47          * @throws IOException\r
48          * @throws ClassNotFoundException\r
49          * @throws RuntimeException unexpected problem with OSGi environment\r
50          */\r
51         public static ServerManager createOSGI() throws IOException, RuntimeException {\r
52                 try {\r
53                         ClassLoader cl = ServerManager.class.getClassLoader();\r
54 \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
60 \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
65 \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
70 \r
71                         @SuppressWarnings("unused")\r
72             String buildFile = URLDecoder.decode(db_build_file_url.getPath(), "UTF-8");\r
73 \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
89                 }\r
90         }\r
91 \r
92         /**\r
93          * Create a server manager in an POJO environment.\r
94          *\r
95          * @return\r
96          * @throws IOException\r
97          */\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
106                 } else {\r
107                         System.out.println("Loading Core drivers from "+driverDir);\r
108                 }\r
109                 //return new ServerManager(driverDir);\r
110         throw new RuntimeException("ServerManager.createPOJO not implemented.");\r
111         }\r
112 \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
116     }\r
117 \r
118     static ServerManager cached;\r
119 \r
120     public static boolean isOSGi() {\r
121         try {\r
122                 ServerManager.class.getClassLoader().loadClass("org.osgi.framework.Bundle");\r
123                 return true;\r
124         } catch (ClassNotFoundException e) {\r
125                 return false;\r
126         }\r
127     }\r
128 \r
129     public synchronized static ServerManager getServerManager() {\r
130         if (cached!=null) return cached;\r
131         try {\r
132                 try {\r
133                         return createOSGI();\r
134                 } catch(RuntimeException e) {\r
135                         return createPOJO();\r
136                 }\r
137         } catch (IOException e) {\r
138                 throw new RuntimeException(e);\r
139         }\r
140     }\r
141 \r
142 \r
143     /**\r
144      * Extracts drivers files to a location.\r
145      *\r
146      * @param path location where to extract application files\r
147      * @throws IOException\r
148      */\r
149         public static void extractDrivers(File path)\r
150         throws IOException\r
151         {\r
152                 // Extract org.simantics.db.build.zip to workspace\r
153                 InputStream is = ServerManager.class.getResource("org.simantics.db.build.zip").openStream();\r
154                 try {\r
155                         extractZip(is, path);\r
156                 } finally {\r
157                         is.close();\r
158                 }\r
159         }\r
160 \r
161     /**\r
162      * Extract a zip file into a directory\r
163      *\r
164      * @param zipInput\r
165      * @param dst directory\r
166      * @throws IOException\r
167      */\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
172         ZipEntry entry;\r
173 \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
180 \r
181             if (entry.isDirectory())\r
182             {\r
183                 if ( !file.exists() ) file.mkdirs();\r
184             } else {\r
185                 File parent = file.getParentFile();\r
186                 if (!parent.exists()) parent.mkdirs();\r
187                 if (!file.exists()) file.createNewFile();\r
188 \r
189                 FileOutputStream fileoutputstream = new FileOutputStream(file);\r
190                 try {\r
191                     int n = 0;\r
192                     while ((n = zis.read(buf, 0, buf.length)) > -1)\r
193                         fileoutputstream.write(buf, 0, n);\r
194                 } finally {\r
195                     fileoutputstream.close();\r
196                 }\r
197             }\r
198 \r
199             zis.closeEntry();\r
200             entry = zis.getNextEntry();\r
201         }// while\r
202 \r
203         zis.close();\r
204     }\r
205 \r
206 \r
207 }\r
208 \r