001/*
002 * JPPF.
003 * Copyright (C) 2005-2019 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.logging.jdk;
020
021import java.util.logging.*;
022
023import org.jppf.logging.jmx.JmxMessageNotifier;
024
025/**
026 * A handler that prints log messages as JMX notifications.
027 * @author Laurent Cohen
028 */
029public class JmxHandler extends Handler {
030  /**
031   * The notifier that sends formatted log messages as JMX notifications.
032   */
033  private JmxMessageNotifier notifier = null;
034  /**
035   * The name of the mbean that sends messages as JMX notifications.
036   */
037  private String mbeanName = null;
038
039  /**
040   * Initialize this appender from its configuration.
041   */
042  private void init() {
043    final LogManager lm = LogManager.getLogManager();
044    mbeanName = lm.getProperty(getClass().getName() + ".mbeanName");
045    notifier = new JmxMessageNotifier(mbeanName);
046  }
047
048  /**
049   * {@inheritDoc}
050   */
051  @Override
052  public void publish(final LogRecord record) {
053    if (notifier == null) init();
054    Formatter f = getFormatter();
055    if (f == null) f = new JPPFLogFormatter();
056    final String s = f.format(record);
057    notifier.sendMessage(s);
058  }
059
060  /**
061   * {@inheritDoc}
062   */
063  @Override
064  public void flush() {
065  }
066
067  /**
068   * {@inheritDoc}
069   */
070  @Override
071  public void close() throws SecurityException {
072  }
073}