KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > DbAdapterFactoryChain


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba;
21
22 import java.sql.DatabaseMetaData JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * A facade for a collection of DbAdapterFactories. Can be configured to autodetect all
30  * adapters known to Cayenne or can work with custom factories.
31  *
32  * @since 1.2
33  * @author Andrus Adamchik
34  */

35 // TODO, Andrus 11/01/2005, how can custom adapters be autodetected? I.e. is there a way
36
// to plug a custom factory into configuration loading process? Of course users can simply
37
// specify the adapter class in the modeler, so this may be a non-issue.
38
class DbAdapterFactoryChain implements DbAdapterFactory {
39
40     List JavaDoc factories;
41
42     DbAdapterFactoryChain(Collection JavaDoc factories) {
43         this.factories = new ArrayList JavaDoc();
44         this.factories.addAll(factories);
45     }
46
47     /**
48      * Iterates through predicated factories, stopping when the first one returns non-null
49      * DbAdapter. If none of the factories match the database, returns null.
50      */

51     public DbAdapter createAdapter(DatabaseMetaData JavaDoc md) throws SQLException JavaDoc {
52
53         // match against configured predicated factories
54

55         // iterate in reverse order to allow custom factories to take precedence over the
56
// default ones configured in constructor
57
for (int i = factories.size() - 1; i >= 0; i--) {
58             DbAdapterFactory factory = (DbAdapterFactory) factories.get(i);
59             DbAdapter adapter = factory.createAdapter(md);
60
61             if (adapter != null) {
62                 return adapter;
63             }
64         }
65
66         return null;
67     }
68
69     /**
70      * Removes all configured factories.
71      */

72     void clearFactories() {
73         this.factories.clear();
74     }
75
76     /**
77      * Adds a new DbAdapterFactory to the factory chain.
78      */

79     void addFactory(DbAdapterFactory factory) {
80         this.factories.add(factory);
81     }
82 }
83
Popular Tags