1 /**
2 *
3 * Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.console.derbylogmanager;
19
20 import org.apache.geronimo.console.BasePortlet;
21 import org.apache.geronimo.console.util.PortletManager;
22 import org.apache.geronimo.derby.DerbyLog;
23
24 import javax.portlet.PortletConfig;
25 import javax.portlet.PortletContext;
26 import javax.portlet.PortletException;
27 import javax.portlet.PortletRequestDispatcher;
28 import javax.portlet.PortletSession;
29 import javax.portlet.RenderRequest;
30 import javax.portlet.RenderResponse;
31 import javax.portlet.WindowState;
32 import java.io.IOException;
33 import java.io.Serializable;
34
35 public class DerbyLogViewerPortlet extends BasePortlet {
36 private final static String CRITERIA_KEY = "org.apache.geronimo.console.derby.log.CRITERIA";
37
38 protected PortletRequestDispatcher normalView;
39
40 protected PortletRequestDispatcher helpView;
41
42 public void destroy() {
43 super.destroy();
44 normalView = null;
45 helpView = null;
46 }
47
48 protected void doHelp(RenderRequest renderRequest,
49 RenderResponse renderResponse) throws PortletException, IOException {
50 helpView.include(renderRequest, renderResponse);
51 }
52
53 protected void doView(RenderRequest renderRequest,
54 RenderResponse renderResponse) throws PortletException, IOException {
55 if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
56 return;
57 }
58 String action = renderRequest.getParameter("action");
59
60 DerbyLog log = (DerbyLog) PortletManager.getManagedBeans(renderRequest, DerbyLog.class)[0];
61 Criteria criteria;
62 if ("refresh".equals(action)) {
63 criteria = (Criteria) renderRequest.getPortletSession(true).getAttribute(CRITERIA_KEY, PortletSession.PORTLET_SCOPE);
64 } else {
65 String startPos = renderRequest.getParameter("startPos");
66 String endPos = renderRequest.getParameter("endPos");
67 String maxRows = renderRequest.getParameter("maxRows");
68 String searchString = renderRequest.getParameter("searchString");
69 if(maxRows == null || maxRows.equals("")) {
70 maxRows = "10";
71 }
72 criteria = new Criteria();
73 criteria.max = new Integer(maxRows);
74 criteria.start = startPos == null || startPos.equals("") ? null : new Integer(startPos);
75 criteria.stop = endPos == null || endPos.equals("") ? null : new Integer(endPos);
76 criteria.text = searchString == null || searchString.equals("") ? null : searchString;
77 renderRequest.getPortletSession(true).setAttribute(CRITERIA_KEY, criteria, PortletSession.PORTLET_SCOPE);
78 }
79
80 DerbyLog.SearchResults results = log.searchLog(criteria.start, criteria.stop,
81 criteria.max, criteria.text);
82 renderRequest.setAttribute("searchResults", results.getResults());
83 renderRequest.setAttribute("lineCount", new Integer(results.getLineCount()));
84 renderRequest.setAttribute("startPos", criteria.start);
85 renderRequest.setAttribute("endPos", criteria.stop);
86 renderRequest.setAttribute("searchString", criteria.text);
87 renderRequest.setAttribute("maxRows", criteria.max);
88 if(results.isCapped()) {
89 renderRequest.setAttribute("capped", Boolean.TRUE);
90 }
91
92 normalView.include(renderRequest, renderResponse);
93 }
94
95 private static class Criteria implements Serializable {
96 Integer max;
97 Integer start;
98 Integer stop;
99 String text;
100 }
101
102 public void init(PortletConfig portletConfig) throws PortletException {
103 PortletContext pc = portletConfig.getPortletContext();
104 normalView = pc
105 .getRequestDispatcher("/WEB-INF/view/derbylogmanager/view.jsp");
106 helpView = pc
107 .getRequestDispatcher("/WEB-INF/view/derbylogmanager/help.jsp");
108 super.init(portletConfig);
109 }
110 }