Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * 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 org.dbflute.hook;
 
The context of callback in DBFlute deep logic.

Author(s):
jflute
 
 public class CallbackContext {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     
The logger instance for this class. (NotNull)
 
     private static final Logger _log = LoggerFactory.getLogger(CallbackContext.class);
 
     // ===================================================================================
     //                                                                        Thread Local
     //                                                                        ============
     // -----------------------------------------------------
     //                                         Thread Object
     //                                         -------------
     
The default thread-local for this.
 
     protected static final ThreadLocal<CallbackContext_defaultThreadLocal = new ThreadLocal<CallbackContext>();

    
The default holder for callback context, using thread local. (NotNull)
 
     protected static final CallbackContextHolder _defaultHolder = new CallbackContextHolder() {
 
         public CallbackContext provide() {
             return .get();
         }
 
         public void save(CallbackContext context) {
             .set(context);
         }
     };

    
The holder for callback context, might be changed. (NotNull: null setting is not allowed)
 
     protected static CallbackContextHolder _holder = // as default
 
    
Is this static world locked? e.g. you should unlock it to set your own provider.
 
     protected static boolean _locked = true// at first locked
 
    
The holder of for callback context.
Basically for asynchronous of web framework e.g. Play2.
 
     public static interface CallbackContextHolder {

        
Provide callback context.
You should return same instance in same request.

Returns:
The instance of callback context. (NullAllowed: when no context, but should exist in real handling)
 
         CallbackContext provide();

        
Hold callback context and save it in holder.

Parameters:
callbackContext The callback context set by static setter. (NullAllowed: if null, context is removed)
 
         void save(CallbackContext callbackContext);
     }
 
     // -----------------------------------------------------
     //                                        Basic Handling
     //                                        --------------
     
Get callback context on thread.

Returns:
The context of callback. (NullAllowed)
 
     public static CallbackContext getCallbackContextOnThread() {
         return getActiveHolder().provide();
     }

    
Set callback context on thread.
You can use setting methods per interface instead of this method.

Parameters:
callbackContext The context of callback. (NotNull)
 
     public static void setCallbackContextOnThread(CallbackContext callbackContext) {
        if (callbackContext == null) {
            String msg = "The argument 'callbackContext' must not be null.";
            throw new IllegalArgumentException(msg);
        }
        getActiveHolder().save(callbackContext);
    }

    
Is existing callback context on thread?
You can use determination methods per interface instead of this method.

Returns:
The determination, true or false.
    public static boolean isExistCallbackContextOnThread() {
        return getActiveHolder().provide() != null;
    }

    
Clear callback context on thread.
Basically you should call other clear methods per interfaces, because this clear method clears all interfaces.
    public static void clearCallbackContextOnThread() {
        getActiveHolder().save(null);
    }

    
Get the active holder for callback context.

Returns:
The holder instance to handle callback context. (NotNull)
    protected static CallbackContextHolder getActiveHolder() {
        return ;
    }
    // -----------------------------------------------------
    //                                            Management
    //                                            ----------
    
Use the surrogate holder for callback context. (automatically locked after setting)
You should call this in application initialization if it needs.

Parameters:
holder The holder instance. (NullAllowed: if null, use default holder)
    public static void useSurrogateHolder(CallbackContextHolder holder) {
        assertNotLocked();
        if (.isInfoEnabled()) {
            .info("...Setting surrogate holder for callback context: " + holder);
        }
        if (holder != null) {
             = holder;
        } else {
             = ;
        }
         = true;
    }

    
Is this static world locked?

Returns:
The determination, true or false.
    public static boolean isLocked() {
        return ;
    }

    
Lock this static world, e.g. not to set the holder of thread-local.
    public static void lock() {
        if (.isInfoEnabled()) {
            .info("...Locking the static world of the callback context!");
        }
         = true;
    }

    
Unlock this static world, e.g. to set the holder of thread-local.
    public static void unlock() {
        if (.isInfoEnabled()) {
            .info("...Unlocking the static world of the callback context!");
        }
         = false;
    }

    
Assert this is not locked.
    protected static void assertNotLocked() {
        if (!isLocked()) {
            return;
        }
        String msg = "The callback context is locked! Don't access at this timing!";
        throw new IllegalStateException(msg);
    }
    // -----------------------------------------------------
    //                                   BehaviorCommandHook
    //                                   -------------------
    
Set the hook interface of behavior commands.
This hook interface is called back before executing behavior commands and finally.
The hook methods may be called by nested process so you should pay attention to it when you implements this.
 context.setBehaviorCommandHook(new BehaviorCommandHook() {
     public void hookBefore(BehaviorCommandMeta meta) {
         // You can implement your favorite callback here.
     }
     public void hookFinally(BehaviorCommandMeta meta, RuntimeException cause) {
         // You can implement your favorite callback here.
     }
 });
 

Parameters:
behaviorCommandHook The hook interface of behavior commands. (NullAllowed)
    public static void setBehaviorCommandHookOnThread(BehaviorCommandHook behaviorCommandHook) {
        final CallbackContext context = getOrCreateContext();
        context.setBehaviorCommandHook(behaviorCommandHook);
    }

    
Is existing the hook interface of behavior commands on thread?

Returns:
The determination, true or false.
    public static boolean isExistBehaviorCommandHookOnThread() {
    }

    
Clear the hook interface of behavior commands from callback context on thread.
If the callback context does not have other interfaces, the context is removed from thread.
    public static void clearBehaviorCommandHookOnThread() {
        if (isExistCallbackContextOnThread()) {
            final CallbackContext context = getCallbackContextOnThread();
            context.setBehaviorCommandHook(null);
            clearIfNoInterface(context);
        }
    }
    // -----------------------------------------------------
    //                                           SqlFireHook
    //                                           -----------
    
Set the hook interface of SQL fires.
This hook interface is called back before firing SQL and finally.
The hook methods may be called by nested process so you should pay attention to it when you implements this.
 context.setSqlFireHook(new SqlFireHook() {
     public void hookBefore(BehaviorCommandMeta meta, SqlFireReadyInfo fireReadyInfo) {
         // You can implement your favorite callback here.
     }
     public void hookFinally(BehaviorCommandMeta meta, SqlFireResultInfo fireResultInfo) {
         // You can implement your favorite callback here.
     }
 });
 

Parameters:
sqlFireHook The hook interface of behavior commands. (NullAllowed)
    public static void setSqlFireHookOnThread(SqlFireHook sqlFireHook) {
        final CallbackContext context = getOrCreateContext();
        context.setSqlFireHook(sqlFireHook);
    }

    
Is existing the hook interface of behavior commands on thread?

Returns:
The determination, true or false.
    public static boolean isExistSqlFireHookOnThread() {
    }

    
Clear the hook interface of behavior commands from callback context on thread.
If the callback context does not have other interfaces, the context is removed from thread.
    public static void clearSqlFireHookOnThread() {
        if (isExistCallbackContextOnThread()) {
            final CallbackContext context = getCallbackContextOnThread();
            context.setSqlFireHook(null);
            clearIfNoInterface(context);
        }
    }
    // -----------------------------------------------------
    //                                         SqlLogHandler
    //                                         -------------
    
Set the handler of SQL log.
This handler is called back before executing the SQL.
 context.setSqlLogHandler(new SqlLogHandler() {
     public void handle(SqlLogInfo info) {
         // You can get your SQL string here.
     }
 });
 

Parameters:
sqlLogHandler The handler of SQL log. (NullAllowed)
    public static void setSqlLogHandlerOnThread(SqlLogHandler sqlLogHandler) {
        final CallbackContext context = getOrCreateContext();
        context.setSqlLogHandler(sqlLogHandler);
    }

    
Is existing the handler of SQL log on thread?

Returns:
The determination, true or false.
    public static boolean isExistSqlLogHandlerOnThread() {
    }

    
Clear the handler of SQL log from callback context on thread.
If the callback context does not have other interfaces, the context is removed from thread.
    public static void clearSqlLogHandlerOnThread() {
        if (isExistCallbackContextOnThread()) {
            final CallbackContext context = getCallbackContextOnThread();
            context.setSqlLogHandler(null);
            clearIfNoInterface(context);
        }
    }
    // -----------------------------------------------------
    //                                      SqlResultHandler
    //                                      ----------------
    
Set the handler of SQL result.
This handler is called back before executing the SQL.
 context.setSqlResultHandler(new SqlResultHandler() {
     public void handle(SqlResultInfo info) {
         // You can get your SQL result information here.
     }
 });
 

Parameters:
sqlResultHandler The handler of SQL result. (NullAllowed)
    public static void setSqlResultHandlerOnThread(SqlResultHandler sqlResultHandler) {
        final CallbackContext context = getOrCreateContext();
        context.setSqlResultHandler(sqlResultHandler);
    }

    
Is existing the handler of SQL result on thread?

Returns:
The determination, true or false.
    public static boolean isExistSqlResultHandlerOnThread() {
    }

    
Clear the handler of SQL result from callback context on thread.
If the callback context does not have other interfaces, the context is removed from thread.
    public static void clearSqlResultHandlerOnThread() {
        if (isExistCallbackContextOnThread()) {
            final CallbackContext context = getCallbackContextOnThread();
            context.setSqlResultHandler(null);
            clearIfNoInterface(context);
        }
    }
    // -----------------------------------------------------
    //                                       SqlStringFilter
    //                                       ---------------
    
Set the filter of SQL string.
This handler is called back before executing the SQL.
 context.setSqlStringFilter(new SqlStringFilter() {
     public String filter(String executedSql) {
         // You can filter your executed SQL string here.
     }
 });
 

Parameters:
sqlStringFilter The filter of SQL string. (NullAllowed)
    public static void setSqlStringFilterOnThread(SqlStringFilter sqlStringFilter) {
        final CallbackContext context = getOrCreateContext();
        context.setSqlStringFilter(sqlStringFilter);
    }

    
Is existing the handler of SQL result on thread?

Returns:
The determination, true or false.
    public static boolean isExistSqlStringFilterOnThread() {
    }

    
Clear the filter of SQL string from callback context on thread.
If the callback context does not have other interfaces, the context is removed from thread.
    public static void clearSqlStringFilterOnThread() {
        if (isExistCallbackContextOnThread()) {
            final CallbackContext context = getCallbackContextOnThread();
            context.setSqlStringFilter(null);
            clearIfNoInterface(context);
        }
    }
    // -----------------------------------------------------
    //                                         Assist Helper
    //                                         -------------
    protected static CallbackContext getOrCreateContext() {
        if (isExistCallbackContextOnThread()) {
            return getCallbackContextOnThread();
        } else {
            final CallbackContext context = new CallbackContext();
            setCallbackContextOnThread(context);
            return context;
        }
    }
    protected static void clearIfNoInterface(final CallbackContext context) {
        if (!context.hasAnyInterface()) {
            clearCallbackContextOnThread();
        }
    }
    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    protected SqlFireHook _sqlFireHook;
    protected SqlLogHandler _sqlLogHandler;
    // ===================================================================================
    //                                                                       Determination
    //                                                                       =============
    public boolean hasAnyInterface() {
        return  != null ||  != null // hook
                ||  != null ||  != null // handler
                ||  != null// filter
    }
    // ===================================================================================
    //                                                                      Basic Override
    //                                                                      ==============
    @Override
    public String toString() {
        final String title = DfTypeUtil.toClassTitle(this);
        final StringBuilder sb = new StringBuilder();
        sb.append(title);
        sb.append(":{behaviorCommandHook=").append();
        sb.append(", sqlFireHook=").append();
        sb.append(", sqlLogHandler=").append();
        sb.append(", sqlResultHandler=").append();
        sb.append(", sqlStringFilter=").append();
        sb.append("}");
        return sb.toString();
    }
    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    // -----------------------------------------------------
    //                                   BehaviorCommandHook
    //                                   -------------------
        return ;
    }

    
