Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 International Business Machines Corp.
   * 
   * See the NOTICE file distributed with this work for additional information
   * regarding copyright ownership. 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.ibm.jbatch.container.impl;
 
 import java.util.List;
 import java.util.Set;
 
 
 
 public class SkipHandler {

Logic for handling skipped records.
 
 
 	  private static final String className = SkipHandler.class.getName();
 		private static Logger logger = Logger.getLogger(SkipHandler.class.getPackage().getName());
 
 	  public static final String SKIP_COUNT      = "skip-limit";
 	  public static final String SKIP_INCLUDE_EX = "include class";
 	  public static final String SKIP_EXCLUDE_EX = "exclude class";
 
 	  private List<SkipReadListenerProxy_skipReadListener = null;
 
 	  private long _jobId = 0;
 	  private String _stepId = null;
 	  private Set<String_skipIncludeExceptions = null;
 	  private Set<String_skipExcludeExceptions = null;
 	  private int _skipLimit = .;
 	  private long _skipCount = 0;
 
 	  public SkipHandler(Chunk chunklong lString stepId)
 	  {
 	     = l;
 	     = stepId;
 
 	    initialize(chunk);
 	  }

  
Add the user-defined SkipReadListeners.
 
 	  public void addSkipReadListener(List<SkipReadListenerProxyskipReadListener)
 	  {
 	     = skipReadListener;
 	  }
  
  
Add the user-defined SkipWriteListeners.
 
 	  public void addSkipWriteListener(List<SkipWriteListenerProxyskipWriteListener)
 	  {
 	     = skipWriteListener;
 	  }
  
  
Add the user-defined SkipReadListeners.
 
 	  public void addSkipProcessListener(List<SkipProcessListenerProxyskipProcessListener)
 	  {
 	     = skipProcessListener;
 	  }


  
Read the skip exception lists from the BDS props.
 
	  private void initialize(Chunk chunk)
	  {
	    final String mName = "initialize";
	      .entering(mName);
	    try
	    {
	    	if (chunk.getSkipLimit() != null){
	    		 = Integer.parseInt(chunk.getSkipLimit());
	    		if ( < 0) {
	    		    throw new IllegalArgumentException("The skip-limit attribute on a chunk cannot be a negative value");
	    		}
	    	}
	    }
	    catch (NumberFormatException nfe)
	    {
	      throw new RuntimeException("NumberFormatException reading " + nfe);
	    }
        // Read the include/exclude exceptions.
         = new HashSet<String>();
         = new HashSet<String>();
        // boolean done = false;
        List<StringincludeEx = new ArrayList<String>();
        List<StringexcludeEx = new ArrayList<String>();
        if (chunk.getSkippableExceptionClasses() != null) {
            if (chunk.getSkippableExceptionClasses().getIncludeList() != null) {
                List<ExceptionClassFilter.Includeincludes = chunk.getSkippableExceptionClasses().getIncludeList();
                for (ExceptionClassFilter.Include include : includes) {
                    .add(include.getClazz().trim());
                    .finer("SKIPHANDLE: include: " + include.getClazz().trim());
                }
                if (.size() == 0) {
                    .finer("SKIPHANDLE: include element not present");
                }
            }
        }
        if (chunk.getSkippableExceptionClasses() != null) {
            if (chunk.getSkippableExceptionClasses().getExcludeList() != null) {
                List<ExceptionClassFilter.Excludeexcludes = chunk.getSkippableExceptionClasses().getExcludeList();
                for (ExceptionClassFilter.Exclude exclude : excludes) {
                    .add(exclude.getClazz().trim());
                    .finer("SKIPHANDLE: exclude: " + exclude.getClazz().trim());
                }
                if (.size() == 0) {
                    .finer("SKIPHANDLE: exclude element not present");
                }
            }
        }
        if (.isLoggable(.))
            .logp(.mName"added include exception " + includeEx + "; added exclude exception " + excludeEx);
	        
	      .exiting(mNamethis.toString());
	  }


  
Handle exception from a read failure.
	  public void handleExceptionRead(Exception e)
	  {
	    final String mName = "handleException";
	    
	    .finer("SKIPHANDLE: in skiphandler handle exception on a read");
	      .logp(.mNamee.getClass().getName() + "; " + this.toString());
	    
	    if (!isSkipLimitReached() && isSkippable(e))
	    {
	      // Skip it.  Log it.  Call the SkipListener.
	      ++;
	      logSkip(e);
	      if ( != null) {
	    	  for (SkipReadListenerProxy skipReadListenerProxy : ) {
	    		  skipReadListenerProxy.onSkipReadItem(e);
				}
	      }
	    }
	    else
	    {
	      // No skip.  Throw it back. don't throw it back - we might want to retry ...
	      if(.isLoggable(.)) 
	        .logp(.mName"No skip.  Rethrow"e);
	      	throw new BatchContainerRuntimeException(e);
	    }
	      .exiting(mNamee);
	  }

  
Handle exception from a process failure.
	  {
	    final String mName = "handleExceptionWithRecordProcess";
	      .logp(.mNamee.getClass().getName() + "; " + this.toString());
	    if (!isSkipLimitReached() && isSkippable(e))
	    {
	      // Skip it.  Log it.  Call the SkipProcessListener.
	      ++;
	      logSkip(e);
	      if ( != null) {
	    	  for (SkipProcessListenerProxy skipProcessListenerProxy : ) {
	    		  skipProcessListenerProxy.onSkipProcessItem(we);
				}
	      }
	    }
	    else
	    {
	      // No skip.  Throw it back.
	      if(.isLoggable(.)) 
	        .logp(.mName"No skip.  Rethrow "e);
	      throw new BatchContainerRuntimeException(e);
	    }
	  }
  
Handle exception from a write failure.
	  public void handleExceptionWithRecordListWrite(Exception eList<?> items)
	  {
	    final String mName = "handleExceptionWithRecordListWrite(Exception, List<?>)";
	    
	      .logp(.mNamee.getClass().getName() + "; " + this.toString());
	    if (!isSkipLimitReached() && isSkippable(e))
	    {
	      // Skip it.  Log it.  Call the SkipListener.
	      ++;
	      logSkip(e);
	      if ( != null) {
	    	  for (SkipWriteListenerProxy skipWriteListenerProxy : ) {
	    		  skipWriteListenerProxy.onSkipWriteItem(itemse);
				}
	      }
	    }
	    else
	    {
	      ..println("## NO SKIP");
	      // No skip.  Throw it back. - No, exit without throwing
	      if(.isLoggable(.)) 
	        .logp(.mName"No skip.  Rethrow "e);
	      throw new BatchContainerRuntimeException(e);
	    }
	  }


  
Check the skipCount and skippable exception lists to determine whether the given Exception is skippable.
	  private boolean isSkippable(Exception e)
	  {
	    final String mName = "isSkippable";
	    String exClassName = e.getClass().getName();
	    if(.isLoggable(.)) 
	      .logp(.mNamemName + ": " + retVal + ": " + exClassName);
	    return retVal;
	  }

  
Check whether given exception is in skippable exception list
	  private boolean containsSkippable(Set<StringskipListException e)
	  {
	    final String mName = "containsSkippable";
	    boolean retVal = false;
	    for ( Iterator it = skipList.iterator(); it.hasNext(); ) {
	        String exClassName = (Stringit.next();   
	        try {
	            ClassLoader tccl = Thread.currentThread().getContextClassLoader();	            
	        	if (retVal = tccl.loadClass(exClassName).isInstance(e))
	        		break;
	        } catch (ClassNotFoundException cnf) {
	        	.logp(.mNamecnf.getLocalizedMessage());
	        }
	    }
	    if(.isLoggable(.)) 
	      .logp(.mNamemName + ": " + retVal );
	    return retVal;
	  }
  

    
Check if the skip limit has been reached. Note: if skip handling isn't enabled (i.e. not configured in xJCL), then this method will always return TRUE.
    private boolean isSkipLimitReached() {
        // Unlimited skips if it is never defined
        if ( == .) {
            return false;
        }
        return ( >= );
    }
	  
	  private void logSkip(Exception e)
	  {
	    Object[] details = { e.getClass().getName() + ": " + e.getMessage() };
	    if(.isLoggable(.)) 
	      .logp(."logSkip""Logging details: "details); 
	  }
	  public long getSkipCount()
	  {
	    return ;
	  }
	  public void setSkipCount(long skipCount)
	  {
	    final String mName = "setSkipCount";
	     = skipCount;
	    if(.isLoggable(.)) 
	      .logp(.mName"setSkipCount: " + );
	  }
	  public String toString()
	  {
	    return "SkipHandler{" + super.toString() + "}count:limit=" +  + ":" + ;
	  }
New to GrepCode? Check out our FAQ X