2 * Copyright (C) 2010, 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.server;
23 import java.io.FilenameFilter;
24 import java.io.InputStream;
25 import java.sql.SQLException;
26 import java.util.ArrayList;
27 import java.util.Hashtable;
28 import java.util.Properties;
30 import javax.servlet.ServletException;
32 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
33 import org.glom.libglom.Document;
34 import org.glom.libglom.Glom;
35 import org.glom.libglom.TranslatableItem;
36 import org.glom.web.client.OnlineGlomService;
37 import org.glom.web.shared.DataItem;
38 import org.glom.web.shared.DetailsLayoutAndData;
39 import org.glom.web.shared.DocumentInfo;
40 import org.glom.web.shared.Documents;
41 import org.glom.web.shared.NavigationRecord;
42 import org.glom.web.shared.TypedDataItem;
43 import org.glom.web.shared.layout.LayoutGroup;
45 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
46 import com.mchange.v2.c3p0.DataSources;
49 * The servlet class for setting up the server side of Online Glom. The public methods in this class are the methods
50 * that can be called by the client side code.
52 @SuppressWarnings("serial")
53 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
55 private static final String GLOM_FILE_EXTENSION = ".glom";
57 // convenience class for dealing with the Online Glom configuration file
58 private class OnlineGlomProperties extends Properties {
59 public String getKey(final String value) {
60 for (final String key : stringPropertyNames()) {
61 if (getProperty(key).trim().equals(value))
68 private final Hashtable<String, ConfiguredDocument> documentMapping = new Hashtable<String, ConfiguredDocument>();
69 private Exception configurtionException = null;
72 * This is called when the servlet is started or restarted.
76 * @see javax.servlet.GenericServlet#init()
79 public void init() throws ServletException {
81 // All of the initialisation code is surrounded by a try/catch block so that the servlet can be in an
82 // initialised state and the error message can be retrieved by the client code.
84 // Find the configuration file. See this thread for background info:
85 // http://stackoverflow.com/questions/2161054/where-to-place-properties-files-in-a-jsp-servlet-web-application
86 // FIXME move onlineglom.properties to the WEB-INF folder (option number 2 from the stackoverflow question)
87 final OnlineGlomProperties config = new OnlineGlomProperties();
88 final InputStream is = Thread.currentThread().getContextClassLoader()
89 .getResourceAsStream("onlineglom.properties");
91 final String errorMessage = "onlineglom.properties not found.";
92 Log.fatal(errorMessage);
93 throw new Exception(errorMessage);
95 config.load(is); // can throw an IOException
97 // check if we can read the configured glom file directory
98 final String documentDirName = config.getProperty("glom.document.directory");
99 final File documentDir = new File(documentDirName);
100 if (!documentDir.isDirectory()) {
101 final String errorMessage = documentDirName + " is not a directory.";
102 Log.fatal(errorMessage);
103 throw new Exception(errorMessage);
105 if (!documentDir.canRead()) {
106 final String errorMessage = "Can't read the files in directory " + documentDirName + " .";
107 Log.fatal(errorMessage);
108 throw new Exception(errorMessage);
111 // get and check the glom files in the specified directory
112 final File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
114 public boolean accept(final File dir, final String name) {
115 return name.endsWith(GLOM_FILE_EXTENSION);
119 // don't continue if there aren't any Glom files to configure
120 if (glomFiles.length <= 0) {
121 final String errorMessage = "Unable to find any Glom documents in the configured directory "
123 + " . Check the onlineglom.properties file to ensure that 'glom.document.directory' is set to the correct directory.";
124 Log.error(errorMessage);
125 throw new Exception(errorMessage);
128 // Check to see if the native library of java libglom is visible to the JVM
129 if (!isNativeLibraryVisibleToJVM()) {
130 final String errorMessage = "The java-libglom shared library is not visible to the JVM."
131 + " Ensure that 'java.library.path' is set with the path to the java-libglom shared library.";
132 Log.error(errorMessage);
133 throw new Exception(errorMessage);
136 // Check for a specified default locale,
137 // for table titles, field titles, etc:
138 final String localeID = config.getProperty("glom.document.locale");
140 if (localeID != null && !localeID.isEmpty()) {
141 TranslatableItem.set_current_locale(localeID.trim());
144 // This initialisation method can throw an UnsatisfiedLinkError if the java-libglom native library isn't
145 // available. But since we're checking for the error condition above, the UnsatisfiedLinkError will never be
149 // Allow a fake connection, so sqlbuilder_get_full_query() can work:
150 Glom.set_fake_connection();
152 for (final File glomFile : glomFiles) {
153 final Document document = new Document();
154 document.set_file_uri("file://" + glomFile.getAbsolutePath());
156 final boolean retval = document.load(error);
157 if (retval == false) {
159 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
160 message = "Could not find file: " + glomFile.getAbsolutePath();
162 message = "An unknown error occurred when trying to load file: " + glomFile.getAbsolutePath();
165 // continue with for loop because there may be other documents in the directory
169 final ConfiguredDocument configuredDocument = new ConfiguredDocument(document); // can throw a
170 // PropertyVetoException
172 // check if a username and password have been set and work for the current document
173 final String filename = glomFile.getName();
174 final String key = config.getKey(filename);
176 final String[] keyArray = key.split("\\.");
177 if (keyArray.length == 3 && "filename".equals(keyArray[2])) {
178 // username/password could be set, let's check to see if it works
179 final String usernameKey = key.replaceAll(keyArray[2], "username");
180 final String passwordKey = key.replaceAll(keyArray[2], "password");
181 configuredDocument.setUsernameAndPassword(config.getProperty(usernameKey).trim(),
182 config.getProperty(passwordKey)); // can throw an SQLException
186 // check the if the global username and password have been set and work with this document
187 if (!configuredDocument.isAuthenticated()) {
188 configuredDocument.setUsernameAndPassword(config.getProperty("glom.document.username").trim(),
189 config.getProperty("glom.document.password")); // can throw an SQLException
192 // The key for the hash table is the file name without the .glom extension and with spaces ( ) replaced
193 // with pluses (+). The space/plus replacement makes the key more friendly for URLs.
194 final String documentID = filename.substring(0, glomFile.getName().length() - GLOM_FILE_EXTENSION.length())
196 configuredDocument.setDocumentID(documentID);
197 documentMapping.put(documentID, configuredDocument);
200 } catch (final Exception e) {
201 // Don't throw the Exception so that servlet will be initialised and the error message can be retrieved.
202 configurtionException = e;
208 * Checks if the java-libglom native library is visible to the JVM.
210 * @return true if the java-libglom native library is visible to the JVM, false if it's not
212 private boolean isNativeLibraryVisibleToJVM() {
213 final String javaLibraryPath = System.getProperty("java.library.path");
215 // Go through all the library paths and check for the java_libglom .so.
216 for (final String libDirName : javaLibraryPath.split(":")) {
217 final File libDir = new File(libDirName);
219 if (!libDir.isDirectory())
221 if (!libDir.canRead())
224 final File[] libs = libDir.listFiles(new FilenameFilter() {
226 public boolean accept(final File dir, final String name) {
227 return name.matches("libjava_libglom-[0-9]\\.[0-9].+\\.so");
231 // if at least one directory had the .so, we're done
240 * This is called when the servlet is stopped or restarted.
242 * @see javax.servlet.GenericServlet#destroy()
245 public void destroy() {
246 Glom.libglom_deinit();
248 for (final String documenTitle : documentMapping.keySet()) {
249 final ConfiguredDocument configuredDoc = documentMapping.get(documenTitle);
251 DataSources.destroy(configuredDoc.getCpds());
252 } catch (final SQLException e) {
253 Log.error(documenTitle, "Error cleaning up the ComboPooledDataSource.", e);
261 * @see org.glom.web.client.OnlineGlomService#getConfigurationErrorMessage()
264 public String getConfigurationErrorMessage() {
265 if (configurtionException == null)
266 return "No configuration errors to report.";
267 else if (configurtionException.getMessage() == null)
268 return configurtionException.toString();
270 return configurtionException.getMessage();
276 * @see org.glom.web.client.OnlineGlomService#getDocumentInfo(java.lang.String)
279 public DocumentInfo getDocumentInfo(final String documentID) {
281 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
283 // FIXME check for authentication
285 return configuredDoc.getDocumentInfo();
292 * @see org.glom.web.client.OnlineGlomService#getListViewLayout(java.lang.String, java.lang.String)
295 public LayoutGroup getListViewLayout(final String documentID, final String tableName) {
296 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
298 // FIXME check for authentication
300 return configuredDoc.getListViewLayoutGroup(tableName);
306 * @see org.glom.web.client.OnlineGlomService#getListViewData(java.lang.String, java.lang.String, int, int)
309 public ArrayList<DataItem[]> getListViewData(final String documentID, final String tableName, final int start, final int length) {
310 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
311 if (!configuredDoc.isAuthenticated()) {
312 return new ArrayList<DataItem[]>();
314 return configuredDoc.getListViewData(tableName, start, length, false, 0, false);
320 * @see org.glom.web.client.OnlineGlomService#getSortedListViewData(java.lang.String, java.lang.String, int, int,
324 public ArrayList<DataItem[]> getSortedListViewData(final String documentID, final String tableName,
325 final int start, final int length,
326 final int sortColumnIndex, final boolean isAscending) {
327 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
328 if (!configuredDoc.isAuthenticated()) {
329 return new ArrayList<DataItem[]>();
331 return configuredDoc.getListViewData(tableName, start, length, true, sortColumnIndex, isAscending);
337 * @see org.glom.web.client.OnlineGlomService#getDocuments()
340 public Documents getDocuments() {
341 final Documents documents = new Documents();
342 for (final String documentID : documentMapping.keySet()) {
343 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
344 documents.addDocument(documentID, configuredDoc.getDocument().get_database_title());
352 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
355 public boolean isAuthenticated(final String documentID) {
356 return documentMapping.get(documentID).isAuthenticated();
362 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
366 public boolean checkAuthentication(final String documentID, final String username, final String password) {
367 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
369 return configuredDoc.setUsernameAndPassword(username, password);
370 } catch (final SQLException e) {
371 Log.error(documentID, "Unknown SQL Error checking the database authentication.", e);
379 * @see org.glom.web.client.OnlineGlomService#getDetailsData(java.lang.String, java.lang.String, java.lang.String)
382 public DataItem[] getDetailsData(final String documentID, final String tableName, final TypedDataItem primaryKeyValue) {
383 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
385 // FIXME check for authentication
387 return configuredDoc.getDetailsData(tableName, primaryKeyValue);
393 * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutAndData(java.lang.String, java.lang.String,
397 public DetailsLayoutAndData getDetailsLayoutAndData(final String documentID, final String tableName,
398 final TypedDataItem primaryKeyValue) {
399 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
400 if (configuredDoc == null)
403 // FIXME check for authentication
405 final DetailsLayoutAndData initalDetailsView = new DetailsLayoutAndData();
406 initalDetailsView.setLayout(configuredDoc.getDetailsLayoutGroup(tableName));
407 initalDetailsView.setData(configuredDoc.getDetailsData(tableName, primaryKeyValue));
409 return initalDetailsView;
415 * @see org.glom.web.client.OnlineGlomService#getRelatedListData(java.lang.String, java.lang.String, int, int)
418 public ArrayList<DataItem[]> getRelatedListData(final String documentID, final String tableName, final String relationshipName,
419 final TypedDataItem foreignKeyValue, final int start, final int length) {
420 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
422 // FIXME check for authentication
424 return configuredDoc.getRelatedListData(tableName, relationshipName, foreignKeyValue, start, length, false, 0,
431 * @see org.glom.web.client.OnlineGlomService#getSortedRelatedListData(java.lang.String, java.lang.String, int, int,
435 public ArrayList<DataItem[]> getSortedRelatedListData(final String documentID, final String tableName, final String relationshipName,
436 final TypedDataItem foreignKeyValue, final int start, final int length, final int sortColumnIndex, final boolean ascending) {
437 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
439 // FIXME check for authentication
441 return configuredDoc.getRelatedListData(tableName, relationshipName, foreignKeyValue, start, length, true,
442 sortColumnIndex, ascending);
446 public int getRelatedListRowCount(final String documentID, final String tableName, final String relationshipName,
447 final TypedDataItem foreignKeyValue) {
448 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
450 // FIXME check for authentication
452 return configuredDoc.getRelatedListRowCount(tableName, relationshipName, foreignKeyValue);
458 * @see org.glom.web.client.OnlineGlomService#getSuitableRecordToViewDetails(java.lang.String, java.lang.String,
459 * java.lang.String, java.lang.String)
462 public NavigationRecord getSuitableRecordToViewDetails(final String documentID, final String tableName,
463 final String relationshipName, final TypedDataItem primaryKeyValue) {
464 final ConfiguredDocument configuredDoc = documentMapping.get(documentID);
466 // FIXME check for authentication
468 return configuredDoc.getSuitableRecordToViewDetails(tableName, relationshipName, primaryKeyValue);