View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.jmx;
18  
19  import java.util.List;
20  import java.util.concurrent.Executor;
21  import java.util.concurrent.atomic.AtomicLong;
22  
23  import javax.management.MBeanNotificationInfo;
24  import javax.management.Notification;
25  import javax.management.NotificationBroadcasterSupport;
26  import javax.management.ObjectName;
27  
28  import org.apache.logging.log4j.Level;
29  import org.apache.logging.log4j.status.StatusData;
30  import org.apache.logging.log4j.status.StatusListener;
31  import org.apache.logging.log4j.status.StatusLogger;
32  
33  /**
34   * Implementation of the {@code StatusLoggerAdminMBean} interface.
35   */
36  public class StatusLoggerAdmin extends NotificationBroadcasterSupport implements
37          StatusListener, StatusLoggerAdminMBean {
38  
39      private final AtomicLong sequenceNo = new AtomicLong();
40      private final ObjectName objectName;
41  
42      /**
43       * Constructs a new {@code StatusLoggerAdmin} with the {@code Executor} to
44       * be used for sending {@code Notification}s asynchronously to listeners.
45       * 
46       * @param executor
47       */
48      public StatusLoggerAdmin(Executor executor) {
49          super(executor, createNotificationInfo());
50          try {
51              objectName = new ObjectName(NAME);
52          } catch (Exception e) {
53              throw new IllegalStateException(e);
54          }
55          StatusLogger.getLogger().registerListener(this);
56      }
57  
58      private static MBeanNotificationInfo createNotificationInfo() {
59          String[] notifTypes = new String[] { NOTIF_TYPE_DATA,
60                  NOTIF_TYPE_MESSAGE };
61          String name = Notification.class.getName();
62          String description = "StatusLogger has logged an event";
63          return new MBeanNotificationInfo(notifTypes, name, description);
64      }
65  
66      @Override
67      public String[] getStatusDataHistory() {
68          List<StatusData> data = getStatusData();
69          String[] result = new String[data.size()];
70          for (int i = 0; i < result.length; i++) {
71              result[i] = data.get(i).getFormattedStatus();
72          }
73          return result;
74      }
75  
76      @Override
77      public List<StatusData> getStatusData() {
78          return StatusLogger.getLogger().getStatusData();
79      }
80  
81      @Override
82      public String getLevel() {
83          return StatusLogger.getLogger().getLevel().name();
84      }
85  
86      @Override
87      public void setLevel(String level) {
88          StatusLogger.getLogger().setLevel(Level.valueOf(level));
89      }
90  
91      /*
92       * (non-Javadoc)
93       * 
94       * @see
95       * org.apache.logging.log4j.status.StatusListener#log(org.apache.logging
96       * .log4j.status.StatusData)
97       */
98      @Override
99      public void log(StatusData data) {
100         Notification notifMsg = new Notification(NOTIF_TYPE_MESSAGE,
101                 getObjectName(), nextSeqNo(), now(), data.getFormattedStatus());
102         sendNotification(notifMsg);
103 
104         Notification notifData = new Notification(NOTIF_TYPE_DATA,
105                 getObjectName(), nextSeqNo(), now());
106         notifData.setUserData(data);
107         sendNotification(notifData);
108     }
109 
110     /** @see StatusLoggerAdminMBean#NAME */
111     public ObjectName getObjectName() {
112         return objectName;
113     }
114 
115     private long nextSeqNo() {
116         return sequenceNo.getAndIncrement();
117     }
118 
119     private long now() {
120         return System.currentTimeMillis();
121     }
122 }