Browse files

adding system-jmx module with ServiceMBean interface, adding new modu…

…le as automatically dependency for sar deployments, reusing existent integration test to validate deploy of ServiceMBeans

I (Kabir squashed this commit) so it also includes the following:
-adding limited support for ServiceMBeanSupport
-reverting usage of servicembean by an existent integration test
-removing dep from sar to system-jmx, only needed by deployments, and making deployment dep to system-jmx optional
-changing all lifecycle related logs to debug level, except exceptions due to user code, and activating checkstyle maven plugin
-reverting pom.xml license info mess introduced by Eclipse
  • Loading branch information...
1 parent 5648726 commit 618fe966690e8951305858235c8b0b5ba9ea9f22 @emmartins emmartins committed with kabir Jul 11, 2012
View
6 build/build.xml
@@ -885,7 +885,11 @@
<module-def name="org.jboss.as.security">
<maven-resource group="org.jboss.as" artifact="jboss-as-security"/>
</module-def>
-
+
+ <module-def name="org.jboss.as.system-jmx">
+ <maven-resource group="org.jboss.as" artifact="jboss-as-system-jmx"/>
+ </module-def>
+
<module-def name="org.jboss.as.transactions">
<maven-resource group="org.jboss.as" artifact="jboss-as-transactions"/>
</module-def>
View
13 build/pom.xml
@@ -997,19 +997,24 @@
<dependency>
<groupId>org.jboss.as</groupId>
+ <artifactId>jboss-as-sar</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.jboss.as</groupId>
<artifactId>jboss-as-security</artifactId>
</dependency>
-
+
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-sos</artifactId>
</dependency>
-
+
<dependency>
<groupId>org.jboss.as</groupId>
- <artifactId>jboss-as-sar</artifactId>
+ <artifactId>jboss-as-system-jmx</artifactId>
</dependency>
-
+
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-threads</artifactId>
View
48 build/src/main/resources/modules/org/jboss/as/system-jmx/main/module.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ 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.
+ -->
+
+<module xmlns="urn:jboss:module:1.1" name="org.jboss.as.system-jmx">
+ <properties>
+ <property name="jboss.api" value="private"/>
+ </properties>
+
+ <resources>
+ <!-- Insert resources here -->
+ </resources>
+
+ <dependencies>
+ <module name="javax.api"/>
+ <module name="org.jboss.common-beans"/>
+ <module name="org.jboss.staxmapper"/>
+ <module name="org.jboss.as.controller"/>
+ <module name="org.jboss.as.server"/>
+ <module name="org.jboss.as.ee"/>
+ <module name="org.jboss.as.jmx"/>
+ <module name="org.jboss.modules"/>
+ <module name="org.jboss.msc"/>
+ <module name="org.jboss.logging"/>
+ <module name="org.jboss.vfs"/>
+ <module name="org.jboss.metadata"/>
+ </dependencies>
+</module>
View
9 pom.xml
@@ -1102,6 +1102,12 @@
<artifactId>jboss-as-server</artifactId>
<version>${project.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.jboss.as</groupId>
+ <artifactId>jboss-as-sos</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.jboss.as</groupId>
@@ -1111,7 +1117,7 @@
<dependency>
<groupId>org.jboss.as</groupId>
- <artifactId>jboss-as-sos</artifactId>
+ <artifactId>jboss-as-system-jmx</artifactId>
<version>${project.version}</version>
</dependency>
@@ -6084,6 +6090,7 @@
<module>osgi</module>
<module>pojo</module>
<module>remoting-test</module>
+ <module>system-jmx</module>
<module>sar</module>
<module>security</module>
<module>spec-api</module>
View
2 sar/src/main/java/org/jboss/as/service/SarModuleDependencyProcessor.java
@@ -41,6 +41,7 @@
public class SarModuleDependencyProcessor implements DeploymentUnitProcessor {
private static ModuleIdentifier JBOSS_MODULES_ID = ModuleIdentifier.create("org.jboss.modules");
+ private static ModuleIdentifier JBOSS_AS_SYSTEM_JMX_ID = ModuleIdentifier.create("org.jboss.as.system-jmx");
/**
* Add dependencies for modules required for manged bean deployments, if managed bean configurations are attached
@@ -58,6 +59,7 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
}
moduleSpecification.addSystemDependency(new ModuleDependency(Module.getBootModuleLoader(), JBOSS_MODULES_ID, false, false, false, false));
+ moduleSpecification.addSystemDependency(new ModuleDependency(Module.getBootModuleLoader(), JBOSS_AS_SYSTEM_JMX_ID, true, false, false, false));
}
public void undeploy(final DeploymentUnit context) {
View
84 system-jmx/pom.xml
@@ -0,0 +1,84 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.jboss.as</groupId>
+ <artifactId>jboss-as-parent</artifactId>
+ <version>7.2.0.Alpha1-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.jboss.as</groupId>
+ <artifactId>jboss-as-system-jmx</artifactId>
+ <version>7.2.0.Alpha1-SNAPSHOT</version>
+
+ <name>JBoss Application Server: System JMX Module</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <compilerArgument>
+ -AgeneratedTranslationFilesPath=${project.build.directory}/generated-translation-files
+ </compilerArgument>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <includes>
+ <include>**/*TestCase.java</include>
+ </includes>
+ <enableAssertions>false</enableAssertions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.logging</groupId>
+ <artifactId>jboss-logging</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.logging</groupId>
+ <artifactId>jboss-logging-processor</artifactId>
+ <!-- This is a compile-time dependency of this project, but is not needed at compile or runtime by other
+ projects that depend on this project.-->
+ <scope>provided</scope>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
View
54 system-jmx/src/main/java/org/jboss/system/Service.java
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat Middleware LLC, 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.system;
+
+/**
+ * The Service interface.
+ *
+ * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>.
+ * @version $Revision: 81032 $
+ */
+public interface Service {
+ /**
+ * create the service, do expensive operations etc
+ *
+ * @throws Exception for any error
+ */
+ void create() throws Exception;
+
+ /**
+ * start the service, create is already called
+ *
+ * @throws Exception for any error
+ */
+ void start() throws Exception;
+
+ /**
+ * stop the service
+ */
+ void stop();
+
+ /**
+ * destroy the service, tear down
+ */
+ void destroy();
+}
View
76 system-jmx/src/main/java/org/jboss/system/ServiceMBean.java
@@ -0,0 +1,76 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat Middleware LLC, 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.system;
+
+/**
+ * An interface describing a JBoss service MBean.
+ *
+ * @see Service
+ * @see ServiceMBeanSupport
+ *
+ * @author <a href="mailto:rickard.oberg@telkel.com">Rickard Oberg</a>
+ * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
+ * @author Scott.Stark@jboss.org
+ * @version $Revision: 81033 $
+ */
+public interface ServiceMBean extends Service {
+ // Constants -----------------------------------------------------
+
+ /** ServiceController notification types corresponding to service lifecycle events */
+ String CREATE_EVENT = "org.jboss.system.ServiceMBean.create";
+ String START_EVENT = "org.jboss.system.ServiceMBean.start";
+ String STOP_EVENT = "org.jboss.system.ServiceMBean.stop";
+ String DESTROY_EVENT = "org.jboss.system.ServiceMBean.destroy";
+
+ String[] states = { "Stopped", "Stopping", "Starting", "Started", "Failed", "Destroyed", "Created",
+ "Unregistered", "Registered" };
+
+ /** The Service.stop has completed */
+ int STOPPED = 0;
+ /** The Service.stop has been invoked */
+ int STOPPING = 1;
+ /** The Service.start has been invoked */
+ int STARTING = 2;
+ /** The Service.start has completed */
+ int STARTED = 3;
+ /** There has been an error during some operation */
+ int FAILED = 4;
+ /** The Service.destroy has completed */
+ int DESTROYED = 5;
+ /** The Service.create has completed */
+ int CREATED = 6;
+ /** The MBean has been created but has not completed MBeanRegistration.postRegister */
+ int UNREGISTERED = 7;
+ /** The MBean has been created and has completed MBeanRegistration.postRegister */
+ int REGISTERED = 8;
+
+ // Public --------------------------------------------------------
+
+ String getName();
+
+ int getState();
+
+ String getStateString();
+
+ /** Detyped lifecycle invocation */
+ void jbossInternalLifecycle(String method) throws Exception;
+}
View
453 system-jmx/src/main/java/org/jboss/system/ServiceMBeanSupport.java
@@ -0,0 +1,453 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat Middleware LLC, 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.system;
+
+import javax.management.MBeanRegistration;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.jboss.logging.Logger;
+
+/**
+ * An abstract base class JBoss services can subclass to implement a service that conforms to the ServiceMBean interface.
+ * Subclasses must override {@link #getName} method and should override {@link #startService}, and {@link #stopService} as
+ * approriate.
+ *
+ * @see ServiceMBean
+ *
+ * @author <a href="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
+ * @author Scott.Stark@jboss.org
+ * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
+ * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
+ * @author Eduardo Martins (AS7)
+ */
+public class ServiceMBeanSupport implements ServiceMBean, MBeanRegistration {
+
+ protected Logger log;
+
+ /** The MBeanServer which we have been register with. */
+ protected MBeanServer server;
+
+ /** The object name which we are registered under. */
+ protected ObjectName serviceName;
+
+ /** The current state this service is in. */
+ private int state = UNREGISTERED;
+
+ // on AS7 MBean lifecycle is CREATED -> STARTED -> REGISTERED -> UNREGISTERED -> STOP -> DESTROY,
+ // on previous AS versions is REGISTERED -> CREATED -> STARTED -> STOP -> DESTROYED -> UNREGISTERED
+ // to maintain compatibility with old ServiceMBeanSupport, we ignore some state changes, but redo
+ // these when proper state change happens
+ // the flags below are used to mark ignored lifecycle methods invocations
+ private boolean createIgnored = false;
+ private boolean startIgnored = false;
+ private boolean stopIgnored = false;
+ private boolean destroyIgnored = false;
+ private boolean unregisterIgnored = false;
+
+ /**
+ * Construct a <t>ServiceMBeanSupport</tt>.
+ *
+ * <p>
+ * 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 <t>ServiceMBeanSupport</tt>.
+ *
+ * <p>
+ * Sets up logging.
+ *
+ * @param type The class type to determine category name from.
+ */
+ public ServiceMBeanSupport(final Class<?> type) {
+ this(type.getName());
+ }
+
+ /**
+ * Construct a <t>ServiceMBeanSupport</tt>.
+ *
+ * <p>
+ * Sets up logging.
+ *
+ * @param category The logger category name.
+ */
+ public ServiceMBeanSupport(final String category) {
+ this(Logger.getLogger(category));
+ }
+
+ /**
+ * Construct a <t>ServiceMBeanSupport</tt>.
+ *
+ * @param log The logger to use.
+ */
+ public ServiceMBeanSupport(final Logger log) {
+ this.log = log;
+ if (log.isTraceEnabled()) {
+ log.trace("Constructing");
+ }
+ }
+
+ /**
+ * Use the short class name as the default for the service name.
+ *
+ * @return a description of the mbean
+ */
+ public String getName() {
+ final String s = log.getName();
+ final int i = s.lastIndexOf(".");
+ return i != -1 ? s.substring(i + 1, s.length()) : s;
+ }
+
+ public ObjectName getServiceName() {
+ return serviceName;
+ }
+
+ public MBeanServer getServer() {
+ return server;
+ }
+
+ public int getState() {
+ return state;
+ }
+
+ public String getStateString() {
+ return states[state];
+ }
+
+ public Logger getLog() {
+ return log;
+ }
+
+ // /////////////////////////////////////////////////////////////////////////
+ // State Mutators //
+ // /////////////////////////////////////////////////////////////////////////
+
+ public void create() throws Exception {
+ jbossInternalCreate();
+ }
+
+ public void start() throws Exception {
+ jbossInternalStart();
+ }
+
+ public void stop() {
+ try {
+ jbossInternalStop();
+ } catch (Throwable t) {
+ log.warn("Error in stop " + jbossInternalDescription(), t);
+ }
+ }
+
+ public void destroy() {
+ try {
+ jbossInternalDestroy();
+ } catch (Throwable t) {
+ log.warn("Error in destroy " + jbossInternalDescription(), t);
+ }
+ }
+
+ protected String jbossInternalDescription() {
+ if (serviceName != null)
+ return serviceName.toString();
+ else
+ return getName();
+ }
+
+ public void jbossInternalLifecycle(String method) throws Exception {
+ if (method == null)
+ throw new IllegalArgumentException("Null method name");
+
+ if (method.equals("create"))
+ jbossInternalCreate();
+ else if (method.equals("start"))
+ jbossInternalStart();
+ else if (method.equals("stop"))
+ jbossInternalStop();
+ else if (method.equals("destroy"))
+ jbossInternalDestroy();
+ else
+ throw new IllegalArgumentException("Unknown lifecyle method " + method);
+ }
+
+ protected void jbossInternalCreate() throws Exception {
+ // if (state == CREATED || state == STARTING || state == STARTED
+ // || state == STOPPING || state == STOPPED)
+ if (state != REGISTERED) {
+ createIgnored = true;
+ if (log.isDebugEnabled()) {
+ log.debug("Ignoring create call; current state is " + getStateString());
+ }
+ return;
+ }
+
+ createIgnored = false;
+ if (log.isDebugEnabled()) {
+ log.debug("Creating " + jbossInternalDescription());
+ }
+
+ try {
+ createService();
+ state = CREATED;
+ } catch (Exception e) {
+ log.warn("Initialization failed " + jbossInternalDescription(), e);
+ throw e;
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Created " + jbossInternalDescription());
+ }
+
+ if (startIgnored) {
+ start();
+ }
+ }
+
+ protected void jbossInternalStart() throws Exception {
+ if (state != CREATED && state != STOPPED) {
+ startIgnored = true;
+ if (log.isDebugEnabled()) {
+ log.debug("Ignoring start call; current state is " + getStateString());
+ }
+ return;
+ }
+
+ startIgnored = false;
+
+ state = STARTING;
+ if (log.isDebugEnabled()) {
+ log.debug("Starting " + jbossInternalDescription());
+ }
+
+ try {
+ startService();
+ } catch (Exception e) {
+ state = FAILED;
+ log.warn("Starting failed " + jbossInternalDescription(), e);
+ throw e;
+ }
+
+ state = STARTED;
+ if (log.isDebugEnabled()) {
+ log.debug("Started " + jbossInternalDescription());
+ }
+
+ if (stopIgnored) {
+ stop();
+ }
+ }
+
+ protected void jbossInternalStop() {
+ if (state != STARTED) {
+ stopIgnored = true;
+ if (log.isDebugEnabled()) {
+ log.debug("Ignoring stop call; current state is " + getStateString());
+ }
+ return;
+ }
+
+ stopIgnored = false;
+ state = STOPPING;
+ if (log.isDebugEnabled()) {
+ log.debug("Stopping " + jbossInternalDescription());
+ }
+
+ try {
+ stopService();
+ } catch (Throwable e) {
+ state = FAILED;
+ log.warn("Stopping failed " + jbossInternalDescription(), e);
+ return;
+ }
+
+ state = STOPPED;
+ if (log.isDebugEnabled()) {
+ log.debug("Stopped " + jbossInternalDescription());
+ }
+
+ if (destroyIgnored) {
+ destroy();
+ }
+ }
+
+ protected void jbossInternalDestroy() {
+ if (state != STOPPED) {
+ destroyIgnored = true;
+ if (log.isDebugEnabled()) {
+ log.debug("Ignoring destroy call; current state is " + getStateString());
+ }
+ return;
+ }
+
+ destroyIgnored = false;
+ if (log.isDebugEnabled()) {
+ log.debug("Destroying " + jbossInternalDescription());
+ }
+
+ try {
+ destroyService();
+ } catch (Throwable t) {
+ log.warn("Destroying failed " + jbossInternalDescription(), t);
+ }
+ state = DESTROYED;
+ if (log.isDebugEnabled()) {
+ log.debug("Destroyed " + jbossInternalDescription());
+ }
+
+ if (unregisterIgnored) {
+ postDeregister();
+ }
+ }
+
+ // /////////////////////////////////////////////////////////////////////////
+ // JMX Hooks //
+ // /////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Callback method of {@link MBeanRegistration} before the MBean is registered at the JMX Agent.
+ *
+ * <p>
+ * <b>Attention</b>: 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.
+ *
+ * <p>
+ * 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.
+ *
+ * <p>
+ * 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.
+ *
+ * <p>
+ * 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.
+ *
+ * <p>
+ * 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 {
+ }
+
+}
View
118 ...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);
+ }
+
+}
View
124 ...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
+
+ }
+
+}
View
49 .../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);
+
+}
View
67 ...ation/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));
+ }
+
+}
View
34 .../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();
+}
View
28 ...tion/basic/src/test/java/org/jboss/as/test/integration/sar/servicembean/jboss-service.xml
@@ -0,0 +1,28 @@
+<!--
+ ~ 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.
+ -->
+<server xmlns="urn:jboss:service:7.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:service:7.0 jboss-service_7_0.xsd">
+
+ <mbean name="jboss:name=service-mbean-support-test" code="org.jboss.as.test.integration.sar.servicembean.TestService" />
+
+</server>
View
28 ...sic/src/test/java/org/jboss/as/test/integration/sar/servicembean/result-jboss-service.xml
@@ -0,0 +1,28 @@
+<!--
+ ~ 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.
+ -->
+<server xmlns="urn:jboss:service:7.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:service:7.0 jboss-service_7_0.xsd">
+
+ <mbean name="jboss:name=service-mbean-support-test-result" code="org.jboss.as.test.integration.sar.servicembean.TestResultService" />
+
+</server>

0 comments on commit 618fe96

Please sign in to comment.