KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > management > UtilMEJB


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * UtilMEJB.java
22  *
23  * Created on August 13, 2003, 12:31 PM
24  */

25
26 package org.netbeans.modules.j2ee.sun.share.management;
27
28
29
30 import javax.management.MBeanServerConnection 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 java.util.Set JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.rmi.RemoteException JavaDoc;
40 import javax.management.Attribute JavaDoc;
41 import javax.management.AttributeList JavaDoc;
42 import javax.management.MBeanInfo JavaDoc;
43 import javax.management.ObjectName JavaDoc;
44 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
45 import org.netbeans.modules.j2ee.sun.ide.controllers.ControllerUtil;
46
47 import org.netbeans.modules.j2ee.sun.ide.j2ee.mbmapping.Constants;
48 import org.netbeans.modules.j2ee.sun.util.AppserverConnectionFactory;
49
50 /**
51  *
52  * @author nityad
53  */

54 public class UtilMEJB implements Constants{
55     
56     private MBeanServerConnection JavaDoc conn = null;
57     
58     
59     /** Creates a new instance of UtilMEJB */
60     
61     
62     public UtilMEJB(SunDeploymentManagerInterface dm) throws RemoteException JavaDoc{
63         
64         
65         ClassLoader JavaDoc origClassLoader=Thread.currentThread().getContextClassLoader();
66         try{
67             Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
68             conn = ControllerUtil.getMBeanServerConnWithInterceptor(
69                         (SunDeploymentManagerInterface)dm,
70                         AppserverConnectionFactory.getHTTPAppserverConnection(
71                             dm.getHost(), dm.getPort(), dm.getUserName(),
72                             dm.getPassword(), dm.isSecure()));
73         }catch(Exception JavaDoc ex){
74             throw new RemoteException JavaDoc(ex.getMessage(), ex.getCause());
75         }
76         
77         finally{
78             Thread.currentThread().setContextClassLoader(origClassLoader);
79         }
80     }
81     
82     
83     protected MBeanServerConnection JavaDoc getConnection(){
84         return this.conn;
85     }
86     
87     protected AttributeList JavaDoc updateGetAttributes(ObjectName JavaDoc objName, String JavaDoc[] attributes){
88         AttributeList JavaDoc attList = null;
89         try{
90             String JavaDoc isResource = objName.getKeyProperty("type"); //NOI18N
91
Set JavaDoc configMods = new HashSet JavaDoc(Arrays.asList(CONFIG_MODULE));
92             if((isResource != null) && (! configMods.contains(isResource))){
93                 attList = this.conn.getAttributes(objName, attributes);
94             }
95         }catch(Exception JavaDoc ex){
96             //Empty list is returned.
97
}
98         return attList;
99     }
100     
101     protected Object JavaDoc updateGetAttribute(ObjectName JavaDoc objName, String JavaDoc attribute){
102         Object JavaDoc attr = null;
103         try{
104             String JavaDoc isResource = objName.getKeyProperty("type"); //NOI18N
105
Set JavaDoc configMods = new HashSet JavaDoc(Arrays.asList(CONFIG_MODULE));
106             if((isResource != null) && (! configMods.contains(isResource))){
107                 attr = this.conn.getAttribute(objName, attribute);
108             }
109             String JavaDoc type = objName.getKeyProperty("j2eeType"); //NOI18N
110
if(type.equals("J2EEServer")) { //NOI18N
111
attr = this.conn.getAttribute(objName, attribute);
112             }
113         }catch(Exception JavaDoc ex){
114             //Null value is returned
115
}
116         return attr;
117     }
118     
119     protected Object JavaDoc updateInvoke(ObjectName JavaDoc objName, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws RemoteException JavaDoc{
120         //If condition is satisfied only for createResource
121
if(objName.toString().equals(MAP_RESOURCES))
122             return invokeServerForResource(objName, operationName, params, signature);
123         else
124             return invokeServer(objName, operationName, params, signature);
125     }
126     
127     private Object JavaDoc invokeServer(ObjectName JavaDoc objName, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws RemoteException JavaDoc{
128         Object JavaDoc retVal = null;
129         try{
130             retVal = this.conn.invoke(objName, operationName, params, signature);
131         }catch(Exception JavaDoc ex){
132             // if (!objName.toString().startsWith("ias:type=domain,category=config")){
133
// System.out.println(" Error in invokeServer " + ex.getMessage());
134
// }
135
throw new RemoteException JavaDoc(ex.getMessage(), ex.getCause());
136             
137         }
138         return retVal;
139     }
140     
141     private Object JavaDoc invokeServerForResource(ObjectName JavaDoc objName, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws RemoteException JavaDoc{
142         Object JavaDoc retVal = null;
143         try{
144             if(operationName.equals("setProperty")){ //NOI18N
145
this.conn.invoke(objName, operationName, params, signature);
146             }else{
147                 retVal = this.conn.invoke(objName, operationName, params, signature);
148             }
149         }catch(Exception JavaDoc ex){
150             throw new RemoteException JavaDoc(ex.getMessage(),ex.getCause());
151         }
152         return retVal;
153     }
154     
155     protected MBeanInfo JavaDoc updateMBeanInfo(ObjectName JavaDoc objName){
156         MBeanInfo JavaDoc bnInfo = null;
157         try{
158             String JavaDoc isResource = objName.getKeyProperty("type"); //NOI18N
159
Set JavaDoc configMods = new HashSet JavaDoc(Arrays.asList(CONFIG_MODULE));
160             if((isResource != null) && (! configMods.contains(isResource))){
161                 bnInfo = this.conn.getMBeanInfo(objName);
162             }
163         }catch(Exception JavaDoc ex){
164             //Property sheet is not created
165
}
166         return bnInfo;
167     }
168     
169     protected void updateSetAttribute(ObjectName JavaDoc objName, Attribute JavaDoc attribute) throws RemoteException JavaDoc, InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
170             InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
171         try{
172             String JavaDoc isResource = objName.getKeyProperty("type"); //NOI18N
173
Set JavaDoc configMods = new HashSet JavaDoc(Arrays.asList(CONFIG_MODULE));
174             if((isResource != null) && (! configMods.contains(isResource))){
175                 this.conn.setAttribute(objName, attribute);
176             }
177         }catch(java.io.IOException JavaDoc ex){
178             throw new RemoteException JavaDoc(ex.getLocalizedMessage(), ex.getCause());
179         }
180     }
181     
182 }
183
184
185
Popular Tags