001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)AbstractServiceImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.esb.management.base.services;
030:
031: import java.io.IOException;
032: import java.io.Serializable;
033: import java.util.Map;
034: import java.util.Set;
035:
036: import javax.management.InstanceNotFoundException;
037: import javax.management.MBeanException;
038: import javax.management.MBeanServerConnection;
039: import javax.management.MalformedObjectNameException;
040: import javax.management.ObjectName;
041: import javax.management.QueryExp;
042: import javax.management.ReflectionException;
043:
044: import com.sun.esb.management.common.ManagementRemoteException;
045: import com.sun.jbi.ui.common.I18NBundle;
046: import com.sun.jbi.ui.common.JBIJMXObjectNames;
047: import com.sun.jbi.ui.common.JBIRemoteException;
048:
049: /**
050: * @author graj
051: *
052: */
053: public abstract class AbstractServiceImpl implements Serializable {
054: static final long serialVersionUID = -1L;
055:
056: protected static String CANNED_RESPONSE = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
057: + "<jbi-task version=\"1.0\" xmlns=\"http://java.sun.com/xml/ns/jbi/management-message\">"
058: + "<jbi-task-result><frmwk-task-result>"
059: + "<frmwk-task-result-details>"
060: + "<task-result-details>"
061: + "<task-id>OPERATION</task-id>"
062: + "<task-result>SUCCESS</task-result>"
063: + "<message-type>INFO</message-type>"
064: + "</task-result-details>"
065: + "<locale>en</locale>"
066: + "</frmwk-task-result-details>"
067: + "</frmwk-task-result></jbi-task-result>" + "</jbi-task>";
068:
069: /** i18n */
070: protected static I18NBundle I18NBUNDLE = null;
071:
072: /** remote MBeanServer connection */
073: protected transient MBeanServerConnection remoteConnection;
074:
075: /** is this a local or remote connection */
076: protected boolean isRemoteConnection;
077:
078: /** Constructor - Constructs a new instance of AbstractServiceImpl */
079: public AbstractServiceImpl() {
080: this (null, false);
081: }
082:
083: /**
084: * Constructor - Constructs a new instance of AbstractServiceImpl
085: *
086: * @param serverConnection
087: */
088: public AbstractServiceImpl(MBeanServerConnection serverConnection) {
089: this (serverConnection, false);
090: }
091:
092: /**
093: * Constructor - Constructs a new instance of AbstractServiceImpl
094: *
095: * @param serverConnection
096: * @param isRemoteConnection
097: */
098: public AbstractServiceImpl(MBeanServerConnection serverConnection,
099: boolean isRemoteConnection) {
100: this .remoteConnection = serverConnection;
101: this .isRemoteConnection = isRemoteConnection;
102: }
103:
104: /**
105: * is this a local or remote connection
106: *
107: * @return true if remote, false if local
108: */
109: protected boolean isRemoteConnection() {
110: return this .isRemoteConnection;
111: }
112:
113: // //////////////////////////////////////////////////////
114: // -- Common Operations --
115: // //////////////////////////////////////////////////////
116: /**
117: * gives the I18N bundle
118: *
119: * @return I18NBundle object
120: */
121: protected I18NBundle getI18NBundle() {
122: // lazzy initialize the Client
123: if (I18NBUNDLE == null) {
124: I18NBUNDLE = new I18NBundle("com.sun.jbi.ui.client");
125: }
126: return I18NBUNDLE;
127: }
128:
129: /**
130: * returns mbean server connection.
131: *
132: * @throws IllegalStateException
133: * on error
134: * @return mbeanserver interface
135: */
136: protected MBeanServerConnection getMBeanServerConnection()
137: throws IllegalStateException {
138: if (this .remoteConnection == null) {
139: throw new IllegalStateException(
140: "caps.management.client.jmx.connection.not.open");
141: }
142: return this .remoteConnection;
143: }
144:
145: /**
146: * Test whether an mbean is registered.
147: *
148: * @param objectName
149: * @return true when the mbean is registered, false otherwise
150: * @throws JBIRemoteException
151: */
152: protected boolean isRegistered(ObjectName objectName) {
153: boolean result = false;
154: Boolean resultObject = null;
155: try {
156: resultObject = (Boolean) this .remoteConnection
157: .isRegistered(objectName);
158: if (resultObject != null) {
159: result = resultObject.booleanValue();
160: }
161: } catch (IOException e) {
162: result = false;
163: } catch (RuntimeException exception) {
164: result = false;
165: }
166:
167: return result;
168: }
169:
170: /**
171: * return Administration Service MBean object name
172: * @return object name of the Administration Service UI MBean
173: * @throws ManagementRemoteException
174: */
175: protected ObjectName getAdministrationServiceMBeanObjectName()
176: throws ManagementRemoteException {
177: try {
178: return JBIJMXObjectNames
179: .getJavaCapsAdministrationServiceMBeanObjectName();
180: } catch (MalformedObjectNameException e) {
181: throw ManagementRemoteException.filterJmxExceptions(e);
182: }
183: }
184:
185: /**
186: * return Configuration Service MBean object name
187: * @return object name of the Configuration Service UI MBean
188: * @throws ManagementRemoteException
189: */
190: protected ObjectName getConfigurationServiceMBeanObjectName()
191: throws ManagementRemoteException {
192: try {
193: return JBIJMXObjectNames
194: .getJavaCapsConfigurationServiceMBeanObjectName();
195: } catch (MalformedObjectNameException e) {
196: throw ManagementRemoteException.filterJmxExceptions(e);
197: }
198: }
199:
200: /**
201: * return Deployment Service MBean object name
202: * @return object name of the Deployment Service UI MBean
203: * @throws ManagementRemoteException
204: */
205: protected ObjectName getDeploymentServiceMBeanObjectName()
206: throws ManagementRemoteException {
207: try {
208: return JBIJMXObjectNames
209: .getJavaCapsDeploymentServiceMBeanObjectName();
210: } catch (MalformedObjectNameException e) {
211: throw ManagementRemoteException.filterJmxExceptions(e);
212: }
213: }
214:
215: /**
216: * return Installation Service MBean object name
217: * @return object name of the Installation Service UI MBean
218: * @throws ManagementRemoteException
219: */
220: protected ObjectName getInstallationServiceMBeanObjectName()
221: throws ManagementRemoteException {
222: try {
223: return JBIJMXObjectNames
224: .getJavaCapsInstallationServiceMBeanObjectName();
225: } catch (MalformedObjectNameException e) {
226: throw ManagementRemoteException.filterJmxExceptions(e);
227: }
228: }
229:
230: /**
231: * return Runtime Management Service MBean object name
232: * @return object name of the Runtime Management Service UI MBean
233: * @throws ManagementRemoteException
234: */
235: protected ObjectName getRuntimeManagementServiceMBeanObjectName()
236: throws ManagementRemoteException {
237: try {
238: return JBIJMXObjectNames
239: .getJavaCapsRuntimeManagementServiceMBeanObjectName();
240: } catch (MalformedObjectNameException e) {
241: throw ManagementRemoteException.filterJmxExceptions(e);
242: }
243: }
244:
245: /**
246: * return Performance Measurement Service MBean object name
247: * @return object name of the Performance Measurement Service UI MBean
248: * @throws ManagementRemoteException
249: */
250: protected ObjectName getPerformanceMeasurementServiceMBeanObjectName()
251: throws ManagementRemoteException {
252: try {
253: return JBIJMXObjectNames
254: .getJavaCapsPerformanceMeasurementServiceMBeanObjectName();
255: } catch (MalformedObjectNameException e) {
256: throw ManagementRemoteException.filterJmxExceptions(e);
257: }
258: }
259:
260: /**
261: * return Notification Service MBean object name
262: * @return object name of the Notification Service UI MBean
263: * @throws ManagementRemoteException
264: */
265: protected ObjectName getNotificationServiceMBeanObjectName()
266: throws ManagementRemoteException {
267: try {
268: return JBIJMXObjectNames
269: .getJavaCapsNotificationServiceMBeanObjectName();
270: } catch (MalformedObjectNameException e) {
271: throw ManagementRemoteException.filterJmxExceptions(e);
272: }
273: }
274:
275: /**
276: * get a list of object names matching the pattern/query
277: * @param pattern pattern string
278: * @param query query expression
279: * @return a set of object names matching the pattern and query
280: */
281: @SuppressWarnings("unchecked")
282: protected Set<ObjectName> queryNames(String pattern, QueryExp query)
283: throws ManagementRemoteException {
284: Set<ObjectName> objectNames = null;
285: if (remoteConnection != null) {
286: try {
287: objectNames = (Set<ObjectName>) remoteConnection
288: .queryNames(new ObjectName(pattern), query);
289: } catch (Exception e) {
290: throw ManagementRemoteException.filterJmxExceptions(e);
291: }
292: }
293: return objectNames;
294: }
295:
296: /**
297: * Returns a map of target names to an array of target instance names.
298: * In the case cluster targets, the key contains the cluster target name,
299: * and the the value contains an array of the "target instance" names.
300: * If it is not a cluster target, the key contains the targetName, and
301: * the value is null.
302: *
303: * @return map of target names to array of target instance names
304: * @throws ManagementRemoteException
305: */
306: @SuppressWarnings("unchecked")
307: protected Map<String /*targetName*/, String[] /*targetInstanceNames*/> listTargetNames()
308: throws ManagementRemoteException {
309: Map<String /*targetName*/, String[] /*targetInstanceNames*/> targetNameToTargetInstanceNameMap = null;
310: ObjectName mbeanName = this
311: .getAdministrationServiceMBeanObjectName();
312: Object[] params = null;
313: String[] signature = null;
314: targetNameToTargetInstanceNameMap = (Map<String /*targetName*/, String[] /*targetInstanceNames*/>) this
315: .invokeMBeanOperation(mbeanName, "listTargetNames",
316: params, signature);
317: return targetNameToTargetInstanceNameMap;
318: }
319:
320: /**
321: * open connection, invokes the operation on mbean and closes connection.
322: * This should not be used if the connection is already opened or not to be
323: * close after the invoke operation.
324: *
325: * @return result object
326: * @param objectName
327: * object name
328: * @param operationName
329: * operation name
330: * @param params
331: * parameters
332: * @param signature
333: * signature of the parameters
334: * @throws JBIRemoteException
335: * on error
336: */
337: protected Object invokeMBeanOperation(ObjectName objectName,
338: String operationName, Object[] params, String[] signature)
339: throws ManagementRemoteException {
340:
341: Object result = null;
342: if (this .remoteConnection != null) {
343: try {
344: result = this .remoteConnection.invoke(objectName,
345: operationName, params, signature);
346: } catch (InstanceNotFoundException jmxException) {
347: throw ManagementRemoteException
348: .filterJmxExceptions(jmxException);
349: } catch (MBeanException jmxException) {
350: throw ManagementRemoteException
351: .filterJmxExceptions(jmxException);
352: } catch (ReflectionException jmxException) {
353: throw ManagementRemoteException
354: .filterJmxExceptions(jmxException);
355: } catch (IOException jmxException) {
356: throw ManagementRemoteException
357: .filterJmxExceptions(jmxException);
358: } finally {
359: }
360: }
361:
362: return result;
363:
364: }
365:
366: /**
367: * Single param operation invocation.
368: *
369: * @return result object
370: * @param objectName
371: * object name
372: * @param operationName
373: * operation name
374: * @param param
375: * String parameter
376: * @throws ManagementRemoteException
377: * on error
378: */
379: protected Object invokeMBeanOperation(ObjectName objectName,
380: String operationName, String param)
381: throws ManagementRemoteException {
382:
383: Object[] params = new Object[1];
384: params[0] = param;
385:
386: String[] signature = new String[1];
387: signature[0] = "java.lang.String";
388:
389: return this.invokeMBeanOperation(objectName, operationName,
390: params, signature);
391: }
392: }
|