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;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import org.apache.log4j.Logger;
29  import org.fosstrak.llrp.adaptor.exception.LLRPRuntimeException;
30  
31  /**
32   * helper class to instantiate the repository.
33   * @author sawielan
34   *
35   */
36  public class RepositoryFactory {
37  	
38  	/** user-name in the arguments table. */
39  	public static final String ARG_USERNAME = "username";
40  	
41  	/** password in the arguments table. */
42  	public static final String ARG_PASSWRD = "password";
43  	
44  	/** JDBC string in the arguments table. */
45  	public static final String ARG_JDBC_STRING = "jdbcString";
46  	
47  	/** parameter whether to wipe DB in the arguments table. */
48  	public static final String ARG_WIPE_DB = "wipeDB";
49  	
50  	/** parameter whether to log RO_ACCESS_REPORT. */
51  	public static final String ARG_LOG_RO_ACCESS_REPORT = "logROAccess";
52  	
53  	/** parameter whether to wipe RO_ACCESS_REPORTS DB in the arguments table.*/
54  	public static final String ARG_WIPE_RO_ACCESS_REPORTS_DB = "wipeROAccessDB";
55  	
56  	/** class name in the arguments table. */
57  	public static final String ARG_DB_CLASSNAME = "dbClassName";
58  	
59  	// the log4j logger.
60  	private static Logger log = Logger.getLogger(RepositoryFactory.class);
61  	
62  	/**
63  	 * helper to create a hash-map with key-value pairs. just provide a 2D 
64  	 * array, with pairs of (key, value).
65  	 * <h3>Example:</h3>
66  	 * <code>Map&lt;String,String&gt; m = createMap(new String[][] {</code><br/>
67  	 * <code>&nbsp;&nbsp;&nbsp;&nbsp;{key1, value1},</code><br/>
68  	 * <code>&nbsp;&nbsp;&nbsp;&nbsp;{key2, value2},</code><br/>
69  	 * <code>&nbsp;&nbsp;&nbsp;&nbsp;{key3, value3}</code><br/>
70  	 * <code>&nbsp;&nbsp;}</code>
71  	 * @param keyValue the key values 2D array.
72  	 * @return a hash-map mapping the 2D array in a hash-table.
73  	 */
74  	public static Map<String, String> createMap(String [][] keyValue) {
75  		Map<String, String> map = new HashMap<String, String> ();
76  		if (null == keyValue) return map;
77  		
78  		final int len = keyValue.length;
79  		for (int i=0; i<len; i++) {
80  			map.put(keyValue[i][0], keyValue[i][1]);
81  		}
82  		return map;
83  	}
84  	
85  	/**
86  	 * create a new repository and read the configuration from a Properties 
87  	 * data structure.
88  	 * @param properties the properties where to obtain the configuration from.
89  	 * @return an instance of a {@link Repository}.
90  	 * @throws InstantiationException when no instantiation was possible.
91  	 * @throws IllegalAccessException access to class was denied.
92  	 * @throws ClassNotFoundException when the class is not existing.
93  	 * @throws LLRPRuntimeException when something other went wrong.
94  	 */
95  	public static Repository create(Properties properties) 
96  		throws InstantiationException, LLRPRuntimeException, 
97  			IllegalAccessException, ClassNotFoundException {
98  		
99  		// extract the settings from the properties.
100 		Map<String, String> args = new HashMap<String, String>();
101 		
102 		args.put(ARG_USERNAME, properties.getProperty(ARG_USERNAME));
103 		args.put(ARG_PASSWRD, properties.getProperty(ARG_PASSWRD));
104 		args.put(ARG_JDBC_STRING, properties.getProperty(ARG_JDBC_STRING));
105 		args.put(ARG_WIPE_DB, properties.getProperty(ARG_WIPE_DB));
106 		args.put(ARG_LOG_RO_ACCESS_REPORT, 
107 				properties.getProperty(ARG_LOG_RO_ACCESS_REPORT));
108 		args.put(ARG_WIPE_RO_ACCESS_REPORTS_DB, 
109 				properties.getProperty(ARG_WIPE_RO_ACCESS_REPORTS_DB));
110 		args.put(ARG_DB_CLASSNAME, properties.getProperty(ARG_DB_CLASSNAME));
111 		
112 		return create(args);
113 	}
114 	
115 	/**
116 	 * create a new repository with the configuration parameters provided 
117 	 * via the parameters hash map.
118 	 * @param args a hash-map providing the parameters.
119 	 * @return an instance of a {@link Repository}.
120 	 * @throws InstantiationException when no instantiation was possible.
121 	 * @throws IllegalAccessException access to class was denied.
122 	 * @throws ClassNotFoundException when the class is not existing.
123 	 * @throws LLRPRuntimeException when something other went wrong.
124 	 */
125 	public static Repository create(Map<String, String> args) 
126 	
127 		throws InstantiationException, LLRPRuntimeException,
128 			IllegalAccessException, ClassNotFoundException {
129 
130 		if (null == args) throw new InstantiationException(
131 				"Args map is null!!! - aborting");
132 		
133 		Object db = Class.forName(args.get(ARG_DB_CLASSNAME)).newInstance();
134 		Repository repository = null;
135 		if (db instanceof Repository) {
136 			repository = (Repository) db;
137 			try {
138 			repository.initialize(args);
139 			String logRO = args.get(ARG_LOG_RO_ACCESS_REPORT);
140 			if ((null != logRO) && (Boolean.parseBoolean(logRO))) {
141 				repository.getROAccessRepository().initialize(repository);
142 			} else {
143 				log.debug("NOT enabling RO_ACCESS_REPORT logging.");
144 			}
145 			} catch (LLRPRuntimeException llrpe) {
146 				log.error(String.format("could not initialize: '%s'", 
147 						llrpe.getMessage()));
148 				throw new LLRPRuntimeException(llrpe);
149 			}
150 		} else {
151 			// throw an Exception
152 			log.error(String.format(
153 					"Implementing class is not of type Repository: 's'",
154 					args.get(RepositoryFactory.ARG_DB_CLASSNAME)));
155 			throw new InstantiationException(String.format(
156 					"Illegal implementing class: '%s'", 
157 					args.get(RepositoryFactory.ARG_DB_CLASSNAME)));
158 		}
159 		return repository;
160 	}
161 }