001/* 002 * (C) Copyright 2012-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 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.management.jtajca.internal; 020 021import java.util.HashMap; 022import java.util.Map; 023 024import javax.management.InstanceAlreadyExistsException; 025import javax.management.InstanceNotFoundException; 026import javax.management.MBeanRegistrationException; 027import javax.management.MBeanServer; 028import javax.management.MalformedObjectNameException; 029import javax.management.NotCompliantMBeanException; 030import javax.management.ObjectInstance; 031import javax.management.ObjectName; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.ecm.core.api.CoreInstance; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.management.jtajca.ConnectionPoolMonitor; 038import org.nuxeo.ecm.core.management.jtajca.CoreSessionMonitor; 039import org.nuxeo.ecm.core.management.jtajca.Defaults; 040import org.nuxeo.ecm.core.management.jtajca.TransactionMonitor; 041import org.nuxeo.ecm.core.repository.RepositoryService; 042import org.nuxeo.runtime.api.Framework; 043import org.nuxeo.runtime.jtajca.NuxeoConnectionManager; 044import org.nuxeo.runtime.jtajca.NuxeoContainer; 045import org.nuxeo.runtime.jtajca.NuxeoContainerListener; 046import org.nuxeo.runtime.management.ServerLocator; 047import org.nuxeo.runtime.metrics.MetricsService; 048import org.nuxeo.runtime.model.Component; 049import org.nuxeo.runtime.model.ComponentContext; 050import org.nuxeo.runtime.model.DefaultComponent; 051 052/** 053 * Component used to install/uninstall the monitors (transaction and connections). 054 * 055 * @since 5.6 056 */ 057public class DefaultMonitorComponent extends DefaultComponent { 058 059 private final ConnectionManagerUpdater cmUpdater = new ConnectionManagerUpdater(); 060 061 private class ConnectionManagerUpdater implements NuxeoContainerListener { 062 @Override 063 public void handleNewConnectionManager(String name, NuxeoConnectionManager cm) { 064 ConnectionPoolMonitor monitor = new DefaultConnectionPoolMonitor(name, cm); 065 monitor.install(); 066 poolConnectionMonitors.put(name, monitor); 067 } 068 069 @Override 070 public void handleConnectionManagerReset(String name, NuxeoConnectionManager cm) { 071 DefaultConnectionPoolMonitor monitor = (DefaultConnectionPoolMonitor) poolConnectionMonitors.get(name); 072 monitor.handleNewConnectionManager(cm); 073 } 074 075 @Override 076 public void handleConnectionManagerDispose(String name, NuxeoConnectionManager mgr) { 077 ConnectionPoolMonitor monitor = poolConnectionMonitors.remove(name); 078 monitor.uninstall(); 079 } 080 081 } 082 083 protected final Log log = LogFactory.getLog(DefaultMonitorComponent.class); 084 085 protected CoreSessionMonitor coreSessionMonitor; 086 087 protected TransactionMonitor transactionMonitor; 088 089 protected Map<String, ConnectionPoolMonitor> poolConnectionMonitors = new HashMap<>(); 090 091 @Override 092 public void start(ComponentContext context) { 093 RepositoryService repositoryService = Framework.getService(RepositoryService.class); 094 if (repositoryService == null) { 095 // RepositoryService failed to start, no need to go further 096 return; 097 } 098 uninstall(); 099 install(); 100 } 101 102 @Override 103 public int getApplicationStartedOrder() { 104 // should deploy after metrics service 105 Component component = (Component) Framework.getRuntime().getComponent(MetricsService.class.getName()); 106 return component.getApplicationStartedOrder() + 1; 107 } 108 109 @Override 110 public void stop(ComponentContext context) { 111 uninstall(); 112 } 113 114 protected boolean installed; 115 116 protected void install() { 117 installed = true; 118 119 coreSessionMonitor = new DefaultCoreSessionMonitor(); 120 coreSessionMonitor.install(); 121 122 transactionMonitor = new DefaultTransactionMonitor(); 123 transactionMonitor.install(); 124 125 NuxeoContainer.addListener(cmUpdater); 126 } 127 128 /** 129 * Make sure we open the repository, to initialize its connection manager. 130 */ 131 protected void activateRepository(String repositoryName) { 132 try (CoreSession session = CoreInstance.openCoreSessionSystem(repositoryName)) { 133 // do nothing, just open and close 134 } 135 } 136 137 protected void uninstall() { 138 if (!installed) { 139 return; 140 } 141 // temporary log to help diagnostics 142 log.info("Total commits during server life: " + transactionMonitor.getTotalCommits()); 143 installed = false; 144 NuxeoContainer.removeListener(cmUpdater); 145 for (ConnectionPoolMonitor storage : poolConnectionMonitors.values()) { 146 storage.uninstall(); 147 } 148 coreSessionMonitor.uninstall(); 149 transactionMonitor.uninstall(); 150 poolConnectionMonitors.clear(); 151 coreSessionMonitor = null; 152 transactionMonitor = null; 153 } 154 155 public static class ServerInstance { 156 public final MBeanServer server; 157 158 public final ObjectName name; 159 160 ServerInstance(MBeanServer server, ObjectName name) { 161 this.server = server; 162 this.name = name; 163 } 164 } 165 166 protected static ServerInstance bind(Object managed) { 167 return bind(managed, "default"); 168 } 169 170 protected static ServerInstance bind(Class<?> itf, Object managed) { 171 return bind(itf, managed, "default"); 172 } 173 174 protected static ServerInstance bind(Object managed, String name) { 175 return bind(managed.getClass().getInterfaces()[0], managed, name); 176 } 177 178 protected static ServerInstance bind(Class<?> itf, Object managed, String name) { 179 MBeanServer mbs = Framework.getService(ServerLocator.class).lookupServer(); 180 name = Defaults.instance.name(itf, name); 181 try { 182 ObjectInstance oi = mbs.registerMBean(managed, new ObjectName(name)); 183 return new ServerInstance(mbs, oi.getObjectName()); 184 } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException 185 | MalformedObjectNameException e) { 186 throw new UnsupportedOperationException("Cannot bind " + managed + " on " + name, e); 187 } 188 } 189 190 protected static void unbind(ServerInstance instance) { 191 try { 192 instance.server.unregisterMBean(instance.name); 193 } catch (MBeanRegistrationException | InstanceNotFoundException e) { 194 LogFactory.getLog(DefaultMonitorComponent.class).error("Cannot unbind " + instance, e); 195 } 196 } 197 198}