KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > internal > MBeanServerConnectionExceptionThrower


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /* CVS information
25  * $Header: /cvs/glassfish/jmx-remote/rjmx-impl/src/java/com/sun/enterprise/admin/jmx/remote/internal/MBeanServerConnectionExceptionThrower.java,v 1.3 2005/12/25 04:26:33 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 04:26:33 $
28  */

29
30 /** A Class
31  * @author Kedar Mhaswade
32  */

33
34 package com.sun.enterprise.admin.jmx.remote.internal;
35
36 import javax.management.*;
37 import java.io.IOException JavaDoc;
38 import java.io.NotSerializableException JavaDoc;
39
40 /** A purely convenience class. MBeanServerConnection interface methods throw a lot of
41  * exceptions and an implementation of this interface generally ends up writing
42  * large try-catch blocks.
43  * <P>
44  * This class has methods whose names are chosen from the
45  * class MBeanServerRequestMessage {@link javax.management.remote.message.MBeanServerRequestMessage}
46  * and all they do is return specific exceptions cast from the passed exception.
47  * If the passed exception is none of the exceptions that a method throws, (which
48  * should not be the case most of the times) then a ClassCastException results.
49  * <P>
50  * The principal advantage of this class is that it does the repetitive work at
51  * one place and moves the exception conversion task away from actual MBeanServerConnection
52  * implementation. This way some reuse of the methods throwing the same exceptions
53  * is achieved.
54  *
55  * @author Kedar Mhaswade.
56  * @since S1AS8.0
57  * @version 1.0
58  */

