1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
33
34
35
36 public class RepositoryFactory {
37
38
39 public static final String ARG_USERNAME = "username";
40
41
42 public static final String ARG_PASSWRD = "password";
43
44
45 public static final String ARG_JDBC_STRING = "jdbcString";
46
47
48 public static final String ARG_WIPE_DB = "wipeDB";
49
50
51 public static final String ARG_LOG_RO_ACCESS_REPORT = "logROAccess";
52
53
54 public static final String ARG_WIPE_RO_ACCESS_REPORTS_DB = "wipeROAccessDB";
55
56
57 public static final String ARG_DB_CLASSNAME = "dbClassName";
58
59
60 private static Logger log = Logger.getLogger(RepositoryFactory.class);
61
62
63
64
65
66
67
68
69
70
71
72
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
87
88
89
90
91
92
93
94
95 public static Repository create(Properties properties)
96 throws InstantiationException, LLRPRuntimeException,
97 IllegalAccessException, ClassNotFoundException {
98
99
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
117
118
119
120
121
122
123
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
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 }