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: 560981 $ $Date: 2007-07-30 09:54:48 -0400 (Mon, 30 Jul 2007) $
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            // list of encodings for special chars
164            private String[][] ENCODINGS = {{":", "0x0"},{"\\", "0x1"}};
165    
166            public ApacheModel(PortletRequest request) {
167                Map map = request.getParameterMap();
168                os = request.getParameter("os");
169                // logFilePath and workersPath need to be encoded before saving
170                // and decoded after fetching
171                logFilePath = encodePath(request.getParameter("logFilePath"));
172                if(logFilePath == null) {
173                    logFilePath = encodePath(PortletManager.getCurrentServer(request).getServerInfo().resolve("var/log/apache_mod_jk.log").getPath());
174                }
175                workersPath = encodePath(request.getParameter("workersPath"));
176                if(workersPath == null) {
177                    workersPath = encodePath(PortletManager.getCurrentServer(request).getServerInfo().resolve("var/config/workers.properties").getPath());
178                }
179                String ajp = request.getParameter("addAjpPort");
180                if(!isEmpty(ajp)) addAjpPort = new Integer(ajp);
181                int index = 0;
182                boolean found = false;
183                while(true) {
184                    String key = "webapp."+(index++)+".";
185                    if(!map.containsKey(key+"configId")) {
186                        break;
187                    }
188                    found = true;
189                    WebAppData data = new WebAppData(request, key);
190                    webApps.add(data);
191                }
192                if(!found) {
193                    List list = (List) request.getPortletSession(true).getAttribute(WEB_APP_SESSION_KEY);
194                    if(list != null) {
195                        webApps = list;
196                    }
197                }
198            }
199    
200            public void save(ActionResponse response, PortletSession session) {
201                if(!isEmpty(os)) response.setRenderParameter("os", os);
202                if(!isEmpty(logFilePath)) response.setRenderParameter("logFilePath", logFilePath);
203                if(!isEmpty(workersPath)) response.setRenderParameter("workersPath", workersPath);
204                if(addAjpPort != null) response.setRenderParameter("addAjpPort", addAjpPort.toString());
205                if(webApps.size() > 0) {
206                    session.setAttribute(WEB_APP_SESSION_KEY, webApps);
207                }
208            }
209    
210            private String encodePath(String value) {
211                if(value == null) {
212                    return value;
213                }
214                for(int i = 0; i < ENCODINGS.length; i++) {
215                    if(value.contains(ENCODINGS[i][0])) {
216                        value = value.replace(ENCODINGS[i][0], ENCODINGS[i][1]);
217                    }
218                }
219                return value;
220            }
221            
222            private String decodePath(String value) {
223                if(value == null) {
224                    return value;
225                }
226                for(int i = 0; i < ENCODINGS.length; i++) {
227                    if(value.contains(ENCODINGS[i][1])) {
228                        value = value.replace(ENCODINGS[i][1], ENCODINGS[i][0]);
229                    }
230                }
231                return value;
232            }
233            
234            public String getOs() {
235                return os;
236            }
237    
238            public void setOs(String os) {
239                this.os = os;
240            }
241    
242            public Integer getAddAjpPort() {
243                return addAjpPort;
244            }
245    
246            public void setAddAjpPort(Integer addAjpPort) {
247                this.addAjpPort = addAjpPort;
248            }
249    
250            public String getLogFilePath() {
251                return decodePath(logFilePath);
252            }
253    
254            public void setLogFilePath(String logFilePath) {
255                this.logFilePath = logFilePath;
256            }
257    
258            public String getWorkersPath() {
259                return decodePath(workersPath);
260            }
261    
262            public void setWorkersPath(String workersPath) {
263                this.workersPath = workersPath;
264            }
265    
266            public List getWebApps() {
267                return webApps;
268            }
269    
270            public void setWebApps(List webApps) {
271                this.webApps = webApps;
272            }
273        }
274    }