Set the hook interface of behavior commands. (overriding existing hook as default)
This hook interface is called back before executing behavior commands and finally.
 context.setBehaviorCommandHook(new BehaviorCommandHook() {
     public void hookBefore(BehaviorCommandMeta meta) {
         // You can implement your favorite callback here.
     }
     public void hookFinally(BehaviorCommandMeta meta, RuntimeException cause) {
         // You can implement your favorite callback here.
     }
 });
 

The hook methods may be called by nested process so you should pay attention to it when you implements this.

Also you can inherit the existing hook with your hook by overriding inheritsExistingHook().

 context.setBehaviorCommandHook(new BehaviorCommandHook() {
     public void hookBefore(BehaviorCommandMeta meta) {
         ...
     }
     public void hookFinally(BehaviorCommandMeta meta, RuntimeException cause) {
         ...
     }
     public boolean inheritsExistingHook() {
         return true; // also existing hooks will be executed, 
     }
 });
 

Parameters:
behaviorCommandHook The hook interface of behavior commands. (NullAllowed)
    public void setBehaviorCommandHook(BehaviorCommandHook behaviorCommandHook) {
        if (behaviorCommandHook != null && behaviorCommandHook.inheritsExistingHook()) {
             = createInheritableBehaviorCommandHook(behaviorCommandHook);
        } else {
             = behaviorCommandHook;
        }
    }
        return new InheritableBehaviorCommandHook(behaviorCommandHook);
    }
    protected static class InheritableBehaviorCommandHook implements BehaviorCommandHook {
        protected final BehaviorCommandHook _originally// might be null
        protected final BehaviorCommandHook _yourHook;
        public InheritableBehaviorCommandHook(BehaviorCommandHook originallyBehaviorCommandHook yourHook) {
             = originally;
             = yourHook;
        }
        public void hookBefore(BehaviorCommandMeta meta) {
            if ( != null) {
                .hookBefore(meta);
            }
            .hookBefore(meta);
        }
        public void hookFinally(BehaviorCommandMeta metaRuntimeException cause) {
            .hookFinally(metacause);
            if ( != null) {
                .hookFinally(metacause);
            }
        }
    }
    // -----------------------------------------------------
    //                                           SqlFireHook
    //                                           -----------
    public SqlFireHook getSqlFireHook() {
        return ;
    }

    
