Make the authentication popup work again.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / ConfiguredDocument.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.beans.PropertyVetoException;
23 import java.sql.Connection;
24 import java.sql.SQLException;
25
26 import org.glom.libglom.Document;
27
28 import com.mchange.v2.c3p0.ComboPooledDataSource;
29
30 /**
31  * A class to hold configuration information for related to the glom document and db access.
32  * 
33  * @author Ben Konrath <ben@bagu.org>
34  * 
35  */
36 final class ConfiguredDocument {
37
38         private Document document;
39         private ComboPooledDataSource cpds;
40         private boolean authenticated = false;
41
42         @SuppressWarnings("unused")
43         private ConfiguredDocument() {
44         }
45
46         public ConfiguredDocument(Document document) throws PropertyVetoException {
47
48                 // load the jdbc driver
49                 cpds = new ComboPooledDataSource();
50                 try {
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);
55                         throw e;
56                 }
57
58                 // setup the JDBC driver for the current glom document
59                 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
60                                 + document.get_connection_database());
61
62                 this.document = document;
63         }
64
65         /**
66          * Sets the username and password for the database associated with the Glom document.
67          * 
68          * @return true if the username and password works, false otherwise
69          */
70         boolean setUsernameAndPassword(String username, String password) throws SQLException {
71                 cpds.setUser(username);
72                 cpds.setPassword(password);
73
74                 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
75                 cpds.setAcquireRetryAttempts(1);
76                 Connection conn = null;
77                 try {
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();
81                         authenticated = true;
82                 } catch (SQLException e) {
83                         Log.info(document.get_database_title(), "Username and/or password are not correct.");
84                         authenticated = false;
85                 } finally {
86                         if (conn != null)
87                                 conn.close();
88                         cpds.setAcquireRetryAttempts(acquireRetryAttempts);
89                 }
90                 return authenticated;
91         }
92
93         public Document getDocument() {
94                 return document;
95         }
96
97         public ComboPooledDataSource getCpds() {
98                 return cpds;
99         }
100
101         public boolean isAuthenticated() {
102                 return authenticated;
103         }
104
105 }