+ * Sets up logging.
+ */
+ public ServiceMBeanSupport() {
+ // can not call this(Class) because we need to call getClass()
+ this.log = Logger.getLogger(getClass().getName());
+ if (log.isTraceEnabled()) {
+ log.trace("Constructing");
+ }
+ }
+
+ /**
+ * Construct a
+ * Sets up logging.
+ *
+ * @param type The class type to determine category name from.
+ */
+ public ServiceMBeanSupport(final Class> type) {
+ this(type.getName());
+ }
+
+ /**
+ * Construct a
+ * Sets up logging.
+ *
+ * @param category The logger category name.
+ */
+ public ServiceMBeanSupport(final String category) {
+ this(Logger.getLogger(category));
+ }
+
+ /**
+ * Construct a
+ * Attention: Always call this method when you overwrite it in a subclass because it saves the Object Name of the
+ * MBean.
+ *
+ * @param server Reference to the JMX Agent this MBean is registered on
+ * @param name Name specified by the creator of the MBean. Note that you can overwrite it when the given ObjectName is null
+ * otherwise the change is discarded (maybe a bug in JMX-RI).
+ * @return the ObjectName
+ * @throws Exception for any error
+ */
+ public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
+ this.server = server;
+
+ serviceName = getObjectName(server, name);
+
+ return serviceName;
+ }
+
+ public void postRegister(Boolean registrationDone) {
+ if (!registrationDone.booleanValue()) {
+ if (log.isDebugEnabled()) {
+ log.debug("Registration is not done -> stop");
+ }
+ stop();
+ } else {
+ state = REGISTERED;
+ if (createIgnored) {
+ try {
+ create();
+ } catch (Exception e) {
+ log.error(e);
+ }
+ }
+ }
+ }
+
+ public void preDeregister() throws Exception {
+ }
+
+ public void postDeregister() {
+ if (state != DESTROYED) {
+ unregisterIgnored = true;
+ if (log.isDebugEnabled()) {
+ log.debug("Ignoring postDeregister call; current state is " + getStateString());
+ }
+ return;
+ }
+ unregisterIgnored = false;
+ server = null;
+ serviceName = null;
+ state = UNREGISTERED;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////
+ // Concrete Service Overrides //
+ // /////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Sub-classes should override this method if they only need to set their object name during MBean pre-registration.
+ *
+ * @param server the mbeanserver
+ * @param name the suggested name, maybe null
+ * @return the object name
+ * @throws MalformedObjectNameException for a bad object name
+ */
+ protected ObjectName getObjectName(MBeanServer server, ObjectName name) throws MalformedObjectNameException {
+ return name;
+ }
+
+ /**
+ * Sub-classes should override this method to provide custum 'create' logic.
+ *
+ *
+ * This method is empty, and is provided for convenience when concrete service classes do not need to perform anything
+ * specific for this state change.
+ *
+ * @throws Exception for any error
+ */
+ protected void createService() throws Exception {
+ }
+
+ /**
+ * Sub-classes should override this method to provide custum 'start' logic.
+ *
+ *
+ * This method is empty, and is provided for convenience when concrete service classes do not need to perform anything
+ * specific for this state change.
+ *
+ * @throws Exception for any error
+ */
+ protected void startService() throws Exception {
+ }
+
+ /**
+ * Sub-classes should override this method to provide custum 'stop' logic.
+ *
+ *
+ * This method is empty, and is provided for convenience when concrete service classes do not need to perform anything
+ * specific for this state change.
+ *
+ * @throws Exception for any error
+ */
+ protected void stopService() throws Exception {
+ }
+
+ /**
+ * Sub-classes should override this method to provide custum 'destroy' logic.
+ *
+ *
+ * This method is empty, and is provided for convenience when concrete service classes do not need to perform anything
+ * specific for this state change.
+ *
+ * @throws Exception for any error
+ */
+ protected void destroyService() throws Exception {
+ }
+
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/ServiceMBeanSupportTestCase.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/ServiceMBeanSupportTestCase.java
new file mode 100644
index 00000000000..29467775309
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/ServiceMBeanSupportTestCase.java
@@ -0,0 +1,118 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.as.test.integration.sar.servicembean;
+
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnectorFactory;
+
+import org.jboss.arquillian.container.test.api.Deployer;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.container.test.api.RunAsClient;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.as.arquillian.api.ContainerResource;
+import org.jboss.as.arquillian.container.ManagementClient;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.system.ServiceMBean;
+import org.jboss.system.ServiceMBeanSupport;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Test MBeans which implement {@link ServiceMBean} and extend {@link ServiceMBeanSupport}.
+ *
+ * @author Eduardo Martins
+ */
+@RunWith(Arquillian.class)
+@RunAsClient
+public class ServiceMBeanSupportTestCase {
+
+ private static final String UNMANAGED_SAR_DEPLOYMENT_NAME = "service-mbean-support-test";
+
+ @ContainerResource
+ private ManagementClient managementClient;
+
+ @ArquillianResource
+ private Deployer deployer;
+
+ @Deployment(name = ServiceMBeanSupportTestCase.UNMANAGED_SAR_DEPLOYMENT_NAME, managed = false)
+ public static JavaArchive geTestMBeanSar() {
+ final JavaArchive sar = ShrinkWrap.create(JavaArchive.class, "service-mbean-support-test.sar");
+ sar.addClasses(TestServiceMBean.class, TestService.class);
+ sar.addAsManifestResource(ServiceMBeanSupportTestCase.class.getPackage(), "jboss-service.xml", "jboss-service.xml");
+ return sar;
+ }
+
+ @Deployment
+ public static JavaArchive getTestResultMBeanSar() {
+ final JavaArchive sar = ShrinkWrap.create(JavaArchive.class, "service-mbean-support-test-result.sar");
+ sar.addClasses(TestResultServiceMBean.class, TestResultService.class);
+ sar.addAsManifestResource(ServiceMBeanSupportTestCase.class.getPackage(), "result-jboss-service.xml",
+ "jboss-service.xml");
+ return sar;
+ }
+
+ /**
+ * Tests that invocation on a service deployed within a .sar, inside a .ear without an application.xml, is successful.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSarWithServiceMBeanSupport() throws Exception {
+ // get mbean server
+ final MBeanServerConnection mBeanServerConnection = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL())
+ .getMBeanServerConnection();
+ try {
+ // deploy the unmanaged sar
+ deployer.deploy(ServiceMBeanSupportTestCase.UNMANAGED_SAR_DEPLOYMENT_NAME);
+ // check the unmanaged mbean state
+ int state = (Integer) mBeanServerConnection.getAttribute(new ObjectName("jboss:name=service-mbean-support-test"),
+ "State");
+ Assert.assertEquals("Unexpected return state from Test MBean: " + state, ServiceMBean.STARTED, state);
+ } finally {
+ // undeploy it
+ deployer.undeploy(ServiceMBeanSupportTestCase.UNMANAGED_SAR_DEPLOYMENT_NAME);
+ }
+ // check the result of life-cycle methods invocation, using result mbean
+ String attribute = "CreateServiceInvoked";
+ Boolean result = (Boolean) mBeanServerConnection.getAttribute(new ObjectName("jboss:name=service-mbean-support-test-result"),
+ attribute);
+ Assert.assertTrue("Unexpected result for " + attribute + ": " + result, result);
+ attribute = "StartServiceInvoked";
+ result = (Boolean) mBeanServerConnection.getAttribute(new ObjectName("jboss:name=service-mbean-support-test-result"),
+ attribute);
+ Assert.assertTrue("Unexpected result for " + attribute + ": " + result, result);
+ attribute = "StopServiceInvoked";
+ result = (Boolean) mBeanServerConnection.getAttribute(new ObjectName("jboss:name=service-mbean-support-test-result"),
+ attribute);
+ Assert.assertTrue("Unexpected result for " + attribute + ": " + result, result);
+ attribute = "DestroyServiceInvoked";
+ result = (Boolean) mBeanServerConnection.getAttribute(new ObjectName("jboss:name=service-mbean-support-test-result"),
+ attribute);
+ Assert.assertTrue("Unexpected result for " + attribute + ": " + result, result);
+ }
+
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestResultService.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestResultService.java
new file mode 100644
index 00000000000..1f710e42474
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestResultService.java
@@ -0,0 +1,124 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.as.test.integration.sar.servicembean;
+
+import org.jboss.system.ServiceMBean;
+
+/**
+ * An MBean that collects results of life-cycle methods invocations of {@link TestServiceMBean}.
+ *
+ * @author Eduardo Martins
+ *
+ */
+public class TestResultService implements TestResultServiceMBean, ServiceMBean {
+
+ private boolean createServiceInvoked;
+ private boolean startServiceInvoked;
+ private boolean stopServiceInvoked;
+ private boolean destroyServiceInvoked;
+
+ @Override
+ public boolean isCreateServiceInvoked() {
+ return createServiceInvoked;
+ }
+
+ @Override
+ public boolean isDestroyServiceInvoked() {
+ return destroyServiceInvoked;
+ }
+
+ @Override
+ public boolean isStartServiceInvoked() {
+ return startServiceInvoked;
+ }
+
+ @Override
+ public boolean isStopServiceInvoked() {
+ return stopServiceInvoked;
+ }
+
+ public void setCreateServiceInvoked(boolean createServiceInvoked) {
+ this.createServiceInvoked = createServiceInvoked;
+ }
+
+ public void setDestroyServiceInvoked(boolean destroyServiceInvoked) {
+ this.destroyServiceInvoked = destroyServiceInvoked;
+ }
+
+ public void setStartServiceInvoked(boolean startServiceInvoked) {
+ this.startServiceInvoked = startServiceInvoked;
+ }
+
+ public void setStopServiceInvoked(boolean stopServiceInvoked) {
+ this.stopServiceInvoked = stopServiceInvoked;
+ }
+
+ @Override
+ public void create() throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void start() throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void stop() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void destroy() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public String getName() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public int getState() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public String getStateString() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void jbossInternalLifecycle(String method) throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestResultServiceMBean.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestResultServiceMBean.java
new file mode 100644
index 00000000000..37c5a2e3a72
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestResultServiceMBean.java
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.as.test.integration.sar.servicembean;
+
+/**
+ * MBean interface for {@link TestResultService}.
+ *
+ * @author Eduardo Martins
+ *
+ */
+public interface TestResultServiceMBean {
+
+ public boolean isCreateServiceInvoked();
+
+ public boolean isStartServiceInvoked();
+
+ public boolean isStopServiceInvoked();
+
+ public boolean isDestroyServiceInvoked();
+
+ public void setCreateServiceInvoked(boolean createServiceInvoked);
+
+ public void setDestroyServiceInvoked(boolean destroyServiceInvoked);
+
+ public void setStartServiceInvoked(boolean startServiceInvoked);
+
+ public void setStopServiceInvoked(boolean stopServiceInvoked);
+
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestService.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestService.java
new file mode 100644
index 00000000000..a0d1a93940c
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestService.java
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.as.test.integration.sar.servicembean;
+
+import javax.management.Attribute;
+import javax.management.ObjectName;
+
+import org.jboss.system.ServiceMBeanSupport;
+
+/**
+ * An MBean that extends legacy {@link ServiceMBeanSupport}.
+ *
+ * @author Eduardo Martins
+ *
+ */
+public class TestService extends ServiceMBeanSupport implements TestServiceMBean {
+
+ @Override
+ protected void createService() throws Exception {
+ getLog().info("createService()");
+ setTestResultMBeanAttribute("CreateServiceInvoked", true);
+ }
+
+ @Override
+ protected void startService() throws Exception {
+ getLog().info("startService()");
+ setTestResultMBeanAttribute("StartServiceInvoked", true);
+ }
+
+ @Override
+ protected void stopService() throws Exception {
+ getLog().info("stopService()");
+ setTestResultMBeanAttribute("StopServiceInvoked", true);
+ }
+
+ @Override
+ protected void destroyService() throws Exception {
+ getLog().info("destroyService()");
+ setTestResultMBeanAttribute("DestroyServiceInvoked", true);
+ }
+
+ private void setTestResultMBeanAttribute(String attributeName, boolean attributeValue) throws Exception {
+ server.setAttribute(new ObjectName("jboss:name=service-mbean-support-test-result"), new Attribute(attributeName,
+ attributeValue));
+ }
+
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestServiceMBean.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestServiceMBean.java
new file mode 100644
index 00000000000..f9d0d2d8f26
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/TestServiceMBean.java
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.as.test.integration.sar.servicembean;
+
+/**
+ * MBean interface for {@link TestService}.
+ *
+ * @author Eduardo Martins
+ *
+ */
+public interface TestServiceMBean {
+
+ public int getState();
+}
diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/jboss-service.xml b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/jboss-service.xml
new file mode 100644
index 00000000000..120fa134826
--- /dev/null
+++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/jboss-service.xml
@@ -0,0 +1,28 @@
+
+