KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > runtime > MediatorConfiguration


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.mediator.runtime;
24
25 import java.io.IOException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.parsers.SAXParserFactory JavaDoc;
33
34 import org.xml.sax.*;
35 import org.xml.sax.helpers.DefaultHandler JavaDoc;
36 import org.xquark.mediator.XDBCWrapper;
37 import org.xquark.schema.SchemaManager;
38 import org.xquark.schema.validation.SchemaValidationContext;
39 import org.xquark.schema.validation.ValidatingSchemaFilter;
40 import org.xquark.xml.xdbc.XMLDBCException;
41 import org.xquark.xml.xdbc.XMLDriver;
42 import org.xquark.xml.xdbc.XMLDriverManager;
43
44 /**
45  * This class is used to parse a mediator accessor file.
46  *
47  */

48
49 public class MediatorConfiguration extends DefaultHandler JavaDoc {
50     // **********************************************************************
51
// * VERSIONING
52
// **********************************************************************
53
private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
54     private static final String JavaDoc RCSName = "$Name: $";
55     // **********************************************************************
56
// * CLASS VARIABLE
57
// **********************************************************************
58
private final static String JavaDoc LABEL_ACCESSOR = "accessor";
59     private final static String JavaDoc LABEL_NAME = "name";
60     private final static String JavaDoc LABEL_DRIVER = "driver";
61     private final static String JavaDoc LABEL_CONNECTION = "connection";
62     private final static String JavaDoc LABEL_SUBACCESSOR = "subaccessor";
63
64     private final static String JavaDoc LABEL_LOGIN = "login";
65     private final static String JavaDoc LABEL_PASSWD = "password";
66
67     public final static String JavaDoc KEY_NAME = "key_name";
68     public final static String JavaDoc KEY_SUBACCESSORS = "key_subaccessors";
69     public final static String JavaDoc KEY_BASE_URI = "key_base_uri";
70     public final static String JavaDoc KEY_DRIVER = "key_driver";
71     public final static String JavaDoc KEY_CONNECTION = "key_connection";
72     public final static String JavaDoc KEY_LOGIN = "key_login";
73     public final static String JavaDoc KEY_PASSWD = "key_passwd";
74
75
76     private static SAXParserFactory JavaDoc spf = null;
77     private static SchemaManager schemamanager = new SchemaManager();
78     public static final String JavaDoc MEDIATOR_XSD = "/org/xquark/mediator/resources/Mediator.xsd";
79
80     static {
81         // Create a JAXP SAXParserFactory and configure it
82
spf = SAXParserFactory.newInstance();
83         spf.setNamespaceAware(true);
84         spf.setValidating(false);
85         
86         try {
87             URL JavaDoc url = MediatorConfiguration.class.getResource(MEDIATOR_XSD);
88             InputSource source = new InputSource(url.toString());
89             schemamanager.loadSchema(source);
90         } catch (SAXException e) {
91             // do nothing !!!
92
}
93     }
94
95     private Properties JavaDoc xmlproperties = new Properties JavaDoc();
96     private ArrayList JavaDoc wrappers = new ArrayList JavaDoc();
97     private URL JavaDoc baseDir = null;
98     private Locator locator = null;
99
100     protected StringBuffer JavaDoc cur_car = new StringBuffer JavaDoc();
101     protected String JavaDoc sub_name = null;
102     protected String JavaDoc sub_driver = null;
103     protected String JavaDoc sub_url = null;
104     protected String JavaDoc sub_user = null;
105     protected String JavaDoc sub_password = null;
106     
107
108     // **********************************************************************
109
// * CONSTRUCTOR
110
// **********************************************************************
111
/**
112      *
113      */

114     public MediatorConfiguration() {
115         xmlproperties.put(KEY_SUBACCESSORS, wrappers);
116     }
117
118     public Properties JavaDoc getProperties() {
119         return xmlproperties;
120     }
121     
122     public static Properties JavaDoc loadConfiguration(String JavaDoc configURI) throws XMLDBCException, MalformedURLException JavaDoc {
123         URL JavaDoc url = new URL JavaDoc(configURI);
124         try {
125             MediatorConfiguration handler = new MediatorConfiguration();
126             // Get the encapsulated SAX XMLReader
127
XMLReader reader = spf.newSAXParser().getXMLReader();
128             ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, new SchemaValidationContext(schemamanager));
129             filter.setContentHandler(handler);
130             filter.setErrorHandler(handler);
131             filter.parse(configURI);
132             return handler.getProperties();
133         } catch (SAXParseException ex) {
134             String JavaDoc msg = "Error in parsing file " + ex.getSystemId() + " at line " + Integer.toString(ex.getLineNumber()) +
135                          " : " + ex.getMessage();
136             throw new XMLDBCException(msg, ex);
137         } catch (SAXException ex) {
138             throw new XMLDBCException("Could not parse configuration file", ex);
139         } catch (IOException JavaDoc ex) {
140             throw new XMLDBCException("Could not read configuration file", ex);
141         } catch (ParserConfigurationException JavaDoc ex) {
142             throw new XMLDBCException("Could not allocate XML parser", ex);
143         }
144     }
145     
146     // **********************************************************************
147
// * TOOLS METHODS
148
// **********************************************************************
149
/**
150      * Return a property of the accessor.
151      *
152      * @return a property of the accessor.
153      */

