001/* 002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Stephane Lacoin 018 */ 019package org.nuxeo.ecm.core.management.standby; 020 021import javax.management.JMException; 022import javax.management.MBeanServer; 023import javax.management.ObjectInstance; 024 025import org.nuxeo.runtime.api.Framework; 026import org.nuxeo.runtime.management.ObjectNameFactory; 027import org.nuxeo.runtime.model.ComponentManager; 028 029public class StandbyCommand implements StandbyMXBean { 030 @Override 031 public void standby(int delay) throws InterruptedException { 032 ComponentManager mgr = Framework.getRuntime().getComponentManager(); 033 if (mgr.isStandby()) { 034 return; 035 } 036 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 037 Thread.currentThread().setContextClassLoader(Framework.class.getClassLoader()); 038 try { 039 mgr.standby(delay); 040 } finally { 041 Thread.currentThread().setContextClassLoader(loader); 042 } 043 } 044 045 @Override 046 public void resume() { 047 ComponentManager mgr = Framework.getRuntime().getComponentManager(); 048 if (!mgr.isStandby()) { 049 return; 050 } 051 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 052 Thread.currentThread().setContextClassLoader(Framework.class.getClassLoader()); 053 try { 054 mgr.resume(); 055 } finally { 056 Thread.currentThread().setContextClassLoader(loader); 057 } 058 } 059 060 @Override 061 public boolean isStandby() { 062 return Framework.getRuntime().getComponentManager().isStandby(); 063 } 064 065 protected final Registration registration = new Registration(); 066 067 protected class Registration { 068 069 protected MBeanServer server; 070 071 protected ObjectInstance instance; 072 073 protected Registration with(MBeanServer server) { 074 this.server = server; 075 return this; 076 } 077 078 protected Registration register() throws JMException { 079 instance = server.registerMBean(StandbyCommand.this, ObjectNameFactory.getObjectName(StandbyCommand.class.getName())); 080 return this; 081 } 082 083 protected void unregister() throws JMException { 084 try { 085 server.unregisterMBean(instance.getObjectName()); 086 } finally { 087 instance = null; 088 } 089 } 090 } 091}