001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transport; 018 019import java.io.IOException; 020 021import javax.management.ObjectName; 022 023import org.apache.activemq.broker.jmx.AnnotatedMBean; 024import org.apache.activemq.broker.jmx.ManagementContext; 025import org.apache.activemq.util.IOExceptionSupport; 026import org.apache.activemq.util.LogWriterFinder; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030/** 031 * Singleton class to create TransportLogger objects. 032 * When the method getInstance() is called for the first time, 033 * a TransportLoggerControlMBean is created and registered. 034 * This MBean permits enabling and disabling the logging for 035 * all TransportLogger objects at once. 036 * 037 * @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com 038 * 039 * @see TransportLoggerControlMBean 040 */ 041public class TransportLoggerFactory { 042 043 private static final Logger LOG = LoggerFactory.getLogger(TransportLoggerFactory.class); 044 045 private static TransportLoggerFactory instance; 046 private static int lastId=0; 047 private static final LogWriterFinder logWriterFinder = new LogWriterFinder("META-INF/services/org/apache/activemq/transport/logwriters/"); 048 049 /** 050 * LogWriter that will be used if none is specified. 051 */ 052 public static String defaultLogWriterName = "default"; 053 /** 054 * If transport logging is enabled, it will be possible to control 055 * the transport loggers or not based on this value 056 */ 057 private static boolean defaultDynamicManagement = false; 058 /** 059 * If transport logging is enabled, the transport loggers will initially 060 * output or not depending on this value. 061 * This setting only has a meaning if 062 */ 063 private static boolean defaultInitialBehavior = true; 064 /** 065 * Default port to control the transport loggers through JMX 066 */ 067 private static int defaultJmxPort = 1099; 068 069 private boolean transportLoggerControlCreated = false; 070 private ManagementContext managementContext; 071 private ObjectName objectName; 072 073 /** 074 * Private constructor. 075 */ 076 private TransportLoggerFactory() { 077 } 078 079 /** 080 * Returns a TransportLoggerFactory object which can be used to create TransportLogger objects. 081 * @return a TransportLoggerFactory object 082 */ 083 public static synchronized TransportLoggerFactory getInstance() { 084 if (instance == null) { 085 instance = new TransportLoggerFactory(); 086 } 087 return instance; 088 } 089 090 public void stop() { 091 try { 092 if (this.transportLoggerControlCreated) { 093 this.managementContext.unregisterMBean(this.objectName); 094 this.managementContext.stop(); 095 this.managementContext = null; 096 } 097 } catch (Exception e) { 098 LOG.error("TransportLoggerFactory could not be stopped, reason: " + e, e); 099 } 100 101 } 102 103 /** 104 * Creates a TransportLogger object, that will be inserted in the Transport Stack. 105 * Uses the default initial behavior, the default log writer, and creates a new 106 * log4j object to be used by the TransportLogger. 107 * @param next The next Transport layer in the Transport stack. 108 * @return A TransportLogger object. 109 * @throws IOException 110 */ 111 public TransportLogger createTransportLogger(Transport next) throws IOException { 112 int id = getNextId(); 113 return createTransportLogger(next, id, createLog(id), defaultLogWriterName, defaultDynamicManagement, defaultInitialBehavior, defaultJmxPort); 114 } 115 116 /** 117 * Creates a TransportLogger object, that will be inserted in the Transport Stack. 118 * Uses the default initial behavior and the default log writer. 119 * @param next The next Transport layer in the Transport stack. 120 * @param log The log4j log that will be used by the TransportLogger. 121 * @return A TransportLogger object. 122 * @throws IOException 123 */ 124 public TransportLogger createTransportLogger(Transport next, Logger log) throws IOException { 125 return createTransportLogger(next, getNextId(), log, defaultLogWriterName, defaultDynamicManagement, defaultInitialBehavior, defaultJmxPort); 126 } 127 128 /** 129 * Creates a TransportLogger object, that will be inserted in the Transport Stack. 130 * Creates a new log4j object to be used by the TransportLogger. 131 * @param next The next Transport layer in the Transport stack. 132 * @param startLogging Specifies if this TransportLogger should be initially active or not. 133 * @param logWriterName The name or the LogWriter to be used. Different log writers can output 134 * logs with a different format. 135 * @return A TransportLogger object. 136 * @throws IOException 137 */ 138 public TransportLogger createTransportLogger(Transport next, String logWriterName, 139 boolean useJmx, boolean startLogging, int jmxport) throws IOException { 140 int id = getNextId(); 141 return createTransportLogger(next, id, createLog(id), logWriterName, useJmx, startLogging, jmxport); 142 } 143 144 145 146 /** 147 * Creates a TransportLogger object, that will be inserted in the Transport Stack. 148 * @param next The next Transport layer in the Transport stack. 149 * @param id The id of the transport logger. 150 * @param log The log4j log that will be used by the TransportLogger. 151 * @param logWriterName The name or the LogWriter to be used. Different log writers can output 152 * @param dynamicManagement Specifies if JMX will be used to switch on/off the TransportLogger to be created. 153 * @param startLogging Specifies if this TransportLogger should be initially active or not. Only has a meaning if 154 * dynamicManagement = true. 155 * @param jmxport the port to be used by the JMX server. It should only be different from 1099 (broker's default JMX port) 156 * when it's a client that is using Transport Logging. In a broker, if the port is different from 1099, 2 JMX servers will 157 * be created, both identical, with all the MBeans. 158 * @return A TransportLogger object. 159 * @throws IOException 160 */ 161 public TransportLogger createTransportLogger(Transport next, int id, Logger log, 162 String logWriterName, boolean dynamicManagement, boolean startLogging, int jmxport) throws IOException { 163 try { 164 LogWriter logWriter = logWriterFinder.newInstance(logWriterName); 165 TransportLogger tl = new TransportLogger (next, log, startLogging, logWriter); 166 if (dynamicManagement) { 167 synchronized (this) { 168 if (!this.transportLoggerControlCreated) { 169 this.createTransportLoggerControl(jmxport); 170 } 171 } 172 TransportLoggerView tlv = new TransportLoggerView(tl, next.toString(), id, this.managementContext); 173 tl.setView(tlv); 174 } 175 return tl; 176 } catch (Throwable e) { 177 throw IOExceptionSupport.create("Could not create log writer object for: " + logWriterName + ", reason: " + e, e); 178 } 179 } 180 181 synchronized private static int getNextId() { 182 return ++lastId; 183 } 184 185 private static Logger createLog(int id) { 186 return LoggerFactory.getLogger(TransportLogger.class.getName()+".Connection:" + id); 187 } 188 189 /** 190 * Starts the management context. 191 * Creates and registers a TransportLoggerControl MBean which enables the user 192 * to enable/disable logging for all transport loggers at once. 193 */ 194 private void createTransportLoggerControl(int port) { 195 try { 196 this.managementContext = new ManagementContext(); 197 this.managementContext.setConnectorPort(port); 198 this.managementContext.start(); 199 } catch (Exception e) { 200 LOG.error("Management context could not be started, reason: " + e, e); 201 } 202 203 try { 204 this.objectName = new ObjectName(this.managementContext.getJmxDomainName()+":"+ "Type=TransportLoggerControl"); 205 AnnotatedMBean.registerMBean(this.managementContext, new TransportLoggerControl(this.managementContext),this.objectName); 206 207 this.transportLoggerControlCreated = true; 208 209 } catch (Exception e) { 210 LOG.error("TransportLoggerControlMBean could not be registered, reason: " + e, e); 211 } 212 } 213 214}