Search in sources :

Example 1 with ConfigurationException

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

the class CompatibilityDataChannelDescriptorLoader method load.

@Override
public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
    if (configurationResource == null) {
        throw new NullPointerException("Null configurationResource");
    }
    if (!(upgradeServiceProvider.get() instanceof CompatibilityUpgradeService)) {
        throw new ConfigurationException("CompatibilityUpgradeService expected");
    }
    CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) upgradeServiceProvider.get();
    UpgradeMetaData metaData = upgradeService.getUpgradeType(configurationResource);
    if (metaData.getUpgradeType() == UpgradeType.UPGRADE_NOT_NEEDED) {
        return super.load(configurationResource);
    }
    if (metaData.getUpgradeType() == UpgradeType.DOWNGRADE_NEEDED) {
        throw new ConfigurationException("Unable to load configuration from %s: " + "It was created using a newer version of the Modeler", configurationResource.getURL());
    }
    if (metaData.getUpgradeType() == UpgradeType.INTERMEDIATE_UPGRADE_NEEDED) {
        throw new ConfigurationException("Unable to load configuration from %s: " + "Open the project in the older Modeler to do an intermediate upgrade.", configurationResource.getURL());
    }
    URL configurationURL = configurationResource.getURL();
    upgradeService.upgradeProject(configurationResource);
    Document projectDocument = documentProvider.getDocument(configurationURL);
    if (projectDocument == null) {
        throw new ConfigurationException("Unable to upgrade " + configurationURL);
    }
    logger.info("Loading XML configuration resource from " + configurationURL);
    final DataChannelDescriptor descriptor = new DataChannelDescriptor();
    descriptor.setConfigurationSource(configurationResource);
    descriptor.setName(nameMapper.configurationNodeName(DataChannelDescriptor.class, configurationResource));
    try {
        DOMSource source = new DOMSource(projectDocument);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory transFactory = TransformerFactory.newInstance();
        transFactory.newTransformer().transform(source, new StreamResult(baos));
        InputSource isource = new InputSource(source.getSystemId());
        isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
        XMLReader parser = Util.createXmlReader();
        LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
        loaderContext.addDataMapListener(new DataMapLoaderListener() {

            @Override
            public void onDataMapLoaded(DataMap dataMap) {
                descriptor.getDataMaps().add(dataMap);
            }
        });
        DataChannelHandler rootHandler = new DataChannelHandler(this, descriptor, loaderContext);
        parser.setContentHandler(rootHandler);
        parser.setErrorHandler(rootHandler);
        parser.parse(isource);
    } catch (Exception e) {
        throw new ConfigurationException("Error loading configuration from %s", e, configurationURL);
    }
    // Finally upgrade model, if needed
    upgradeService.upgradeModel(configurationResource, descriptor);
    return new ConfigurationTree<>(descriptor, null);
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) UpgradeMetaData(org.apache.cayenne.project.upgrade.UpgradeMetaData) URL(java.net.URL) ConfigurationException(org.apache.cayenne.ConfigurationException) DataMap(org.apache.cayenne.map.DataMap) ConfigurationException(org.apache.cayenne.ConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) CompatibilityUpgradeService(org.apache.cayenne.project.compatibility.CompatibilityUpgradeService) XMLReader(org.xml.sax.XMLReader)

Example 2 with ConfigurationException

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

the class CompatibilityDataMapLoader method load.

@Override
public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
    Document document = documentProvider.getDocument(configurationResource.getURL());
    // no document yet in provider, maybe DataMap is directly loaded
    if (document == null) {
        if (!(upgradeServiceProvider.get() instanceof CompatibilityUpgradeService)) {
            throw new ConfigurationException("CompatibilityUpgradeService expected");
        }
        // try to upgrade datamap directly
        CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) upgradeServiceProvider.get();
        upgradeService.upgradeDataMap(configurationResource);
        document = documentProvider.getDocument(configurationResource.getURL());
        // still no document, try to load it without upgrade, though it likely will fail
        if (document == null) {
            return super.load(configurationResource);
        }
    }
    final DataMap[] maps = new DataMap[1];
    try {
        DOMSource source = new DOMSource(document);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory transFactory = TransformerFactory.newInstance();
        transFactory.newTransformer().transform(source, new StreamResult(baos));
        InputSource isource = new InputSource(source.getSystemId());
        isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
        XMLReader parser = Util.createXmlReader();
        LoaderContext loaderContext = new LoaderContext(parser, handlerFactory);
        loaderContext.addDataMapListener(dataMap -> maps[0] = dataMap);
        RootDataMapHandler rootHandler = new RootDataMapHandler(loaderContext);
        parser.setContentHandler(rootHandler);
        parser.setErrorHandler(rootHandler);
        parser.parse(isource);
    } catch (Exception e) {
        throw new CayenneRuntimeException("Error loading configuration from %s", e, configurationResource.getURL());
    }
    if (maps[0] == null) {
        throw new CayenneRuntimeException("Unable to load data map from %s", configurationResource.getURL());
    }
    DataMap map = maps[0];
    if (map.getName() == null) {
        // set name based on location if no name provided by map itself
        map.setName(mapNameFromLocation(configurationResource.getURL().getFile()));
    }
    return map;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ConfigurationException(org.apache.cayenne.ConfigurationException) DataMap(org.apache.cayenne.map.DataMap) ConfigurationException(org.apache.cayenne.ConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) CompatibilityUpgradeService(org.apache.cayenne.project.compatibility.CompatibilityUpgradeService) XMLReader(org.xml.sax.XMLReader)

Example 3 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 = (Driver) objectFactory.getJavaClass(descriptor.getJdbcDriver()).newInstance();
    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 4 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 5 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)

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