Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com 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 com.helger.webbasics.ajax;
 
 import java.util.Map;
 
 
 
The default implementation of IAjaxInvoker.

Author(s):
Philip Helger
 
 public class AjaxInvoker implements IAjaxInvoker
 {
  
Default milliseconds until an implementation is considered long running.
 
 
   private static final Logger s_aLogger = LoggerFactory.getLogger (AjaxInvoker.class);
   private static final IStatisticsHandlerCounter s_aStatsGlobalInvoke = StatisticsManager.getCounterHandler (AjaxInvoker.class.getName () +
                                                                                                              "$invocations");
   private static final IStatisticsHandlerKeyedCounter s_aStatsFunctionInvoke = StatisticsManager.getKeyedCounterHandler (AjaxInvoker.class.getName () +
                                                                                                                          "$func");
   private static final IStatisticsHandlerKeyedTimer s_aStatsFunctionTimer = StatisticsManager.getKeyedTimerHandler (AjaxInvoker.class.getName () +
                                                                                                                     "$timer");
 
   private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock ();
   @GuardedBy ("m_aRWLock")
   @GuardedBy ("m_aRWLock")
   private final Map <StringIFactory <? extends IAjaxExecutor>> m_aMap = new HashMap <StringIFactory <? extends IAjaxExecutor>> ();
 
   public AjaxInvoker ()
   {
     // Register default handler
   }
 
   public static boolean isValidFunctionName (@Nullable final String sFunctionName)
   {
     // All characters allowed should be valid in URLs without masking
     return StringHelper.hasText (sFunctionName) &&
            RegExHelper.stringMatchesPattern ("^[a-zA-Z0-9\\-_]+$"sFunctionName);
   }
 
   @Nonnull
   @ReturnsMutableObject (reason = "design")
   {
     return ;
   }
 
  @ReturnsMutableObject (reason = "design")
  {
  }
  @ReturnsMutableObject (reason = "design")
  {
  }
  {
    .readLock ().lock ();
    try
    {
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public void setLongRunningExecutionLimitTime (final long nLongRunningExecutionLimitTime)
  {
    .writeLock ().lock ();
    try
    {
       = nLongRunningExecutionLimitTime;
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }
  @ReturnsMutableObject (reason = "design")
  {
  }
  public Map <StringIFactory <? extends IAjaxExecutor>> getAllRegisteredExecutors ()
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newMap ();
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public IFactory <? extends IAjaxExecutorgetRegisteredExecutor (@Nullable final String sFunctionName)
  {
    .readLock ().lock ();
    try
    {
      return .get (sFunctionName);
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public IAjaxExecutor createExecutor (@Nullable final String sFunctionName)
  {
    final IFactory <? extends IAjaxExecutoraFactory = getRegisteredExecutor (sFunctionName);
    return aFactory == null ? null : aFactory.create ();
  }
  public boolean isRegisteredFunction (@Nullable final String sFunctionName)
  {
    .readLock ().lock ();
    try
    {
      return .containsKey (sFunctionName);
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public void registerFunction (@Nonnull final IAjaxFunctionDeclaration aFunction)
  {
    ValueEnforcer.notNull (aFunction"Function");
    final String sFunctionName = aFunction.getName ();
    final IFactory <? extends IAjaxExecutoraFactory = aFunction.getExecutorFactory ();
    .writeLock ().lock ();
    try
    {
      if (.containsKey (sFunctionName))
        throw new IllegalArgumentException ("An Ajax function with the name '" +
                                            sFunctionName +
                                            "' is already registered");
      .put (sFunctionNameaFactory);
      if (.isDebugEnabled ())
        .debug ("Registered AJAX function '" + sFunctionName + "' with handler factory " + aFactory);
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }
  public IAjaxResponse invokeFunction (@Nonnull final String sFunctionName,
                                       @Nonnull final IAjaxExecutor aAjaxExecutor,
                                       @Nonnull final IRequestWebScopeWithoutResponse aRequestScopethrows Throwable
  {
    ValueEnforcer.notNull (sFunctionName"FunctionName");
    ValueEnforcer.notNull (aAjaxExecutor"AjaxExecutor");
    ValueEnforcer.notNull (aRequestScope"RequestScope");
    if (.isDebugEnabled ())
      .debug ("Invoking Ajax function '" + sFunctionName + "'");
    try
    {
      final StopWatch aSW = new StopWatch (true);
      // Global increment before invocation
      // Invoke before handler
      for (final IAjaxBeforeExecutionCallback aBeforeCallback : getBeforeExecutionCallbacks ().getAllCallbacks ())
        try
        {
          aBeforeCallback.onBeforeExecution (thissFunctionNameaRequestScopeaAjaxExecutor);
        }
        catch (final Throwable t)
        {
          .error ("Error invoking Ajax function before execution callback handler " + aBeforeCallbackt);
        }
      // Register all external resources, prior to handling the main request, as
      // the JS/CSS elements will be contained in the AjaxDefaultResponse in
      // case of success
      aAjaxExecutor.registerExternalResources ();
      // Main handle request
      final IAjaxResponse aAjaxResponse = aAjaxExecutor.handleRequest (aRequestScope);
      if (aAjaxResponse.isFailure ())
      {
        // Execution failed
        .warn ("Invoked Ajax function '" +
                        sFunctionName +
                        "' returned a failure: " +
                        aAjaxResponse.toString ());
      }
      // Invoke after handler
      for (final IAjaxAfterExecutionCallback aAfterCallback : getAfterExecutionCallbacks ().getAllCallbacks ())
        try
        {
          aAfterCallback.onAfterExecution (thissFunctionNameaRequestScopeaAjaxExecutoraAjaxResponse);
        }
        catch (final Throwable t)
        {
          .error ("Error invoking Ajax after execution callback handler " + aAfterCallbackt);
        }
      // Increment statistics after successful call
      .increment (sFunctionName);
      // Long running AJAX request?
      final long nExecutionMillis = aSW.stopAndGetMillis ();
      .addTime (sFunctionNamenExecutionMillis);
      final long nLimitMS = getLongRunningExecutionLimitTime ();
      if (nLimitMS > 0 && nExecutionMillis > nLimitMS)
      {
        // Long running execution
        for (final IAjaxLongRunningExecutionCallback aLongRunningExecutionCallback : getLongRunningExecutionCallbacks ().getAllCallbacks ())
          try
          {
            aLongRunningExecutionCallback.onLongRunningExecution (this,
                                                                  sFunctionName,
                                                                  aRequestScope,
                                                                  aAjaxExecutor,
                                                                  nExecutionMillis);
          }
          catch (final Throwable t)
          {
            .error ("Error invoking Ajax long running execution callback handler " +
                             aLongRunningExecutionCallbackt);
          }
      }
      return aAjaxResponse;
    }
    catch (final Throwable t)
    {
      for (final IAjaxExceptionCallback aExceptionCallback : getExceptionCallbacks ().getAllCallbacks ())
        try
        {
          aExceptionCallback.onAjaxExecutionException (thissFunctionNameaAjaxExecutoraRequestScopet);
        }
        catch (final Throwable t2)
        {
          .error ("Error invoking Ajax exception callback handler " + aExceptionCallbackt2);
        }
      // Re-throw
      throw t;
    }
  }
  public String toString ()
  {
    return new ToStringGenerator (this).append ("map")
                                       .append ("exceptionCallbacks")
                                       .append ("beforeExecutionCallbacks")
                                       .append ("afterExecutionCallbacks")
                                       .append ("longRunningExecutionLimitTime")
                                       .append ("longRunningExecutionCallbacks")
                                       .toString ();
  }
New to GrepCode? Check out our FAQ X