KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > nfe > TestNFE


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31
32 package org.objectweb.proactive.examples.nfe;
33
34 import org.objectweb.proactive.ProActive;
35 import org.objectweb.proactive.core.config.ProActiveConfiguration;
36 import org.objectweb.proactive.core.exceptions.NonFunctionalException;
37 import org.objectweb.proactive.core.exceptions.communication.ProActiveCommunicationException;
38 import org.objectweb.proactive.core.exceptions.communication.SendRequestCommunicationException;
39 import org.objectweb.proactive.core.exceptions.handler.HandlerNonFunctionalException;
40 import org.objectweb.proactive.core.exceptions.handler.IHandler;
41
42
43 /**
44  * Class to test non functional exception
45  */

46 public class TestNFE {
47     
48     /**
49     * Raise a non functional exception
50     */

51     public static void raiseException(Class JavaDoc classOfException) throws NonFunctionalException {
52
53         System.out.println("*** RAISE " + classOfException.getName());
54
55         // We create a non functional exception of the desired class
56
NonFunctionalException ex = null;
57         try {
58             ex = (NonFunctionalException) classOfException.getConstructor(new Class JavaDoc[] {String JavaDoc.class, Throwable JavaDoc.class}).newInstance(new Object JavaDoc[] {classOfException.getName(), null});
59         } catch (Exception JavaDoc e) {
60             if (e instanceof ClassNotFoundException JavaDoc)
61                 System.out.println("*** ERROR : cannot find class " + ex);
62             else
63                 e.printStackTrace();
64         }
65
66         // We throw the exception
67
throw ex;
68     }
69
70
71     // Main program
72
public static void main(String JavaDoc[] args) {
73         ProActiveConfiguration.load();
74     
75         ProActiveConfiguration.load();
76         // We need one arg : the name of a class of non functional exception
77
if (args.length != 1) {
78             System.out.println("Usage : java org.objectweb.proactive.examples.nfeCreation NFE");
79             System.exit(0);
80         }
81     
82         // A non functional Exception
83
NonFunctionalException ex = null;
84     
85         // Then we try to create this exception
86
try {
87             ex = (NonFunctionalException) Class.forName(args[0]).getConstructor(new Class JavaDoc[] {String JavaDoc.class, Throwable JavaDoc.class}).newInstance(new Object JavaDoc[] {args[0], null});
88         } catch (ClassNotFoundException JavaDoc e) {
89             System.out.println("Class " + args[0] + " is not a valid NFE");
90             System.exit(0);
91         } catch (InstantiationException JavaDoc e) {
92             System.out.println("Problems occurs when instantiating " + args[0]);
93             e.printStackTrace();
94             System.exit(0);
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97             System.exit(0);
98         }
99     
100         // We then show information about the exception
101
System.out.println("PARAMETER : " + ex.getDescription() + ex.getMessage());
102         
103         // We try some basic stuff (set and remove handlers)
104
System.out.println();
105         ProActive.setExceptionHandler(IHandler.ID_defaultLevel, null, HandlerNonFunctionalException.class, NonFunctionalException.class);
106         ProActive.unsetExceptionHandler(IHandler.ID_defaultLevel, null, HandlerNonFunctionalException.class);
107         ProActive.setExceptionHandler(IHandler.ID_VMLevel, null, HandlerNonFunctionalException.class, NonFunctionalException.class);
108         ProActive.unsetExceptionHandler(IHandler.ID_VMLevel, null, NonFunctionalException.class);
109         
110         // Raise an exception
111
System.out.println();
112         try {
113             raiseException(ProActiveCommunicationException.class);
114         } catch (NonFunctionalException nfe) {
115             IHandler handler = ProActive.searchExceptionHandler(nfe);
116             if (handler != null) handler.handle(nfe);
117         }
118
119         // Raise an exception
120
System.out.println();
121         try {
122             raiseException(SendRequestCommunicationException.class);
123         } catch (NonFunctionalException nfe) {
124             IHandler handler = ProActive.searchExceptionHandler(nfe);
125             if (handler != null) handler.handle(nfe);
126         }
127         
128         // Exit program
129
System.exit(0);
130     }
131 }
Popular Tags