KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > comm > AdminConnectorClient


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 package com.sun.enterprise.admin.comm;
25
26 import java.io.IOException JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.Attribute JavaDoc;
30 import javax.management.AttributeList JavaDoc;
31 import javax.management.MBeanException JavaDoc;
32 import javax.management.ReflectionException JavaDoc;
33 import javax.management.InstanceNotFoundException JavaDoc;
34 import javax.management.AttributeNotFoundException JavaDoc;
35 import javax.management.InvalidAttributeValueException JavaDoc;
36 import javax.management.RuntimeErrorException JavaDoc;
37
38 import com.sun.enterprise.admin.util.*;
39 import com.sun.enterprise.admin.common.AdminRequest;
40 import com.sun.enterprise.admin.common.AdminRequestType;
41 import com.sun.enterprise.admin.common.AdminResponse;
42 import com.sun.enterprise.admin.common.AdminRequestConfigurator;
43 import com.sun.enterprise.admin.common.AdminResponseConfigurator;
44 import com.sun.enterprise.admin.common.exception.AFRuntimeException;
45
46 /**
47  */

48 public class AdminConnectorClient implements ConnectorClient
49 {
50     private final HttpConnectorAddress connectorAddress;
51     private final static String JavaDoc ADMIN_CLIENT_VERSION = "2.0";
52
53     public AdminConnectorClient(HttpConnectorAddress address)
54     {
55         this.connectorAddress = address;
56         Debug.println("AdminConnectorClient.<init> : " +
57             "host = " + address.getHost() +
58             " port = " + address.getPort() +
59             " user = " + address.getAuthenticationInfo().getUser() );
60     }
61
62     /**
63      */

64     public String JavaDoc getClientVersion()
65     {
66         return ADMIN_CLIENT_VERSION;
67     }
68     
69     /**
70      */

71     public Object JavaDoc invoke(ObjectName JavaDoc mbeanName,
72                          String JavaDoc operationName,
73                          Object JavaDoc[] params,
74                          String JavaDoc[] signature)
75         throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
76     {
77         Object JavaDoc returnValue = null;
78         AdminRequest request = new AdminRequest(AdminRequestType.INVOKE);
79         AdminRequestConfigurator reqConfigurator =
80             new AdminRequestConfigurator(request);
81         reqConfigurator.setObjectName(mbeanName);
82         reqConfigurator.setOperationName(operationName);
83         reqConfigurator.setOperationParams(params);
84         reqConfigurator.setOperationSignature(signature);
85         reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION);
86
87         AdminResponse response = sendRequest(request);
88         AdminResponseConfigurator resConfigurator =
89             new AdminResponseConfigurator(response);
90         if (resConfigurator.hasException())
91         {
92             Throwable JavaDoc t = resConfigurator.getException();
93             if (t instanceof InstanceNotFoundException JavaDoc)
94             {
95                 throw (InstanceNotFoundException JavaDoc)t;
96             }
97             else if (t instanceof MBeanException JavaDoc)
98             {
99                 throw (MBeanException JavaDoc)t;
100             }
101             else if (t instanceof ReflectionException JavaDoc)
102             {
103                 throw (ReflectionException JavaDoc)t;
104             }
105             else if (t instanceof RuntimeException JavaDoc)
106             {
107                 throw (RuntimeException JavaDoc)t;
108             }
109             else if (t instanceof Error JavaDoc)
110             {
111                 throw new RuntimeErrorException JavaDoc((Error JavaDoc)t);
112             }
113         }
114         else
115         {
116             returnValue = resConfigurator.getReturnValue();
117         }
118         return returnValue;
119     }
120
121     /**
122      */

