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.geronimo.console.apache.jk;
018    
019    import java.io.Serializable;
020    import java.util.ArrayList;
021    import java.util.List;
022    import java.util.Map;
023    import javax.portlet.ActionResponse;
024    import javax.portlet.PortletRequest;
025    import javax.portlet.PortletSession;
026    import org.apache.geronimo.console.MultiPageAbstractHandler;
027    import org.apache.geronimo.console.MultiPageModel;
028    import org.apache.geronimo.console.util.PortletManager;
029    import org.apache.geronimo.kernel.repository.Artifact;
030    import org.apache.geronimo.gbean.AbstractName;
031    
032    /**
033     * The base class for all handlers for this portlet
034     *
035     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
036     */
037    public abstract class BaseApacheHandler extends MultiPageAbstractHandler {
038        protected static final String INDEX_MODE = "index";
039        protected static final String BASIC_CONFIG_MODE = "basic";
040        protected static final String AJP_MODE = "ajp";
041        protected static final String WEB_APP_MODE = "webapp";
042        protected static final String RESULTS_MODE = "results";
043    
044        protected BaseApacheHandler(String mode, String viewName) {
045            super(mode, viewName);
046        }
047    
048        public final static class WebAppData implements Serializable {
049            private String parentConfigId;
050            private String childName;
051            private String moduleBeanName;
052            private boolean enabled;
053            private String dynamicPattern;
054            private boolean serveStaticContent;
055            private String contextRoot;
056            private String webAppDir;
057    
058            public WebAppData(Artifact parentConfigId, String childName, AbstractName moduleBeanName, boolean enabled, String dynamicPattern, boolean serveStaticContent) {
059                this.parentConfigId = parentConfigId.toString();
060                this.enabled = enabled;
061                this.dynamicPattern = dynamicPattern;
062                this.serveStaticContent = serveStaticContent;
063                this.moduleBeanName = moduleBeanName == null ? null : moduleBeanName.toString();
064                this.childName = childName;
065            }
066    
067            public WebAppData(PortletRequest request, String prefix) {
068                parentConfigId = request.getParameter(prefix+"configId");
069                childName = request.getParameter(prefix+"childName");
070                moduleBeanName = request.getParameter(prefix+"moduleBeanName");
071                dynamicPattern = request.getParameter(prefix+"dynamicPattern");
072                String test = request.getParameter(prefix+"enabled");
073                enabled = test != null && !test.equals("") && !test.equals("false");
074                test = request.getParameter(prefix+"serveStaticContent");
075                serveStaticContent = test != null && !test.equals("") && !test.equals("false");
076                contextRoot = request.getParameter(prefix+"contextRoot");
077                webAppDir = request.getParameter(prefix+"webAppDir");
078            }
079    
080            public void save(ActionResponse response, String prefix) {
081                response.setRenderParameter(prefix+"configId", parentConfigId);
082                response.setRenderParameter(prefix+"moduleBeanName", moduleBeanName);
083                response.setRenderParameter(prefix+"dynamicPattern", dynamicPattern);
084                response.setRenderParameter(prefix+"enabled", Boolean.toString(enabled));
085                response.setRenderParameter(prefix+"serveStaticContent", Boolean.toString(serveStaticContent));
086                if(!isEmpty(contextRoot)) response.setRenderParameter(prefix+"contextRoot", contextRoot);
087                if(!isEmpty(webAppDir)) response.setRenderParameter(prefix+"webAppDir", webAppDir);
088                if(!isEmpty(childName)) response.setRenderParameter(prefix+"childName", childName);
089            }
090    
091            public boolean isEnabled() {
092                return enabled;
093            }
094    
095            public void setEnabled(boolean enabled) {
096                this.enabled = enabled;
097            }
098    
099            public String getParentConfigId() {
100                return parentConfigId;
101            }
102    
103            public void setParentConfigId(String parentConfigId) {
104                this.parentConfigId = parentConfigId;
105            }
106    
107            public String getDynamicPattern() {
108                return dynamicPattern;
109            }
110    
111            public void setDynamicPattern(String dynamicPattern) {
112                this.dynamicPattern = dynamicPattern;
113            }
114    
115            public boolean isServeStaticContent() {
116                return serveStaticContent;
117            }
118    
119            public void setServeStaticContent(boolean serveStaticContent) {
120                this.serveStaticContent = serveStaticContent;
121            }
122    
123            public String getContextRoot() {
124                return contextRoot;
125            }
126    
127            public void setContextRoot(String contextRoot) {
128                this.contextRoot = contextRoot;
129            }
130    
131            public String getWebAppDir() {
132                return webAppDir;
133            }
134    
135            public void setWebAppDir(String webAppDir) {
136                this.webAppDir = webAppDir;
137            }
138    
139            public String getChildName() {
140                return childName;
141            }
142    
143            public String getModuleBeanName() {
144                return moduleBeanName;
145            }
146    
147            public String getName() {
148                return isEmpty(childName) ? parentConfigId : childName;
149            }
150    
151            public boolean isRunning() {
152                return webAppDir != null;
153            }
154        }
155    
156        public final static class ApacheModel implements MultiPageModel {
157            public final static String WEB_APP_SESSION_KEY = "console.apache.jk.WebApps";
158            private String os;
159            private Integer addAjpPort;
160            private String logFilePath;
161            private String workersPath;
162            private List webApps = new ArrayList();
163    
164            public ApacheModel(PortletRequest request) {
165                Map map = request.getParameterMap();
166                os = request.getParameter("os");
167                // logFilePath and workersPath need to be encoded before saving
168                // and decoded after fetching
169                logFilePath = request.getParameter("logFilePath");
170                if(logFilePath == null) {
171                    logFilePath = PortletManager.getCurrentServer(request).getServerInfo().resolve("var/log/apache_mod_jk.log").getPath();
172                }
173                workersPath = request.getParameter("workersPath");
174                if(workersPath == null) {
175                    workersPath = PortletManager.getCurrentServer(request).getServerInfo().resolve("var/config/workers.properties").getPath();
176                }
177                String ajp = request.getParameter("addAjpPort");
178                if(!isEmpty(ajp)) addAjpPort = new Integer(ajp);
179                int index = 0;
180                boolean found = false;
181                while(true) {
182                    String key = "webapp."+(index++)+".";
183                    if(!map.containsKey(key+"configId")) {
184                        break;
185                    }
186                    found = true;
187                    WebAppData data = new WebAppData(request, key);
188                    webApps.add(data);
189                }
190                if(!found) {
191                    List list = (List) request.getPortletSession(true).getAttribute(WEB_APP_SESSION_KEY);
192                    if(list != null) {
193                        webApps = list;
194                    }
195                }
196            }
197    
198            public void save(ActionResponse response, PortletSession session) {
199                if(!isEmpty(os)) response.setRenderParameter("os", os);
200                if(!isEmpty(logFilePath)) response.setRenderParameter("logFilePath", logFilePath);
201                if(!isEmpty(workersPath)) response.setRenderParameter("workersPath", workersPath);
202                if(addAjpPort != null) response.setRenderParameter("addAjpPort", addAjpPort.toString());
203                if(webApps.size() > 0) {
204                    session.setAttribute(WEB_APP_SESSION_KEY, webApps);
205                }
206            }
207            
208            public String getOs() {
209                return os;
210            }
211    
212            public void setOs(String os) {
213                this.os = os;
214            }
215    
216            public Integer getAddAjpPort() {
217                return addAjpPort;
218            }
219    
220            public void setAddAjpPort(Integer addAjpPort) {
221                this.addAjpPort = addAjpPort;
222            }
223    
224            public String getLogFilePath() {
225                return logFilePath;
226            }
227    
228            public void setLogFilePath(String logFilePath) {
229                this.logFilePath = logFilePath;
230            }
231    
232            public String getWorkersPath() {
233                return workersPath;
234            }
235    
236            public void setWorkersPath(String workersPath) {
237                this.workersPath = workersPath;
238            }
239    
240            public List getWebApps() {
241                return webApps;
242            }
243    
244            public void setWebApps(List webApps) {
245                this.webApps = webApps;
246            }
247        }
248    }