Use filename in Log for incorrect passwords.
[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.io.File;
24 import java.sql.Connection;
25 import java.sql.SQLException;
26
27 import org.glom.libglom.Document;
28
29 import com.mchange.v2.c3p0.ComboPooledDataSource;
30
31 /**
32  * A class to hold configuration information for related to the glom document and db access.
33  * 
34  * @author Ben Konrath <ben@bagu.org>
35  * 
36  */
37 final class ConfiguredDocument {
38
39         private Document document;
40         private ComboPooledDataSource cpds;
41         private boolean authenticated = false;
42
43         @SuppressWarnings("unused")
44         private ConfiguredDocument() {
45         }
46
47         public ConfiguredDocument(Document document) throws PropertyVetoException {
48
49                 // load the jdbc driver
50                 cpds = new ComboPooledDataSource();
51                 try {
52                         cpds.setDriverClass("org.postgresql.Driver");
53                 } catch (PropertyVetoException e) {
54                         Log.fatal("Error loading the PostgreSQL JDBC driver."
55                                         + " Is the PostgreSQL JDBC jar available to the servlet?", e);
56                         throw e;
57                 }
58
59                 // setup the JDBC driver for the current glom document
60                 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
61                                 + document.get_connection_database());
62
63                 this.document = document;
64         }
65
66         /**
67          * Sets the username and password for the database associated with the Glom document.
68          * 
69          * @return true if the username and password works, false otherwise
70          */
71         boolean setUsernameAndPassword(String username, String password) throws SQLException {
72                 cpds.setUser(username);
73                 cpds.setPassword(password);
74
75                 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
76                 cpds.setAcquireRetryAttempts(1);
77                 Connection conn = null;
78                 try {
79                         // FIXME find a better way to check authentication
80                         // it's possible that the connection could be failing for another reason
81                         conn = cpds.getConnection();
82                         authenticated = true;
83                 } catch (SQLException e) {
84                         Log.info(getFileName(document.get_file_uri()), "The username or password is not correct.");
85                         authenticated = false;
86                 } finally {
87                         if (conn != null)
88                                 conn.close();
89                         cpds.setAcquireRetryAttempts(acquireRetryAttempts);
90                 }
91                 return authenticated;
92         }
93
94         public Document getDocument() {
95                 return document;
96         }
97
98         public ComboPooledDataSource getCpds() {
99                 return cpds;
100         }
101
102         public boolean isAuthenticated() {
103                 return authenticated;
104         }
105
106         private static String getFileName(String fileURI) {
107                 String[] splitURI = fileURI.split(File.separator);
108                 return splitURI[splitURI.length - 1];
109         }
110
111 }