Android Open Source - jepldroid J E P L Data Source Impl






From Project

Back to project page jepldroid.

License

The source code is released under:

Apache License

If you think the Android project jepldroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
   Copyright 2011 Jose Maria Arranz Santamaria
/*w w w  . j a  va2  s  .c  o m*/
   Licensed 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 jepl.impl;

import jepl.impl.query.JEPLResultSetDAOListenerDefaultImpl;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import jepl.JEPLBoot;
import jepl.JEPLConnection;
import jepl.JEPLDAL;
import jepl.JEPLDAO;
import jepl.JEPLDataSource;
import jepl.JEPLException;
import jepl.JEPLListener;
import jepl.JEPLResultSetDAOListenerDefault;
import jepl.JEPLRowBeanMapper;

/**
 * A la hora de heredar entre los sistemas NonJTA y JTA hay que conseguir que
 * JEPLayer funcionen sin dependencias JTA (clases java.transaction)
 * pues mientras el hilo de ejecucin no pase por las clases JTA internas
 * no se intentarn cargar dependencias JTA
 *
 * @author jmarranz
 */
public abstract class JEPLDataSourceImpl implements JEPLDataSource
{
    protected JEPLBootImpl boot;
    protected DataSource ds;
    protected ThreadLocal<JEPLConnectionImpl> connectionByThread = new ThreadLocal<JEPLConnectionImpl>();
    protected JEPLListenerListImpl listenerList = new JEPLListenerListImpl();
    protected JEPLUserDataMultiThreadImpl userData = new JEPLUserDataMultiThreadImpl();
    protected boolean preparedStatementCached = true;
    protected volatile boolean inUse = false; // La verdad es que el volatile sobra pues es para detectar errores en tiempo de desarollo
    protected volatile boolean isC3PO = false;
    
    public JEPLDataSourceImpl(JEPLBootImpl boot,DataSource ds)
    {
        this.boot = boot;
        this.ds = ds;
    }

    public String[] getUserDataNames()
    {
        return userData.getUserDataNames();
    }

    public boolean containsName(String name)
    {
        return userData.containsName(name);
    }

    public Object getUserData(String name)
    {
        return userData.getUserData(name);
    }

    public <T> T getUserData(String name, Class<T> returnType)
    {
        return userData.getUserData(name, returnType);
    }

    public Object setUserData(String name, Object value)
    {
        return userData.setUserData(name, value);
    }

    public Object removeUserData(String name)
    {
        return userData.removeUserData(name);
    }

    public <T> T removeUserData(String name, Class<T> returnType)
    {
        return userData.removeUserData(name, returnType);
    }

    public boolean isInUse()
    {
        return inUse;
    }

    public boolean isC3PO()
    {
        return isC3PO;
    }
    
    public void setIsC3PO(boolean isC3PO)
    {
        this.isC3PO = isC3PO;
    }
    
    public JEPLBoot getJEPLBoot()
    {
        return boot;
    }

    public JEPLBootImpl getJEPLBootImpl()
    {
        return boot;
    }

    public DataSource getDataSource()
    {
        return ds;
    }

    public boolean isPreparedStatementCached()
    {
        return preparedStatementCached;
    }

    public void setPreparedStatementCached(boolean value)
    {
        checkIsInUse();
        this.preparedStatementCached = value;
    }   

    public JEPLListenerListImpl getJEPLListenerList()
    {
        return listenerList;
    }

    public void addJEPLListener(JEPLListener listener)
    {
        listenerList.addJEPLListener(listener);
    }

    public void removeJEPLListener(JEPLListener listener)
    {
        listenerList.removeJEPLListener(listener);
    }

    public JEPLConnection getCurrentJEPLConnection()
    {
        return getCurrentJEPLConnectionImpl(); // Puede ser null
    }

    public JEPLConnectionImpl getCurrentJEPLConnectionImpl() 
    {
        return connectionByThread.get(); // Puede ser null
    }

    protected void checkIsInUse()
    {
        if (inUse) throw new JEPLException("DataSource is already in use");
    }

    public JEPLConnectionImpl getJEPLConnectionFromPool() throws SQLException
    {
        Connection con = ds.getConnection();
        return createJEPLConnection(con); // Este constructor no dar nunca error
    }

