KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dba > TypesHandler


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.dba;
57
58 import java.io.IOException JavaDoc;
59 import java.io.InputStream JavaDoc;
60 import java.net.URL JavaDoc;
61 import java.sql.Types JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.HashMap JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.Map JavaDoc;
66
67 import org.apache.log4j.Logger;
68 import org.objectstyle.cayenne.CayenneRuntimeException;
69 import org.objectstyle.cayenne.util.Util;
70 import org.xml.sax.Attributes JavaDoc;
71 import org.xml.sax.InputSource JavaDoc;
72 import org.xml.sax.SAXException JavaDoc;
73 import org.xml.sax.XMLReader JavaDoc;
74 import org.xml.sax.helpers.DefaultHandler JavaDoc;
75
76 /**
77  * TypesHandler provides JDBC-RDBMS types mapping. Loads types info from
78  * an XML file.
79  *
80  * @author Andrei Adamchik
81  */

82 public class TypesHandler {
83     private static Logger logObj = Logger.getLogger(TypesHandler.class);
84
85     private static Map JavaDoc handlerMap = new HashMap JavaDoc();
86
87     protected Map JavaDoc typesMap;
88
89     /**
90      * @since 1.1
91      */

92     public static TypesHandler getHandler(URL JavaDoc typesConfig) {
93         synchronized (handlerMap) {
94             TypesHandler handler = (TypesHandler) handlerMap.get(typesConfig);
95
96             if (handler == null) {
97                 handler = new TypesHandler(typesConfig);
98                 handlerMap.put(typesConfig, handler);
99             }
100
101             return handler;
102         }
103     }
104
105     /**
106      * Creates new TypesHandler loading configuration info from the XML
107      * file specified as <code>typesConfigPath</code> parameter.
108      *
109      * @since 1.1
110      */

111     public TypesHandler(URL JavaDoc typesConfig) {
112         try {
113             InputStream JavaDoc in = typesConfig.openStream();
114
115             try {
116                 XMLReader JavaDoc parser = Util.createXmlReader();
117                 TypesParseHandler ph = new TypesParseHandler();
118                 parser.setContentHandler(ph);
119                 parser.setErrorHandler(ph);
120                 parser.parse(new InputSource JavaDoc(in));
121
122                 typesMap = ph.getTypes();
123             }
124             catch (Exception JavaDoc ex) {
125                 throw new CayenneRuntimeException(
126                     "Error creating TypesHandler '" + typesConfig + "'.",
127                     ex);
128             }
129             finally {
130                 try {
131                     in.close();
132                 }
133                 catch (IOException JavaDoc ioex) {
134                 }
135             }
136         }
137         catch (IOException JavaDoc ioex) {
138             throw new CayenneRuntimeException(
139                 "Error opening config file '" + typesConfig + "'.",
140                 ioex);
141         }
142     }
143
144     public String JavaDoc[] externalTypesForJdbcType(int type) {
145         return (String JavaDoc[]) typesMap.get(new Integer JavaDoc(type));
146     }
147
148     /**
149      * Helper class to load types data from XML.
150      */

151     final class TypesParseHandler extends DefaultHandler JavaDoc {
152         private static final String JavaDoc JDBC_TYPE_TAG = "jdbc-type";
153         private static final String JavaDoc DB_TYPE_TAG = "db-type";
154         private static final String JavaDoc NAME_ATTR = "name";
155
156         private Map JavaDoc types = new HashMap JavaDoc();
157         private List JavaDoc currentTypes = new ArrayList JavaDoc();
158         private int currentType = TypesMapping.NOT_DEFINED;
159
160         public Map JavaDoc getTypes() {
161             return types;
162         }
163
164         public void startElement(
165             String JavaDoc namespaceURI,
166             String JavaDoc localName,
167             String JavaDoc qName,
168             Attributes JavaDoc atts)
169             throws SAXException JavaDoc {
170             if (JDBC_TYPE_TAG.equals(localName)) {
171                 currentTypes.clear();
172                 String JavaDoc strType = atts.getValue("", NAME_ATTR);
173
174                 // convert to Types int value
175
try {
176                     currentType = Types JavaDoc.class.getDeclaredField(strType).getInt(null);
177                 }
178                 catch (Exception JavaDoc ex) {
179                     currentType = TypesMapping.NOT_DEFINED;
180                     logObj.info("type not found: '" + strType + "', ignoring.");
181                 }
182             }
183             else if (DB_TYPE_TAG.equals(localName)) {
184                 currentTypes.add(atts.getValue("", NAME_ATTR));
185             }
186         }
187
188         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
189             throws SAXException JavaDoc {
190             if (JDBC_TYPE_TAG.equals(localName)
191                 && currentType != TypesMapping.NOT_DEFINED) {
192                 String JavaDoc[] typesAsArray = new String JavaDoc[currentTypes.size()];
193                 types.put(new Integer JavaDoc(currentType), currentTypes.toArray(typesAsArray));
194             }
195         }
196     }
197 }
198
Popular Tags