Search in sources :

Example 6 with ConfigurationException

use of org.apache.cayenne.ConfigurationException in project cayenne by apache.

the class XMLDataChannelDescriptorLoaderTest method testLoad_MissingConfig.

@Test
public void testLoad_MissingConfig() throws Exception {
    // create and initialize loader instance to test
    XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
    injector.injectMembers(loader);
    try {
        loader.load(new URLResource(new URL("file:///no_such_resource")));
        fail("No exception was thrown on bad absent config name");
    } catch (ConfigurationException e) {
    // expected
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) ConfigurationException(org.apache.cayenne.ConfigurationException) URL(java.net.URL) Test(org.junit.Test)

Example 7 with ConfigurationException

use of org.apache.cayenne.ConfigurationException in project cayenne by apache.

the class XMLPoolingDataSourceFactory method getDataSource.

@Override
public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
    DataSourceInfo descriptor = nodeDescriptor.getDataSourceDescriptor();
    if (descriptor == null) {
        String message = "Null dataSourceDescriptor for nodeDescriptor '" + nodeDescriptor.getName() + "'";
        logger.info(message);
        throw new ConfigurationException(message);
    }
    long maxQueueWaitTime = properties.getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME, UnmanagedPoolingDataSource.MAX_QUEUE_WAIT_DEFAULT);
    Driver driver = objectFactory.newInstance(Driver.class, descriptor.getJdbcDriver());
    return DataSourceBuilder.url(descriptor.getDataSourceUrl()).driver(driver).userName(descriptor.getUserName()).password(descriptor.getPassword()).pool(descriptor.getMinConnections(), descriptor.getMaxConnections()).maxQueueWaitTime(maxQueueWaitTime).build();
}
Also used : DataSourceInfo(org.apache.cayenne.conn.DataSourceInfo) ConfigurationException(org.apache.cayenne.ConfigurationException) Driver(java.sql.Driver)

Example 8 with ConfigurationException

use of org.apache.cayenne.ConfigurationException in project cayenne by apache.

the class DataSourceChildrenHandler method configureConnectionPool.

void configureConnectionPool(Attributes attributes) {
    String min = attributes.getValue("min");
    if (min != null) {
        try {
            dataSourceDescriptor.setMinConnections(Integer.parseInt(min));
        } catch (NumberFormatException nfex) {
            logger.info("Non-numeric 'min' attribute", nfex);
            throw new ConfigurationException("Non-numeric 'min' attribute '%s'", nfex, min);
        }
    }
    String max = attributes.getValue("max");
    if (max != null) {
        try {
            dataSourceDescriptor.setMaxConnections(Integer.parseInt(max));
        } catch (NumberFormatException nfex) {
            logger.info("Non-numeric 'max' attribute", nfex);
            throw new ConfigurationException("Non-numeric 'max' attribute '%s'", nfex, max);
        }
    }
}
Also used : ConfigurationException(org.apache.cayenne.ConfigurationException)

Example 9 with ConfigurationException

use of org.apache.cayenne.ConfigurationException in project cayenne by apache.

the class XMLDataChannelDescriptorLoader method load.

@Override
public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
    if (configurationResource == null) {
        throw new NullPointerException("Null configurationResource");
    }
    URL configurationURL = configurationResource.getURL();
    logger.info("Loading XML configuration resource from " + configurationURL);
    final DataChannelDescriptor descriptor = new DataChannelDescriptor();
    descriptor.setConfigurationSource(configurationResource);
    descriptor.setName(nameMapper.configurationNodeName(DataChannelDescriptor.class, configurationResource));
    try (InputStream in = configurationURL.openStream()) {
        XMLReader parser = xmlReaderProvider.get();
        LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
        loaderContext.addDataMapListener(dataMap -> descriptor.getDataMaps().add(dataMap));
        DataChannelHandler rootHandler = new DataChannelHandler(this, descriptor, loaderContext);
        parser.setContentHandler(rootHandler);
        parser.setErrorHandler(rootHandler);
        InputSource input = new InputSource(in);
        input.setSystemId(configurationURL.toString());
        parser.parse(input);
        loaderContext.dataChannelLoaded(descriptor);
    } catch (Exception e) {
        throw new ConfigurationException("Error loading configuration from %s", e, configurationURL);
    }
    // TODO: andrus 03/10/2010 - actually provide load failures here...
    return new ConfigurationTree<>(descriptor, null);
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) InputSource(org.xml.sax.InputSource) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) ConfigurationException(org.apache.cayenne.ConfigurationException) InputStream(java.io.InputStream) URL(java.net.URL) XMLReader(org.xml.sax.XMLReader) IOException(java.io.IOException) ConfigurationException(org.apache.cayenne.ConfigurationException)

Example 10 with ConfigurationException

use of org.apache.cayenne.ConfigurationException in project cayenne by apache.

the class Util method readDocument.

/**
 * @since 4.1
 * @param url to read
 * @return org.w3c.dom.Document from the given URL
 */
public static Document readDocument(URL url) {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(false);
    try {
        DocumentBuilder domBuilder = documentBuilderFactory.newDocumentBuilder();
        try (InputStream inputStream = url.openStream()) {
            return domBuilder.parse(inputStream);
        } catch (IOException | SAXException e) {
            throw new ConfigurationException("Error loading configuration from %s", e, url);
        }
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.apache.cayenne.ConfigurationException) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

ConfigurationException (org.apache.cayenne.ConfigurationException)16 URL (java.net.URL)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ConfigurationTree (org.apache.cayenne.configuration.ConfigurationTree)3 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)3 InputSource (org.xml.sax.InputSource)3 XMLReader (org.xml.sax.XMLReader)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 DataMap (org.apache.cayenne.map.DataMap)2 CompatibilityUpgradeService (org.apache.cayenne.project.compatibility.CompatibilityUpgradeService)2 Document (org.w3c.dom.Document)2 File (java.io.File)1 ObjectInputStream (java.io.ObjectInputStream)1 Driver (java.sql.Driver)1