KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > bridge > Mapping


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.bridge;
24
25 import java.io.CharArrayWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.sql.DataSource JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.parsers.SAXParserFactory JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.xml.sax.*;
40 import org.xquark.mapper.dbms.AbstractConnection;
41 import org.xquark.mapper.dbms.AbstractConnectionFactory;
42 import org.xquark.mapper.mapping.*;
43 import org.xquark.mapper.storage.MapperTupleBuilder;
44 import org.xquark.mapper.storage.SAXHandler;
45 import org.xquark.mapper.storage.ValidatingFilerAdapter;
46 import org.xquark.mapper.util.RepositoryProperties;
47 import org.xquark.schema.SchemaManager;
48 import org.xquark.schema.validation.SchemaValidationContext;
49 import org.xquark.schema.validation.ValidatingSchemaFilter;
50 import org.xquark.serialize.XMLSerializer;
51 import org.xquark.util.SAXConstants;
52 import org.xquark.xml.xdbc.XMLDBCException;
53 import org.xquark.xpath.XTreeReader;
54
55 /**
56  * An object implementing this class represents a processed mapping file.
57  * Objects of this class are thread-safe and can be reused multiple times.<BR>
58  * This class uses a cache for loaded mapping files, so that the loading overhead is
59  * reduced. The cache mechanism can be overriden to reload a modified file.
60  */

