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.jmx.gui;
18  
19  import java.awt.BorderLayout;
20  import java.awt.Color;
21  import java.awt.Dimension;
22  import java.awt.Font;
23  import java.awt.event.ActionEvent;
24  import java.io.PrintWriter;
25  import java.io.StringWriter;
26  
27  import javax.swing.AbstractAction;
28  import javax.swing.Box;
29  import javax.swing.BoxLayout;
30  import javax.swing.JButton;
31  import javax.swing.JLabel;
32  import javax.swing.JOptionPane;
33  import javax.swing.JPanel;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTextArea;
36  import javax.swing.JTextField;
37  
38  import org.apache.logging.log4j.core.jmx.LoggerContextAdminMBean;
39  
40  /**
41   * Panel for editing Log4j configurations.
42   */
43  public class ClientEditConfigPanel extends JPanel {
44      private static final long serialVersionUID = -7544651740950723394L;
45      private static final int HORIZONTAL_GAP = 20;
46      private static final int ERR_MSG_INITIAL_BUFFER_SIZE = 2048;
47      private static final int LOCATION_TEXT_COLS = 50;
48      private static final int CONFIG_TEXT_COLS = 60;
49      private static final int CONFIG_TEXT_ROWS = 20;
50      private static final int BUFFER_SIZE = 2048;
51  
52      private JTextField locationTextField;
53      private JLabel locationLabel;
54      private JButton buttonSendLocation;
55      private JButton buttonSendConfigText;
56      private JTextArea configTextArea;
57      private final LoggerContextAdminMBean contextAdmin;
58  
59      private final AbstractAction actionReconfigureFromLocation = new AbstractAction(
60              "Reconfigure from Location") {
61          private static final long serialVersionUID = 6995219797596745774L;
62  
63          @Override
64          public void actionPerformed(final ActionEvent e) {
65              try {
66                  contextAdmin.setConfigLocationUri(locationTextField.getText());
67                  populateWidgets();
68                  showConfirmation();
69              } catch (final Exception ex) {
70                  populateWidgets();
71                  handle("Could not reconfigure from location", ex);
72              }
73          }
74      };
75      private final AbstractAction actionReconfigureFromText = new AbstractAction(
76              "Reconfigure with XML Below") {
77          private static final long serialVersionUID = -2846103707134292312L;
78  
79          @Override
80          public void actionPerformed(final ActionEvent e) {
81              final String encoding = System.getProperty("file.encoding");
82              try {
83                  contextAdmin.setConfigText(configTextArea.getText(), encoding);
84                  populateWidgets();
85                  showConfirmation();
86              } catch (final Exception ex) {
87                  populateWidgets();
88                  handle("Could not reconfigure from XML", ex);
89              }
90          }
91      };
92  
93      public ClientEditConfigPanel(final LoggerContextAdminMBean contextAdmin) {
94          this.contextAdmin = contextAdmin;
95          createWidgets();
96          populateWidgets();
97      }
98  
99      private void handle(final String msg, final Exception ex) {
100         final StringWriter sr = new StringWriter(BUFFER_SIZE);
101         final PrintWriter pw = new PrintWriter(sr);
102         pw.println("Please check the StatusLogger tab for details");
103         pw.println();
104         ex.printStackTrace(pw);
105         JOptionPane.showMessageDialog(this, sr.toString(), msg,
106                 JOptionPane.ERROR_MESSAGE);
107     }
108 
109     private void showConfirmation() {
110         JOptionPane.showMessageDialog(this, "Reconfiguration complete.",
111                 "Reconfiguration complete", JOptionPane.INFORMATION_MESSAGE);
112     }
113 
114     private void populateWidgets() {
115         try {
116             configTextArea.setText(contextAdmin.getConfigText());
117         } catch (final Exception ex) {
118             final StringWriter sw = new StringWriter(ERR_MSG_INITIAL_BUFFER_SIZE);
119             ex.printStackTrace(new PrintWriter(sw));
120             configTextArea.setText(sw.toString());
121         }
122         final String uri = contextAdmin.getConfigLocationUri();
123         locationTextField.setText(uri);
124     }
125 
126     private void createWidgets() {
127         configTextArea = new JTextArea(CONFIG_TEXT_ROWS, CONFIG_TEXT_COLS);
128         // configTextArea.setEditable(false);
129         configTextArea.setBackground(Color.white);
130         configTextArea.setForeground(Color.black);
131         configTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, configTextArea.getFont().getSize()));
132         final JScrollPane scrollConfig = new JScrollPane(configTextArea);
133 
134         locationTextField = new JTextField(LOCATION_TEXT_COLS);
135         locationLabel = new JLabel("Location: ");
136         locationLabel.setLabelFor(locationTextField);
137         buttonSendLocation = new JButton(actionReconfigureFromLocation);
138         buttonSendConfigText = new JButton(actionReconfigureFromText);
139 
140         final JPanel north = new JPanel();
141         north.setLayout(new BoxLayout(north, BoxLayout.LINE_AXIS));
142         north.add(locationLabel);
143         north.add(locationTextField);
144         north.add(buttonSendLocation);
145         north.add(Box.createRigidArea(new Dimension(HORIZONTAL_GAP, 0)));
146         north.add(buttonSendConfigText);
147 
148         this.setLayout(new BorderLayout());
149         this.add(north, BorderLayout.NORTH);
150         this.add(scrollConfig, BorderLayout.CENTER);
151     }
152 }