Home » apache-openjpa-1.1.0-source » org.apache.openjpa.lib » jdbc » [javadoc | source]
    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   package org.apache.openjpa.lib.jdbc;
   20   
   21   import java.sql.Connection;
   22   import java.sql.SQLException;
   23   import java.util.Collection;
   24   import java.util.Collections;
   25   import java.util.Iterator;
   26   import java.util.List;
   27   import javax.sql.DataSource;
   28   
   29   import java.util.concurrent.CopyOnWriteArrayList;
   30   
   31   /**
   32    * Delegating data source that maintains a list of {@link ConnectionDecorator}s.
   33    *
   34    * @author Abe White
   35    * @nojavadoc
   36    */
   37   public class DecoratingDataSource extends DelegatingDataSource {
   38   
   39       private List _decorators = new CopyOnWriteArrayList();
   40   
   41       /**
   42        * Constructor. Supply wrapped data source.
   43        */
   44       public DecoratingDataSource(DataSource ds) {
   45           super(ds);
   46       }
   47   
   48       /**
   49        * Return a read-only list of connection decorators in the order they were
   50        * added.
   51        */
   52       public Collection getDecorators() {
   53           return Collections.unmodifiableCollection(_decorators);
   54       }
   55   
   56       /**
   57        * Add a connection decorator.
   58        */
   59       public void addDecorator(ConnectionDecorator decorator) {
   60           if (decorator != null)
   61               _decorators.add(decorator);
   62       }
   63   
   64       /**
   65        * Add multiple connection decorators efficiently.
   66        */
   67       public void addDecorators(Collection decorators) {
   68           if (decorators != null)
   69               _decorators.addAll(decorators);
   70       }
   71   
   72       /**
   73        * Remove a connection decorator.
   74        */
   75       public boolean removeDecorator(ConnectionDecorator decorator) {
   76           return _decorators.remove(decorator);
   77       }
   78   
   79       /**
   80        * Clear all decorators.
   81        */
   82       public void clearDecorators() {
   83           _decorators.clear();
   84       }
   85   
   86       public Connection getConnection() throws SQLException {
   87           Connection conn = super.getConnection();
   88           return decorate(conn);
   89       }
   90   
   91       public Connection getConnection(String user, String pass)
   92           throws SQLException {
   93           Connection conn = super.getConnection(user, pass);
   94           return decorate(conn);
   95       }
   96   
   97       private Connection decorate(Connection conn) throws SQLException {
   98           if (!_decorators.isEmpty())
   99               for (Iterator itr = _decorators.iterator(); itr.hasNext();)
  100                   conn = ((ConnectionDecorator) itr.next()).decorate(conn);
  101           return conn;
  102       }
  103   }

Save This Page
Home » apache-openjpa-1.1.0-source » org.apache.openjpa.lib » jdbc » [javadoc | source]