2 * Copyright (C) 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;
22 import java.beans.PropertyVetoException;
24 import java.sql.Connection;
25 import java.sql.SQLException;
27 import org.glom.libglom.Document;
29 import com.mchange.v2.c3p0.ComboPooledDataSource;
32 * A class to hold configuration information for related to the glom document and db access.
34 * @author Ben Konrath <ben@bagu.org>
37 final class ConfiguredDocument {
39 private Document document;
40 private ComboPooledDataSource cpds;
41 private boolean authenticated = false;
43 @SuppressWarnings("unused")
44 private ConfiguredDocument() {
47 public ConfiguredDocument(Document document) throws PropertyVetoException {
49 // load the jdbc driver
50 cpds = new ComboPooledDataSource();
52 // We don't support sqlite or self-hosting yet.
53 if(document.get_hosting_mode() != Document.HostingMode.HOSTING_MODE_POSTGRES_CENTRAL) {
54 Log.fatal("Error configuring the database connection."
55 + " Only central PostgreSQL hosting is supported.");
56 // FIXME: Throw exception?
60 cpds.setDriverClass("org.postgresql.Driver");
61 } catch (PropertyVetoException e) {
62 Log.fatal("Error loading the PostgreSQL JDBC driver."
63 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
67 // setup the JDBC driver for the current glom document
68 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server()
69 + ":" + document.get_connection_port()
70 + "/" + document.get_connection_database());
72 this.document = document;
76 * Sets the username and password for the database associated with the Glom document.
78 * @return true if the username and password works, false otherwise
80 boolean setUsernameAndPassword(String username, String password) throws SQLException {
81 cpds.setUser(username);
82 cpds.setPassword(password);
84 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
85 cpds.setAcquireRetryAttempts(1);
86 Connection conn = null;
88 // FIXME find a better way to check authentication
89 // it's possible that the connection could be failing for another reason
90 conn = cpds.getConnection();
92 } catch (SQLException e) {
93 Log.info(getFileName(document.get_file_uri()), e.getMessage());
94 Log.info(getFileName(document.get_file_uri()), "Connection Failed. Maybe the username or password is not correct.");
95 authenticated = false;
99 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
101 return authenticated;
104 public Document getDocument() {
108 public ComboPooledDataSource getCpds() {
112 public boolean isAuthenticated() {
113 return authenticated;
116 private static String getFileName(String fileURI) {
117 String[] splitURI = fileURI.split(File.separator);
118 return splitURI[splitURI.length - 1];