Set the hook interface of SQL fires. (overriding existing hook as default)
This hook interface is called back before firing SQL and finally.
 context.setSqlFireHook(new SqlFireHook() {
     public void hookBefore(BehaviorCommandMeta meta, SqlFireReadyInfo fireReadyInfo) {
         // You can implement your favorite callback here.
     }
     public void hookFinally(BehaviorCommandMeta meta, SqlFireResultInfo fireResultInfo) {
         // You can implement your favorite callback here.
     }
 });
 

The hook methods may be called by nested process so you should pay attention to it when you implements this.

Also you can inherit the existing hook with your hook by overriding inheritsExistingHook().

 context.setSqlFireHook(new SqlFireHook() {
     public void hookBefore(BehaviorCommandMeta meta, SqlFireReadyInfo fireReadyInfo) {
         ...
     }
     public void hookFinally(BehaviorCommandMeta meta, SqlFireResultInfo fireResultInfo) {
         ...
     }
     public boolean inheritsExistingHook() {
         return true; // also existing hooks will be executed, 
     }
 });
 

Parameters:
sqlFireHook The hook interface of SQL fires. (NullAllowed)
    public void setSqlFireHook(SqlFireHook sqlFireHook) {
        if (sqlFireHook != null && sqlFireHook.inheritsExistingHook()) {
             = createInheritableSqlFireHook(sqlFireHook);
        } else {
             = sqlFireHook;
        }
    }
        return new InheritableSqlFireHook(sqlFireHook);
    }
    protected static class InheritableSqlFireHook implements SqlFireHook {
        protected final SqlFireHook _originally// might be null
        protected final SqlFireHook _yourHook;
        public InheritableSqlFireHook(SqlFireHook originallySqlFireHook yourHook) {
             = originally;
             = yourHook;
        }
        public void hookBefore(BehaviorCommandMeta metaSqlFireReadyInfo fireReadyInfo) {
            if ( != null) {
                .hookBefore(metafireReadyInfo);
            }
            .hookBefore(metafireReadyInfo);
        }
        public void hookFinally(BehaviorCommandMeta metaSqlFireResultInfo fireResultInfo) {
            .hookFinally(metafireResultInfo);
            if ( != null) {
                .hookFinally(metafireResultInfo);
            }
        }
    }
    // -----------------------------------------------------
    //                                         SqlLogHandler
    //                                         -------------
    public SqlLogHandler getSqlLogHandler() {
        return ;
    }

    
