1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.openr66.dao.database;
22
23 import org.waarp.common.database.exception.WaarpDatabaseSqlException;
24 import org.waarp.common.lru.SynchronizedLruCache;
25 import org.waarp.openr66.dao.HostDAO;
26 import org.waarp.openr66.pojo.Host;
27 import org.waarp.openr66.pojo.UpdatedInfo;
28 import org.waarp.openr66.protocol.configuration.Configuration;
29
30 import java.sql.Connection;
31 import java.sql.ResultSet;
32 import java.sql.SQLException;
33
34
35
36
37 public class DBHostDAO extends StatementExecutor<Host> implements HostDAO {
38
39 protected static final String TABLE = "hosts";
40
41 public static final String HOSTID_FIELD = "hostid";
42 public static final String ADDRESS_FIELD = "address";
43 public static final String PORT_FIELD = "port";
44 public static final String IS_SSL_FIELD = "isssl";
45 public static final String IS_CLIENT_FIELD = "isclient";
46 public static final String IS_ACTIVE_FIELD = "isactive";
47 public static final String IS_PROXIFIED_FIELD = "isproxified";
48 public static final String HOSTKEY_FIELD = "hostkey";
49 public static final String ADMINROLE_FIELD = "adminrole";
50 public static final String UPDATED_INFO_FIELD = "updatedinfo";
51
52 protected static final String SQL_DELETE_ALL = "DELETE FROM " + TABLE;
53 protected static final String SQL_DELETE =
54 "DELETE FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
55 protected static final String SQL_GET_ALL = "SELECT * FROM " + TABLE;
56 protected static final String SQL_COUNT_ALL = SQL_COUNT_ALL_PREFIX + TABLE;
57 protected static final String SQL_EXIST =
58 "SELECT 1 FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
59 protected static final String SQL_SELECT =
60 "SELECT * FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
61 protected static final String SQL_INSERT =
62 "INSERT INTO " + TABLE + " (" + HOSTID_FIELD + ", " + ADDRESS_FIELD +
63 ", " + PORT_FIELD + ", " + IS_SSL_FIELD + ", " + IS_CLIENT_FIELD + ", " +
64 IS_ACTIVE_FIELD + ", " + IS_PROXIFIED_FIELD + ", " + HOSTKEY_FIELD +
65 ", " + ADMINROLE_FIELD + ", " + UPDATED_INFO_FIELD +
66 ") VALUES (?,?,?,?,?,?,?,?,?,?)";
67 protected static final String SQL_UPDATE =
68 "UPDATE " + TABLE + " SET " + HOSTID_FIELD + PARAMETER_COMMA +
69 ADDRESS_FIELD + PARAMETER_COMMA + PORT_FIELD + PARAMETER_COMMA +
70 IS_SSL_FIELD + PARAMETER_COMMA + IS_CLIENT_FIELD + PARAMETER_COMMA +
71 IS_ACTIVE_FIELD + PARAMETER_COMMA + IS_PROXIFIED_FIELD + PARAMETER_COMMA +
72 HOSTKEY_FIELD + PARAMETER_COMMA + ADMINROLE_FIELD + PARAMETER_COMMA +
73 UPDATED_INFO_FIELD + " = ? WHERE " + HOSTID_FIELD + PARAMETER;
74
75
76
77
78
79 private static final SynchronizedLruCache<String, Host>
80 reentrantConcurrentHashMap =
81 new SynchronizedLruCache<String, Host>(500, 180000);
82
83 public DBHostDAO(final Connection con) {
84 super(con);
85 }
86
87 @Override
88 protected final boolean isCachedEnable() {
89 return Configuration.configuration.getMultipleMonitors() <= 1;
90 }
91
92 @Override
93 protected final SynchronizedLruCache<String, Host> getCache() {
94 return reentrantConcurrentHashMap;
95 }
96
97 @Override
98 protected final String getId(final Host e1) {
99 return e1.getHostid();
100 }
101
102 @Override
103 protected final String getTable() {
104 return TABLE;
105 }
106
107 @Override
108 protected final String getSelectRequest() {
109 return SQL_SELECT;
110 }
111
112 @Override
113 protected final String getGetAllRequest() {
114 return SQL_GET_ALL;
115 }
116
117 @Override
118 protected final String getCountRequest() {
119 return SQL_COUNT_ALL;
120 }
121
122 @Override
123 protected final String getExistRequest() {
124 return SQL_EXIST;
125 }
126
127 @Override
128 protected final Object[] getInsertValues(final Host host)
129 throws WaarpDatabaseSqlException {
130 host.checkValues();
131 return new Object[] {
132 host.getHostid(), host.getAddress(), host.getPort(), host.isSSL(),
133 host.isClient(), host.isActive(), host.isProxified(), host.getHostkey(),
134 host.isAdmin(), host.getUpdatedInfo().ordinal()
135 };
136 }
137
138 @Override
139 protected final String getInsertRequest() {
140 return SQL_INSERT;
141 }
142
143 @Override
144 protected final Object[] getUpdateValues(final Host host)
145 throws WaarpDatabaseSqlException {
146 host.checkValues();
147 return new Object[] {
148 host.getHostid(), host.getAddress(), host.getPort(), host.isSSL(),
149 host.isClient(), host.isActive(), host.isProxified(), host.getHostkey(),
150 host.isAdmin(), host.getUpdatedInfo().ordinal(), host.getHostid()
151 };
152 }
153
154 @Override
155 protected final String getUpdateRequest() {
156 return SQL_UPDATE;
157 }
158
159 @Override
160 protected final String getDeleteRequest() {
161 return SQL_DELETE;
162 }
163
164 @Override
165 protected final String getDeleteAllRequest() {
166 return SQL_DELETE_ALL;
167 }
168
169 @Override
170 public final Host getFromResultSet(final ResultSet set) throws SQLException {
171 try {
172 return new Host(set.getString(HOSTID_FIELD), set.getString(ADDRESS_FIELD),
173 set.getInt(PORT_FIELD), set.getBytes(HOSTKEY_FIELD),
174 set.getBoolean(IS_SSL_FIELD),
175 set.getBoolean(IS_CLIENT_FIELD),
176 set.getBoolean(IS_PROXIFIED_FIELD),
177 set.getBoolean(ADMINROLE_FIELD),
178 set.getBoolean(IS_ACTIVE_FIELD),
179 UpdatedInfo.valueOf(set.getInt(UPDATED_INFO_FIELD)));
180 } catch (final WaarpDatabaseSqlException e) {
181 throw new SQLException(e);
182 }
183 }
184
185 @Override
186 protected final boolean isDbTransfer() {
187 return false;
188 }
189 }