61
62 public class Mapping
63 {
64     private static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
65     private static final String JavaDoc RCSName = "$Name: $";
66     
67     private static Log log = LogFactory.getLog(Mapping.class);
68     private static SAXParserFactory JavaDoc parserFactory;
69     static
70     {
71         parserFactory = SAXParserFactory.newInstance();
72         parserFactory.setNamespaceAware(true);
73         
74         /* load configuration information for batch for instance */
75         RepositoryProperties.load();
76     }
77     
78     private DataSource JavaDoc dataSource;
79     private String JavaDoc systemId = null;
80     private RepositoryMapping metadata = null;
81     private HashSet JavaDoc mappers = new HashSet JavaDoc();
82     private SchemaManager schemaManager; // this is to be independant if MappingMetadata is reset
83

84     /**
85      * Constructor.
86      * @param dataSource a JDBC data source
87      * @param source the SAX InputSource encapsulating the mapping file
88      * @param reload true means that the mapping file should be reloaded, even
89      * if it is present in the cache
90      * @param classLoader ClassLoader used for loading user generators
91      * @param schemaManager SchemaManager for schemas
92      * @param mappingMetadata if not null, a HashMap holding previously loaded
93      * mapping metadata
94      * @throws XMLDBCException if an error occured while loading the mapping
95      * file. The underlying exception provides the root cause for the exception
96      */

97     public Mapping(DataSource JavaDoc dataSource, InputSource source, boolean reload,
98             ClassLoader JavaDoc classLoader, SchemaManager schemaManager, HashMap JavaDoc mappingMetadata)
99     throws XMLDBCException {
100         this.dataSource = dataSource;
101         this.schemaManager = schemaManager;
102         Connection JavaDoc jdbcConn = null;
103         AbstractConnection connection = null;
104         try {
105             jdbcConn = dataSource.getConnection();
106             connection = AbstractConnectionFactory.newConnection(jdbcConn, true);
107             systemId = source.getSystemId();
108             if (mappingMetadata != null && systemId != null && !reload) {
109                 metadata = (RepositoryMapping) mappingMetadata.get(systemId);
110             }
111             if (metadata == null) {
112                 XMLReader reader = new ValidatingSchemaFilter(parserFactory.newSAXParser().getXMLReader(),
113                                                               new SchemaValidationContext(schemaManager));
114                 reader.setErrorHandler(new ValidationErrorHandler());
115                 Loader loader = new Loader(reader, connection, schemaManager, classLoader, false);
116                 MappingFactory mappingFactory = new MappingFactory(schemaManager, schemaManager);
117                 mappingFactory.createTree();
118                 metadata = loader.load(source, mappingFactory, true);
119                 if (log.isDebugEnabled()) {
120                     XMLReader parser = new XTreeReader(metadata);
121                     parser.setFeature(SAXConstants.TRACE_FEATURE, true);
122                     CharArrayWriter JavaDoc caw = new CharArrayWriter JavaDoc();
123                     parser.setContentHandler(new XMLSerializer(caw));
124                     parser.parse("");
125                     log.debug(caw);
126                 }
127                 if (mappingMetadata != null && systemId != null) {
128                     mappingMetadata.put(systemId, metadata);
129                 }
130             }
131         } catch (ParserConfigurationException JavaDoc ex) {
132             throw new XMLDBCException("Could not load SAX parser", ex);
133         } catch (SQLException JavaDoc e) {
134             throw new XMLDBCException("Error while loading mapping", e);
135         } catch (IOException JavaDoc e) {
136             throw new XMLDBCException("Error while loading mapping", e);
137         } catch (SAXException e) {
138             throw new XMLDBCException("Error while loading mapping", e);
139         } finally {
140             try {
141                 if (connection != null) connection.close(); // this closes also jdbcConn
142
else if (jdbcConn != null) jdbcConn.close();
143             } catch (SQLException JavaDoc e) {
144             }
145         }
146     }
147     
148     /**
149      * Frees all resources associated to the object
150      * @throws XMLDBCException if a database connection error occured.
151      */

152     public synchronized void close() throws XMLDBCException
153     {
154         Iterator JavaDoc it = mappers.iterator();
155         while (it.hasNext()) ((Mapper)it.next()).close();
156     }
157     
158     /**
159      * Creates a new Mapper object for storing a set of XML documents
160      * in the database
161      * @return a Mapper object
162      * @throws XMLDBCException if a database connection error occured.
163      * @see Mapper
164      */

165     public Mapper getMapper() throws XMLDBCException
166     {
167         SchemaValidationContext validationContext = new SchemaValidationContext(schemaManager);
168         ValidatingFilerAdapter validator = new ValidatingFilerAdapter(validationContext, schemaManager);
169         Connection JavaDoc jdbcConn = null;
170         AbstractConnection connection = null;
171         try {
172             jdbcConn = dataSource.getConnection();
173             connection = AbstractConnectionFactory.newConnection(jdbcConn, true);
174         } catch (SQLException JavaDoc ex) {
175             throw new XMLDBCException("Could not create connection", ex);
176         }
177         SAXHandler handler = new SAXHandler(new MappingIterator(metadata),
178                                             connection,
179                                             new MapperTupleBuilder(connection,
180                                                                    metadata,
181                                                                    validationContext)
182                                             );
183         validator.setSAXHandler(handler);
184         Mapper result = new MapperImpl(this, connection, validator, handler, handler, handler);
185         synchronized (mappers) {
186             mappers.add(result);
187         }
188         return result;
189     }
190     
191     /**
192      * Returns the system id (the URL) of the mapping file, if known.
193      * @return the system id of the mapping file, or null if unknown.
194      */

195     public String JavaDoc getSystemId() { return systemId;}
196     
197     /**
198      * Returns the JDBC data source used by this mapping.
199      * @return a JDBC datasource.
200      */

201     public DataSource JavaDoc getDataSource() { return dataSource;}
202     
203     /**
204      * Package-private method returning the internal description of the
205      * mapping file
206      * @return the internal representation of the mapping file
207      */

208     RepositoryMapping getMetaData() { return metadata;}
209     
210     /**
211      * Package-private maethod used to remove a Mapper from its parent list
212      */

213     synchronized void remove(MapperImpl mapper) { mappers.remove(mapper);}
214
215     private class ValidationErrorHandler implements ErrorHandler
216     {
217         public static final String JavaDoc RCSRevision = "$Revision: 1.12 $";
218         public static final String JavaDoc RCSName = "$Name: $";
219         
220         ////////////////////////////////////////////////////////////////////
221
// Implementation of ErrorHandler interface.
222
////////////////////////////////////////////////////////////////////
223
public void warning(SAXParseException ex) throws SAXException {
224             throw ex;
225         }
226         
227         public void error(SAXParseException ex) throws SAXException {
228             throw ex;
229         }
230         
231         public void fatalError(SAXParseException ex) throws SAXException {
232             throw ex;
233         }
234     }
235 }
236
237
238
Popular Tags