View Javadoc

1   
2   package org.paneris.jal.model;
3   
4   import org.paneris.util.ExtendedHash;
5   
6   /**
7    * This class is a Singleton that provides access to datadictionary objects.
8    * if a datadictionary has not yet been cached, it is created in the
9    * conventional manner.
10   * A client gets access to the single instance through the static getInstance()
11   * method and can then grap datadictionary objects
12   */
13  
14  public class DataCache {
15      private static DataCache instance;       // The single instance
16      private ExtendedHash tableMetaDatas;
17      private ExtendedHash tableMetaDatasByName;
18      private ExtendedHash fieldMetaDatas;
19      private ExtendedHash DDRecords;
20      private static int clients;
21      private static Log log = new Log("DataCache", "DataCache Log");
22      private static boolean debug = false;
23  
24      /**
25       * Returns the single instance, creating one if it's the
26       * first time this method is called.
27       *
28       * @return DataCache - The single instance.
29       */
30      public static synchronized DataCache getInstance() {
31          if (instance == null) {
32              instance = new DataCache();
33              if (debug) log.debug("new instance");
34          }
35          clients++;
36          return instance;
37      }
38  
39      /**
40       * A private constructor since this is a Singleton
41       */
42      private DataCache() {
43          tableMetaDatas = new ExtendedHash();
44          tableMetaDatasByName = new ExtendedHash();
45          fieldMetaDatas = new ExtendedHash();
46          DDRecords = new ExtendedHash();
47      }
48  
49  
50  
51      /**
52       * returns a reference to tablemetadata for a given table (given as a string)
53       */
54      public TableMetaData getTableMetaData(String db, String tableName) throws Exception {
55          String lookup = new String(db + tableName);
56          TableMetaData md = (TableMetaData) tableMetaDatasByName.get(lookup);
57          if (md != null) {
58              return md;
59          } else {
60              return putTableMetaData(db, tableName);
61          }
62      }
63  
64  
65      /**
66       * adds a tablemetadata for a given table (given as a string)
67       */
68      public TableMetaData putTableMetaData(String db, String tableName) throws Exception {
69          String lookup = new String(db + tableName);
70          try {
71              TableMetaData md = new TableMetaData(db, tableName);
72              String lookup2 = new String(db + md.getTableNumber());
73              tableMetaDatas.put(lookup2, md);
74              tableMetaDatasByName.put(lookup, md);
75              if (debug) log.debug("new TableMetaData by name:" + lookup);
76              return md;
77          } catch (Exception e) {
78              throw new Exception(e.toString() + " table: " + lookup);
79          }
80      }
81  
82      /**
83       * removes a tablemetadata for a given table (given as a string)
84       */
85      public void removeTableMetaData(String db, String tableName) throws Exception {
86          String lookup = new String(db + tableName);
87          TableMetaData md = (TableMetaData) tableMetaDatas.get(lookup);
88          if (md != null) {
89              String lookup2 = new String(db + md.getTableNumber());
90              tableMetaDatas.remove(lookup2);
91              tableMetaDatasByName.remove(lookup);
92              if (debug) log.debug("deleted TableMetaData by name:" + lookup);
93          }
94      }
95  
96      /**
97       * returns a reference to tablemetadata for a given table (given as an Integer)
98       */
99      public TableMetaData getTableMetaData(String db, Integer tableNumber) throws Exception {
100         String lookup = new String(db + tableNumber);
101         TableMetaData md = (TableMetaData) tableMetaDatas.get(lookup);
102         if (md != null) {
103             return md;
104         } else {
105             return putTableMetaData(db, tableNumber);
106         }
107     }
108 
109     /**
110      * adds a tablemetadata for a given table (given as a Integer)
111      */
112     public TableMetaData putTableMetaData(String db, Integer tableNumber) throws Exception {
113         String lookup = new String(db + tableNumber);
114         try {
115             TableMetaData md = new TableMetaData(db, tableNumber);
116             String lookup2 = new String(db + md.getTableName());
117             tableMetaDatasByName.put(lookup, md);
118             tableMetaDatas.put(lookup2, md);
119             if (debug) log.debug("new TableMetaData by number:" + lookup);
120             return md;
121         } catch (Exception e) {
122             throw new Exception(e.toString() + " table: " + lookup);
123         }
124     }
125     /**
126      * removes a tablemetadata for a given table (given as a string)
127      */
128     public void removeTableMetaData(String db, Integer tableNumber) throws Exception {
129         String lookup = new String(db + tableNumber);
130         TableMetaData md = (TableMetaData) tableMetaDatas.get(lookup);
131         if (md != null) {
132             String lookup2 = new String(db + md.getTableName());
133             tableMetaDatas.remove(lookup);
134             tableMetaDatasByName.remove(lookup2);
135             if (debug) log.debug("deleted TableMetaData:" + lookup);
136         }
137     }
138 
139     /**
140      * returns a reference to fieldmetadata for a given field (given as an integer)
141      */
142     public FieldMetaData getFieldMetaData(String db, Integer fieldNumber) throws Exception {
143         String lookup = new String(db + fieldNumber);
144         FieldMetaData md = (FieldMetaData) fieldMetaDatas.get(lookup);
145         if (md != null) {
146             return md;
147         } else {
148             return putFieldMetaData(db, fieldNumber);
149         }
150     }
151 
152     /**
153      * adds a fieldmetadata for a given field (given as a integer)
154      */
155     public FieldMetaData putFieldMetaData(String db, Integer fieldNumber) throws Exception {
156         String lookup = new String(db + fieldNumber);
157         try {
158             FieldMetaData md = new FieldMetaData(db, fieldNumber);
159             fieldMetaDatas.put(lookup, md);
160             if (debug) log.debug("new FieldMetaData: " + lookup);
161             return md;
162         } catch (Exception e) {
163             throw new Exception(e.toString() + " table: " + lookup);
164         }
165     }
166 
167     /**
168      * removes a tablemetadata for a given table (given as a string)
169      */
170     public void removeFieldMetaData(String db, Integer fieldNumber) throws Exception {
171         String lookup = new String(db + fieldNumber);
172         FieldMetaData md = (FieldMetaData) fieldMetaDatas.get(lookup);
173         if (md != null) {
174             fieldMetaDatas.remove(lookup);
175             if (debug) log.debug("deleted FieldMetaData:" + lookup);
176         }
177     }
178 
179     /**
180      * returns a reference to a cached DDRecord (assuming it is cached).  if it
181      * isn't, then it just does the sql lookup itself.  This is used for caching of
182      * automatic lookup fields
183      */
184     public DDRecord getDDRecord(String db, String tn, Integer fn) throws Exception {
185         String lookup = new String(db + tn + fn);
186         DDRecord rs = (DDRecord) DDRecords.get(lookup);
187         if (rs != null) {
188             return rs;
189         } else {
190             return putDDRecords(db, tn, fn);
191         }
192     }
193 
194     public DDRecord putDDRecords(String db, String tn, Integer fn) throws Exception {
195         String lookup = new String(db + tn + fn);
196         try {
197             DDRecord rs = new DDRecord(db, tn, fn);
198          // cache it if we can
199             if (rs.getMetaData().getCached()) {
200                 DDRecords.put(lookup, rs);
201                 if (debug) log.debug("new updateDDRecords:" + lookup);
202             }
203             return rs;
204         } catch (Exception e) {
205             throw (Exception)new Exception(e.toString() + " failed to DDRecord: "
206                                            + lookup).initCause(e);
207         }
208     }
209 
210     /**
211      * removes a tablemetadata for a given table (given as a string)
212      */
213     public void removeDDRecords(String db, String tn, Integer fn) throws Exception {
214         String lookup = new String(db + tn + fn);
215         DDRecord rs = (DDRecord) DDRecords.get(lookup);
216         if (rs != null) {
217             DDRecords.remove(lookup);
218             if (debug) log.debug("deleted DDRecord:" + lookup);
219         }
220     }
221 
222 }