123     public Object JavaDoc getAttribute(ObjectName JavaDoc mbeanName, String JavaDoc attributeName)
124         throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc,
125                 InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
126     {
127         Object JavaDoc returnValue = null;
128         AdminRequest request = new AdminRequest(AdminRequestType.GET_ATTRIBUTE);
129         AdminRequestConfigurator reqConfigurator =
130             new AdminRequestConfigurator(request);
131         reqConfigurator.setObjectName(mbeanName);
132         reqConfigurator.setAttributeName(attributeName);
133         reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION);
134         AdminResponse response = sendRequest(request);
135         //Assert.assert(response);
136
AdminResponseConfigurator resConfigurator =
137             new AdminResponseConfigurator(response);
138         if (resConfigurator.hasException())
139         {
140             Throwable JavaDoc t = resConfigurator.getException();
141             if (t instanceof AttributeNotFoundException JavaDoc)
142             {
143                 throw (AttributeNotFoundException JavaDoc)t;
144             }
145             else if (t instanceof InstanceNotFoundException JavaDoc)
146             {
147                 throw (InstanceNotFoundException JavaDoc)t;
148             }
149             else if (t instanceof MBeanException JavaDoc)
150             {
151                 throw (MBeanException JavaDoc)t;
152             }
153             else if (t instanceof ReflectionException JavaDoc)
154             {
155                 throw (ReflectionException JavaDoc)t;
156             }
157             else if (t instanceof RuntimeException JavaDoc)
158             {
159                 throw (RuntimeException JavaDoc)t;
160             }
161             else if (t instanceof Error JavaDoc)
162             {
163                 throw new RuntimeErrorException JavaDoc((Error JavaDoc)t);
164             }
165         }
166         else
167         {
168             returnValue = resConfigurator.getReturnValue();
169         }
170         return returnValue;
171     }
172
173     /**
174      */

175     public void setAttribute(ObjectName JavaDoc mbeanName, Attribute JavaDoc attribute)
176         throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
177                 InvalidAttributeValueException JavaDoc, MBeanException JavaDoc,
178                 ReflectionException JavaDoc
179     {
180         AdminRequest request = new AdminRequest(AdminRequestType.SET_ATTRIBUTE);
181         AdminRequestConfigurator reqConfigurator =
182             new AdminRequestConfigurator(request);
183         reqConfigurator.setObjectName(mbeanName);
184         reqConfigurator.setAttribute(attribute);
185         reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION);
186         AdminResponse response = sendRequest(request);
187         //Assert.assert(response);
188
AdminResponseConfigurator resConfigurator =
189             new AdminResponseConfigurator(response);
190         if (resConfigurator.hasException())
191         {
192             Throwable JavaDoc t = resConfigurator.getException();
193             if (t instanceof AttributeNotFoundException JavaDoc)
194             {
195                 throw (AttributeNotFoundException JavaDoc)t;
196             }
197             else if (t instanceof InvalidAttributeValueException JavaDoc)
198             {
199                 throw (InvalidAttributeValueException JavaDoc)t;
200             }
201             else if (t instanceof InstanceNotFoundException JavaDoc)
202             {
203                 throw (InstanceNotFoundException JavaDoc)t;
204             }
205             else if (t instanceof MBeanException JavaDoc)
206             {
207                 throw (MBeanException JavaDoc)t;
208             }
209             else if (t instanceof ReflectionException JavaDoc)
210             {
211                 throw (ReflectionException JavaDoc)t;
212             }
213             else if (t instanceof RuntimeException JavaDoc)
214             {
215                 throw (RuntimeException JavaDoc)t;
216             }
217             else if (t instanceof Error JavaDoc)
218             {
219                 throw new RuntimeErrorException JavaDoc((Error JavaDoc)t);
220             }
221         }
222     }
223
224     /**
225      */