    public void returnJEPLConnectionToPool(JEPLConnectionImpl jcon) throws SQLException
    {
        jcon.closePreparedStatements();

        Connection con = jcon.getConnection();
        con.close(); // Devolver al pool
    }

    @Override
    public <T> JEPLResultSetDAOListenerDefault<T> createJEPLResultSetDAOListenerDefault(Class<T> clasz,JEPLRowBeanMapper<T> mapper)
    {
        try
        {
            return new JEPLResultSetDAOListenerDefaultImpl<T>(clasz,mapper);
        }
        catch(Exception ex)
        {
            throw new JEPLException(ex);
        }
    }

    @Override
    public <T> JEPLResultSetDAOListenerDefault<T> createJEPLResultSetDAOListenerDefault(Class<T> clasz)
    {
        return createJEPLResultSetDAOListenerDefault(clasz,null);
    }

    public <T> JEPLConnectionImpl pushJEPLTask(JEPLTaskExecContextInConnectionImpl<T> task) throws SQLException
    {
        this.inUse = true;
        listenerList.setInUse();
        getJEPLBootImpl().setInUse();

        JEPLConnectionImpl jcon = connectionByThread.get();
        if (jcon == null)
        {
            jcon = getJEPLConnectionFromPool(); // Si ocurre una excepcin ser aqu no devolviendo la conexin
            connectionByThread.set(jcon);
        }

        jcon.pushJEPLTaskExecContex(task);
        task.setJEPLConnection(jcon);
        return jcon;
    }

    public <T> void popJEPLTask(JEPLTaskExecContextInConnectionImpl<T> task) throws SQLException
    {
        task.setJEPLConnection(null);

        JEPLConnectionImpl jcon = connectionByThread.get(); // jcon NO puede ser nulo
        if (jcon.popJEPLTaskExecContex() != task) throw new JEPLException("INTERNAL ERROR");

        if (jcon.isEmptyOfJEPLTasks())
        {
            // La vida del JEPLConnectionImpl coincide con la vida del Connection fuera del pool
            connectionByThread.set(null);

            returnJEPLConnectionToPool(jcon); // Devolver al pool
        }
    }

    public JEPLDAL createJEPLDAL()
    {
        return new JEPLDALDefaultImpl(this);
    }

    public <T> JEPLDAO<T> createJEPLDAO(Class<T> type)
    {
        return new JEPLDAOImpl<T>(this);
    }

    public abstract <T> T execInternal(JEPLTaskOneExecWithConnectionImpl<T> task,JEPLDALImpl dal,JEPLListenerListImpl paramListener) throws Exception;

    public abstract JEPLConnectionImpl createJEPLConnection(Connection con) throws SQLException;
}




Java Source Code List

