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    
018    package org.apache.geronimo.console.logmanager;
019    
020    import java.io.IOException;
021    
022    import javax.portlet.ActionRequest;
023    import javax.portlet.ActionResponse;
024    import javax.portlet.PortletConfig;
025    import javax.portlet.PortletContext;
026    import javax.portlet.PortletException;
027    import javax.portlet.PortletRequestDispatcher;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    import javax.portlet.WindowState;
031    
032    import org.apache.geronimo.console.BasePortlet;
033    import org.apache.geronimo.console.util.PortletManager;
034    import org.apache.geronimo.system.logging.SystemLog;
035    
036    public class LogManagerPortlet extends BasePortlet {
037    
038        protected PortletRequestDispatcher normalView;
039    
040        protected PortletRequestDispatcher helpView;
041    
042        protected void doHelp(RenderRequest renderRequest,
043                RenderResponse renderRespose) throws PortletException, IOException {
044            helpView.include(renderRequest, renderRespose);
045        }
046    
047        protected void doView(RenderRequest renderRequest,
048                RenderResponse renderRespose) throws PortletException, IOException {
049            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
050                return;
051            }
052            SystemLog log = PortletManager.getCurrentSystemLog(renderRequest);
053            renderRequest.setAttribute("configFile", log.getConfigFileName());
054    //        renderRequest.setAttribute("configuration", LogHelper.getConfiguration());
055            renderRequest.setAttribute("logLevel", log.getRootLoggerLevel());
056            try{
057                renderRequest.setAttribute("refreshPeriod", new Integer(log.getRefreshPeriodSeconds()));
058            }catch(NumberFormatException e){
059                //ignore
060            }
061    
062            normalView.include(renderRequest, renderRespose);
063        }
064    
065        public void init(PortletConfig portletConfig) throws PortletException {
066            PortletContext pc = portletConfig.getPortletContext();
067            normalView = pc.getRequestDispatcher("/WEB-INF/view/logmanager/view.jsp");
068            helpView = pc.getRequestDispatcher("/WEB-INF/view/logmanager/help.jsp");
069            super.init(portletConfig);
070        }
071    
072        public void processAction(ActionRequest actionRequest,
073                ActionResponse actionResponse) throws PortletException, IOException {
074            SystemLog log = PortletManager.getCurrentSystemLog(actionRequest);
075    
076            String action = actionRequest.getParameter("action");
077            String logLevel = actionRequest.getParameter("logLevel");
078            String configFile = actionRequest.getParameter("configFile");
079            String configuration = actionRequest.getParameter("append");
080            String refreshPeriod = actionRequest.getParameter("refreshPeriod");
081            String currentLevel = log.getRootLoggerLevel();
082    
083            if ("update".equals(action)) {
084                if (refreshPeriod != null) {
085                    int refreshPeriodInt = 0;
086                    try{
087                        refreshPeriodInt = Integer.parseInt(refreshPeriod);
088                    }catch(NumberFormatException e){
089                        //ignore
090                    }
091                    if (refreshPeriodInt != log.getRefreshPeriodSeconds()) {
092                        log.setRefreshPeriodSeconds(refreshPeriodInt);
093                    }
094                }
095                if (!log.getConfigFileName().equals(configFile)) {
096                    log.setConfigFileName(configFile);
097                }
098                if (!currentLevel.equals(logLevel)) {
099                    log.setRootLoggerLevel(logLevel);
100                }
101            }
102        }
103    }