KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > exception > SMIMEException


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.exception;
10
11 import java.io.*;
12 import java.util.Date JavaDoc;
13 import java.text.SimpleDateFormat JavaDoc;
14
15 import javax.crypto.BadPaddingException;
16 import javax.crypto.IllegalBlockSizeException;
17 import javax.crypto.NoSuchPaddingException;
18 import java.security.NoSuchProviderException JavaDoc;
19 import java.security.NoSuchAlgorithmException JavaDoc;
20 import java.security.InvalidKeyException JavaDoc;
21 import java.security.cert.CertificateException JavaDoc;
22 import java.security.UnrecoverableKeyException JavaDoc;
23 import java.security.KeyStoreException JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.security.SignatureException JavaDoc;
27 import java.security.cert.CertificateEncodingException JavaDoc;
28
29 /**
30  * SMIMEException is exception which is thrown in the classes specific for SMIME
31  * and in the processes associated with SMIME. It contains messages describing
32  * appeared problems and contains name of the class where the exception was
33  * thrown.<BR>
34  *<BR>
35  * SMIMEException, as one of his arguments, can convey non SMIME exception (if it
36  * is reason of rising SMIMEException). Later this argument (Exception) can be
37  * restored by getNonSMIMEException method and examined (for example by using
38  * instanceof operator).<BR>
39  *<BR>
40  * This exception also has posibilities for displaying errors or warnings on
41  * screen, and for logging errors and warnings into SMIMEerr.log file (at
42  * defined directory).
43  */

