View Javadoc

1   package org.apache.velocity.exception;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.velocity.runtime.log.Log;
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.
23   */
24  
25  
26  
27  /**
28   *  Application-level exception thrown when a reference method is
29   *  invoked and an exception is thrown.
30   *  <br>
31   *  When this exception is thrown, a best effort will be made to have
32   *  useful information in the exception's message.  For complete
33   *  information, consult the runtime log.
34   *
35   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
36   * @version $Id: MethodInvocationException.java 703544 2008-10-10 18:15:53Z nbubna $
37   */
38  public class MethodInvocationException extends VelocityException implements ExtendedParseException
39  {
40      /**
41       * Version Id for serializable
42       */
43      private static final long serialVersionUID = 7305685093478106342L;
44  
45      private String referenceName = "";
46  
47      private final String methodName;
48      
49      private final int lineNumber;
50      private final int columnNumber;
51      private final String templateName;
52  
53      /**
54       *  CTOR - wraps the passed in exception for
55       *  examination later
56       *
57       *  @param message
58       *  @param e Throwable that we are wrapping
59       *  @param methodName name of method that threw the exception
60       *  @param templateName The name of the template where the exception occured.
61       */
62      public MethodInvocationException(final String message, final Throwable e, final String methodName, final String templateName, final int lineNumber, final int columnNumber)
63      {
64          super(message, e);
65  
66          this.methodName = methodName;
67          this.templateName = templateName;
68          this.lineNumber = lineNumber;
69          this.columnNumber = columnNumber;
70      }
71  
72      /**
73       *  Returns the name of the method that threw the
74       *  exception.
75       *
76       *  @return String name of method
77       */
78      public String getMethodName()
79      {
80          return methodName;
81      }
82  
83      /**
84       *  Sets the reference name that threw this exception.
85       *
86       *  @param ref name of reference
87       */
88      public void setReferenceName(String ref)
89      {
90          referenceName = ref;
91      }
92  
93      /**
94       *  Retrieves the name of the reference that caused the
95       *  exception.
96       *
97       *  @return name of reference.
98       */
99      public String getReferenceName()
100     {
101         return referenceName;
102     }
103 
104     /**
105      * @see ExtendedParseException#getColumnNumber()
106      * @since 1.5
107      */
108     public int getColumnNumber()
109     {
110 	    return columnNumber;
111     }
112 
113     /**
114      * @see ExtendedParseException#getLineNumber()
115      * @since 1.5
116      */
117     public int getLineNumber()
118     {
119 	    return lineNumber;
120     }
121 
122     /**
123      * @see ExtendedParseException#getTemplateName()
124      * @since 1.5
125      */
126     public String getTemplateName()
127     {
128 	    return templateName;
129     }
130 
131     /**
132      * @see Exception#getMessage()
133      * @since 1.5
134      */
135     public String getMessage()
136     {
137         StringBuffer message = new StringBuffer();
138         message.append(super.getMessage());
139         message.append(" at ");
140         message.append(Log.formatFileString(templateName, lineNumber, columnNumber));
141         return message.toString();
142     }
143 }