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;
23 import java.sql.Connection;
24 import java.sql.SQLException;
26 import org.glom.libglom.Document;
28 import com.mchange.v2.c3p0.ComboPooledDataSource;
31 * A class to hold configuration information for related to the glom document and db access.
33 * @author Ben Konrath <ben@bagu.org>
36 final class ConfiguredDocument {
38 private Document document;
39 private ComboPooledDataSource cpds;
40 private boolean authenticated = false;
42 @SuppressWarnings("unused")
43 private ConfiguredDocument() {
46 public ConfiguredDocument(Document document) throws PropertyVetoException {
48 // load the jdbc driver
49 cpds = new ComboPooledDataSource();
51 cpds.setDriverClass("org.postgresql.Driver");
52 } catch (PropertyVetoException e) {
53 Log.fatal("Error loading the PostgreSQL JDBC driver."
54 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
58 // setup the JDBC driver for the current glom document
59 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
60 + document.get_connection_database());
62 this.document = document;
66 * Sets the username and password for the database associated with the Glom document.
68 * @return true if the username and password works, false otherwise
70 boolean setUsernameAndPassword(String username, String password) throws SQLException {
71 cpds.setUser(username);
72 cpds.setPassword(password);
74 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
75 cpds.setAcquireRetryAttempts(1);
76 Connection conn = null;
78 // FIXME find a better way to check authentication
79 // it's possible that the connection could be failing for another reason
80 conn = cpds.getConnection();
82 } catch (SQLException e) {
83 Log.info(document.get_database_title(), "Username and/or password are not correct.");
84 authenticated = false;
88 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
93 public Document getDocument() {
97 public ComboPooledDataSource getCpds() {
101 public boolean isAuthenticated() {
102 return authenticated;