Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd.plugins; 16 : 17 : import com.google.common.collect.Maps; 18 : import com.google.common.flogger.FluentLogger; 19 : import com.google.gerrit.common.Version; 20 : import com.google.gerrit.server.plugins.Plugin; 21 : import java.io.InputStream; 22 : import java.lang.reflect.InvocationHandler; 23 : import java.lang.reflect.Method; 24 : import java.lang.reflect.Proxy; 25 : import java.net.URL; 26 : import java.util.Collections; 27 : import java.util.Enumeration; 28 : import java.util.Set; 29 : import java.util.concurrent.ConcurrentMap; 30 : import javax.servlet.RequestDispatcher; 31 : import javax.servlet.Servlet; 32 : import javax.servlet.ServletContext; 33 : 34 : class PluginServletContext { 35 1 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 36 : 37 : static ServletContext create(Plugin plugin, String contextPath) { 38 1 : return (ServletContext) 39 1 : Proxy.newProxyInstance( 40 1 : PluginServletContext.class.getClassLoader(), 41 : new Class<?>[] {ServletContext.class, API.class}, 42 : new Handler(plugin, contextPath)); 43 : } 44 : 45 : private PluginServletContext() {} 46 : 47 : private static class Handler implements InvocationHandler, API { 48 : private final Plugin plugin; 49 : private final String contextPath; 50 : private final ConcurrentMap<String, Object> attributes; 51 : 52 1 : Handler(Plugin plugin, String contextPath) { 53 1 : this.plugin = plugin; 54 1 : this.contextPath = contextPath; 55 1 : this.attributes = Maps.newConcurrentMap(); 56 1 : } 57 : 58 : @Override 59 : public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 60 : Method handler; 61 : try { 62 0 : handler = API.class.getDeclaredMethod(method.getName(), method.getParameterTypes()); 63 0 : } catch (NoSuchMethodException e) { 64 0 : throw new NoSuchMethodError( 65 0 : String.format( 66 : "%s does not implement %s", 67 0 : PluginServletContext.class, method.toGenericString())) 68 0 : .initCause(e); 69 0 : } 70 0 : return handler.invoke(this, args); 71 : } 72 : 73 : @Override 74 : public String getContextPath() { 75 0 : return contextPath; 76 : } 77 : 78 : @Override 79 : public String getInitParameter(String name) { 80 0 : return null; 81 : } 82 : 83 : @SuppressWarnings("rawtypes") 84 : @Override 85 : public Enumeration getInitParameterNames() { 86 0 : return Collections.enumeration(Collections.emptyList()); 87 : } 88 : 89 : @Override 90 : public ServletContext getContext(String name) { 91 0 : return null; 92 : } 93 : 94 : @Override 95 : public RequestDispatcher getNamedDispatcher(String name) { 96 0 : return null; 97 : } 98 : 99 : @Override 100 : public RequestDispatcher getRequestDispatcher(String name) { 101 0 : return null; 102 : } 103 : 104 : @Override 105 : public URL getResource(String name) { 106 0 : return null; 107 : } 108 : 109 : @Override 110 : public InputStream getResourceAsStream(String name) { 111 0 : return null; 112 : } 113 : 114 : @SuppressWarnings("rawtypes") 115 : @Override 116 : public Set getResourcePaths(String name) { 117 0 : return null; 118 : } 119 : 120 : @Override 121 : public Servlet getServlet(String name) { 122 0 : return null; 123 : } 124 : 125 : @Override 126 : public String getRealPath(String name) { 127 0 : return null; 128 : } 129 : 130 : @Override 131 : public String getServletContextName() { 132 0 : return plugin.getName(); 133 : } 134 : 135 : @SuppressWarnings("rawtypes") 136 : @Override 137 : public Enumeration getServletNames() { 138 0 : return Collections.enumeration(Collections.emptyList()); 139 : } 140 : 141 : @SuppressWarnings("rawtypes") 142 : @Override 143 : public Enumeration getServlets() { 144 0 : return Collections.enumeration(Collections.emptyList()); 145 : } 146 : 147 : @Override 148 : public void log(Exception reason, String msg) { 149 0 : log(msg, reason); 150 0 : } 151 : 152 : @Override 153 : public void log(String msg) { 154 0 : log(msg, null); 155 0 : } 156 : 157 : @Override 158 : public void log(String msg, Throwable reason) { 159 0 : logger.atWarning().withCause(reason).log("[plugin %s] %s", plugin.getName(), msg); 160 0 : } 161 : 162 : @Override 163 : public Object getAttribute(String name) { 164 0 : return attributes.get(name); 165 : } 166 : 167 : @Override 168 : public Enumeration<String> getAttributeNames() { 169 0 : return Collections.enumeration(attributes.keySet()); 170 : } 171 : 172 : @Override 173 : public void setAttribute(String name, Object value) { 174 0 : attributes.put(name, value); 175 0 : } 176 : 177 : @Override 178 : public void removeAttribute(String name) { 179 0 : attributes.remove(name); 180 0 : } 181 : 182 : @Override 183 : public String getMimeType(String file) { 184 0 : return null; 185 : } 186 : 187 : @Override 188 : public int getMajorVersion() { 189 0 : return 2; 190 : } 191 : 192 : @Override 193 : public int getMinorVersion() { 194 0 : return 5; 195 : } 196 : 197 : @Override 198 : public String getServerInfo() { 199 0 : String v = Version.getVersion(); 200 0 : return "Gerrit Code Review/" + (v != null ? v : "dev"); 201 : } 202 : 203 : @Override 204 : public String getVirtualServerName() { 205 0 : return null; 206 : } 207 : } 208 : 209 : interface API { 210 : String getContextPath(); 211 : 212 : String getInitParameter(String name); 213 : 214 : @SuppressWarnings("rawtypes") 215 : Enumeration getInitParameterNames(); 216 : 217 : ServletContext getContext(String name); 218 : 219 : RequestDispatcher getNamedDispatcher(String name); 220 : 221 : RequestDispatcher getRequestDispatcher(String name); 222 : 223 : URL getResource(String name); 224 : 225 : InputStream getResourceAsStream(String name); 226 : 227 : @SuppressWarnings("rawtypes") 228 : Set getResourcePaths(String name); 229 : 230 : Servlet getServlet(String name); 231 : 232 : String getRealPath(String name); 233 : 234 : String getServletContextName(); 235 : 236 : @SuppressWarnings("rawtypes") 237 : Enumeration getServletNames(); 238 : 239 : @SuppressWarnings("rawtypes") 240 : Enumeration getServlets(); 241 : 242 : void log(Exception reason, String msg); 243 : 244 : void log(String msg); 245 : 246 : void log(String msg, Throwable reason); 247 : 248 : Object getAttribute(String name); 249 : 250 : Enumeration<String> getAttributeNames(); 251 : 252 : void setAttribute(String name, Object value); 253 : 254 : void removeAttribute(String name); 255 : 256 : String getMimeType(String file); 257 : 258 : int getMajorVersion(); 259 : 260 : int getMinorVersion(); 261 : 262 : String getServerInfo(); 263 : 264 : String getVirtualServerName(); 265 : } 266 : }