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 */
017 package org.apache.logging.log4j.jmx.gui;
018
019 import java.awt.BorderLayout;
020 import java.awt.Color;
021 import java.awt.Dimension;
022 import java.awt.Font;
023 import java.awt.event.ActionEvent;
024 import java.io.PrintWriter;
025 import java.io.StringWriter;
026
027 import javax.swing.AbstractAction;
028 import javax.swing.Box;
029 import javax.swing.BoxLayout;
030 import javax.swing.JButton;
031 import javax.swing.JLabel;
032 import javax.swing.JOptionPane;
033 import javax.swing.JPanel;
034 import javax.swing.JScrollPane;
035 import javax.swing.JTextArea;
036 import javax.swing.JTextField;
037
038 import org.apache.logging.log4j.core.jmx.LoggerContextAdminMBean;
039
040 /**
041 * Panel for editing Log4j configurations.
042 */
043 public class ClientEditConfigPanel extends JPanel {
044 private static final long serialVersionUID = -7544651740950723394L;
045 private static final int HORIZONTAL_GAP = 20;
046 private static final int ERR_MSG_INITIAL_BUFFER_SIZE = 2048;
047 private static final int LOCATION_TEXT_COLS = 50;
048 private static final int CONFIG_TEXT_COLS = 60;
049 private static final int CONFIG_TEXT_ROWS = 20;
050 private static final int BUFFER_SIZE = 2048;
051
052 private JTextField locationTextField;
053 private JLabel locationLabel;
054 private JButton buttonSendLocation;
055 private JButton buttonSendConfigText;
056 private JTextArea configTextArea;
057 private final LoggerContextAdminMBean contextAdmin;
058
059 private final AbstractAction actionReconfigureFromLocation = new AbstractAction(
060 "Reconfigure from Location") {
061 private static final long serialVersionUID = 6995219797596745774L;
062
063 @Override
064 public void actionPerformed(final ActionEvent e) {
065 try {
066 contextAdmin.setConfigLocationUri(locationTextField.getText());
067 populateWidgets();
068 showConfirmation();
069 } catch (final Exception ex) {
070 populateWidgets();
071 handle("Could not reconfigure from location", ex);
072 }
073 }
074 };
075 private final AbstractAction actionReconfigureFromText = new AbstractAction(
076 "Reconfigure with XML Below") {
077 private static final long serialVersionUID = -2846103707134292312L;
078
079 @Override
080 public void actionPerformed(final ActionEvent e) {
081 final String encoding = System.getProperty("file.encoding");
082 try {
083 contextAdmin.setConfigText(configTextArea.getText(), encoding);
084 populateWidgets();
085 showConfirmation();
086 } catch (final Exception ex) {
087 populateWidgets();
088 handle("Could not reconfigure from XML", ex);
089 }
090 }
091 };
092
093 public ClientEditConfigPanel(final LoggerContextAdminMBean contextAdmin) {
094 this.contextAdmin = contextAdmin;
095 createWidgets();
096 populateWidgets();
097 }
098
099 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 }