Java tutorial
/* * Copyright 2013 The Skfiy Open Association. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.skfiy.typhon; import javax.management.MBeanRegistration; import javax.management.MBeanServer; import javax.management.ObjectName; import org.apache.commons.modeler.Registry; /** * MBeanLifecycle.{@code MBeanRegistration}?. * ??MBeanServer. * * @author Kevin Zou <kevinz@skfiy.org> */ public abstract class AbstractMBeanLifecycle extends AbstractLifecycle implements MBeanRegistration { private String domain; private ObjectName oname; /** * ??MBean??. * {@code NULL}??{@link Globals#DEFAULT_MBEAN_DOMAIN}. * * @return {@code NULL}??{@link Globals#DEFAULT_MBEAN_DOMAIN} */ protected abstract String getMBeanDomain(); /** * {@code ObjectName}. * * @return ?{@code NULL} */ protected abstract String getObjectNameKeyProperties(); @Override public ObjectName preRegister(final MBeanServer server, final ObjectName name) throws Exception { this.oname = name; this.domain = name.getDomain(); return oname; } @Override public void postRegister(Boolean registrationDone) { } @Override public void preDeregister() throws Exception { } @Override public void postDeregister() { } @Override protected void initInternal() throws LifecycleException { if (oname == null) { oname = register(this, getObjectNameKeyProperties()); } } @Override protected void destroyInternal() throws LifecycleException { unregister(oname); } /** * ?{@code ObjectName}. * * @return {@code ObjectName} */ protected final ObjectName getObjectName() { return oname; } /** * ??MBean??. * {@code NULL}??{@link Globals#DEFAULT_MBEAN_DOMAIN}. * * @return {@code NULL}??{@link Globals#DEFAULT_MBEAN_DOMAIN} */ protected final String getDomain() { if (domain == null) { domain = getMBeanDomain(); } if (domain == null) { domain = Globals.DEFAULT_MBEAN_DOMAIN; } return domain; } /** * MBean??. * * @param domain MBean?? */ public final void setDomain(final String domain) { this.domain = domain; } /** * MBean. * * @param obj MBeanServer * @param objectNameKeyProperties {@code ObjectName}?? * @return {@code ObjectName} * @throws LifecycleException {@code LifecycleException} */ protected final ObjectName register(final Object obj, final String objectNameKeyProperties) throws LifecycleException { String name = getDomain() + ":" + objectNameKeyProperties; try { ObjectName on = new ObjectName(name); Registry.getRegistry(null, null).registerComponent(obj, on, null); return on; } catch (Exception e) { throw new LifecycleException("" + name + " MBean", e); } } /** * MBean. * * @param on ?{@code ObjectName} */ protected final void unregister(ObjectName on) { // If null ObjectName, just return without complaint if (on == null) { return; } Registry.getRegistry(null, null).unregisterComponent(on); } }