View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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   * Implementation of HostDAO for standard SQL databases
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     * Hashmap for Host
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 }