Search in sources :

Example 1 with CallbackContext

use of org.dbflute.hook.CallbackContext in project lastaflute by lastaflute.

the class SimpleAsyncManager method createRunnable.

// ===================================================================================
// Create Runnable
// ===============
protected Runnable createRunnable(ConcurrentAsyncCall call, String keyword) {
    // in caller thread
    final Map<String, Object> threadCacheMap = inheritThreadCacheContext(call);
    final AccessContext accessContext = inheritAccessContext(call);
    final CallbackContext callbackContext = inheritCallbackContext(call);
    final Map<String, Object> variousContextMap = findCallerVariousContextMap();
    return () -> {
        // in new thread
        prepareThreadCacheContext(call, threadCacheMap);
        preparePreparedAccessContext(call, accessContext);
        prepareCallbackContext(call, callbackContext);
        final Object variousPreparedObj = prepareVariousContext(call, variousContextMap);
        final long before = showRunning(keyword);
        Throwable cause = null;
        try {
            call.callback();
        } catch (Throwable e) {
            handleAsyncCallbackException(call, before, e);
            cause = e;
        } finally {
            // should be before clearing because of using them
            showFinishing(keyword, before, cause);
            clearVariousContext(call, variousContextMap, variousPreparedObj);
            clearCallbackContext(call);
            clearPreparedAccessContext(call);
            clearThreadCacheContext(call);
        }
    };
}
Also used : CallbackContext(org.dbflute.hook.CallbackContext) AccessContext(org.dbflute.hook.AccessContext) PreparedAccessContext(org.lastaflute.db.dbflute.accesscontext.PreparedAccessContext)

Example 2 with CallbackContext

use of org.dbflute.hook.CallbackContext in project lastaflute by lastaflute.

the class SimpleAsyncManager method extractSqlCounter.

// ===================================================================================
// SQL Counter
// ===========
protected OptionalThing<ExecutedSqlCounter> extractSqlCounter() {
    final CallbackContext context = CallbackContext.getCallbackContextOnThread();
    if (context == null) {
        return OptionalThing.empty();
    }
    final SqlStringFilter filter = context.getSqlStringFilter();
    if (filter == null || !(filter instanceof ExecutedSqlCounter)) {
        return OptionalThing.empty();
    }
    return OptionalThing.of(((ExecutedSqlCounter) filter));
}
Also used : RomanticTraceableSqlStringFilter(org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlStringFilter) SqlStringFilter(org.dbflute.hook.SqlStringFilter) CallbackContext(org.dbflute.hook.CallbackContext) ExecutedSqlCounter(org.dbflute.bhv.proposal.callback.ExecutedSqlCounter)

Example 3 with CallbackContext

use of org.dbflute.hook.CallbackContext in project lastaflute by lastaflute.

the class SimpleAsyncManager method doInheritCallbackContext.

