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 http://www.apache.org/licenses/LICENSE-2.0 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.io.File;
 import java.util.List;
 import java.util.Map;
 
 
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.

Author(s):
Andrus Adamchik
Since:
1.2
 
 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
     }

    

Deprecated:
since 1.2
 
     public DataSource getDataSource(String locationLevel logLevelthrows Exception {
         return getDataSource(location);
     }

    
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"db.properties").exists()) {
            throw new CayenneRuntimeException(
                    "No preferences database exists in directory " + dbDir);
        }
        String preferencesDB = new File(dbDir"db").getAbsolutePath();
        // create custom preferences service...
                preferencesDB,
                .,
                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 {
            service.startService();
            return toDataSource(service.getDataContext(), location);
        }
        finally {
            // make sure we cleanup after ourselves...
            try {
                service.stopService();
            }
            catch (Throwable th) {
                // ignore..
            }
        }
    }
    DataSource toDataSource(DataContext contextString locationthrows Exception {
        // grep through all domains ... maybe a bit naive...
        Expression locationFilter = ExpressionFactory.matchExp(
                .,
                location);
        List preferences = context.performQuery(new SelectQuery(
                DomainPreference.class,
                locationFilter));
        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 = (DomainPreferenceit.next();
            ids.add(DataObjectUtils.pkForObject(pref));
        }
        Expression qualifier = Expression.fromString("db:"
                + .
                + " in $ids");
        Map params = Collections.singletonMap("ids"ids);
        SelectQuery query = new SelectQuery(DBConnectionInfo.classqualifier
                .expWithParameters(params));
        // 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