001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.as.system;
023:
024: import javax.management.InstanceNotFoundException;
025: import javax.management.MBeanException;
026: import javax.management.ObjectName;
027: import javax.management.ReflectionException;
028:
029: /**
030: * A jmx method invoker based on the lifecycle.
031: *
032: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
033: * @version $Revision: 8784 $
034: */
035: public class LifecycleInvoker extends AbstractJBossService {
036:
037: /** . */
038: private ObjectName target;
039:
040: /** . */
041: private String onCreate;
042:
043: /** . */
044: private String onStart;
045:
046: /** . */
047: private String onStop;
048:
049: /** . */
050: private String onDestroy;
051:
052: public ObjectName getTarget() {
053: return target;
054: }
055:
056: public void setTarget(ObjectName target) {
057: this .target = target;
058: }
059:
060: public String getOnCreate() {
061: return onCreate;
062: }
063:
064: public void setOnCreate(String onCreate) {
065: this .onCreate = onCreate;
066: }
067:
068: public String getOnStart() {
069: return onStart;
070: }
071:
072: public void setOnStart(String onStart) {
073: this .onStart = onStart;
074: }
075:
076: public String getOnStop() {
077: return onStop;
078: }
079:
080: public void setOnStop(String onStop) {
081: this .onStop = onStop;
082: }
083:
084: public String getOnDestroy() {
085: return onDestroy;
086: }
087:
088: public void setOnDestroy(String onDestroy) {
089: this .onDestroy = onDestroy;
090: }
091:
092: private void invoke(String methodName) {
093: try {
094: log.debug("About to invoke " + methodName + " on mbean "
095: + target);
096: server.invoke(target, methodName, new Object[0],
097: new String[0]);
098: } catch (InstanceNotFoundException e) {
099: log.debug("The mbean " + target + " does not exist and "
100: + methodName + " cannot be invoked");
101: } catch (MBeanException e) {
102: Throwable t = e.getCause();
103: log
104: .error(
105: "The mbean "
106: + target
107: + " threw an exception during the invocation of the method "
108: + methodName, t);
109: } catch (ReflectionException e) {
110: log.error("The mbean " + target + " invocation of method "
111: + methodName, e);
112: }
113: }
114:
115: protected void createService() throws Exception {
116: if (onCreate != null) {
117: invoke(onCreate);
118: }
119: }
120:
121: protected void startService() throws Exception {
122: if (onStart != null) {
123: invoke(onStart);
124: }
125: }
126:
127: protected void stopService() throws Exception {
128: if (onStop != null) {
129: invoke(onStop);
130: }
131: }
132:
133: protected void destroyService() throws Exception {
134: if (onDestroy != null) {
135: invoke(onDestroy);
136: }
137: }
138: }
|