KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > error > Errors


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package cintoo.messages.error;
18
19 import api.cintoo.messages.context.Context;
20
21 import java.lang.reflect.Field JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * Static facade class for error handling to
27  * make the usage of the error and error handling
28  * classes simplier
29  *
30  * @author Stephan J. Schmidt
31  * @version $id$
32  * @since 1.0
33  */

34 public class Errors {
35   private static ErrorHandler handler;
36
37   /**
38    * Singleton method to create a singleton for
39    * the error handler.
40    *
41    * @return error handler
42    */

43   private static synchronized ErrorHandler handler() {
44     if (null == handler) {
45       handler = new ErrorHandler();
46     }
47     return handler;
48   }
49
50   public static String JavaDoc error(Context context, ErrorCode errorCode) {
51     return handler().error(context, errorCode);
52   }
53
54   public static String JavaDoc error(String JavaDoc context, ErrorCode errorCode) {
55     return handler().error(context, errorCode);
56   }
57
58   public static String JavaDoc error(Object JavaDoc context, ErrorCode errorCode) {
59     return handler().error(context, errorCode);
60   }
61
62   public static String JavaDoc error(ErrorCode errorCode) {
63     return handler().error(errorCode);
64   }
65
66   public static void setPattern(String JavaDoc pattern) {
67     handler().setPattern(pattern);
68   }
69
70   public static void clearPattern() {
71     handler().clearPattern();
72   }
73
74    /**
75    * Set global bundle name
76    *
77    * @param bundleName bundle name for the bundle to be used
78    */

79   public static void setBundle(String JavaDoc bundleName) {
80     handler().setBundle(bundleName);
81   }
82
83   /**
84    * Set bundle for the package scope
85    *
86    * @param bundleName name of the bundle to set
87    * @param packageName package name which defines the scope
88    */

89   public static void setBundle(String JavaDoc bundleName, String JavaDoc packageName) {
90     handler().setBundle(bundleName, packageName);
91   }
92
93   /**
94    * Set bundle for the context scope
95    *
96    * @param bundleName name of the bundle to set
97    * @param context context wich defines the scope
98    */

99   public static void setBundle(String JavaDoc bundleName, Context context) {
100     handler().setBundle(bundleName, context);
101   }
102
103   public static boolean validateErrorCodes(Class JavaDoc[] classesToCheck) {
104     boolean result = true;
105     Set JavaDoc<Integer JavaDoc> codes = new HashSet JavaDoc<Integer JavaDoc>();
106     for (Class JavaDoc toCheck : classesToCheck) {
107       if (! validateErrorCodes(toCheck, codes)) {
108         result = false;
109         break;
110       }
111     }
112     return result;
113   }
114
115   private static boolean validateErrorCodes(Class JavaDoc toCheck, Set JavaDoc<Integer JavaDoc> codes) {
116     boolean result = true;
117     for (Field JavaDoc field : toCheck.getDeclaredFields()) {
118       if (field.getType().equals(ErrorCode.class)) {
119         try {
120           ErrorCode error = (ErrorCode) field.get(toCheck);
121           int code = error.getCode();
122           if (codes.contains(code)) {
123             result = false;
124             break;
125           }
126           codes.add(code);
127         } catch (IllegalAccessException JavaDoc e) {
128           e.printStackTrace();
129         }
130       }
131     }
132     return result;
133   }
134
135   public static boolean validateErrorCodes(Class JavaDoc classToCheck) {
136     return validateErrorCodes(classToCheck, new HashSet JavaDoc<Integer JavaDoc>());
137   }
138 }
Popular Tags