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.CmsVaadinUtils;
033import org.opencms.util.CmsRfsException;
034import org.opencms.util.CmsRfsFileViewer;
035
036import java.io.ByteArrayOutputStream;
037import java.io.OutputStreamWriter;
038import java.nio.charset.Charset;
039
040import org.apache.commons.logging.Log;
041
042import com.vaadin.server.FileDownloader;
043import com.vaadin.ui.Panel;
044import com.vaadin.v7.data.Property.ValueChangeEvent;
045import com.vaadin.v7.data.Property.ValueChangeListener;
046import com.vaadin.v7.shared.ui.combobox.FilteringMode;
047import com.vaadin.v7.ui.ComboBox;
048import com.vaadin.v7.ui.Label;
049import com.vaadin.v7.ui.VerticalLayout;
050
051/**
052 * Class for the view of log files.<p>
053 */
054@SuppressWarnings("deprecation")
055public class CmsLogFileView extends VerticalLayout {
056
057    /**Session attribute to store charset setting.*/
058    protected static String ATTR_FILE_VIEW_CHARSET = "log-file-char";
059
060    /**Session attribute to store currently viewed log file.*/
061    protected static String ATTR_FILE_VIEW_PATH = "log-file";
062
063    /**Session attribute to store line number to display. */
064    protected static String ATTR_FILE_VIEW_SIZE = "log-file-size";
065
066    /**Window size.*/
067    protected static int WINDOW_SIZE = 1000;
068
069    /** Logger instance for this class. */
070    private static final Log LOG = CmsLog.getLog(CmsLogFileView.class);
071
072    /**vaadin serial id.*/
073    private static final long serialVersionUID = -6323034856756469160L;
074
075    /**Flag indicates if change event should be blocked. */
076    protected boolean m_blockChangeEvent;
077
078    /**Vaadin component. */
079    protected FileDownloader m_fileDownloader;
080
081    /**Vaadin component. */
082
083    private Label m_fileContent;
084
085    /**Vaadin component. */
086    private ComboBox m_logfile;
087
088    /**RfsFileView holding data for log to show. */
089    private CmsRfsFileViewer m_logView;
090
091    /**vaadin component. */
092    private Panel m_panelComp;
093
094    /**App instance. */
095    private CmsLogFileApp m_app;
096
097    /**
098     * constructor.<p>
099     *
100     * @param app which uses this view
101     */
102    protected CmsLogFileView(final CmsLogFileApp app) {
103
104        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
105
106        m_app = app;
107
108        m_logView = (CmsRfsFileViewer)OpenCms.getWorkplaceManager().getFileViewSettings().clone();
109        m_logView.setAdditionalRoots(CmsLogFileOptionProvider.getAdditionalLogDirectories());
110        m_logView.setWindowSize(WINDOW_SIZE);
111        if (CmsVaadinUtils.getRequest().getSession().getAttribute(ATTR_FILE_VIEW_SIZE) == null) {
112            CmsVaadinUtils.getRequest().getSession().setAttribute(
113                ATTR_FILE_VIEW_SIZE,
114                String.valueOf(m_logView.getWindowSize()));
115        }
116        initLogFileCombo();
117
118        m_logfile.setFilteringMode(FilteringMode.CONTAINS);
119        m_logfile.setNullSelectionAllowed(false);
120        m_logfile.setNewItemsAllowed(false);
121
122        m_logfile.addValueChangeListener(new ValueChangeListener() {
123
124            private static final long serialVersionUID = 1899253995224124911L;
125
126            public void valueChange(ValueChangeEvent event) {
127
128                if (m_blockChangeEvent) {
129                    return;
130                }
131                CmsVaadinUtils.getRequest().getSession().setAttribute(ATTR_FILE_VIEW_PATH, getCurrentFile());
132                updateView();
133            }
134        });
135
136        updateView();
137        m_fileContent.addStyleName("v-scrollable");
138        m_fileContent.addStyleName("o-report");
139
140    }
141
142    /**
143     * Updates the log file view after changes.<p>
144     */
145    public void updateView() {
146
147        if (CmsLogFileApp.LOG_FOLDER.isEmpty()) {
148            return;
149        }
150
151        try {
152            initLogFileCombo();
153            m_logView.setWindowSize(getSize());
154            m_logView.setFileEncoding(getChar());
155            String content = "<pre style='line-height:1.1;'>";
156            content += m_app.getLogFilePortion(m_logView, getCurrentFile());
157            content += "</pre>";
158            m_fileContent.setValue(content);
159            m_panelComp.setScrollTop(100000000);
160        } catch (CmsRfsException e) {
161            LOG.error(e.getLocalizedMessage(), e);
162        }
163
164    }
165
166    /**
167     * Gets currently shown file.<p>
168     *
169     * @return path of shown file
170     */
171    protected String getCurrentFile() {
172
173        return (String)m_logfile.getValue();
174    }
175
176    /**
177     * Gets  the char set.<p>
178     *
179     * @return the name of the charset
180     */
181    private String getChar() {
182
183        return ((Charset)CmsVaadinUtils.getRequest().getSession().getAttribute(ATTR_FILE_VIEW_CHARSET)).name();
184    }
185
186    /**
187     * Gets the size to be displayed.<p>
188     *
189     * @return line number
190     */
191    private int getSize() {
192
193        return Integer.valueOf(
194            (String)CmsVaadinUtils.getRequest().getSession().getAttribute(ATTR_FILE_VIEW_SIZE)).intValue();
195    }
196
197    /**
198     * Initializes the Log file combo-box.
199     */
200    private void initLogFileCombo() {
201
202        m_blockChangeEvent = true;
203        m_logfile.removeAllItems();
204        for (String path : m_app.getAvailableLogFilePaths()) {
205            m_logfile.addItem(path);
206        }
207
208        if (CmsVaadinUtils.getRequest().getSession().getAttribute(ATTR_FILE_VIEW_CHARSET) == null) {
209            Charset defaultCs = Charset.forName(new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding());
210            CmsVaadinUtils.getRequest().getSession().setAttribute(ATTR_FILE_VIEW_CHARSET, defaultCs);
211        }
212
213        if (CmsVaadinUtils.getRequest().getSession().getAttribute(ATTR_FILE_VIEW_PATH) != null) {
214            m_logfile.select(CmsVaadinUtils.getRequest().getSession().getAttribute(ATTR_FILE_VIEW_PATH));
215        } else {
216            selectLogFile();
217        }
218        m_blockChangeEvent = false;
219
220    }
221
222    /**
223     * Selects the currently set log file.<p>
224     *
225     */
226    private void selectLogFile() {
227
228        m_logfile.select(m_app.getDefaultLogFilePath(m_logView));
229
230    }
231}