226     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc mbeanName,
227                                        String JavaDoc[] attributes)
228         throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
229     {
230         AttributeList JavaDoc values = null;
231         AdminRequest request = new AdminRequest(
232                                     AdminRequestType.GET_ATTRIBUTES);
233         AdminRequestConfigurator reqConfigurator =
234             new AdminRequestConfigurator(request);
235         reqConfigurator.setObjectName(mbeanName);
236         reqConfigurator.setAttributeNames(attributes);
237         reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION);
238         AdminResponse response = sendRequest(request);
239         //Assert.assert(response);
240
AdminResponseConfigurator resConfigurator =
241             new AdminResponseConfigurator(response);
242         if (resConfigurator.hasException())
243         {
244             Throwable JavaDoc t = resConfigurator.getException();
245             if (t instanceof InstanceNotFoundException JavaDoc)
246             {
247                 throw (InstanceNotFoundException JavaDoc)t;
248             }
249             else if (t instanceof ReflectionException JavaDoc)
250             {
251                 throw (ReflectionException JavaDoc)t;
252             }
253             else if (t instanceof RuntimeException JavaDoc)
254             {
255                 throw (RuntimeException JavaDoc)t;
256             }
257             else if (t instanceof Error JavaDoc)
258             {
259                 throw new RuntimeErrorException JavaDoc((Error JavaDoc)t);
260             }
261         }
262         else
263         {
264             values = (AttributeList JavaDoc) resConfigurator.getReturnValue();
265         }
266         return values;
267     }
268
269     /**
270      */

271     public AttributeList JavaDoc setAttributes(ObjectName JavaDoc mbeanName,
272                                        AttributeList JavaDoc attributes)
273         throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
274     {
275         AttributeList JavaDoc values = null;
276         AdminRequest request = new AdminRequest(
277                                     AdminRequestType.SET_ATTRIBUTES);
278         AdminRequestConfigurator reqConfigurator =
279             new AdminRequestConfigurator(request);
280         reqConfigurator.setObjectName(mbeanName);
281         reqConfigurator.setAttributeList(attributes);
282         reqConfigurator.setClientVersion(ADMIN_CLIENT_VERSION);
283         AdminResponse response = sendRequest(request);
284         //Assert.assert(response);
285
AdminResponseConfigurator resConfigurator =
286             new AdminResponseConfigurator(response);
287         if (resConfigurator.hasException())
288         {
289             Throwable JavaDoc t = resConfigurator.getException();
290             if (t instanceof InstanceNotFoundException JavaDoc)
291             {
292                 throw (InstanceNotFoundException JavaDoc)t;
293             }
294             else if (t instanceof ReflectionException JavaDoc)
295             {
296                 throw (ReflectionException JavaDoc)t;
297             }
298             else if (t instanceof RuntimeException JavaDoc)
299             {
300                 throw (RuntimeException JavaDoc)t;
301             }
302             else if (t instanceof Error JavaDoc)
303             {
304                 throw new RuntimeErrorException JavaDoc((Error JavaDoc)t);
305             }
306         }
307         else
308         {
309             values = (AttributeList JavaDoc) resConfigurator.getReturnValue();
310         }
311         return values;
312     }
313
314     private AdminResponse sendRequest(AdminRequest request)
315         throws AFConnectionException, AFRuntimeException
316     {
317         AdminResponse response = null;
318         IConnection connection = null;
319         try
320         {
321           connection = ConnectionFactory.createConnection(connectorAddress);
322             connection.send(request);
323             response = (AdminResponse)connection.receive();
324         }
325         catch (IOException JavaDoc e)
326         {
327             Debug.printStackTrace(e);
328             throw new AFConnectionException(e.getMessage());
329         }
330         catch (ClassNotFoundException JavaDoc cnfe)
331         {
332             throw new AFRuntimeException(cnfe.toString());
333         }
334         catch (Exception JavaDoc e)
335         {
336             throw new AFRuntimeException(e.getLocalizedMessage());
337         }
338         finally
339         {
340             if (connection != null)
341             {
342                 try
343                 {
344                     connection.close();
345                 }
346                 catch (Exception JavaDoc e)
347                 {
348                     ExceptionUtil.ignoreException(e);
349                 }
350             }
351         }
352         return response;
353     }
354 }
355
Popular Tags