]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/ServerManagerFactory.java
c34f9ef8019ec285bc68585467dcf81fc4c67791
[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         System.out.println("ServerManagerFactory.create called with databaseId=" + databaseId + " and driver is " + driver.toString());\r
36         DatabaseUserAgent agent = Manager.getUserAgent(databaseId);\r
37         if (agent != null)\r
38             driver.setDatabaseUserAgent(address, agent);\r
39         return new ServerManager(driver);\r
40     }\r
41         /**\r
42          * Create a server manager in OSGi platform.\r
43          *\r
44          * @return\r
45          * @throws IOException\r
46          * @throws ClassNotFoundException\r
47          * @throws RuntimeException unexpected problem with OSGi environment\r
48          */\r
49         public static ServerManager createOSGI() throws IOException, RuntimeException {\r
50                 try {\r
51                         ClassLoader cl = ServerManager.class.getClassLoader();\r
52 \r
53                         // Object bundle = Platform.getBundle("org.simantics.db.build");\r
54                         Class<?> Platform_class = cl.loadClass("org.eclipse.core.runtime.Platform");\r
55                         Method Platform_getBundle = Platform_class.getMethod("getBundle", String.class);\r
56                         Object bundle = Platform_getBundle.invoke(null, "org.simantics.db.build");\r
57                         if (bundle==null) throw new RuntimeException("Bundle org.simantics.db.build not found.");\r
58 \r
59                         // URL db_build_url = bundle.getEntry("/");\r
60                         Class<?> Bundle_class = cl.loadClass("org.osgi.framework.Bundle");\r
61                         Method Bundle_getEntry = Bundle_class.getMethod("getEntry", String.class);\r
62                         URL db_build_url = (URL) Bundle_getEntry.invoke(bundle, "/");\r
63 \r
64                 // URL db_build_file_url = FileLocator.toFileURL( db_build_url );\r
65                         Class<?> FileLocator_class = cl.loadClass("org.eclipse.core.runtime.FileLocator");\r
66                         Method FileLocator_toFileURL = FileLocator_class.getMethod("toFileURL", URL.class);\r
67                         URL db_build_file_url = (URL) FileLocator_toFileURL.invoke(null, db_build_url);\r
68 \r
69                         @SuppressWarnings("unused")\r
70             String buildFile = URLDecoder.decode(db_build_file_url.getPath(), "UTF-8");\r
71 \r
72 //              File db_build_file = new File( buildFile );\r
73                 //return new ServerManager( db_build_file );\r
74                 throw new RuntimeException("ServerManager.createOSGI not implemented.");\r
75                 } catch( ClassNotFoundException e ) {\r
76                         throw new RuntimeException(e);\r
77                 } catch (SecurityException e) {\r
78                         throw new RuntimeException(e);\r
79                 } catch (NoSuchMethodException e) {\r
80                         throw new RuntimeException(e);\r
81                 } catch (IllegalArgumentException e) {\r
82                         throw new RuntimeException(e);\r
83                 } catch (IllegalAccessException e) {\r
84                         throw new RuntimeException(e);\r
85                 } catch (InvocationTargetException e) {\r
86                         throw new RuntimeException(e);\r
87                 }\r
88         }\r
89 \r
90         /**\r
91          * Create a server manager in an POJO environment.\r
92          *\r
93          * @return\r
94          * @throws IOException\r
95          */\r
96         public static ServerManager createPOJO() throws IOException {\r
97                 String tempPath = System.getenv("tmp");\r
98                 if (tempPath==null) tempPath = "c:/temp/";\r
99                 File driverDir = new File(tempPath+"/core_drivers");\r
100                 if (!driverDir.exists()) {\r
101                         System.out.println("Extracting Core drivers to "+driverDir);\r
102                         driverDir.mkdirs();\r
103                         ServerManagerFactory.extractDrivers(driverDir);\r
104                 } else {\r
105                         System.out.println("Loading Core drivers from "+driverDir);\r
106                 }\r
107                 //return new ServerManager(driverDir);\r
108         throw new RuntimeException("ServerManager.createPOJO not implemented.");\r
109         }\r
110 \r
111     public static ServerManager createPOJO(File driverDir) throws IOException {\r
112         //return new ServerManager(driverDir);\r
113         throw new RuntimeException("ServerManager.createPOJO not implemented.");\r
114     }\r
115 \r
116     static ServerManager cached;\r
117 \r
118     public static boolean isOSGi() {\r
119         try {\r
120                 ServerManager.class.getClassLoader().loadClass("org.osgi.framework.Bundle");\r
121                 return true;\r
122         } catch (ClassNotFoundException e) {\r
123                 return false;\r
124         }\r
125     }\r
126 \r
127     public synchronized static ServerManager getServerManager() {\r
128         if (cached!=null) return cached;\r
129         try {\r
130                 try {\r
131                         return createOSGI();\r
132                 } catch(RuntimeException e) {\r
133                         return createPOJO();\r
134                 }\r
135         } catch (IOException e) {\r
136                 throw new RuntimeException(e);\r
137         }\r
138     }\r
139 \r
140 \r
141     /**\r
142      * Extracts drivers files to a location.\r
143      *\r
144      * @param path location where to extract application files\r
145      * @throws IOException\r
146      */\r
147         public static void extractDrivers(File path)\r
148         throws IOException\r
149         {\r
150                 // Extract org.simantics.db.build.zip to workspace\r
151                 InputStream is = ServerManager.class.getResource("org.simantics.db.build.zip").openStream();\r
152                 try {\r
153                         extractZip(is, path);\r
154                 } finally {\r
155                         is.close();\r
156                 }\r
157         }\r
158 \r
159     /**\r
160      * Extract a zip file into a directory\r
161      *\r
162      * @param zipInput\r
163      * @param dst directory\r
164      * @throws IOException\r
165      */\r
166     private static void extractZip(InputStream zipInput, File dst) throws IOException {\r
167         Logger myLogger = Logger.getLogger(FileUtils.class);\r
168         byte[] buf = new byte[8192];\r
169         ZipInputStream zis = new ZipInputStream(zipInput);\r
170         ZipEntry entry;\r
171 \r
172         entry = zis.getNextEntry();\r
173         while (entry != null) {\r
174             // for each entry to be extracted\r
175             String name = entry.getName();\r
176             myLogger.debug("Extracting "+name);\r
177             File file = new File(dst, name);\r
178 \r
179             if (entry.isDirectory())\r
180             {\r
181                 if ( !file.exists() ) file.mkdirs();\r
182             } else {\r
183                 File parent = file.getParentFile();\r
184                 if (!parent.exists()) parent.mkdirs();\r
185                 if (!file.exists()) file.createNewFile();\r
186 \r
187                 FileOutputStream fileoutputstream = new FileOutputStream(file);\r
188                 try {\r
189                     int n = 0;\r
190                     while ((n = zis.read(buf, 0, buf.length)) > -1)\r
191                         fileoutputstream.write(buf, 0, n);\r
192                 } finally {\r
193                     fileoutputstream.close();\r
194                 }\r
195             }\r
196 \r
197             zis.closeEntry();\r
198             entry = zis.getNextEntry();\r
199         }// while\r
200 \r
201         zis.close();\r
202     }\r
203 \r
204 \r
205 }\r
206 \r