1 22 23 package org.xquark.mediator.runtime; 24 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Properties ; 30 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.parsers.SAXParserFactory ; 33 34 import org.xml.sax.*; 35 import org.xml.sax.helpers.DefaultHandler ; 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 48 49 public class MediatorConfiguration extends DefaultHandler { 50 private static final String RCSRevision = "$Revision: 1.1 $"; 54 private static final String RCSName = "$Name: $"; 55 private final static String LABEL_ACCESSOR = "accessor"; 59 private final static String LABEL_NAME = "name"; 60 private final static String LABEL_DRIVER = "driver"; 61 private final static String LABEL_CONNECTION = "connection"; 62 private final static String LABEL_SUBACCESSOR = "subaccessor"; 63 64 private final static String LABEL_LOGIN = "login"; 65 private final static String LABEL_PASSWD = "password"; 66 67 public final static String KEY_NAME = "key_name"; 68 public final static String KEY_SUBACCESSORS = "key_subaccessors"; 69 public final static String KEY_BASE_URI = "key_base_uri"; 70 public final static String KEY_DRIVER = "key_driver"; 71 public final static String KEY_CONNECTION = "key_connection"; 72 public final static String KEY_LOGIN = "key_login"; 73 public final static String KEY_PASSWD = "key_passwd"; 74 75 76 private static SAXParserFactory spf = null; 77 private static SchemaManager schemamanager = new SchemaManager(); 78 public static final String MEDIATOR_XSD = "/org/xquark/mediator/resources/Mediator.xsd"; 79 80 static { 81 spf = SAXParserFactory.newInstance(); 83 spf.setNamespaceAware(true); 84 spf.setValidating(false); 85 86 try { 87 URL url = MediatorConfiguration.class.getResource(MEDIATOR_XSD); 88 InputSource source = new InputSource(url.toString()); 89 schemamanager.loadSchema(source); 90 } catch (SAXException e) { 91 } 93 } 94 95 private Properties xmlproperties = new Properties (); 96 private ArrayList wrappers = new ArrayList (); 97 private URL baseDir = null; 98 private Locator locator = null; 99 100 protected StringBuffer cur_car = new StringBuffer (); 101 protected String sub_name = null; 102 protected String sub_driver = null; 103 protected String sub_url = null; 104 protected String sub_user = null; 105 protected String sub_password = null; 106 107 108 114 public MediatorConfiguration() { 115 xmlproperties.put(KEY_SUBACCESSORS, wrappers); 116 } 117 118 public Properties getProperties() { 119 return xmlproperties; 120 } 121 122 public static Properties loadConfiguration(String configURI) throws XMLDBCException, MalformedURLException { 123 URL url = new URL (configURI); 124 try { 125 MediatorConfiguration handler = new MediatorConfiguration(); 126 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 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 ex) { 140 throw new XMLDBCException("Could not read configuration file", ex); 141 } catch (ParserConfigurationException ex) { 142 throw new XMLDBCException("Could not allocate XML parser", ex); 143 } 144 } 145 146 154 private String getProperty(String key) { 155 return (String ) xmlproperties.getProperty(key); 156 } 157 158 163 private void setProperty(String key, String 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 179 public void setDocumentLocator(Locator loc) { 180 try { 181 baseDir = new URL (loc.getSystemId()); 182 } catch (MalformedURLException e) { 183 } 185 } 186 187 190 public void startElement(String namespaceURI, String localName, String 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 206 public void characters(char ch[], int start, int length) throws SAXException { 207 cur_car.append(ch, start, length); 208 } 209 210 213 public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException { 214 String 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 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 localPart = d.getSpecificPart(sub_url); 229 if (localPart.startsWith("file:")) { 230 URL url = new URL (baseDir, localPart.substring(5)); 231 String 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 e) { 238 } 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 |