protected CallbackContext doInheritCallbackContext(ConcurrentAsyncCall call) {
    // null allowed
    final CallbackContext src = CallbackContext.getCallbackContextOnThread();
    if (src == null) {
        return null;
    }
    final CallbackContext dest = newCallbackContext();
    final ConcurrentAsyncOption option = call.option();
    final ConcurrentAsyncOption defaultOption = defaultConcurrentAsyncOption;
    if (isInherit(option.getBehaviorCommandHookType(), defaultOption.getBehaviorCommandHookType())) {
        final BehaviorCommandHook hook = src.getBehaviorCommandHook();
        if (hook != null) {
            dest.setBehaviorCommandHook(hook);
        }
    }
    if (isInherit(option.getSqlFireHookType(), defaultOption.getSqlFireHookType())) {
        final SqlFireHook hook = src.getSqlFireHook();
        if (hook != null) {
            dest.setSqlFireHook(hook);
        }
    } else {
        // as default
        dest.setSqlFireHook(createDefaultSqlFireHook(call));
    }
    if (isInherit(option.getSqlLogHandlerType(), defaultOption.getSqlLogHandlerType())) {
        final SqlLogHandler handler = src.getSqlLogHandler();
        if (handler != null) {
            dest.setSqlLogHandler(handler);
        }
    }
    if (isInherit(option.getSqlResultHandlerType(), defaultOption.getSqlResultHandlerType())) {
        final SqlResultHandler handler = src.getSqlResultHandler();
        if (handler != null) {
            dest.setSqlResultHandler(handler);
        }
    } else {
        dest.setSqlResultHandler(createDefaultSqlResultHandler(call));
    }
    if (isInherit(option.getSqlStringFilterType(), defaultOption.getSqlStringFilterType())) {
        final SqlStringFilter filter = src.getSqlStringFilter();
        if (filter != null) {
            dest.setSqlStringFilter(filter);
        }
    } else {
        // as default
        dest.setSqlStringFilter(createDefaultSqlStringFilter(call));
    }
    return dest;
}
Also used : SqlLogHandler(org.dbflute.hook.SqlLogHandler) SqlResultHandler(org.dbflute.hook.SqlResultHandler) RomanticTraceableSqlResultHandler(org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlResultHandler) RomanticTraceableSqlStringFilter(org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlStringFilter) SqlStringFilter(org.dbflute.hook.SqlStringFilter) BehaviorCommandHook(org.dbflute.bhv.core.BehaviorCommandHook) CallbackContext(org.dbflute.hook.CallbackContext) RomanticTraceableSqlFireHook(org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlFireHook) SqlFireHook(org.dbflute.hook.SqlFireHook)

Example 4 with CallbackContext

use of org.dbflute.hook.CallbackContext in project dbflute-core by dbflute.

the class TnAbstractBasicSqlHandlerTest method test_logSql_whitebox_sqlResultHandlerOnly.

public void test_logSql_whitebox_sqlResultHandlerOnly() {
    // ## Arrange ##
    final List<String> markList = new ArrayList<String>();
    TnAbstractBasicSqlHandler handler = new TnAbstractBasicSqlHandler(null, null, "select ...") {

        @Override
        protected String buildDisplaySql(String sql, Object[] args) {
            markList.add("buildDisplaySql");
            return "select ...";
        }

        @Override
        protected void logDisplaySql(String displaySql) {
            throw new IllegalStateException("log should not be called!");
        }

        @Override
        protected void log(String msg) {
            throw new IllegalStateException("log should not be called!");
        }

        @Override
        protected void saveResultSqlLogInfo(SqlLogInfo sqlLogInfo) {
            markList.add("saveResultSqlLogInfo");
            super.saveResultSqlLogInfo(sqlLogInfo);
        }

        @Override
        protected boolean isLogEnabled() {
            return false;
        }

        @Override
        protected void assertObjectNotNull(String variableName, Object value) {
        // for no check of constructor
        }
    };
    // ## Act ##
    try {
        CallbackContext callbackContext = new CallbackContext();
        callbackContext.setSqlResultHandler(new SqlResultHandler() {

            public void handle(SqlResultInfo sqlResultInfo) {
                throw new IllegalStateException("handle should not be called!");
            }
        });
        CallbackContext.setCallbackContextOnThread(callbackContext);
        handler.logSql(null, null);
        assertEquals("select ...", InternalMapContext.getResultSqlLogInfo().getDisplaySql());
    } finally {
        CallbackContext.clearCallbackContextOnThread();
        InternalMapContext.clearInternalMapContextOnThread();
    }
    // ## Assert ##
    assertEquals(2, markList.size());
    assertEquals("saveResultSqlLogInfo", markList.get(0));
    assertEquals("buildDisplaySql", markList.get(1));
}
Also used : SqlLogInfo(org.dbflute.hook.SqlLogInfo) SqlResultHandler(org.dbflute.hook.SqlResultHandler) ArrayList(java.util.ArrayList) CallbackContext(org.dbflute.hook.CallbackContext) SqlResultInfo(org.dbflute.hook.SqlResultInfo)

