View Javadoc

1   package org.apache.maven.scm.provider.integrity;
2   
3   /**
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21   
22  import com.mks.api.response.APIException;
23  import com.mks.api.response.Response;
24  import com.mks.api.response.WorkItemIterator;
25  import com.mks.api.response.InterruptedException;
26  
27  /**
28   * Helper class to appropriately diagnose an APIException
29   * @version $Id: ExceptionHandler.java 1.2 2011/08/22 13:06:45EDT Cletus D'Souza (dsouza) Exp  $
30   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
31   */
32  public class ExceptionHandler 
33  {
34  	// Private variables to provide diagnostics on the exception
35  	private String message;
36  	private String command;
37  	private int exitCode;
38  	
39  	/**
40  	 * Constructor requires a single APIException to debug
41  	 * @param ex APIException 
42  	 */
43  	public ExceptionHandler(APIException ex) 
44  	{
45  		
46  		// API Exceptions can be nested.  Hence we will need to recurse the 
47  		// exception hierarchy to dig for a conclusive message
48          Response response = ex.getResponse();
49  
50          // Print the stack trace to standard out for debugging purposes
51          ex.printStackTrace();
52          
53  		// The API failed to execute the command (i.e. a real API error)
54  		if( null == response) 
55  		{			
56  			message = ex.getMessage();
57  			command = new java.lang.String();
58  			//exitCode = Integer.parseInt(ex.getExceptionId());
59  			exitCode = -1;
60  		}
61  		else
62  		{
63  			command = response.getCommandString();
64  			try
65  			{
66  				exitCode = response.getExitCode();
67  			}
68  			catch(InterruptedException ie)
69  			{
70  				// Just print out the stack trace
71  				ie.printStackTrace();
72  				exitCode = -1;
73  			}
74  			WorkItemIterator wit = response.getWorkItems();
75  			// In the event there is a problem with one of the command's elements
76  			// we have to dig deeper into the exception...
77  			try
78  			{
79  				while(wit.hasNext()){ wit.next(); }
80  				// If we got here then just extract the message
81  				if(ex.getMessage() != null) 
82  				{
83  					message = ex.getMessage();
84  				}				
85  			}
86  			catch(APIException ae)
87  			{
88  				// This message will be the real reason for the exception
89  				String curMessage = ae.getMessage();
90  				if(curMessage != null)
91  				{
92  					message = curMessage;
93  				}
94                  ae.printStackTrace();
95  			}
96  		}		
97  	}
98  	
99  	/**
100 	 * Returns the Message obtained from the APIException
101 	 * @return message APIException String
102 	 */
103 	public String getMessage()
104 	{
105 		return message;
106 	}
107 	
108 	/**
109 	 * Returns the executed command that caused the exception
110 	 * @return command Complete CLI Command String
111 	 */
112 	public String getCommand()
113 	{
114 		return command;
115 	}
116 	
117 	/**
118 	 * Returns the exit codes associated with executing the command
119 	 * @return exitCode API/CLI Exit Code
120 	 */
121 	public int getExitCode()
122 	{
123 		return exitCode;
124 	}
125 }