View Javadoc

1   /*
2   Copyright (C) 2008 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.beanfilter;
18  
19  import java.util.MissingResourceException;
20  import java.util.ResourceBundle;
21  
22  import com.gridsystems.innergrid.kernel.KernelException;
23  
24  /**
25   * KernelException implementation for errors in BeanFilter evaluation.
26   *
27   * @author Rodrigo Ruiz
28   */
29  public class EvalException extends KernelException {
30  
31    /**
32     * <code>serialVersionUID</code> attribute.
33     */
34    private static final long serialVersionUID = -3283117668362489095L;
35  
36    /**
37     * Bundle used for error patterns.
38     */
39    private static final ResourceBundle BUNDLE;
40  
41    static {
42      ResourceBundle bundle;
43      try {
44        bundle = ResourceBundle.getBundle("eval-errors");
45      } catch (MissingResourceException e) {
46        bundle = null;
47      }
48      BUNDLE = bundle;
49    }
50  
51    /**
52     * Creates a new instance.
53     *
54     * @param code    Exception code
55     * @param params  Message parameters
56     */
57    public EvalException(String code, Object... params) {
58      this(null, code, params);
59    }
60  
61    /**
62     * Creates a new instance.
63     *
64     * @param cause   Original exception
65     * @param code    Exception code
66     * @param params  Message Parameters
67     */
68    public EvalException(Throwable cause, String code, Object... params) {
69      super(cause, code, getPattern(code), params);
70    }
71  
72    /**
73     * Gets the exception pattern for the specified code.
74     *
75     * @param code Error code
76     * @return This exception pattern for the specified code
77     */
78    private static String getPattern(String code) {
79      try {
80        if (code == null) {
81          return "null#notfound";
82        } else if (BUNDLE == null) {
83          return code + "#notfound";
84        } else {
85          return BUNDLE.getString(code);
86        }
87      } catch (MissingResourceException e) {
88        return code + "#notfound";
89      }
90    }
91  }