1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.ui.servlets; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
import java.util.ArrayList; |
18 | |
import java.util.HashMap; |
19 | |
import java.util.HashSet; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.Set; |
23 | |
import java.util.concurrent.ConcurrentHashMap; |
24 | |
|
25 | |
import javax.servlet.ServletConfig; |
26 | |
import javax.servlet.ServletException; |
27 | |
import javax.servlet.http.HttpServlet; |
28 | |
import javax.servlet.http.HttpServletRequest; |
29 | |
import javax.servlet.http.HttpServletResponse; |
30 | |
|
31 | |
import net.admin4j.deps.commons.collections.map.ListOrderedMap; |
32 | |
import net.admin4j.deps.commons.lang3.ArrayUtils; |
33 | |
import net.admin4j.deps.commons.lang3.StringUtils; |
34 | |
import net.admin4j.util.Admin4jRuntimeException; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 6 | public class Admin4JHomePageServlet extends AdminDisplayServlet { |
42 | |
|
43 | |
private static final long serialVersionUID = -249983381219640941L; |
44 | |
@SuppressWarnings("rawtypes") |
45 | 3 | private static Map<Class, String> requestClassMapping = new ConcurrentHashMap<Class, String>(); |
46 | 3 | private static Set<String> publicHandleSet = new HashSet<String>(); |
47 | |
@SuppressWarnings("unchecked") |
48 | |
private static Map<String, HttpServlet> urlMap = new ListOrderedMap(); |
49 | |
|
50 | |
private ServletConfig servletConfig; |
51 | 6 | private List<Admin4JServlet> servletList = new ArrayList<Admin4JServlet>(); |
52 | |
|
53 | |
static { |
54 | |
|
55 | 3 | requestClassMapping.put(ExceptionDisplayServlet.class, ExceptionDisplayServlet.PUBLIC_HANDLE); |
56 | 3 | requestClassMapping.put(HotSpotDisplayServlet.class, HotSpotDisplayServlet.PUBLIC_HANDLE); |
57 | 3 | requestClassMapping.put(PerformanceDisplayServlet.class, PerformanceDisplayServlet.PUBLIC_HANDLE); |
58 | 3 | requestClassMapping.put(SqlDisplayServlet.class, SqlDisplayServlet.PUBLIC_HANDLE); |
59 | |
|
60 | 3 | requestClassMapping.put(LogLevelServlet.class, LogLevelServlet.PUBLIC_HANDLE); |
61 | 3 | requestClassMapping.put(FileExplorerServlet.class, FileExplorerServlet.PUBLIC_HANDLE); |
62 | 3 | requestClassMapping.put(JmxServlet.class, JmxServlet.PUBLIC_HANDLE); |
63 | |
|
64 | 3 | publicHandleSet.addAll(requestClassMapping.values()); |
65 | |
|
66 | 3 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
@Override |
72 | |
protected void service(HttpServletRequest request, HttpServletResponse response) |
73 | |
throws ServletException, IOException { |
74 | |
|
75 | 3 | if (urlMap.size() == 0) { |
76 | 3 | registerServletMappings(request); |
77 | |
} |
78 | |
|
79 | 3 | HttpServlet delegateServlet = urlMap.get(request.getRequestURI()); |
80 | 3 | if (delegateServlet != null) { |
81 | 0 | delegateServlet.service(request, response); |
82 | 0 | return; |
83 | |
} |
84 | |
|
85 | 3 | request.getSession().setAttribute(ADMIN4J_SESSION_VARIABLE_PREFIX + "IndexUri", request.getRequestURI()); |
86 | |
|
87 | 3 | Map<String,Object> variableMap = new HashMap<String,Object>(); |
88 | 3 | variableMap.put("servletList", urlMap.entrySet()); |
89 | |
|
90 | 3 | displayFreeMarkerPage(request, response, "admin4jIndexDisplayServlet.ftl", variableMap); |
91 | 3 | } |
92 | |
|
93 | |
private void registerServletMappings(HttpServletRequest request) throws ServletException { |
94 | |
|
95 | |
|
96 | 3 | String baseURI = determineBaseUri(request); |
97 | 3 | StringBuffer base = new StringBuffer(baseURI); |
98 | 3 | if ( !baseURI.endsWith("/")) { |
99 | 3 | base.append("/"); |
100 | |
} |
101 | |
|
102 | 3 | for (Admin4JServlet servlet: this.servletList) { |
103 | 21 | urlMap.put(base + requestClassMapping.get(servlet.getClass()), servlet); |
104 | |
} |
105 | |
|
106 | 3 | } |
107 | |
|
108 | |
protected String determineBaseUri(HttpServletRequest request) { |
109 | |
String baseURI; |
110 | |
String[] nodes = StringUtils.split(request.getRequestURI(), "/"); |
111 | 9 | int publicHandleOffset = findPublicHandleOffset(nodes); |
112 | 9 | if (publicHandleOffset > 0) { |
113 | |
baseURI = "/" + StringUtils.join(ArrayUtils.subarray(nodes, 0, publicHandleOffset), "/"); |
114 | |
} |
115 | |
else { |
116 | 6 | baseURI = request.getRequestURI(); |
117 | |
} |
118 | 9 | return baseURI; |
119 | |
} |
120 | |
|
121 | |
private int findPublicHandleOffset(String[] nodes) { |
122 | 21 | for (int i = nodes.length - 1; i >= 0; i--) { |
123 | 15 | if (publicHandleSet.contains(nodes[i])) { |
124 | 3 | return i; |
125 | |
} |
126 | |
} |
127 | |
|
128 | 6 | return -1; |
129 | |
} |
130 | |
|
131 | |
@SuppressWarnings("rawtypes") |
132 | |
private Admin4JServlet initServlet(Class servletClass) |
133 | |
throws ServletException { |
134 | |
Admin4JServlet tempServlet; |
135 | |
try { |
136 | 21 | tempServlet = (Admin4JServlet)servletClass.newInstance(); |
137 | 0 | } catch (Throwable e) { |
138 | 0 | throw new Admin4jRuntimeException(e); |
139 | 21 | } |
140 | 21 | tempServlet.init(this.servletConfig); |
141 | 21 | this.servletList.add(tempServlet); |
142 | 21 | return tempServlet; |
143 | |
} |
144 | |
|
145 | |
@SuppressWarnings("rawtypes") |
146 | |
private void ensureInitializedServlet(Class servletClass) throws ServletException { |
147 | |
Set<HttpServlet> servletSet; |
148 | |
Admin4JServlet tempServlet; |
149 | |
|
150 | 6 | servletSet = getRegisteredServletSet(servletClass); |
151 | 6 | if (servletSet.size() == 0) { |
152 | |
try { |
153 | 6 | tempServlet = (Admin4JServlet)servletClass.newInstance(); |
154 | 0 | } catch (Throwable e) { |
155 | 0 | throw new Admin4jRuntimeException(e); |
156 | 6 | } |
157 | 6 | tempServlet.init(this.servletConfig); |
158 | |
} |
159 | |
|
160 | 6 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
@Override |
166 | |
public String getServletLabel() { |
167 | 3 | return "Admin4J Index Page"; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
@Override |
174 | |
public void init(ServletConfig config) throws ServletException { |
175 | 3 | super.init(config); |
176 | |
|
177 | 3 | this.servletConfig = config; |
178 | |
|
179 | 3 | initServlet(HotSpotDisplayServlet.class); |
180 | 3 | initServlet(ExceptionDisplayServlet.class); |
181 | 3 | initServlet(FileExplorerServlet.class); |
182 | 3 | initServlet(SqlDisplayServlet.class); |
183 | 3 | initServlet(JmxServlet.class); |
184 | 3 | initServlet(LogLevelServlet.class); |
185 | 3 | initServlet(PerformanceDisplayServlet.class); |
186 | |
|
187 | 3 | ensureInitializedServlet(MemoryMonitorStartupServlet.class); |
188 | 3 | ensureInitializedServlet(ThreadMonitorStartupServlet.class); |
189 | |
|
190 | 3 | } |
191 | |
|
192 | |
} |