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