59
60 public class MBeanServerConnectionExceptionThrower {
61     
62     /** Creates a new instance of MBeanServerConnectionExceptionThrower */
63     private MBeanServerConnectionExceptionThrower() {
64     }
65     
66     public static void addNotificationListenerObjectName(Exception JavaDoc e) throws
67     InstanceNotFoundException, IOException JavaDoc {
68         if (e instanceof InstanceNotFoundException)
69             throw (InstanceNotFoundException)e;
70         else if (e instanceof RuntimeException JavaDoc)
71             throw (RuntimeException JavaDoc)e;
72         else if (e instanceof NotSerializableException JavaDoc)
73             throw wrappedSerializationException(e);
74         else
75             throw wrappingIoException(e);
76     }
77
78     public static void addNotificationListeners(Exception JavaDoc e) throws
79     InstanceNotFoundException, IOException JavaDoc {
80         addNotificationListenerObjectName(e);
81     }
82     
83     public static void createMBean(Exception JavaDoc e) throws ReflectionException,
84     InstanceAlreadyExistsException, MBeanRegistrationException,
85     MBeanException, NotCompliantMBeanException, IOException JavaDoc {
86         if (e instanceof ReflectionException)
87             throw (ReflectionException)e;
88         else if (e instanceof InstanceAlreadyExistsException)
89             throw (InstanceAlreadyExistsException)e;
90         else if (e instanceof MBeanRegistrationException)
91             throw (MBeanRegistrationException)e;
92         else if (e instanceof MBeanException)
93             throw (MBeanException)e;
94         else if (e instanceof NotCompliantMBeanException)
95             throw (NotCompliantMBeanException)e;
96         else if (e instanceof RuntimeException JavaDoc)
97             throw (RuntimeException JavaDoc)e;
98         else if (e instanceof NotSerializableException JavaDoc)
99             throw wrappedSerializationException(e);
100         else
101             throw wrappingIoException(e);
102     }
103     
104     public static void createMBeanParams(Exception JavaDoc e) throws ReflectionException,
105     InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException,
106     NotCompliantMBeanException, IOException JavaDoc {
107         createMBean(e); //throws exact same set of exceptions.
108
}
109     
110     public static void createMBeanLoader(Exception JavaDoc e) throws ReflectionException,
111     InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException,
112     NotCompliantMBeanException, InstanceNotFoundException, IOException JavaDoc {
113         if (e instanceof ReflectionException)
114             throw (ReflectionException)e;
115         else if (e instanceof InstanceAlreadyExistsException)
116             throw (InstanceAlreadyExistsException)e;
117         else if (e instanceof MBeanRegistrationException)
118             throw (MBeanRegistrationException)e;
119         else if (e instanceof MBeanException)
120             throw (MBeanException)e;
121         else if (e instanceof NotCompliantMBeanException)
122             throw (NotCompliantMBeanException)e;
123         else if (e instanceof InstanceNotFoundException)
124             throw (InstanceNotFoundException)e;
125         else if (e instanceof RuntimeException JavaDoc)
126             throw (RuntimeException JavaDoc)e;
127         else if (e instanceof NotSerializableException JavaDoc)
128             throw wrappedSerializationException(e);
129         else
130             throw wrappingIoException(e);
131     }
132     
133     public static void createMBeanLoaderParams(Exception JavaDoc e) throws ReflectionException,
134     InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException,
135     NotCompliantMBeanException, InstanceNotFoundException, IOException JavaDoc {
136         createMBeanLoader(e); //throws exact same set of exceptions.
137
}
138     
139     public static void getAttribute(Exception JavaDoc e) throws
140     MBeanException, AttributeNotFoundException, InstanceNotFoundException,
141     ReflectionException, IOException JavaDoc {
142         if (e instanceof MBeanException)
143             throw (MBeanException)e;
144         else if (e instanceof AttributeNotFoundException)
145             throw (AttributeNotFoundException)e;
146         else if (e instanceof InstanceNotFoundException)
147             throw (InstanceNotFoundException)e;
148         else if (e instanceof ReflectionException)
149             throw (ReflectionException)e;
150         else if (e instanceof RuntimeException JavaDoc)
151             throw (RuntimeException JavaDoc)e;
152         else if (e instanceof NotSerializableException JavaDoc)
153             throw wrappedSerializationException(e);
154         else //has to be this, otherwise a ClassCastException results.
155
throw wrappingIoException(e);
156     }
157     
158     public static void getAttributes(Exception JavaDoc e) throws
159     InstanceNotFoundException, ReflectionException, IOException JavaDoc {
160         if (e instanceof InstanceNotFoundException)
161             throw (InstanceNotFoundException)e;
162         else if (e instanceof ReflectionException)
163             throw (ReflectionException)e;
164         else if (e instanceof RuntimeException JavaDoc)
165             throw (RuntimeException JavaDoc)e;
166         else if (e instanceof NotSerializableException JavaDoc)
167             throw wrappedSerializationException(e);
168         else
169             throw wrappingIoException(e);
170     }
171     
172     private static void getIOException(Exception JavaDoc e) throws IOException JavaDoc {
173         throw wrappingIoException(e); // has to be this, otherwise ClassCastException results
174
}
175     
176     public static void getDefaultDomain(Exception JavaDoc e) throws IOException JavaDoc {
177         getIOException(e); //throws exact same set of exceptions
178
}
179     public static void getDomains(Exception JavaDoc e) throws IOException JavaDoc {
180         getIOException(e); // throws exact same set of exceptions
181
}
182     public static void getMBeanCount(Exception JavaDoc e) throws IOException JavaDoc {
183         getIOException(e); // throws exact same set of exceptions
184
}
185     public static void isRegistered(Exception JavaDoc e) throws IOException JavaDoc {
186         getIOException(e); // throws exact same set of exceptions
187
}
188     
189     public static void getMBeanInfo(Exception JavaDoc e) throws
190     InstanceNotFoundException, IntrospectionException, ReflectionException, IOException JavaDoc {
191         if (e instanceof InstanceNotFoundException)
192             throw (InstanceNotFoundException)e;
193         else if (e instanceof IntrospectionException)
194             throw (IntrospectionException)e;
195         else if (e instanceof ReflectionException)
196             throw (ReflectionException)e;
197         else if (e instanceof RuntimeException JavaDoc)
198             throw (RuntimeException JavaDoc)e;
199         else if (e instanceof NotSerializableException JavaDoc)
200             throw wrappedSerializationException(e);
201         else
202             throw wrappingIoException(e);
203     }
204     
205     public static void getObjectInstance(Exception JavaDoc e) throws
206     InstanceNotFoundException, IOException JavaDoc {
207         if (e instanceof InstanceNotFoundException)
208             throw (InstanceNotFoundException)e;
209         else if (e instanceof RuntimeException JavaDoc)
210             throw (RuntimeException JavaDoc)e;
211         else if (e instanceof NotSerializableException JavaDoc)
212             throw wrappedSerializationException(e);
213         else
214             throw wrappingIoException(e);
215     }
216     
217     public static void invoke(Exception JavaDoc e) throws InstanceNotFoundException,
218     MBeanException, ReflectionException, IOException JavaDoc {
219         if (e instanceof InstanceNotFoundException)
220             throw (InstanceNotFoundException)e;
221         else if (e instanceof MBeanException)
222             throw (MBeanException)e;
223         else if (e instanceof ReflectionException)
224             throw (ReflectionException)e;
225         else if (e instanceof RuntimeException JavaDoc)
226             throw (RuntimeException JavaDoc)e;
227         else if (e instanceof NotSerializableException JavaDoc)
228             throw wrappedSerializationException(e);
229         else //CCE Results?
230
throw wrappingIoException(e);
231     }
232     
233     public static void isInstanceOf(Exception JavaDoc e) throws
234     InstanceNotFoundException, IOException JavaDoc {
235         if (e instanceof InstanceNotFoundException)
236             throw (InstanceNotFoundException)e;
237         else if (e instanceof RuntimeException JavaDoc)
238             throw (RuntimeException JavaDoc)e;
239         else if (e instanceof NotSerializableException JavaDoc)
240             throw wrappedSerializationException(e);
241         else
242             throw wrappingIoException(e);
243     }
244     
245     public static void queryMBeans(Exception JavaDoc e) throws IOException JavaDoc {
246         if (e instanceof NotSerializableException JavaDoc)
247             throw wrappedSerializationException(e);
248         getIOException(e); // throws exact same set of exceptions
249
}
250     
251     public static void queryNames(Exception JavaDoc e) throws IOException JavaDoc {
252         if (e instanceof NotSerializableException JavaDoc)
253             throw wrappedSerializationException(e);
254         getIOException(e); // throws exact same set of exceptions
255
}
256     
257     public static void removeNotificationListener(Exception JavaDoc e) throws
258     InstanceNotFoundException, ListenerNotFoundException, IOException JavaDoc {
259         if (e instanceof InstanceNotFoundException)
260             throw (InstanceNotFoundException)e;
261         else if (e instanceof ListenerNotFoundException)
262             throw (ListenerNotFoundException)e;
263         else if (e instanceof RuntimeException JavaDoc)
264             throw (RuntimeException JavaDoc)e;
265         else if (e instanceof NotSerializableException JavaDoc)
266             throw wrappedSerializationException(e);
267         else
268             throw wrappingIoException(e);
269     }
270     
271     public static void removeNotificationListenerFilterHandback(Exception JavaDoc e) throws
272     InstanceNotFoundException, ListenerNotFoundException, IOException JavaDoc {
273         removeNotificationListener(e);
274     }
275     
276     public static void removeNotificationListenerObjectName(Exception JavaDoc e) throws
277     InstanceNotFoundException, ListenerNotFoundException, IOException JavaDoc {
278         removeNotificationListener(e);
279     }
280
281     public static void removeNotificationListenerObjectNameFilterHandback(Exception JavaDoc e) throws
282     InstanceNotFoundException, ListenerNotFoundException, IOException JavaDoc {
283         removeNotificationListener(e);
284     }
285     
286     public static void setAttribute(Exception JavaDoc e) throws InstanceNotFoundException,
287     AttributeNotFoundException, InvalidAttributeValueException, MBeanException,
288     ReflectionException, IOException JavaDoc {
289         if (e instanceof AttributeNotFoundException)
290             throw (AttributeNotFoundException)e;
291         else if (e instanceof InstanceNotFoundException)
292             throw (InstanceNotFoundException)e;
293         else if (e instanceof InvalidAttributeValueException)
294             throw (InvalidAttributeValueException)e;
295         else if (e instanceof MBeanException)
296             throw (MBeanException)e;
297         else if (e instanceof RuntimeException JavaDoc)
298             throw (RuntimeException JavaDoc)e;
299         else if (e instanceof NotSerializableException JavaDoc)
300             throw wrappedSerializationException(e);
301         else
302             throw wrappingIoException(e);
303     }
304     
305     public static void setAttributes(Exception JavaDoc e) throws InstanceNotFoundException,
306     ReflectionException, IOException JavaDoc {
307         if (e instanceof InstanceNotFoundException)
308             throw (InstanceNotFoundException)e;
309         else if (e instanceof ReflectionException)
310             throw (ReflectionException)e;
311         else if (e instanceof RuntimeException JavaDoc)
312             throw (RuntimeException JavaDoc)e;
313         else if (e instanceof NotSerializableException JavaDoc)
314             throw wrappedSerializationException(e);
315         else
316             throw wrappingIoException(e);
317     }
318     
319     public static void unregisterMBean(Exception JavaDoc e) throws
320     InstanceNotFoundException, MBeanRegistrationException, IOException JavaDoc {
321         if (e instanceof InstanceNotFoundException)
322             throw (InstanceNotFoundException)e;
323         else if (e instanceof MBeanRegistrationException)
324             throw (MBeanRegistrationException)e;
325         else if (e instanceof RuntimeException JavaDoc)
326             throw (RuntimeException JavaDoc)e;
327         else if (e instanceof NotSerializableException JavaDoc)
328             throw wrappedSerializationException(e);
329         else
330             throw wrappingIoException(e);
331     }
332     
333     /**
334      * The method which acts as a "catch-all". In the distributed environment, it
335      * is obvious that a large set of exceptions is thrown under various circumstances.
336      * The ultimate exception on which this implementation falls back is {@link
337      * java.io.IOException}. This method receives such an exception, that is
338      * not any of those thrown by MBeanServerConnection's methods, and
339      * carefully arranges its stack and initial cause. The returned instance of
340      * IOException has these properties:
341      * <li> Its stack is intact. (if not null) </li>
342      * <li> The actual exception becomes the init cause. (if not null, which is the case) <li>
343      */

344     private static IOException JavaDoc wrappingIoException(final Throwable JavaDoc t) {
345         final String JavaDoc dm = t.getMessage();
346         final Throwable JavaDoc cause = t.getCause();
347         final StackTraceElement JavaDoc[] st = t.getStackTrace();
348         final IOException JavaDoc ioe = new IOException JavaDoc (dm);
349         if (cause != null) {
350             ioe.initCause(cause);
351         }
352         if (st != null) {
353             ioe.setStackTrace(st);
354         }
355         return ( ioe );
356     }
357     
358     /** A method to create a {@link java.io.NotSerializableException} if that
359      * is what gets thrown by the server. It may happen that the server while
360      * invoking a specific MBeanServerConnection method throws a NotSerializableException
361      * especially if an MBean attribute is not serialiable, invocation result of
362      * invoke method is not serializable etc. There is an argument around what to
363      * do in this case because {@link java.io.IOException} is kind of reserved
364      * for any communication problems and {@link java.io.NotSerializableException}
365      * extends {@link java.io.IOException} :). This means that I can't throw only
366      * IOException. Hence this method.
367      * @return an instance of {@link java.io.NotSerializableException} that is wrapped
368      * inside a {@link RuntimeException}
369      * @param id indicating the integer in @link javax.management.remote.message.MBeanServerRequestMessage}
370      * that gives information about the method to invoke
371      */

372     
373     private static RuntimeException JavaDoc wrappedSerializationException(Exception JavaDoc e) {
374         final String JavaDoc dm = e.getMessage();
375         final Throwable JavaDoc cause = e.getCause();
376         final StackTraceElement JavaDoc[] st = e.getStackTrace();
377         final RuntimeException JavaDoc re = new RuntimeException JavaDoc(dm);
378         if (cause != null) {
379             re.initCause(cause);
380         }
381         if (st != null) {
382             re.setStackTrace(st);
383         }
384         return ( re );
385     }
386 }
Popular Tags