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  import java.util.Map;
27  
28  import org.apache.log4j.Logger;
29  import org.fosstrak.llrp.adaptor.exception.LLRPRuntimeException;
30  import org.fosstrak.llrp.client.ROAccessReportsRepository;
31  import org.fosstrak.llrp.client.repository.sql.roaccess.DerbyROAccessReportsRepository;
32  
33  /**
34   * The LLRP message repository implementation based on Sun JavaDB.
35   * Please make sure the derby.jar in the build path before you can
36   * start the database.
37   *
38   * @author Haoning Zhang
39   * @author sawielan
40   * @version 1.0
41   */
42  
43  public class DerbyRepository extends AbstractSQLRepository {
44  
45  	// Log4j instance.
46  	private static Logger log = Logger.getLogger(DerbyRepository.class);
47  	
48  	// a handle to the RO_ACCESS_REPORTS logging table.
49  	protected DerbyROAccessReportsRepository repoROAccessReports = null;
50  	
51  	// the JDBC protocol.
52  	private static final String DB_PROTOCOL = "jdbc:derby:";
53  	
54  	// whether to create the DB or not.
55  	private static final String DB_CREATE = ";create=true";
56  	
57  	/** the name of the property for the repository location in the args map. */
58  	public static final String ARG_REPO_LOCATION = "argRepoLocation";
59      
60  	/** the location of the repository. */
61  	private String repoLocation;
62  	
63  	/**
64  	 * construct a new java DB repository.
65  	 */
66  	public DerbyRepository() {
67  	}
68  	
69  	@Override
70  	public void initialize(Map<String, String> args) 
71  		throws LLRPRuntimeException {
72  		
73  		super.initialize(args);
74  		
75  		String argRepoLoc = null;
76  		if ((null == args) || (null == args.get(ARG_REPO_LOCATION))) {
77  			argRepoLoc = DB_NAME;
78  		} else {
79  			argRepoLoc = args.get(ARG_REPO_LOCATION);
80  		}
81  		repoLocation = argRepoLoc + DB_NAME;
82  	}
83  
84  	@Override
85  	protected Connection openConnection() throws Exception {
86  		 return DriverManager.getConnection(DB_PROTOCOL + repoLocation + DB_CREATE);
87  	}
88  
89  	public ROAccessReportsRepository getROAccessRepository() {
90  		if (!logROAccess) return null;
91  		
92  		if (null == repoROAccessReports) {
93  			log.debug("No RepoROAccessReports handle yet - Create a new one.");
94  			repoROAccessReports = new DerbyROAccessReportsRepository();
95  			try {
96  				repoROAccessReports.initialize(this);
97  			} catch (LLRPRuntimeException e) {
98  				log.error(String.format(
99  						"Could not initialize the RO_ACCESS_REPORTS repo: '%s'",
100 						e.getMessage()));
101 			}			
102 		}
103 		return repoROAccessReports;
104 	}
105 }
106