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.core.jmx;
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    /**
039     * Panel for editing Log4J configurations.
040     */
041    public class ClientEditConfigPanel extends JPanel {
042        private static final long serialVersionUID = -7544651740950723394L;
043        private static final int LOCATION_TEXT_COLS = 50;
044        private static final int CONFIG_TEXT_COLS = 60;
045        private static final int CONFIG_TEXT_ROWS = 20;
046        private static final int BUFFER_SIZE = 2048;
047        
048        private JTextField locationTextField;
049        private JLabel locationLabel;
050        private JButton buttonSendLocation;
051        private JButton buttonSendConfigText;
052        private JTextArea configTextArea;
053        private LoggerContextAdminMBean contextAdmin;
054    
055        private AbstractAction actionReconfigureFromLocation = new AbstractAction(
056                "Reconfigure from Location") {
057            private static final long serialVersionUID = 6995219797596745774L;
058    
059            @Override
060            public void actionPerformed(ActionEvent e) {
061                try {
062                    contextAdmin.setConfigLocationURI(locationTextField.getText());
063                    populateWidgets();
064                    showConfirmation();
065                } catch (Exception ex) {
066                    populateWidgets();
067                    handle("Could not reconfigure from location", ex);
068                }
069            }
070        };
071        private AbstractAction actionReconfigureFromText = new AbstractAction(
072                "Reconfigure with XML Below") {
073            private static final long serialVersionUID = -2846103707134292312L;
074    
075            @Override
076            public void actionPerformed(ActionEvent e) {
077                String encoding = System.getProperty("file.encoding");
078                try {
079                    contextAdmin.setConfigText(configTextArea.getText(), encoding);
080                    populateWidgets();
081                    showConfirmation();
082                } catch (Exception ex) {
083                    populateWidgets();
084                    handle("Could not reconfigure from XML", ex);
085                }
086            }
087        };
088    
089        private void handle(String msg, Exception ex) {
090            StringWriter sr = new StringWriter(BUFFER_SIZE);
091            PrintWriter pw = new PrintWriter(sr);
092            pw.println("Please check the StatusLogger tab for details");
093            pw.println();
094            ex.printStackTrace(pw);
095            JOptionPane.showMessageDialog(this, sr.toString(), msg,
096                    JOptionPane.ERROR_MESSAGE);
097        }
098    
099        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    }