001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.logfile;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.OpenCms;
032import org.opencms.ui.A_CmsUI;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.FontOpenCms;
035import org.opencms.ui.apps.A_CmsWorkplaceApp;
036import org.opencms.ui.apps.I_CmsAppUIContext;
037import org.opencms.ui.apps.Messages;
038import org.opencms.ui.components.CmsBasicDialog;
039import org.opencms.ui.components.CmsToolBar;
040import org.opencms.util.CmsStringUtil;
041
042import java.io.File;
043import java.lang.reflect.Method;
044import java.util.LinkedHashMap;
045import java.util.List;
046
047import org.apache.commons.logging.Log;
048import org.apache.logging.log4j.core.Appender;
049
050import com.vaadin.data.HasValue.ValueChangeEvent;
051import com.vaadin.data.HasValue.ValueChangeListener;
052import com.vaadin.shared.ui.ValueChangeMode;
053import com.vaadin.ui.Button;
054import com.vaadin.ui.Button.ClickEvent;
055import com.vaadin.ui.Button.ClickListener;
056import com.vaadin.ui.Component;
057import com.vaadin.ui.TextField;
058import com.vaadin.ui.UI;
059import com.vaadin.ui.Window;
060import com.vaadin.ui.Window.CloseEvent;
061import com.vaadin.ui.Window.CloseListener;
062import com.vaadin.ui.themes.ValoTheme;
063
064/**
065 * Main class of Log managment app.<p>
066 */
067public class CmsLogFileApp extends A_CmsWorkplaceApp {
068
069    /**Log folder path.*/
070    protected static final String LOG_FOLDER = OpenCms.getSystemInfo().getLogFileRfsPath() == null
071    ? ""
072    : OpenCms.getSystemInfo().getLogFileRfsPath().substring(
073        0,
074        OpenCms.getSystemInfo().getLogFileRfsPath().lastIndexOf(File.separatorChar) + 1);
075
076    /**Path to channel settings view.*/
077    static String PATH_LOGCHANNEL = "log-channel";
078
079    /**Logger.*/
080    private static Log LOG = CmsLog.getLog(CmsLogFileApp.class);
081
082    /**The log file view layout.*/
083    protected CmsLogFileView m_fileView;
084
085    /** The file table filter input. */
086    private TextField m_tableFilter;
087
088    /**
089     * Returns the file name or <code>null</code> associated with the given appender.<p>
090     *
091     * @param app the appender
092     *
093     * @return the file name
094     */
095    protected static String getFileName(Appender app) {
096
097        String result = null;
098        Method getFileName;
099        try {
100            getFileName = app.getClass().getDeclaredMethod("getFileName", (Class<?>[])null);
101
102            result = (String)getFileName.invoke(app, (Object[])null);
103        } catch (Exception e) {
104            LOG.warn(e.getLocalizedMessage(), e);
105        }
106        return result;
107    }
108
109    /**
110     * Checks whether the given log appender has a getFileName method to identify file based appenders.<p>
111     * As since log4j 2.0 file appenders don't have one common super class that allows access to the file name,
112     * but they all implement a method 'getFileName'.<p>
113     *
114     * @param appender the appender to check
115     *
116     * @return in case of a file based appender
117     */
118    protected static boolean isFileAppender(Appender appender) {
119
120        boolean result = false;
121        try {
122            Method getFileNameMethod = appender.getClass().getDeclaredMethod("getFileName", (Class<?>[])null);
123            result = getFileNameMethod != null;
124
125        } catch (Exception e) {
126            LOG.debug(e.getLocalizedMessage(), e);
127        }
128        return result;
129    }
130
131    /**
132     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#initUI(org.opencms.ui.apps.I_CmsAppUIContext)
133     */
134    @Override
135    public void initUI(I_CmsAppUIContext context) {
136
137        context.addPublishButton(updatedItems -> {
138            // nothing to do
139        });
140        super.initUI(context);
141    }
142
143    /**
144     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getBreadCrumbForState(java.lang.String)
145     */
146    @Override
147    protected LinkedHashMap<String, String> getBreadCrumbForState(String state) {
148
149        LinkedHashMap<String, String> crumbs = new LinkedHashMap<String, String>();
150
151        //Check if state is empty -> start
152        if (CmsStringUtil.isEmptyOrWhitespaceOnly(state)) {
153            crumbs.put("", CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_VIEW_TOOL_NAME_0));
154            return crumbs;
155        }
156        if (state.equals(PATH_LOGCHANNEL)) {
157            crumbs.put(
158                CmsLogFileConfiguration.APP_ID,
159                CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_VIEW_TOOL_NAME_0));
160            crumbs.put("", CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_LOGSETTINGS_TOOL_NAME_0));
161            return crumbs;
162        }
163
164        return new LinkedHashMap<String, String>();
165    }
166
167    /**
168     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getComponentForState(java.lang.String)
169     */
170    @Override
171    protected Component getComponentForState(String state) {
172
173        if (m_tableFilter != null) {
174            m_infoLayout.removeComponent(m_tableFilter);
175            m_tableFilter = null;
176        }
177
178        if (state.isEmpty()) {
179            m_rootLayout.setMainHeightFull(true);
180            m_fileView = new CmsLogFileView(this);
181            addDownloadButton(m_fileView);
182            addSettingsButton();
183            addChannelButton();
184            addRefreshButton();
185            return m_fileView;
186        }
187
188        m_uiContext.clearToolbarButtons();
189
190        if (state.equals(PATH_LOGCHANNEL)) {
191            m_rootLayout.setMainHeightFull(true);
192            final CmsLogChannelTable channelTable = new CmsLogChannelTable();
193            m_tableFilter = new TextField();
194            m_tableFilter.setIcon(FontOpenCms.FILTER);
195            m_tableFilter.setPlaceholder(
196                Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
197            m_tableFilter.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
198            m_tableFilter.setWidth("200px");
199            m_tableFilter.setValueChangeMode(ValueChangeMode.TIMEOUT);
200            m_tableFilter.setValueChangeTimeout(400);
201            m_tableFilter.addValueChangeListener(new ValueChangeListener<String>() {
202
203                private static final long serialVersionUID = 1L;
204
205                public void valueChange(ValueChangeEvent<String> event) {
206
207                    channelTable.filterTable(event.getValue());
208                }
209            });
210
211            m_infoLayout.addComponent(m_tableFilter);
212            return channelTable;
213        }
214
215        return null;
216    }
217
218    /**
219     * @see org.opencms.ui.apps.A_CmsWorkplaceApp#getSubNavEntries(java.lang.String)
220     */
221    @Override
222    protected List<NavEntry> getSubNavEntries(String state) {
223
224        return null;
225    }
226
227    /**
228     * Button to open channel settings path.<p>
229     */
230    private void addChannelButton() {
231
232        Button button = CmsToolBar.createButton(
233            FontOpenCms.LOG,
234            CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_LOGSETTINGS_TOOL_NAME_0));
235        button.addClickListener(new ClickListener() {
236
237            private static final long serialVersionUID = 1L;
238
239            public void buttonClick(ClickEvent event) {
240
241                openSubView(PATH_LOGCHANNEL, true);
242            }
243        });
244        m_uiContext.addToolbarButton(button);
245
246    }
247
248    /**
249     * Adds the download button.
250     *
251     * @param view layout which displays the log file
252     */
253    private void addDownloadButton(final CmsLogFileView view) {
254
255        Button button = CmsToolBar.createButton(
256            FontOpenCms.DOWNLOAD,
257            CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_DOWNLOAD_0));
258        button.addClickListener(new ClickListener() {
259
260            private static final long serialVersionUID = 1L;
261
262            public void buttonClick(ClickEvent event) {
263
264                Window window = CmsBasicDialog.prepareWindow(CmsBasicDialog.DialogWidth.wide);
265                window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_DOWNLOAD_0));
266                window.setContent(new CmsLogDownloadDialog(window, view.getCurrentFile()));
267                A_CmsUI.get().addWindow(window);
268            }
269        });
270        m_uiContext.addToolbarButton(button);
271    }
272
273    /**
274     * Button to refresh the file view.<p>
275     */
276    private void addRefreshButton() {
277
278        Button button = CmsToolBar.createButton(
279            FontOpenCms.RESET,
280            CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_REFRESH_FILEVIEW_0));
281        button.addClickListener(new ClickListener() {
282
283            private static final long serialVersionUID = 1L;
284
285            public void buttonClick(ClickEvent event) {
286
287                m_fileView.updateView();
288
289            }
290        });
291        m_uiContext.addToolbarButton(button);
292    }
293
294    /**
295     * Button to open log file view settings dialog.<p>
296     */
297    private void addSettingsButton() {
298
299        Button button = CmsToolBar.createButton(
300            FontOpenCms.SETTINGS,
301            CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_LOGSETTINGS_TOOL_NAME_SHORT_0));
302        button.addClickListener(new ClickListener() {
303
304            private static final long serialVersionUID = 1L;
305
306            public void buttonClick(ClickEvent event) {
307
308                Window window = CmsBasicDialog.prepareWindow(CmsBasicDialog.DialogWidth.wide);
309                window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_LOGFILE_LOGVIEW_SETTINGS_SHORT_0));
310                window.setContent(new CmsLogFileViewSettings(window));
311                window.addCloseListener(new CloseListener() {
312
313                    private static final long serialVersionUID = -7058276628732771106L;
314
315                    public void windowClose(CloseEvent e) {
316
317                        m_fileView.updateView();
318                    }
319                });
320                A_CmsUI.get().addWindow(window);
321            }
322        });
323        m_uiContext.addToolbarButton(button);
324    }
325}