com.innowhere.jepldroidtest.JEPLDroidTestActivity.java
jepl.JEPLBootNonJTA.java
jepl.JEPLBootRoot.java
jepl.JEPLBoot.java
jepl.JEPLCachedResultSet.java
jepl.JEPLConnectionListener.java
jepl.JEPLConnection.java
jepl.JEPLDALQuery.java
jepl.JEPLDAL.java
jepl.JEPLDAOQuery.java
jepl.JEPLDAO.java
jepl.JEPLDataSource.java
jepl.JEPLException.java
jepl.JEPLListener.java
jepl.JEPLNonJTADataSource.java
jepl.JEPLParameter.java
jepl.JEPLPreparedStatementListener.java
jepl.JEPLPreparedStatement.java
jepl.JEPLResultSetDALListener.java
jepl.JEPLResultSetDAOListenerDefault.java
jepl.JEPLResultSetDAOListener.java
jepl.JEPLResultSetDAO.java
jepl.JEPLResultSet.java
jepl.JEPLRowBeanMapper.java
jepl.JEPLStatement.java
jepl.JEPLTask.java
jepl.JEPLTransactionPropagation.java
jepl.JEPLTransaction.java
jepl.JEPLTransactionalJTA.java
jepl.JEPLTransactionalNonJTA.java
jepl.JEPLUserData.java
jepl.impl.JEPLBootImpl.java
jepl.impl.JEPLBootRootImpl.java
jepl.impl.JEPLConnectionImpl.java
jepl.impl.JEPLCurrentTransactionImpl.java
jepl.impl.JEPLDALDefaultImpl.java
jepl.impl.JEPLDALImpl.java
jepl.impl.JEPLDAOImpl.java
jepl.impl.JEPLDataSourceImpl.java
jepl.impl.JEPLListenerListImpl.java
jepl.impl.JEPLPreparedStatementDefaultImpl.java
jepl.impl.JEPLPreparedStatementImpl.java
jepl.impl.JEPLStatementImpl.java
jepl.impl.JEPLTaskExecContextImpl.java
jepl.impl.JEPLTaskExecContextInConnectionImpl.java
jepl.impl.JEPLTaskOneExecWithConnectionImpl.java
jepl.impl.JEPLTaskOneExecWithConnectionWrapperImpl.java
jepl.impl.JEPLTaskOneExecutionImpl.java
jepl.impl.JEPLTaskOneExecutionWrapperImpl.java
jepl.impl.JEPLUserDataMonoThreadImpl.java
jepl.impl.JEPLUserDataMultiThreadImpl.java
jepl.impl.JEPLUtilImpl.java
jepl.impl.lex.Cursor.java
jepl.impl.lex.Identifier.java
jepl.impl.lex.JDBCParamStandardToken.java
jepl.impl.lex.JDBCParamToken.java
jepl.impl.lex.JDBCParamWithNameToken.java
jepl.impl.lex.JDBCParamWithNumberToken.java
jepl.impl.lex.OtherCharToken.java
jepl.impl.lex.SourceCode.java
jepl.impl.lex.Space.java
jepl.impl.lex.StringDoubleQuote.java
jepl.impl.lex.StringLiteral.java
jepl.impl.lex.StringSimpleQuote.java
jepl.impl.lex.TestSourceCode.java
jepl.impl.lex.TokenFilter.java
jepl.impl.lex.Token.java
jepl.impl.nonjta.JEPLBootNonJTAImpl.java
jepl.impl.nonjta.JEPLCurrentTransactionNonJTAImpl.java
jepl.impl.nonjta.JEPLNonJTAConnectionDefaultImpl.java
jepl.impl.nonjta.JEPLNonJTAConnectionImpl.java
jepl.impl.nonjta.JEPLNonJTADataSourceDefaultImpl.java
jepl.impl.nonjta.JEPLNonJTADataSourceImpl.java
jepl.impl.nonjta.JEPLTaskExecContextInConnectionNonJTAImpl.java
jepl.impl.nonjta.android.JEPLNonJTAConnectionSQLDroidImpl.java
jepl.impl.nonjta.android.JEPLNonJTADataSourceAndroidImpl.java
jepl.impl.nonjta.android.JEPLPreparedStatementSQLDroidImpl.java
jepl.impl.query.JEPLCachedResultSetImpl.java
jepl.impl.query.JEPLDALQueryImpl.java
jepl.impl.query.JEPLDAOQueryImpl.java
jepl.impl.query.JEPLParameterDecImpl.java
jepl.impl.query.JEPLParameterDecWithNameImpl.java
jepl.impl.query.JEPLParameterDecWithNumberImpl.java
jepl.impl.query.JEPLParameterImpl.java
jepl.impl.query.JEPLParameterValueImpl.java
jepl.impl.query.JEPLParameterValueNamedImpl.java
jepl.impl.query.JEPLParameterWithNameImpl.java
jepl.impl.query.JEPLParameterWithNumberImpl.java
jepl.impl.query.JEPLPropertyDescriptorImpl.java
jepl.impl.query.JEPLPropertyDescriptorJavaBeansImpl.java
jepl.impl.query.JEPLQueryParsedCacheImpl.java
jepl.impl.query.JEPLQueryParsedImpl.java
jepl.impl.query.JEPLResultSetBeanInfo.java
jepl.impl.query.JEPLResultSetDAOImpl.java
jepl.impl.query.JEPLResultSetDAOIteratorImpl.java
jepl.impl.query.JEPLResultSetDAOListenerDefaultImpl.java
jepl.impl.query.JEPLResultSetDefaultImpl.java
jepl.impl.query.JEPLResultSetImpl.java