View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.database.data;
22  
23  import goldengate.common.database.DbPreparedStatement;
24  import goldengate.common.database.DbSession;
25  import goldengate.common.database.data.AbstractDbData;
26  import goldengate.common.database.data.DbValue;
27  import goldengate.common.database.exception.GoldenGateDatabaseException;
28  import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
29  import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
30  import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
31  
32  import java.sql.Types;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import openr66.commander.CommanderNoDb;
36  import openr66.protocol.configuration.Configuration;
37  
38  /**
39   * Configuration Table object
40   *
41   * @author Frederic Bregier
42   *
43   */
44  public class DbConfiguration extends AbstractDbData {
45      public static enum Columns {
46          READGLOBALLIMIT,
47          WRITEGLOBALLIMIT,
48          READSESSIONLIMIT,
49          WRITESESSIONLIMIT,
50          DELAYLIMIT,
51          UPDATEDINFO,
52          HOSTID
53      }
54  
55      public static final int[] dbTypes = {
56              Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT,
57              Types.BIGINT, Types.INTEGER, Types.VARCHAR };
58  
59      public static final String table = " CONFIGURATION ";
60  
61      /**
62       * HashTable in case of lack of database
63       */
64      private static final ConcurrentHashMap<String, DbConfiguration> dbR66ConfigurationHashMap =
65          new ConcurrentHashMap<String, DbConfiguration>();
66  
67      private String hostid;
68  
69      private long readgloballimit;
70  
71      private long writegloballimit;
72  
73      private long readsessionlimit;
74  
75      private long writesessionlimit;
76  
77      private long delayllimit;
78  
79      private int updatedInfo = UpdatedInfo.UNKNOWN.ordinal();
80  
81  
82      // ALL TABLE SHOULD IMPLEMENT THIS
83      public static final int NBPRKEY = 1;
84  
85      protected static final String selectAllFields = Columns.READGLOBALLIMIT
86              .name() +
87              "," +
88              Columns.WRITEGLOBALLIMIT.name() +
89              "," +
90              Columns.READSESSIONLIMIT.name() +
91              "," +
92              Columns.WRITESESSIONLIMIT.name() +
93              "," +
94              Columns.DELAYLIMIT.name() +
95              "," + Columns.UPDATEDINFO.name() + "," + Columns.HOSTID.name();
96  
97      protected static final String updateAllFields = Columns.READGLOBALLIMIT
98              .name() +
99              "=?," +
100             Columns.WRITEGLOBALLIMIT.name() +
101             "=?," +
102             Columns.READSESSIONLIMIT.name() +
103             "=?," +
104             Columns.WRITESESSIONLIMIT.name() +
105             "=?," +
106             Columns.DELAYLIMIT.name() +
107             "=?," +
108             Columns.UPDATEDINFO.name() +
109             "=?";
110 
111     protected static final String insertAllValues = " (?,?,?,?,?,?,?) ";
112 
113 
114     /* (non-Javadoc)
115      * @see goldengate.common.database.data.AbstractDbData#initObject()
116      */
117     @Override
118     protected void initObject() {
119         primaryKey = new DbValue[]{new DbValue(hostid, Columns.HOSTID
120                 .name())};
121         otherFields = new DbValue[]{
122                 new DbValue(readgloballimit, Columns.READGLOBALLIMIT.name()),
123                 new DbValue(writegloballimit, Columns.WRITEGLOBALLIMIT.name()),
124                 new DbValue(readsessionlimit, Columns.READSESSIONLIMIT.name()),
125                 new DbValue(writesessionlimit, Columns.WRITESESSIONLIMIT.name()),
126                 new DbValue(delayllimit, Columns.DELAYLIMIT.name()),
127                 new DbValue(updatedInfo, Columns.UPDATEDINFO.name()) };
128         allFields = new DbValue[]{
129                 otherFields[0], otherFields[1], otherFields[2], otherFields[3],
130                 otherFields[4], otherFields[5], primaryKey[0] };
131     }
132 
133     /* (non-Javadoc)
134      * @see goldengate.common.database.data.AbstractDbData#getSelectAllFields()
135      */
136     @Override
137     protected String getSelectAllFields() {
138         return selectAllFields;
139     }
140 
141     /* (non-Javadoc)
142      * @see goldengate.common.database.data.AbstractDbData#getTable()
143      */
144     @Override
145     protected String getTable() {
146         return table;
147     }
148 
149     /* (non-Javadoc)
150      * @see goldengate.common.database.data.AbstractDbData#getInsertAllValues()
151      */
152     @Override
153     protected String getInsertAllValues() {
154         return insertAllValues;
155     }
156 
157     /* (non-Javadoc)
158      * @see goldengate.common.database.data.AbstractDbData#getUpdateAllFields()
159      */
160     @Override
161     protected String getUpdateAllFields() {
162         return updateAllFields;
163     }
164 
165     @Override
166     protected void setToArray() {
167         allFields[Columns.HOSTID.ordinal()].setValue(hostid);
168         allFields[Columns.READGLOBALLIMIT.ordinal()].setValue((readgloballimit/10)*10);
169         allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
170                 .setValue((writegloballimit/10)*10);
171         allFields[Columns.READSESSIONLIMIT.ordinal()]
172                 .setValue((readsessionlimit/10)*10);
173         allFields[Columns.WRITESESSIONLIMIT.ordinal()]
174                 .setValue((writesessionlimit/10)*10);
175         allFields[Columns.DELAYLIMIT.ordinal()].setValue(delayllimit);
176         allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
177     }
178 
179     @Override
180     protected void setFromArray() throws GoldenGateDatabaseSqlException {
181         hostid = (String) allFields[Columns.HOSTID.ordinal()].getValue();
182         readgloballimit = (((Long) allFields[Columns.READGLOBALLIMIT.ordinal()]
183                 .getValue())/10)*10;
184         writegloballimit = (((Long) allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
185                 .getValue())/10)*10;
186         readsessionlimit = (((Long) allFields[Columns.READSESSIONLIMIT.ordinal()]
187                 .getValue())/10)*10;
188         writesessionlimit = (((Long) allFields[Columns.WRITESESSIONLIMIT
189                 .ordinal()].getValue())/10)*10;
190         delayllimit = (Long) allFields[Columns.DELAYLIMIT.ordinal()].getValue();
191         updatedInfo = (Integer) allFields[Columns.UPDATEDINFO.ordinal()]
192                 .getValue();
193     }
194 
195     /* (non-Javadoc)
196      * @see goldengate.common.database.data.AbstractDbData#getWherePrimaryKey()
197      */
198     @Override
199     protected String getWherePrimaryKey() {
200         return primaryKey[0].column + " = ? ";
201     }
202 
203     /* (non-Javadoc)
204      * @see goldengate.common.database.data.AbstractDbData#setPrimaryKey()
205      */
206     @Override
207     protected void setPrimaryKey() {
208         primaryKey[0].setValue(hostid);
209     }
210 
211     /**
212      * @param dbSession
213      * @param hostid
214      * @param rg
215      *            Read Global Limit
216      * @param wg
217      *            Write Global Limit
218      * @param rs
219      *            Read Session Limit
220      * @param ws
221      *            Write Session Limit
222      * @param del
223      *            Delay Limit
224      */
225     public DbConfiguration(DbSession dbSession, String hostid, long rg, long wg, long rs,
226             long ws, long del) {
227         super(dbSession);
228         this.hostid = hostid;
229         readgloballimit = (rg/10)*10;
230         writegloballimit = (wg/10)*10;
231         readsessionlimit = (rs/10)*10;
232         writesessionlimit = (ws/10)*10;
233         delayllimit = del;
234         setToArray();
235         isSaved = false;
236     }
237 
238     /**
239      * @param dbSession
240      * @param hostid
241      * @throws GoldenGateDatabaseException
242      */
243     public DbConfiguration(DbSession dbSession, String hostid) throws GoldenGateDatabaseException {
244         super(dbSession);
245         this.hostid = hostid;
246         // load from DB
247         select();
248     }
249 
250     /*
251      * (non-Javadoc)
252      *
253      * @see openr66.databaseold.data.AbstractDbData#delete()
254      */
255     @Override
256     public void delete() throws GoldenGateDatabaseException {
257         if (dbSession == null) {
258             dbR66ConfigurationHashMap.remove(this.hostid);
259             isSaved = false;
260             return;
261         }
262         super.delete();
263     }
264 
265     /*
266      * (non-Javadoc)
267      *
268      * @see openr66.databaseold.data.AbstractDbData#insert()
269      */
270     @Override
271     public void insert() throws GoldenGateDatabaseException {
272         if (isSaved) {
273             return;
274         }
275         if (dbSession == null) {
276             dbR66ConfigurationHashMap.put(this.hostid, this);
277             isSaved = true;
278             if (this.updatedInfo == UpdatedInfo.TOSUBMIT.ordinal()) {
279                 CommanderNoDb.todoList.add(this);
280             }
281             return;
282         }
283         super.insert();
284     }
285 
286     /* (non-Javadoc)
287      * @see openr66.databaseold.data.AbstractDbData#exist()
288      */
289     @Override
290     public boolean exist() throws GoldenGateDatabaseException {
291         if (dbSession == null) {
292             return dbR66ConfigurationHashMap.containsKey(hostid);
293         }
294         return super.exist();
295     }
296     /*
297      * (non-Javadoc)
298      *
299      * @see openr66.databaseold.data.AbstractDbData#select()
300      */
301     @Override
302     public void select() throws GoldenGateDatabaseException {
303         if (dbSession == null) {
304             DbConfiguration conf = dbR66ConfigurationHashMap.get(this.hostid);
305             if (conf == null) {
306                 throw new GoldenGateDatabaseNoDataException("No row found");
307             } else {
308                 // copy info
309                 for (int i = 0; i < allFields.length; i++){
310                     allFields[i].value = conf.allFields[i].value;
311                 }
312                 setFromArray();
313                 isSaved = true;
314                 return;
315             }
316         }
317         super.select();
318     }
319     /*
320      * (non-Javadoc)
321      *
322      * @see openr66.databaseold.data.AbstractDbData#update()
323      */
324     @Override
325     public void update() throws GoldenGateDatabaseException {
326         if (isSaved) {
327             return;
328         }
329         if (dbSession == null) {
330             dbR66ConfigurationHashMap.put(this.hostid, this);
331             isSaved = true;
332             if (this.updatedInfo == UpdatedInfo.TOSUBMIT.ordinal()) {
333                 CommanderNoDb.todoList.add(this);
334             }
335             return;
336         }
337         super.update();
338     }
339     /**
340      * Private constructor for Commander only
341      */
342     private DbConfiguration(DbSession session) {
343         super(session);
344     }
345     /**
346      * For instance from Commander when getting updated information
347      * @param preparedStatement
348      * @return the next updated Configuration
349      * @throws GoldenGateDatabaseNoConnectionException
350      * @throws GoldenGateDatabaseSqlException
351      */
352     public static DbConfiguration getFromStatement(DbPreparedStatement preparedStatement) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
353         DbConfiguration dbConfiguration = new DbConfiguration(preparedStatement.getDbSession());
354         dbConfiguration.getValues(preparedStatement, dbConfiguration.allFields);
355         dbConfiguration.setFromArray();
356         dbConfiguration.isSaved = true;
357         return dbConfiguration;
358     }
359     /**
360      *
361      * @return the DbPreparedStatement for getting Updated Object
362      * @throws GoldenGateDatabaseNoConnectionException
363      * @throws GoldenGateDatabaseSqlException
364      */
365     public static DbPreparedStatement getUpdatedPrepareStament(DbSession session) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
366         String request = "SELECT " +selectAllFields;
367         request += " FROM "+table+
368             " WHERE "+Columns.UPDATEDINFO.name()+" = "+
369             AbstractDbData.UpdatedInfo.TOSUBMIT.ordinal()+
370             " AND "+ Columns.HOSTID.name()+" = '"+Configuration.configuration.HOST_ID+"'";
371         DbPreparedStatement prep = new DbPreparedStatement(session, request);
372         return prep;
373     }
374     /*
375      * (non-Javadoc)
376      *
377      * @see openr66.databaseold.data.AbstractDbData#changeUpdatedInfo(UpdatedInfo)
378      */
379     @Override
380     public void changeUpdatedInfo(UpdatedInfo info) {
381         if (updatedInfo != info.ordinal()) {
382             updatedInfo = info.ordinal();
383             allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
384             isSaved = false;
385         }
386     }
387     /**
388      * Update configuration according to new value of limits
389      */
390     public void updateConfiguration() {
391         Configuration.configuration.changeNetworkLimit(writegloballimit,
392                 readgloballimit, writesessionlimit, readsessionlimit, delayllimit);
393     }
394     /**
395      *
396      * @return True if this Configuration refers to the current host
397      */
398     public boolean isOwnConfiguration() {
399         return this.hostid.equals(Configuration.configuration.HOST_ID);
400     }
401 }