Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 package org.apache.cayenne.modeler.pref;
 import java.util.List;
 import java.util.Map;
 import  org.apache.cayenne.CayenneRuntimeException;
 import  org.apache.cayenne.DataObjectUtils;
 import  org.apache.cayenne.access.DataContext;
 import  org.apache.cayenne.conf.Configuration;
 import  org.apache.cayenne.conf.DataSourceFactory;
 import  org.apache.cayenne.conn.PoolManager;
 import  org.apache.cayenne.exp.Expression;
 import  org.apache.cayenne.exp.ExpressionFactory;
 import  org.apache.cayenne.project.CayenneUserDir;
 import  org.apache.cayenne.query.SelectQuery;

A DataSourceFactory that loads DataSources from CayenneModeler preferences. Allows integrating Cayenne runtime with preferences engine. Currently JNDIDataSourceFactory uses this factory as a failover loading mechanism, instantiating it via reflection.

 public class PreferencesDataSourceFactory implements DataSourceFactory {
     protected int minPoolSize;
     protected int maxPoolSize;
     public PreferencesDataSourceFactory() {
         // init pool size default
         this(1, 5);
     public PreferencesDataSourceFactory(int minPoolSizeint maxPoolSize) {
         this. = minPoolSize;
         this. = maxPoolSize;
     public int getMaxPoolSize() {
         return ;
     public int getMinPoolSize() {
         return ;
     public void initializeWithParentConfiguration(Configuration configuaration) {
         // noop

Attempts to read named DataSource info from preferences and create a DataSource out of it. If no matching DataSource is found, throws CayenneRuntimeException.
     public DataSource getDataSource(final String locationthrows Exception {
         if (location == null) {
             throw new NullPointerException("Null location");
         // figure out preferences DB location...
         // TODO: once prefs package becomes a part of Cayenne, remove dependency on
         // Application class... also this code is redundant with what Application does in
         // constructor
         String configuredName = System.getProperty(.);
         String name = (configuredName != null)
                 ? configuredName
                 : .;
        String subdir = System.getProperty(.);
        if (subdir == null) {
            subdir = .;
        File dbDir = new File(CayenneUserDir.getInstance().resolveFile(
                .), subdir);
        // check if preferences even exist...
        if (!dbDir.isDirectory()) {
            throw new CayenneRuntimeException(
                    "No preferences database directory exists: " + dbDir);
        else if (!new File(dbDir"").exists()) {
            throw new CayenneRuntimeException(
                    "No preferences database exists in directory " + dbDir);
        String preferencesDB = new File(dbDir"db").getAbsolutePath();
        // create custom preferences service...
                name) {
            protected void startTimer() {
                // noop: disable commit timer
            protected void initPreferences() {
                // noop: disable commit timer
            protected void initSchema() {
                // throw - no schema means no DataSource data
                throw new CayenneRuntimeException("No preferences matching location: "
                        + location);
        try {
            return toDataSource(service.getDataContext(), location);
        finally {
            // make sure we cleanup after ourselves...
            try {
            catch (Throwable th) {
                // ignore..
    DataSource toDataSource(DataContext contextString locationthrows Exception {
        // grep through all domains ... maybe a bit naive...
        Expression locationFilter = ExpressionFactory.matchExp(
        List preferences = context.performQuery(new SelectQuery(
        if (preferences.isEmpty()) {
            throw new CayenneRuntimeException("No preferences matching location: "
                    + location);
        Collection ids = new ArrayList(preferences.size());
        Iterator it = preferences.iterator();
        while (it.hasNext()) {
            DomainPreference pref = (;
        Expression qualifier = Expression.fromString("db:"
                + .
                + " in $ids");
        Map params = Collections.singletonMap("ids"ids);
        SelectQuery query = new SelectQuery(DBConnectionInfo.classqualifier
        // narrow down the results to just DBConnectionInfo
        List connectionData = context.performQuery(query);
        if (connectionData.isEmpty()) {
            throw new CayenneRuntimeException("No preferences matching location: "
                    + location);
        if (connectionData.size() > 1) {
            throw new CayenneRuntimeException(
                    "More than one preference matched location: " + location);
        DBConnectionInfo info = (DBConnectionInfoconnectionData.get(0);
        if (info.getJdbcDriver() == null) {
            throw new CayenneRuntimeException(
                    "Incomplete connection info: no JDBC driver set.");
        if (info.getUrl() == null) {
            throw new SQLException("Incomplete connection info: no DB URL set.");
        // use default values for connection pool ... no info is available from
        // preferences...
        return new PoolManager(info.getJdbcDriver(), info.getUrl(), 1, 5, info
                .getUserName(), info.getPassword());
New to GrepCode? Check out our FAQ X