Example 5 with CallbackContext

use of org.dbflute.hook.CallbackContext in project dbflute-core by dbflute.

the class TnAbstractBasicSqlHandlerTest method test_logSql_whitebox_sqlLogHandlerOnly.

public void test_logSql_whitebox_sqlLogHandlerOnly() {
    // ## Arrange ##
    final List<String> markList = new ArrayList<String>();
    final Object[] args = new Object[] {};
    final Class<?>[] argsTypes = new Class<?>[] {};
    TnAbstractBasicSqlHandler handler = new TnAbstractBasicSqlHandler(null, null, "select ...") {

        @Override
        protected String buildDisplaySql(String sql, Object[] args) {
            markList.add("buildDisplaySql");
            return "select ...";
        }

        @Override
        protected void logDisplaySql(String displaySql) {
            throw new IllegalStateException("log should not be called!");
        }

        @Override
        protected void log(String msg) {
            throw new IllegalStateException("log should not be called!");
        }

        @Override
        protected void saveResultSqlLogInfo(SqlLogInfo sqlLogInfo) {
            throw new IllegalStateException("log should not be called!");
        }

        @Override
        protected boolean isLogEnabled() {
            return false;
        }

        @Override
        protected void assertObjectNotNull(String variableName, Object value) {
        // for no check of constructor
        }
    };
    // ## Act ##
    try {
        CallbackContext callbackContext = new CallbackContext();
        callbackContext.setSqlLogHandler(new SqlLogHandler() {

            public void handle(SqlLogInfo info) {
                markList.add("handle");
                assertEquals("select ...", info.getDisplaySql());
                assertEquals(newArrayList(args), newArrayList(info.getBindArgs()));
                assertEquals(newArrayList(argsTypes), newArrayList(info.getBindArgTypes()));
            }
        });
        CallbackContext.setCallbackContextOnThread(callbackContext);
        handler.logSql(args, argsTypes);
        assertNull(InternalMapContext.getResultSqlLogInfo());
    } finally {
        CallbackContext.clearCallbackContextOnThread();
        InternalMapContext.clearInternalMapContextOnThread();
    }
    // ## Assert ##
    assertEquals(2, markList.size());
    assertEquals("handle", markList.get(0));
    assertEquals("buildDisplaySql", markList.get(1));
}
Also used : SqlLogInfo(org.dbflute.hook.SqlLogInfo) SqlLogHandler(org.dbflute.hook.SqlLogHandler) ArrayList(java.util.ArrayList) CallbackContext(org.dbflute.hook.CallbackContext)

Aggregations

CallbackContext (org.dbflute.hook.CallbackContext)7 ArrayList (java.util.ArrayList)4 SqlResultHandler (org.dbflute.hook.SqlResultHandler)4 SqlLogHandler (org.dbflute.hook.SqlLogHandler)3 SqlLogInfo (org.dbflute.hook.SqlLogInfo)3 SqlResultInfo (org.dbflute.hook.SqlResultInfo)3 SqlStringFilter (org.dbflute.hook.SqlStringFilter)2 RomanticTraceableSqlStringFilter (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlStringFilter)2 BehaviorCommandHook (org.dbflute.bhv.core.BehaviorCommandHook)1 ExecutedSqlCounter (org.dbflute.bhv.proposal.callback.ExecutedSqlCounter)1 AccessContext (org.dbflute.hook.AccessContext)1 SqlFireHook (org.dbflute.hook.SqlFireHook)1 MockBehaviorCommand (org.dbflute.mock.MockBehaviorCommand)1 PreparedAccessContext (org.lastaflute.db.dbflute.accesscontext.PreparedAccessContext)1 RomanticTraceableSqlFireHook (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlFireHook)1 RomanticTraceableSqlResultHandler (org.lastaflute.db.dbflute.callbackcontext.traceablesql.RomanticTraceableSqlResultHandler)1