View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.client.repository.sql;
23  
24  import java.sql.Connection;
25  import java.sql.DriverManager;
26  
27  import org.apache.log4j.Logger;
28  import org.fosstrak.llrp.adaptor.exception.LLRPRuntimeException;
29  import org.fosstrak.llrp.client.ROAccessReportsRepository;
30  import org.fosstrak.llrp.client.repository.sql.roaccess.DerbyROAccessReportsRepository;
31  
32  /**
33   * The {@link PostgreSQLRepository} provides the basis for a PostgreSQL repository 
34   * back-end. The SQL-server is accessed via the PostgreSQL-JDBC connector.<br/>
35   * <h3>NOTICE:</h3>
36   * We share most SQL statements with the Derby implementation of the SQL 
37   * repository. However, the statement to create the LLRP messages table, and 
38   * the way how the connection gets established, differ.<br/>
39   * The user credentials as well as the JDBC connector URL are both obtained 
40   * from the eclipse preference store (The user can configure the settings 
41   * in the Preferences page in the tab LLRP-Commander).
42   * @author sawielan
43   *
44   */
45  public class PostgreSQLRepository extends AbstractSQLRepository {
46  
47  	/** the PostgreSQL JDBC driver. */
48  	protected final String DBDRIVER = "org.postgresql.Driver";
49  
50  	// a handle to the RO_ACCESS_REPORTS logging table. we can reuse the derby 
51  	// database, as there are no conflicts with the data-types.
52  	protected DerbyROAccessReportsRepository repoROAccessReports = null;
53  	
54  	/** default JDBC Connector URL. PostgreSQL needs all lower case*/
55  	public static final String JDBC_STR = 
56  		String.format("jdbc:postgresql://localhost:5432/%s", "llrpmsgdb");
57  	
58  	// log4j instance.
59  	private static Logger log = Logger.getLogger(PostgreSQLRepository.class);
60  	
61  	@Override
62  	protected String getDBDriver() {
63  		return DBDRIVER;
64  	}
65  
66  	@Override
67  	protected Connection openConnection() throws Exception {
68  		log.debug(String.format("Opening PostgreSQL connection with:\n" +
69  				"\tusername: %s\n " +
70  				"\tJDBC connector URL: %s\n", username, connectURL));
71  		
72  		return DriverManager.getConnection(connectURL, username, password);
73  	}
74  	
75  	@Override
76  	protected String sqlCreateTable() {
77  		return "CREATE TABLE " + TABLE_LLRP_REPOSITORY + " "
78  	    	+ "(MSG_ID CHAR(32),"
79  	    	+ "MSG_TYPE CHAR(32),"
80  	    	+ "READER CHAR(64),"
81  	    	+ "ADAPTER CHAR(64),"
82  	    	+ "MSG_TIME TIMESTAMP,"
83  	    	+ "STATUS CHAR(64),"
84  	    	+ "COMMENT VARCHAR(64),"
85  	    	+ "MARK CHAR(3),"
86  			+ "CONTENT text)";
87  	}
88  
89  	public ROAccessReportsRepository getROAccessRepository() {
90  		if (!logROAccess) return null;
91  		
92  		// for the RO_ACCESS_REPORTS repository, we can use the derby one as 
93  		// the SQL set used, works for both PostgreSQL and Derby (only in the case
94  		// RO_ACCESS_REPORTS repository!).
95  		if (null == repoROAccessReports) {
96  			log.debug("No RepoROAccessReports handle yet - Create a new one.");
97  			repoROAccessReports = new DerbyROAccessReportsRepository();
98  			try {
99  				repoROAccessReports.initialize(this);
100 			} catch (LLRPRuntimeException e) {
101 				log.error(String.format(
102 						"Could not initialize the RO_ACCESS_REPORTS repo: '%s'",
103 						e.getMessage()));
104 			}	
105 		}
106 		return repoROAccessReports;
107 	}
108 
109 }