44 public class SMIMEException extends Exception JavaDoc {
45
46 /**
47  * Full class name of object where exception happened.
48  */

49   private String JavaDoc className;
50
51 /**
52  * File for logging errors (SMIMEerr.log) .
53  */

54   public static final String JavaDoc errorFileName = "SMIMEerr.log";
55
56 /**
57  * Path for SMIMEerror.log file.
58  */

59   private static String JavaDoc errorFilePath = "";
60
61 /**
62  * Storage for external exception (non SMIME exception) which is the reason
63  * of rising SMIME exception.
64  */

65   private Exception JavaDoc foreignExc = null;
66
67   /**
68    * Storage for external exception (non SMIME exception) name.
69    */

70   private String JavaDoc exceptionName = "org.enhydra.oyster.exception.SMIMEException";
71
72 /**
73  * Construction with the given object and the error text message
74  * @param obj0 class type - origin of exception (represented by key word
75  * "this"). In static classes, objO is String which represents the class name.
76  * @param text0 message of the exception
77  */

78   public SMIMEException (Object JavaDoc obj0, String JavaDoc text0) {
79     super(text0);
80     if (obj0 instanceof String JavaDoc)
81       className = (String JavaDoc)obj0;
82     else
83       className = obj0.getClass().getName();
84   }
85
86 /**
87  * Construction with the given object and the appropriate error number.
88  * @param obj0 class type - origin of exception (represented by key word
89  * "this"). In static classes, objO is String which represents the class name.
90  * @param errNumb0 error number corresponds to appropriate error message stored
91  * in ErrorStorage class.
92  */

93   public SMIMEException (Object JavaDoc obj0, int errNumb0) {
94     this(obj0, ErrorStorage.getErrorMesage(errNumb0));
95   }
96
97 /**
98  * Static metod which constructs and returns SMIMEException object with the
99  * given object, exception, and name of method inside which this method is called.
100  * This construction is designed for handling non SMIMEExceptions. In
101  * the process of construction, given exception object is explored and stored in
102  * inner private variable. This constructor replaces many lines of code which
103  * can look like following:<BR>
104  * <BR>
105  * <DL>
106  * if (e instanceof javax.mail.MessagingException)<BR>
107  * <DD> excType = "javax.mail.MessagingException";<BR>
108  * else if (e instanceof java.io.IOException)<BR>
109  * <DD> excType = "java.io.IOException";<BR>
110  * else if(e instanceof java.io.UnsupportedEncodingException)
111  * <DD> excType = "java.io.UnsupportedEncodingException";<BR>
112  * <BR>
113  * String err = ErrorStorage.getErrorMesage(1999) +<BR>
114  * <DD> System.getProperty("line.separator") +<BR>
115  * <DD> e.getMessage() +<BR>
116  * <DD> System.getProperty("line.separator") +<BR>
117  * <DD> excType + " captured in MessageConvertor method of" +<BR>
118  * <DD> " MimeAssist class and thrown as SMIMEException.";<BR>
119  * <BR>
120  * SMIMEException es = new <BR>
121  * <DD> SMIMEException("org.enhydra.oyster.util.MimeAssist", err);<BR>
122  * es.addNonSMIMEException(e, excType);<BR>
123  * </DL>
124  * <BR>
125  * If unknown non SMIMEException is stored inside of SMIMEException object
126  * getNonSMIMEExceptionName() method will return string: "java.lang.Exception".
127  * SMIMEException could not be stored inside other SMIMEException object, and
128  * returned value of this method will be the same SMIMEException passed to this
129  * method as argument.
130  * @param obj0 class type - origin of exception (represented by key word
131  * "this"). In static classes, objO is String which represents the class name.
132  * @param e0 non SMIME Exception which will by carried inside of
133  * the generated SMIMEException object.
134  * @param method0 name of method where this method is used. If it is left as
135  * null, it will get value "unknown"
136  */

137   public static SMIMEException getInstance(Object JavaDoc obj0, Exception JavaDoc e0, String JavaDoc method0) {
138
139     String JavaDoc excType = "java.lang.Exception";
140     String JavaDoc name = null;
141     if (obj0 instanceof String JavaDoc)
142       name = (String JavaDoc)obj0;
143     else
144       name = obj0.getClass().getName();
145
146     if(e0 instanceof java.io.UnsupportedEncodingException JavaDoc)
147       excType = "java.io.UnsupportedEncodingException";
148     else if (e0 instanceof java.security.cert.CertificateEncodingException JavaDoc)
149       excType = "java.security.cert.CertificateEncodingException";
150     else if (e0 instanceof java.lang.IndexOutOfBoundsException JavaDoc)
151       excType = "java.lang.IndexOutOfBoundsException";
152     else if (e0 instanceof java.lang.NullPointerException JavaDoc)
153       excType = "java.lang.NullPointerException";
154     else if (e0 instanceof javax.crypto.NoSuchPaddingException)
155       excType = "javax.crypto.NoSuchPaddingException";
156     else if (e0 instanceof java.security.SignatureException JavaDoc)
157       excType = "java.security.SignatureException";
158     else if (e0 instanceof java.security.NoSuchProviderException JavaDoc)
159       excType = "java.security.NoSuchProviderException";
160     else if (e0 instanceof java.security.NoSuchAlgorithmException JavaDoc)
161       excType = "java.security.NoSuchAlgorithmException";
162     else if (e0 instanceof java.security.InvalidKeyException JavaDoc)
163       excType = "java.security.InvalidKeyException";
164     else if (e0 instanceof javax.crypto.BadPaddingException)
165       excType = "javax.crypto.BadPaddingException";
166     else if (e0 instanceof javax.crypto.IllegalBlockSizeException)
167       excType = "javax.crypto.IllegalBlockSizeException";
168     else if (e0 instanceof java.security.KeyStoreException JavaDoc)
169       excType = "java.security.KeyStoreException";
170     else if (e0 instanceof java.security.UnrecoverableKeyException JavaDoc)
171       excType = "java.security.UnrecoverableKeyException";
172     else if (e0 instanceof java.net.UnknownHostException JavaDoc)
173       excType = "java.net.UnknownHostException";
174     else if (e0 instanceof java.io.FileNotFoundException JavaDoc)
175       excType = "java.io.FileNotFoundException";
176     else if (e0 instanceof java.lang.NumberFormatException JavaDoc)
177       excType = "java.lang.NumberFormatException";
178     else if (e0 instanceof java.security.cert.CertificateException JavaDoc)
179       excType = "java.security.cert.CertificateException";
180     else if (e0 instanceof org.enhydra.oyster.exception.SMIMEIOException)
181       return ((SMIMEIOException)e0).getSMIMEException();
182     else if (e0 instanceof javax.mail.MessagingException JavaDoc)
183       excType = "javax.mail.MessagingException";
184     else if (e0 instanceof java.io.IOException JavaDoc)
185       excType = "java.io.IOException";
186     else if (e0 instanceof org.enhydra.oyster.exception.SMIMEException)
187       return (SMIMEException)e0;
188
189     if (method0 == null)
190       method0 = "unknown";
191
192     String JavaDoc err = ErrorStorage.getErrorMesage(1999) +
193                  System.getProperty("line.separator") +
194                  e0.getMessage() +
195                  System.getProperty("line.separator") +
196                  excType + " captured in " + method0 + " method of " +
197                  name + " class and thrown as SMIMEException.";
198     SMIMEException es = new SMIMEException(name, err);
199     es.addNonSMIMEException(e0, excType);
200     return es;
201   }
202
203 /**
204  * Returns the class name of the object where exception arrised
205  * @return Name of the class
206  */

207   public String JavaDoc getClassOfException () {
208     return className;
209   }
210
211 /**
212  * Returns message of the exception with the specified location of the
213  * exception
214  * @return Information message about arrised Exception
215  */

216   public String JavaDoc getInformation () {
217     return super.getMessage() + System.getProperty("line.separator") +
218     "SMIMEException rised in instance of the class: " + className;
219   }
220
221 /**
222  * Sets path for SMIMEerr.log file.
223  * @param path0 path for logging error file. If entered path is
224  * invalid, SMIMEerr.log file will be placed at default location.
225  */

226   public static void setErrorFilePath(String JavaDoc path0) {
227     if(path0!=null & !path0.equalsIgnoreCase("")){
228       errorFilePath = new String JavaDoc(path0);
229       if (errorFilePath.charAt(errorFilePath.length()-1) == File.separatorChar)
230         errorFilePath.substring(0, errorFilePath.length()-1);
231     }
232   }
233
234 /**
235  * Gets SMIMEerror.log file path.
236  * @return Error logging file path.
237  */

238   public static String JavaDoc getErrorFilePath(String JavaDoc path0) {
239     return errorFilePath;
240   }
241
242 /**
243  * Prints error messages to screen.
244  * @param errorText0 error text which will be displayed at screen. If it is
245  * null, error message returned by getInformation() method will be displayed.
246  */

247   public void displayErrors(String JavaDoc errorText0) {
248
249     if ( errorText0 == null || errorText0.equalsIgnoreCase("") )
250       System.out.println(this.getInformation());
251     else
252       System.out.println(errorText0);
253   }
254
255 /**
256  * Prints error messages to SMIMEerror.log file. If destination of log directory
257  * or SMIMEerr.log file do not exist, they will be created.
258  * @param errorText0 error text which will be logged to file. If it is null,
259  * error message returned by getInformation() method will be printed to file.
260  */

261   public void loggingErrors(String JavaDoc errorText0) {
262
263     File logDir = new File(errorFilePath);
264     if ( !(logDir.isFile() | logDir.isDirectory()) )
265       logDir.mkdirs();
266
267     File logFile = new File(logDir, errorFileName);
268
269     PrintWriter outData = null;
270     try {
271       outData = new PrintWriter( new BufferedWriter( new FileWriter(
272                             logFile.getAbsolutePath(),true ) ) );
273
274       Date JavaDoc dat = new Date JavaDoc(System.currentTimeMillis());
275       SimpleDateFormat JavaDoc datForm = new SimpleDateFormat JavaDoc("dd.MM.yyyy. HH:mm:ss");
276       outData.println( "-----> " + datForm.format(dat) +
277       " <-----------------------------------------------------------------" );
278       outData.flush();
279
280       if ( errorText0 == null || errorText0.equalsIgnoreCase("") )
281         outData.println(this.getInformation());
282       else
283         outData.println(errorText0);
284       outData.flush();
285     }
286     catch(FileNotFoundException JavaDoc e){
287       System.err.println( e.getMessage() );
288       outData.close();
289     }
290     catch(IOException e){
291       System.err.println( e.getMessage() );
292       outData.close();
293     }
294     outData.close();
295   }
296
297
298 /**
299  * Stores non SMIME exception (reason for rising SMIMEException) inside
300  * of SMIMEException object.
301  * @param exception0 non SMIME exception which must extend Exception class.
302  * @param exceptionName0 name of non SMIMEException. Default value is:
303  * org.enhydra.oyster.exception.SMIMEException which means that reason for
304  * that exception is not non SMIME exception.
305  */

306   public void addNonSMIMEException(Exception JavaDoc exception0 , String JavaDoc exceptionName0) {
307     exceptionName = new String JavaDoc(exceptionName0);
308     foreignExc = exception0;
309   }
310
311 /**
312  * Returns non SMIME exception stored inside of SMIMEException object,
313  * which was the reason for rising SMIMEException. This method has sense only
314  * if non SMIME exception object was previosly added to SMIMEException object by
315  * addNonSMIMEException() method. Otherwise, it returns null.
316  * @return Exception object which can be examined by user (for example by using
317  * instanceof operator).
318  */

319   public Exception JavaDoc getNonSMIMEException() {
320     return foreignExc;
321   }
322
323 /**
324  * Gets external exception name (non SMIME exception which is reason for rising
325  * SMIMEException).
326  * @return Name of external exception (non SMIME exception).
327  */

328   public String JavaDoc getNonSMIMEExceptionName() {
329     return exceptionName;
330   }
331
332 }
333
334
335
336
Popular Tags