001    package org.apache.ojb.broker.metadata;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.util.DoubleHashtable;
019    import org.apache.ojb.broker.util.logging.LoggerFactory;
020    import org.apache.ojb.broker.util.pooling.PoolConfiguration;
021    
022    /**
023     * this class maintains a table mapping the xml-tags used in the
024     * repository.dtd to their corresponding ids used within OJB.
025     * This table is used in <br>
026     * 1. the RepositoryXmlHandler to identify tags on parsing the
027     * repository.xml in a large switch statement.
028     * 2. in the RepositoryPersistor to get the proper tag for a
029     * given XmlCapable id during assembling the repository.xml
030     * for output.<br>
031     * <b>Important note: This class is the only place where XML tags from the
032     * repository.dtd are maintained.
033     * All usages of these tags within OJB must use this table to ease
034     * changes of the DTD.</b>
035     * @author              Thomas Mahler
036     */
037    public class RepositoryTags implements RepositoryElements
038    {
039        /**
040         * the two-way hashtable holding all entries.
041         */
042        private DoubleHashtable table;
043    
044        /**
045         * the singleton instance of this class.
046         */
047        private static RepositoryTags instance = new RepositoryTags();
048    
049        /**
050         * private Constructor, please use getInstance() to obtain
051         * the singleton instance of this class.
052         */
053        private RepositoryTags()
054        {
055            // construct the mapping table
056            table = new DoubleHashtable();
057            table.put("descriptor-repository", new Integer(MAPPING_REPOSITORY));
058            table.put("version", new Integer(REPOSITORY_VERSION));
059            table.put("isolation-level", new Integer(ISOLATION_LEVEL));
060            table.put("jdbc-connection-descriptor", new Integer(JDBC_CONNECTION_DESCRIPTOR));
061            table.put("platform", new Integer(DBMS_NAME));
062            table.put("schema", new Integer(SCHEMA_NAME));
063            table.put("jcd-alias", new Integer(JCD_ALIAS));
064            table.put("default-connection", new Integer(DEFAULT_CONNECTION));
065            table.put("driver", new Integer(DRIVER_NAME));
066            table.put("protocol", new Integer(URL_PROTOCOL));
067            table.put("subprotocol", new Integer(URL_SUBPROTOCOL));
068            table.put("dbalias", new Integer(URL_DBALIAS));
069            table.put("username", new Integer(USER_NAME));
070            table.put("password", new Integer(USER_PASSWD));
071            table.put("eager-release", new Integer(EAGER_RELEASE));
072            table.put("batch-mode", new Integer(BATCH_MODE));
073            table.put("useAutoCommit", new Integer(USE_AUTOCOMMIT));
074            table.put("ignoreAutoCommitExceptions", new Integer(IGNORE_AUTOCOMMIT_EXCEPTION));
075            table.put("class-descriptor", new Integer(CLASS_DESCRIPTOR));
076            table.put("class", new Integer(CLASS_NAME));
077            table.put("proxy", new Integer(CLASS_PROXY));
078            table.put("extent-class", new Integer(CLASS_EXTENT));
079            table.put("extends", new Integer(EXTENDS));
080            table.put("table", new Integer(TABLE_NAME));
081            table.put("orderby", new Integer(ORDERBY));
082            table.put("conversion", new Integer(FIELD_CONVERSION));
083            table.put("row-reader", new Integer(ROW_READER));
084            table.put("field-descriptor", new Integer(FIELD_DESCRIPTOR));
085            table.put("name", new Integer(FIELD_NAME));
086            table.put("column", new Integer(COLUMN_NAME));
087            table.put("jdbc-type", new Integer(JDBC_TYPE));
088            table.put("primarykey", new Integer(PRIMARY_KEY));
089            table.put("autoincrement", new Integer(AUTO_INCREMENT));
090            table.put("sequence-name", new Integer(SEQUENCE_NAME));
091            table.put("nullable", new Integer(NULLABLE));
092            table.put("indexed", new Integer(INDEXED));
093            table.put("length", new Integer(LENGTH));
094            table.put("precision", new Integer(PRECISION));
095            table.put("scale", new Integer(SCALE));
096            table.put(TAG_ACCESS, new Integer(ACCESS));
097    
098            table.put("reference-descriptor", new Integer(REFERENCE_DESCRIPTOR));
099            table.put("class-ref", new Integer(REFERENCED_CLASS));
100            table.put("foreignkey", new Integer(FOREIGN_KEY));
101            table.put("auto-retrieve", new Integer(AUTO_RETRIEVE));
102            table.put("auto-update", new Integer(AUTO_UPDATE));
103            table.put("auto-delete", new Integer(AUTO_DELETE));
104            table.put("collection-descriptor", new Integer(COLLECTION_DESCRIPTOR));
105            table.put("element-class-ref", new Integer(ITEMS_CLASS));
106            table.put("inverse-foreignkey", new Integer(INVERSE_FK));
107            table.put("collection-class", new Integer(COLLECTION_CLASS));
108            table.put("indirection-table", new Integer(INDIRECTION_TABLE));
109            table.put("fk-pointing-to-element-class", new Integer(FK_POINTING_TO_ITEMS_CLASS));
110            table.put("fk-pointing-to-this-class", new Integer(FK_POINTING_TO_THIS_CLASS));
111            table.put("jndi-datasource-name", new Integer(DATASOURCE_NAME));
112            table.put("jdbc-level", new Integer(JDBC_LEVEL));
113            table.put("locking", new Integer(LOCKING));
114            table.put("update-lock", new Integer(UPDATE_LOCK));
115            table.put("refresh", new Integer(REFRESH));
116            table.put("proxy", new Integer(PROXY_REFERENCE));
117            table.put("sort", new Integer(SORT));
118            table.put("otm-dependent", new Integer(OTM_DEPENDENT));
119    
120            table.put("index-descriptor", new Integer(INDEX_DESCRIPTOR));
121            table.put("index-column", new Integer(INDEX_COLUMN));
122            table.put("unique", new Integer(UNIQUE));
123            table.put("name", new Integer(NAME));
124    
125            table.put(PoolConfiguration.MAX_ACTIVE, new Integer(CON_MAX_ACTIVE));
126            table.put(PoolConfiguration.MAX_IDLE, new Integer(CON_MAX_IDLE));
127            table.put(PoolConfiguration.MAX_WAIT, new Integer(CON_MAX_WAIT));
128            table.put(
129                PoolConfiguration.MIN_EVICTABLE_IDLE_TIME_MILLIS,
130                new Integer(CON_MIN_EVICTABLE_IDLE_TIME_MILLIS));
131            table.put(
132                PoolConfiguration.NUM_TESTS_PER_EVICTION_RUN,
133                new Integer(CON_NUM_TESTS_PER_EVICTION_RUN));
134            table.put(PoolConfiguration.TEST_ON_BORROW, new Integer(CON_TEST_ON_BORROW));
135            table.put(PoolConfiguration.TEST_ON_RETURN, new Integer(CON_TEST_ON_RETURN));
136            table.put(PoolConfiguration.TEST_WHILE_IDLE, new Integer(CON_TEST_WHILE_IDLE));
137            table.put(
138                PoolConfiguration.TIME_BETWEEN_EVICTION_RUNS_MILLIS,
139                new Integer(CON_TIME_BETWEEN_EVICTION_RUNS_MILLIS));
140            table.put(PoolConfiguration.WHEN_EXHAUSTED_ACTION, new Integer(CON_WHEN_EXHAUSTED_ACTION));
141            table.put(PoolConfiguration.VALIDATION_QUERY, new Integer(VALIDATION_QUERY));
142    
143            table.put(PoolConfiguration.LOG_ABANDONED, new Integer(CON_LOG_ABANDONED));
144            table.put(PoolConfiguration.REMOVE_ABANDONED, new Integer(CON_REMOVE_ABANDONED));
145            table.put(
146                PoolConfiguration.REMOVE_ABANDONED_TIMEOUT,
147                new Integer(CON_REMOVE_ABANDONED_TIMEOUT));
148    
149            table.put("connectionFactory", new Integer(CONNECTION_FACTORY));
150            table.put("connection-pool", new Integer(CONNECTION_POOL));
151            table.put("class-ref", new Integer(CLASS_REF));
152            table.put("id", new Integer(ID));
153            table.put("field-id-ref", new Integer(FIELD_ID_REF));
154            table.put("field-ref", new Integer(FIELD_REF));
155            table.put("attribute", new Integer(ATTRIBUTE));
156            table.put("attribute-name", new Integer(ATTRIBUTE_NAME));
157            table.put("attribute-value", new Integer(ATTRIBUTE_VALUE));
158            table.put("documentation", new Integer(DOCUMENTATION));
159            table.put("accept-locks", new Integer(ACCEPT_LOCKS));
160    
161            table.put("sequence-manager", new Integer(SEQUENCE_MANAGER));
162            table.put("className", new Integer(SEQUENCE_MANAGER_CLASS));
163    
164            table.put("query-customizer", new Integer(QUERY_CUSTOMIZER));
165            table.put("initialization-method", new Integer(INITIALIZATION_METHOD));
166            table.put("factory-class", new Integer(FACTORY_CLASS));
167            table.put("factory-method", new Integer(FACTORY_METHOD));
168    
169            table.put("insert-procedure", new Integer(INSERT_PROCEDURE));
170            table.put("update-procedure", new Integer(UPDATE_PROCEDURE));
171            table.put("delete-procedure", new Integer(DELETE_PROCEDURE));
172            table.put("constant-argument", new Integer(CONSTANT_ARGUMENT));
173            table.put("runtime-argument", new Integer(RUNTIME_ARGUMENT));
174            table.put("return", new Integer(RETURN));
175            table.put("value", new Integer(VALUE));
176            table.put("return-field-ref", new Integer(RETURN_FIELD_REF));
177            table.put("include-pk-only", new Integer(INCLUDE_PK_FIELDS_ONLY));
178            table.put("include-all-fields", new Integer(INCLUDE_ALL_FIELDS));
179    
180            table.put("object-cache", new Integer(OBJECT_CACHE));
181    
182            table.put("proxy-prefetching-limit", new Integer(PROXY_PREFETCHING_LIMIT));
183    
184            // add new tags here !
185        }
186    
187        /**
188         * returns the singleton instance.
189         */
190        public static RepositoryTags getInstance()
191        {
192            return instance;
193        }
194    
195        /**
196         * returns the xml-tag literal associated with the repository element with
197         * id <code>elementId</code>.
198         * @return the resulting tag
199         */
200        public String getTagById(int elementId)
201        {
202            return (String) table.getKeyByValue(new Integer(elementId));
203        }
204    
205        /**
206         * returns the opening xml-tag associated with the repository element with
207         * id <code>elementId</code>.
208         * @return the resulting tag
209         */
210        public String getOpeningTagById(int elementId)
211        {
212            return "<" + table.getKeyByValue(new Integer(elementId)) + ">";
213        }
214    
215        /**
216         * returns the opening but non-closing xml-tag
217         * associated with the repository element with
218         * id <code>elementId</code>.
219         * @return the resulting tag
220         */
221        public String getOpeningTagNonClosingById(int elementId)
222        {
223            return "<" + table.getKeyByValue(new Integer(elementId));
224        }
225    
226        /**
227         * returns the opening xml-tag associated with the repository element with
228         * id <code>elementId</code>.
229         * @return the resulting tag
230         */
231        public String getOpeningTagById(int elementId, String attributes)
232        {
233            return "<" + table.getKeyByValue(new Integer(elementId)) + " " + attributes + ">";
234        }
235    
236        /**
237         * returns the opening but non-closing xml-tag
238         * associated with the repository element with
239         * id <code>elementId</code>.
240         * @return the resulting tag
241         */
242        public String getAttribute(int elementId, String value)
243        {
244            return table.getKeyByValue(new Integer(elementId)) + "=\"" + value + "\"";
245        }
246    
247        /**
248         * returns the closing xml-tag associated with the repository element with
249         * id <code>elementId</code>.
250         * @return the resulting tag
251         */
252        public String getClosingTagById(int elementId)
253        {
254            return "</" + table.getKeyByValue(new Integer(elementId)) + ">";
255        }
256    
257        /**
258         * returns the repository element id associated with the xml-tag
259         * literal <code>tag</code>.
260         * @return the resulting repository element id.
261         * @throws NullPointerException if no value was found for <b>tag</b>
262         */
263        public int getIdByTag(String tag)
264        {
265            Integer value = (Integer) table.getValueByKey(tag);
266            if (value == null)
267                LoggerFactory.getDefaultLogger().error(
268                    "** " + this.getClass().getName() + ": Tag '" + tag + "' is not defined. **");
269            return value.intValue();
270        }
271    
272        /**
273         * returns the opening xml-tag associated with the repository element with
274         * id <code>elementId</code>.
275         * @return the resulting tag
276         */
277        public String getCompleteTagById(int elementId, String characters)
278        {
279            String result = getOpeningTagById(elementId);
280            result += characters;
281            result += getClosingTagById(elementId);
282            return result;
283        }
284    }