| 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 | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 17 | import static com.google.gitiles.GitilesServlet.STATIC_PREFIX; |
| 18 | |
| 19 | import com.google.common.base.Objects; |
| 20 | import com.google.gitiles.DebugRenderer; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 21 | import com.google.gitiles.GitilesServlet; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 22 | import com.google.gitiles.PathServlet; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 23 | |
| 24 | import org.eclipse.jetty.server.Connector; |
| 25 | import org.eclipse.jetty.server.Handler; |
| 26 | import org.eclipse.jetty.server.Server; |
| 27 | import org.eclipse.jetty.server.handler.ContextHandler; |
| 28 | import org.eclipse.jetty.server.handler.ContextHandlerCollection; |
| 29 | import org.eclipse.jetty.server.handler.ResourceHandler; |
| 30 | import org.eclipse.jetty.server.nio.SelectChannelConnector; |
| 31 | import org.eclipse.jetty.servlet.ServletContextHandler; |
| 32 | import org.eclipse.jetty.servlet.ServletHolder; |
| 33 | import org.eclipse.jetty.util.resource.FileResource; |
| 34 | import org.eclipse.jetty.util.thread.QueuedThreadPool; |
| 35 | import org.eclipse.jetty.util.thread.ThreadPool; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 36 | import org.eclipse.jgit.errors.ConfigInvalidException; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 37 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 38 | import org.eclipse.jgit.storage.file.FileBasedConfig; |
| 39 | import org.eclipse.jgit.util.FS; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 42 | |
| 43 | import java.io.File; |
| 44 | import java.io.FileNotFoundException; |
| 45 | import java.io.IOException; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 46 | import java.net.InetAddress; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 47 | import java.net.URI; |
| 48 | import java.net.URISyntaxException; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 49 | import java.net.UnknownHostException; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 50 | |
| 51 | class DevServer { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 52 | private static final Logger log = LoggerFactory.getLogger(PathServlet.class); |
| 53 | |
| 54 | private static Config defaultConfig() throws UnknownHostException { |
| 55 | Config cfg = new Config(); |
| 56 | String cwd = System.getProperty("user.dir"); |
| 57 | cfg.setString("gitiles", null, "basePath", cwd); |
| 58 | cfg.setBoolean("gitiles", null, "exportAll", true); |
| 59 | cfg.setString("gitiles", null, "baseGitUrl", "file://" + cwd + "/"); |
| 60 | String networkHostName = InetAddress.getLocalHost().getCanonicalHostName(); |
| 61 | cfg.setString("gitiles", null, "siteTitle", |
| 62 | String.format("Gitiles - %s:%s", networkHostName, cwd)); |
| 63 | cfg.setString("gitiles", null, "canonicalHostName", new File(cwd).getName()); |
| 64 | return cfg; |
| 65 | } |
| 66 | |
| 67 | private static FileNotFoundException badSourceRoot(URI u) { |
| 68 | return new FileNotFoundException("Cannot find source root from " + u); |
| 69 | } |
| 70 | |
| 71 | private static FileNotFoundException badSourceRoot(URI u, Throwable cause) { |
| 72 | FileNotFoundException notFound = badSourceRoot(u); |
| 73 | notFound.initCause(cause); |
| 74 | return notFound; |
| 75 | } |
| 76 | |
| 77 | private static File findSourceRoot() throws IOException { |
| 78 | URI u; |
| 79 | try { |
| 80 | u = DevServer.class.getResource(DevServer.class.getSimpleName() + ".class").toURI(); |
| 81 | } catch (URISyntaxException e) { |
| 82 | u = null; |
| 83 | } |
| 84 | if (u == null) { |
| 85 | throw new FileNotFoundException("Cannot find Gitiles source directory"); |
| 86 | } |
| 87 | if ("jar".equals(u.getScheme())) { |
| Dave Borowitz | b2f4d56 | 2012-12-27 16:36:37 -0800 | [diff] [blame^] | 88 | String path = u.getSchemeSpecificPart(); |
| 89 | int jarEntry = path.indexOf("!/"); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 90 | if (jarEntry < 0) { |
| 91 | throw badSourceRoot(u); |
| 92 | } |
| 93 | try { |
| Dave Borowitz | b2f4d56 | 2012-12-27 16:36:37 -0800 | [diff] [blame^] | 94 | return findSourceRoot(new URI(path.substring(0, jarEntry))); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 95 | } catch (URISyntaxException e) { |
| 96 | throw badSourceRoot(u, e); |
| 97 | } |
| 98 | } else { |
| 99 | return findSourceRoot(u); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private static File findSourceRoot(URI targetUri) throws IOException { |
| 104 | if (!"file".equals(targetUri.getScheme())) { |
| 105 | throw badSourceRoot(targetUri); |
| 106 | } |
| 107 | String targetPath = targetUri.getPath(); |
| 108 | // targetPath is an arbitrary path under gitiles-dev/target in the standard |
| 109 | // Maven package layout. |
| 110 | int targetIndex = targetPath.lastIndexOf("gitiles-dev/target/"); |
| 111 | if (targetIndex < 0) { |
| 112 | throw badSourceRoot(targetUri); |
| 113 | } |
| 114 | String path = targetPath.substring(0, targetIndex); |
| 115 | URI u; |
| 116 | try { |
| 117 | u = new URI("file", path, null).normalize(); |
| 118 | } catch (URISyntaxException e) { |
| 119 | throw new IOException(e); |
| 120 | } |
| 121 | File root = new File(u); |
| 122 | if (!root.exists() || !root.isDirectory()) { |
| 123 | throw badSourceRoot(targetUri); |
| 124 | } |
| 125 | return root; |
| 126 | } |
| 127 | |
| 128 | private final File sourceRoot; |
| 129 | private final Config cfg; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 130 | private final Server httpd; |
| 131 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 132 | DevServer(File cfgFile) throws IOException, ConfigInvalidException { |
| 133 | sourceRoot = findSourceRoot(); |
| 134 | |
| 135 | Config cfg = defaultConfig(); |
| 136 | if (cfgFile.exists() && cfgFile.isFile()) { |
| 137 | FileBasedConfig fcfg = new FileBasedConfig(cfg, cfgFile, FS.DETECTED); |
| 138 | fcfg.load(); |
| 139 | cfg = fcfg; |
| 140 | } else { |
| 141 | // TODO(dborowitz): This is not getting outputted, we're probably missing |
| 142 | // some logging config. |
| 143 | log.info("Config file %s not found, using defaults", cfgFile.getPath()); |
| 144 | } |
| 145 | this.cfg = cfg; |
| 146 | |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 147 | httpd = new Server(); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 148 | httpd.setConnectors(connectors()); |
| 149 | httpd.setThreadPool(threadPool()); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 150 | httpd.setHandler(handler()); |
| 151 | } |
| 152 | |
| 153 | void start() throws Exception { |
| 154 | httpd.start(); |
| 155 | httpd.join(); |
| 156 | } |
| 157 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 158 | private Connector[] connectors() { |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 159 | Connector c = new SelectChannelConnector(); |
| 160 | c.setHost(null); |
| 161 | c.setPort(cfg.getInt("gitiles", null, "port", 8080)); |
| 162 | c.setStatsOn(false); |
| 163 | return new Connector[]{c}; |
| 164 | } |
| 165 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 166 | private ThreadPool threadPool() { |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 167 | QueuedThreadPool pool = new QueuedThreadPool(); |
| 168 | pool.setName("HTTP"); |
| 169 | pool.setMinThreads(2); |
| 170 | pool.setMaxThreads(10); |
| 171 | pool.setMaxQueued(50); |
| 172 | return pool; |
| 173 | } |
| 174 | |
| 175 | private Handler handler() throws IOException { |
| 176 | ContextHandlerCollection handlers = new ContextHandlerCollection(); |
| 177 | handlers.addHandler(staticHandler()); |
| 178 | handlers.addHandler(appHandler()); |
| 179 | return handlers; |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private Handler appHandler() { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 183 | GitilesServlet servlet = new GitilesServlet( |
| 184 | cfg, |
| 185 | new DebugRenderer( |
| 186 | STATIC_PREFIX, |
| 187 | cfg.getString("gitiles", null, "customTemplates"), |
| 188 | new File(sourceRoot, "gitiles-servlet/src/main/resources/com/google/gitiles/templates") |
| 189 | .getPath(), |
| 190 | Objects.firstNonNull(cfg.getString("gitiles", null, "siteTitle"), "Gitiles")), |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 191 | null, null, null, null, null); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 192 | |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 193 | ServletContextHandler handler = new ServletContextHandler(); |
| 194 | handler.setContextPath(""); |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 195 | handler.addServlet(new ServletHolder(servlet), "/*"); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 196 | return handler; |
| 197 | } |
| 198 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 199 | private Handler staticHandler() throws IOException { |
| 200 | File staticRoot = new File(sourceRoot, |
| 201 | "gitiles-servlet/src/main/resources/com/google/gitiles/static"); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 202 | ResourceHandler rh = new ResourceHandler(); |
| 203 | try { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 204 | rh.setBaseResource(new FileResource(staticRoot.toURI().toURL())); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 205 | } catch (URISyntaxException e) { |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 206 | throw new IOException(e); |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 207 | } |
| 208 | rh.setWelcomeFiles(new String[]{}); |
| 209 | rh.setDirectoriesListed(false); |
| 210 | ContextHandler handler = new ContextHandler("/+static"); |
| 211 | handler.setHandler(rh); |
| 212 | return handler; |
| 213 | } |
| Dave Borowitz | 09ff62b | 2012-12-17 14:09:16 -0800 | [diff] [blame] | 214 | } |