154     private String JavaDoc getProperty(String JavaDoc key) {
155         return (String JavaDoc) xmlproperties.getProperty(key);
156     }
157
158     /**
159      * Set a property of the accessor.
160      *
161      * @param a property of the accessor.
162      */

163     private void setProperty(String JavaDoc key, String JavaDoc value) {
164         if (key == null) {
165             System.err.println("Key " + key + " is null.");
166             return;
167         }
168         if (value == null) {
169             System.err.println("Value " + value + " of " + key + " is null.");
170             return;
171         }
172         xmlproperties.setProperty(key, value);
173     }
174
175     // **********************************************************************
176
// * SAX IMPLEMENTATION
177
// **********************************************************************
178

179     public void setDocumentLocator(Locator loc) {
180         try {
181             baseDir = new URL JavaDoc(loc.getSystemId());
182         } catch (MalformedURLException JavaDoc e) {
183             // should not occur
184
}
185     }
186
187     /**
188      *
189      */

190     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qualifiedName, Attributes atts) throws SAXException {
191         cur_car.setLength(0);
192         if (localName.equalsIgnoreCase(LABEL_ACCESSOR)) {
193             setProperty(KEY_NAME, atts.getValue(LABEL_NAME));
194         } else if (localName.equalsIgnoreCase(LABEL_SUBACCESSOR)) {
195             sub_name = atts.getValue(LABEL_NAME);
196             sub_driver = null;
197             sub_url = null;
198             sub_user = null;
199             sub_password = null;
200         }
201     }
202
203     /**
204      *
205      */

206     public void characters(char ch[], int start, int length) throws SAXException {
207         cur_car.append(ch, start, length);
208     }
209
210     /**
211      *
212      */

213     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qualifiedName) throws SAXException {
214         String JavaDoc value = cur_car.toString().trim();
215         cur_car.setLength(0);
216         if (localName.equalsIgnoreCase(LABEL_DRIVER)) {
217             sub_driver = value;
218             try {
219                 Class.forName(sub_driver);
220             } catch (ClassNotFoundException JavaDoc ex) {
221                 throw new SAXParseException("Could not load driver class", locator, ex);
222             }
223         } else if (localName.equalsIgnoreCase(LABEL_CONNECTION)) {
224             sub_url = value;
225             try {
226                 XMLDriver d = XMLDriverManager.getDriver(sub_url);
227                 if (d != null) {
228                     String JavaDoc localPart = d.getSpecificPart(sub_url);
229                     if (localPart.startsWith("file:")) {
230                         URL JavaDoc url = new URL JavaDoc(baseDir, localPart.substring(5));
231                         String JavaDoc driverPart = sub_url.substring(0, sub_url.indexOf(localPart));
232                         sub_url = driverPart+url.toString();
233                     }
234                 }
235             } catch (XMLDBCException e) {
236                 throw new SAXParseException("No suitable driver found for "+sub_url, locator, e);
237             } catch (MalformedURLException JavaDoc e) {
238                 // ignore and leave sub_url as it is
239
}
240         } else if (localName.equalsIgnoreCase(LABEL_LOGIN)) {
241             sub_user = value;
242         } else if (localName.equalsIgnoreCase(LABEL_PASSWD)) {
243             sub_password = value;
244         } else if (localName.equalsIgnoreCase(LABEL_SUBACCESSOR)) {
245             wrappers.add(new XDBCWrapper(sub_name, sub_driver, sub_url, sub_user, sub_password));
246         }
247     }
248
249     public void error(SAXParseException spee) throws SAXException {
250         System.err.println("Warning : " + spee.toString());
251     }
252 }
253
Popular Tags