Set the handler of SQL log.
This handler is called back before executing the SQL.
 context.setSqlLogHandler(new SqlLogHandler() {
     public void handle(String executedSql, String displaySql
                      , Object[] args, Class<?>[] argTypes) {
         // You can get your SQL string here.
     }
 });
 

Parameters:
sqlLogHandler The handler of SQL log. (NullAllowed)
    public void setSqlLogHandler(SqlLogHandler sqlLogHandler) {
         = sqlLogHandler;
    }
    // -----------------------------------------------------
    //                                      SqlResultHandler
    //                                      ----------------
        return ;
    }

    
Set the handler of SQL result.
This handler is called back before executing the SQL.
 context.setSqlResultHandler(new SqlResultHandler() {
     public void handle(SqlResultInfo info) {
         // You can get your SQL result information here.
     }
 });
 

Parameters:
sqlResultHandler The handler of SQL result. (NullAllowed)
    public void setSqlResultHandler(SqlResultHandler sqlResultHandler) {
         = sqlResultHandler;
    }
    // -----------------------------------------------------
    //                                       SqlStringFilter
    //                                       ---------------
        return ;
    }

    
Set the filter of SQL string.
This filter is called back before executing the SQL.
 context.setSqlStringFilter(new SqlStringFilter() {
     public String filterSelectCB(BehaviorCommandMeta meta, String executedSql) {
         // You can filter your SQL string here.
     }
 });
 

Parameters:
sqlStringFilter The filter of SQL string. (NullAllowed)
    public void setSqlStringFilter(SqlStringFilter sqlStringFilter) {
         = sqlStringFilter;
    }
New to GrepCode? Check out our FAQ X