| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 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.gitiles.dev; |
| 16 | |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 17 | import static com.google.common.base.MoreObjects.firstNonNull; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 18 | import static com.google.gitiles.GitilesServlet.STATIC_PREFIX; |
| 19 | |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Strings; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 22 | import com.google.gitiles.DebugRenderer; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 23 | import com.google.gitiles.GitilesAccess; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 24 | import com.google.gitiles.GitilesServlet; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 25 | import com.google.gitiles.PathServlet; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 26 | import com.google.gitiles.RepositoryDescription; |
| 27 | import com.google.gitiles.RootedDocServlet; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 28 | |
| 29 | import org.eclipse.jetty.server.Connector; |
| 30 | import org.eclipse.jetty.server.Handler; |
| 31 | import org.eclipse.jetty.server.Server; |
| Dave Borowitz | e1056bb | 2013-01-11 10:31:37 -0800 | [diff] [blame] | 32 | import org.eclipse.jetty.server.bio.SocketConnector; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 33 | import org.eclipse.jetty.server.handler.ContextHandler; |
| 34 | import org.eclipse.jetty.server.handler.ContextHandlerCollection; |
| 35 | import org.eclipse.jetty.server.handler.ResourceHandler; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 36 | import org.eclipse.jetty.servlet.ServletContextHandler; |
| 37 | import org.eclipse.jetty.servlet.ServletHolder; |
| 38 | import org.eclipse.jetty.util.resource.FileResource; |
| 39 | import org.eclipse.jetty.util.thread.QueuedThreadPool; |
| 40 | import org.eclipse.jetty.util.thread.ThreadPool; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 41 | import org.eclipse.jgit.errors.ConfigInvalidException; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 42 | import org.eclipse.jgit.errors.RepositoryNotFoundException; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 43 | import org.eclipse.jgit.lib.Config; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 44 | import org.eclipse.jgit.lib.Constants; |
| 45 | import org.eclipse.jgit.lib.Repository; |
| 46 | import org.eclipse.jgit.lib.RepositoryCache; |
| 47 | import org.eclipse.jgit.lib.RepositoryCache.FileKey; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 48 | import org.eclipse.jgit.storage.file.FileBasedConfig; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 49 | import org.eclipse.jgit.transport.resolver.RepositoryResolver; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 50 | import org.eclipse.jgit.util.FS; |
| 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 53 | |
| 54 | import java.io.File; |
| 55 | import java.io.FileNotFoundException; |
| 56 | import java.io.IOException; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 57 | import java.net.InetAddress; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 58 | import java.net.URI; |
| 59 | import java.net.URISyntaxException; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 60 | import java.net.UnknownHostException; |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 61 | import java.util.Arrays; |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 62 | import java.util.Collections; |
| 63 | import java.util.Map; |
| 64 | import java.util.Set; |
| 65 | |
| 66 | import javax.servlet.Servlet; |
| 67 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 68 | |
| 69 | class DevServer { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 70 | private static final Logger log = LoggerFactory.getLogger(PathServlet.class); |
| 71 | |
| Dave Borowitz | 823724a | 2013-07-30 13:18:42 -0700 | [diff] [blame] | 72 | private static Config defaultConfig() { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 73 | Config cfg = new Config(); |
| 74 | String cwd = System.getProperty("user.dir"); |
| 75 | cfg.setString("gitiles", null, "basePath", cwd); |
| 76 | cfg.setBoolean("gitiles", null, "exportAll", true); |
| 77 | cfg.setString("gitiles", null, "baseGitUrl", "file://" + cwd + "/"); |
| Dave Borowitz | 823724a | 2013-07-30 13:18:42 -0700 | [diff] [blame] | 78 | String networkHostName; |
| 79 | try { |
| 80 | networkHostName = InetAddress.getLocalHost().getCanonicalHostName(); |
| 81 | } catch (UnknownHostException e) { |
| 82 | networkHostName = "127.0.0.1"; |
| 83 | } |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 84 | cfg.setString("gitiles", null, "siteTitle", |
| 85 | String.format("Gitiles - %s:%s", networkHostName, cwd)); |
| 86 | cfg.setString("gitiles", null, "canonicalHostName", new File(cwd).getName()); |
| 87 | return cfg; |
| 88 | } |
| 89 | |
| 90 | private static FileNotFoundException badSourceRoot(URI u) { |
| 91 | return new FileNotFoundException("Cannot find source root from " + u); |
| 92 | } |
| 93 | |
| 94 | private static FileNotFoundException badSourceRoot(URI u, Throwable cause) { |
| 95 | FileNotFoundException notFound = badSourceRoot(u); |
| 96 | notFound.initCause(cause); |
| 97 | return notFound; |
| 98 | } |
| 99 | |
| 100 | private static File findSourceRoot() throws IOException { |
| 101 | URI u; |
| 102 | try { |
| 103 | u = DevServer.class.getResource(DevServer.class.getSimpleName() + ".class").toURI(); |
| 104 | } catch (URISyntaxException e) { |
| 105 | u = null; |
| 106 | } |
| 107 | if (u == null) { |
| 108 | throw new FileNotFoundException("Cannot find Gitiles source directory"); |
| 109 | } |
| 110 | if ("jar".equals(u.getScheme())) { |
| Dave Borowitz | b2f4d56 | 2012-12-27 16:36:37 -0800 | [diff] [blame] | 111 | String path = u.getSchemeSpecificPart(); |
| 112 | int jarEntry = path.indexOf("!/"); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 113 | if (jarEntry < 0) { |
| 114 | throw badSourceRoot(u); |
| 115 | } |
| 116 | try { |
| Dave Borowitz | b2f4d56 | 2012-12-27 16:36:37 -0800 | [diff] [blame] | 117 | return findSourceRoot(new URI(path.substring(0, jarEntry))); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 118 | } catch (URISyntaxException e) { |
| 119 | throw badSourceRoot(u, e); |
| 120 | } |
| 121 | } else { |
| 122 | return findSourceRoot(u); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private static File findSourceRoot(URI targetUri) throws IOException { |
| 127 | if (!"file".equals(targetUri.getScheme())) { |
| 128 | throw badSourceRoot(targetUri); |
| 129 | } |
| 130 | String targetPath = targetUri.getPath(); |
| David Ostrovsky | 22c45b3 | 2014-02-23 22:22:26 +0100 | [diff] [blame] | 131 | // targetPath is an arbitrary path under buck-out/ in our Buck package |
| 132 | // layout. |
| 133 | int targetIndex = targetPath.lastIndexOf("buck-out/"); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 134 | if (targetIndex < 0) { |
| 135 | throw badSourceRoot(targetUri); |
| 136 | } |
| 137 | String path = targetPath.substring(0, targetIndex); |
| 138 | URI u; |
| 139 | try { |
| 140 | u = new URI("file", path, null).normalize(); |
| 141 | } catch (URISyntaxException e) { |
| 142 | throw new IOException(e); |
| 143 | } |
| 144 | File root = new File(u); |
| 145 | if (!root.exists() || !root.isDirectory()) { |
| 146 | throw badSourceRoot(targetUri); |
| 147 | } |
| 148 | return root; |
| 149 | } |
| 150 | |
| 151 | private final File sourceRoot; |
| 152 | private final Config cfg; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 153 | private final Server httpd; |
| 154 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 155 | DevServer(File cfgFile) throws IOException, ConfigInvalidException { |
| 156 | sourceRoot = findSourceRoot(); |
| 157 | |
| 158 | Config cfg = defaultConfig(); |
| 159 | if (cfgFile.exists() && cfgFile.isFile()) { |
| 160 | FileBasedConfig fcfg = new FileBasedConfig(cfg, cfgFile, FS.DETECTED); |
| 161 | fcfg.load(); |
| 162 | cfg = fcfg; |
| 163 | } else { |
| Dave Borowitz | fd25c3a | 2013-01-11 14:37:11 -0800 | [diff] [blame] | 164 | log.info("Config file {} not found, using defaults", cfgFile.getPath()); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 165 | } |
| 166 | this.cfg = cfg; |
| 167 | |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 168 | httpd = new Server(); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 169 | httpd.setConnectors(connectors()); |
| 170 | httpd.setThreadPool(threadPool()); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 171 | httpd.setHandler(handler()); |
| 172 | } |
| 173 | |
| 174 | void start() throws Exception { |
| 175 | httpd.start(); |
| 176 | httpd.join(); |
| 177 | } |
| 178 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 179 | private Connector[] connectors() { |
| Dave Borowitz | e1056bb | 2013-01-11 10:31:37 -0800 | [diff] [blame] | 180 | Connector c = new SocketConnector(); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 181 | c.setHost(null); |
| 182 | c.setPort(cfg.getInt("gitiles", null, "port", 8080)); |
| 183 | c.setStatsOn(false); |
| 184 | return new Connector[]{c}; |
| 185 | } |
| 186 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 187 | private ThreadPool threadPool() { |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 188 | QueuedThreadPool pool = new QueuedThreadPool(); |
| 189 | pool.setName("HTTP"); |
| 190 | pool.setMinThreads(2); |
| 191 | pool.setMaxThreads(10); |
| 192 | pool.setMaxQueued(50); |
| 193 | return pool; |
| 194 | } |
| 195 | |
| 196 | private Handler handler() throws IOException { |
| 197 | ContextHandlerCollection handlers = new ContextHandlerCollection(); |
| 198 | handlers.addHandler(staticHandler()); |
| 199 | handlers.addHandler(appHandler()); |
| 200 | return handlers; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private Handler appHandler() { |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 204 | DebugRenderer renderer = new DebugRenderer( |
| 205 | STATIC_PREFIX, |
| 206 | Arrays.asList(cfg.getStringList("gitiles", null, "customTemplates")), |
| 207 | new File(sourceRoot, "gitiles-servlet/src/main/resources/com/google/gitiles/templates") |
| 208 | .getPath(), |
| 209 | firstNonNull(cfg.getString("gitiles", null, "siteTitle"), "Gitiles")); |
| 210 | |
| 211 | String docRoot = cfg.getString("gitiles", null, "docroot"); |
| 212 | Servlet servlet; |
| 213 | if (!Strings.isNullOrEmpty(docRoot)) { |
| 214 | servlet = createRootedDocServlet(renderer, docRoot); |
| 215 | } else { |
| 216 | servlet = new GitilesServlet( |
| 217 | cfg, |
| 218 | renderer, |
| 219 | null, null, null, null, null, null, null); |
| 220 | } |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 221 | |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 222 | ServletContextHandler handler = new ServletContextHandler(); |
| 223 | handler.setContextPath(""); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 224 | handler.addServlet(new ServletHolder(servlet), "/*"); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 225 | return handler; |
| 226 | } |
| 227 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 228 | private Handler staticHandler() throws IOException { |
| 229 | File staticRoot = new File(sourceRoot, |
| 230 | "gitiles-servlet/src/main/resources/com/google/gitiles/static"); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 231 | ResourceHandler rh = new ResourceHandler(); |
| 232 | try { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 233 | rh.setBaseResource(new FileResource(staticRoot.toURI().toURL())); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 234 | } catch (URISyntaxException e) { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 235 | throw new IOException(e); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 236 | } |
| 237 | rh.setWelcomeFiles(new String[]{}); |
| 238 | rh.setDirectoriesListed(false); |
| 239 | ContextHandler handler = new ContextHandler("/+static"); |
| 240 | handler.setHandler(rh); |
| 241 | return handler; |
| 242 | } |
| Shawn Pearce | 68311c7 | 2015-06-09 17:01:34 -0700 | [diff] [blame] | 243 | |
| 244 | private Servlet createRootedDocServlet(DebugRenderer renderer, String docRoot) { |
| 245 | File docRepo = new File(docRoot); |
| 246 | final FileKey repoKey = FileKey.exact(docRepo, FS.DETECTED); |
| 247 | |
| 248 | RepositoryResolver<HttpServletRequest> resolver = new RepositoryResolver<HttpServletRequest>() { |
| 249 | @Override |
| 250 | public Repository open(HttpServletRequest req, String name) |
| 251 | throws RepositoryNotFoundException { |
| 252 | try { |
| 253 | return RepositoryCache.open(repoKey, true); |
| 254 | } catch (IOException e) { |
| 255 | throw new RepositoryNotFoundException(repoKey.getFile(), e); |
| 256 | } |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | return new RootedDocServlet( |
| 261 | resolver, |
| 262 | new RootedDocAccess(docRepo), |
| 263 | renderer); |
| 264 | } |
| 265 | |
| 266 | private class RootedDocAccess implements GitilesAccess.Factory { |
| 267 | private final String repoName; |
| 268 | |
| 269 | RootedDocAccess(File docRepo) { |
| 270 | if (Constants.DOT_GIT.equals(docRepo.getName())) { |
| 271 | repoName = docRepo.getParentFile().getName(); |
| 272 | } else { |
| 273 | repoName = docRepo.getName(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public GitilesAccess forRequest(HttpServletRequest req) { |
| 279 | return new GitilesAccess() { |
| 280 | @Override |
| 281 | public Map<String, RepositoryDescription> listRepositories(Set<String> branches) { |
| 282 | return Collections.emptyMap(); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public Object getUserKey() { |
| 287 | return null; |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public String getRepositoryName() { |
| 292 | return repoName; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public RepositoryDescription getRepositoryDescription() { |
| 297 | RepositoryDescription d = new RepositoryDescription(); |
| 298 | d.name = getRepositoryName(); |
| 299 | return d; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public Config getConfig() { |
| 304 | return cfg; |
| 305 | } |
| 306 | }; |
| 307 | } |
| 308 | } |
| 309 | |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 310 | } |