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