summaryrefslogtreecommitdiffstats
path: root/mgrapp/src/com/google/codereview/Main.java
blob: 95a31b948805d329b6d31fb56f67bf5698090168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.codereview;

import com.google.codereview.manager.Backend;
import com.google.codereview.manager.ProjectSync;
import com.google.codereview.manager.RepositoryCache;
import com.google.codereview.manager.merge.PendingMerger;
import com.google.codereview.manager.prune.BuildPruner;
import com.google.codereview.manager.prune.BundlePruner;
import com.google.codereview.manager.unpack.ReceivedBundleUnpacker;
import com.google.codereview.rpc.HttpRpc;
import com.google.githacks.BrokenShallowRepositoryCreator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.spearce.jgit.lib.PersonIdent;
import org.spearce.jgit.lib.RepositoryConfig;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;

/** Server startup, invoked from the command line. */
public class Main {
  private static final Log LOG = LogFactory.getLog("main");
  private static final String SEC_CODEREVIEW = "codereview";
  private static final String SEC_LOG = "log";
  private static final int FOUR_HOURS = 4 * 60 * 60; // seconds
  private static final int ONCE_PER_DAY = 24 * 60 * 60;// seconds

  public static void main(final String[] args) {
    if (args.length == 0) {
      System.err.println("usage: " + Main.class.getName() + " configfile");
      System.exit(1);
    }

    final File configPath = new File(args[0]).getAbsoluteFile();
    final RepositoryConfig config = new RepositoryConfig(null, configPath);
    try {
      config.load();
    } catch (FileNotFoundException e) {
      System.err.println("error: " + configPath + " not found");
      System.exit(1);
    } catch (IOException e) {
      System.err.println("error: " + configPath + " not readable");
      e.printStackTrace(System.err);
      System.exit(1);
    }

    configureLogging(config, args.length > 1);
    LOG.info("Read " + configPath);
    final Main me = new Main(configPath, config);

    if (args.length == 1) {
      me.addShutdownHook();
      me.start();
    } else {
      final String cmd = args[1];
      if (cmd.equals("sync")) {
        new ProjectSync(me.backend).sync();
      } else if (cmd.equals("bsclone")) {
        try {
          final File src = new File(args[2]);
          final File dst = new File(args[3]);
          BrokenShallowRepositoryCreator.createRecursive(src, dst);
        } catch (IOException err) {
          System.err.println("error: " + err);
        }
      } else {
        System.err.println("error: " + cmd + " not recognized");
      }
    }
  }

  private final RepositoryConfig config;
  private final ScheduledThreadPoolExecutor pool;
  private final int taskSleep;
  private final Backend backend;

  private Main(final File configPath, final RepositoryConfig rc) {
    config = rc;

    final int threads = config.getInt(SEC_CODEREVIEW, "threads", 10);
    taskSleep = config.getInt(SEC_CODEREVIEW, "sleep", 10);
    LOG.info("Starting thread pool with " + threads + " initial threads.");
    pool = new ScheduledThreadPoolExecutor(threads);

    final RepositoryCache repoCache = createRepositoryCache();
    final HttpRpc rpc = createHttpRpc(configPath.getParentFile());
    backend = new Backend(repoCache, rpc, pool, createUserPersonIdent());
  }

  private RepositoryCache createRepositoryCache() {
    final File basedir = new File(required(SEC_CODEREVIEW, "basedir"));
    return new RepositoryCache(basedir);
  }

  private HttpRpc createHttpRpc(final File base) throws ThreadDeath {
    final URL serverUrl;
    try {
      serverUrl = new URL(required(SEC_CODEREVIEW, "server"));
    } catch (MalformedURLException err) {
      System.err.println("error: Bad URL in " + SEC_CODEREVIEW + ".server");
      System.exit(1);
      throw new ThreadDeath();
    }

    final String roleUser = config.getString(SEC_CODEREVIEW, null, "username");
    File pwf = new File(required(SEC_CODEREVIEW, "secureconfig"));
    if (!pwf.isAbsolute()) {
      pwf = new File(base, pwf.getPath());
    }

    final RepositoryConfig pwc = new RepositoryConfig(null, pwf);
    try {
      pwc.load();
    } catch (IOException e) {
      System.err.println("error: Cannot read secureconfig: " + pwf + ": "
          + e.getMessage());
      System.exit(1);
      throw new ThreadDeath();
    }

    final String rolePass = pwc.getString(SEC_CODEREVIEW, null, "password");
    final String apiKey = pwc.getString(SEC_CODEREVIEW, null, "internalapikey");
    return new HttpRpc(serverUrl, roleUser, rolePass, apiKey);
  }

  private PersonIdent createUserPersonIdent() {
    final String name = required("user", "name");
    final String email = required("user", "email");
    return new PersonIdent(name, email, System.currentTimeMillis(), 0);
  }

  private void addShutdownHook() {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        LOG.info("Shutting down thread pool.");
        pool.shutdownNow();
      }
    });
  }

  private void start() {
    schedule(new ReceivedBundleUnpacker(backend));
    schedule(new PendingMerger(backend));
    schedule(new BundlePruner(backend), FOUR_HOURS);
    schedule(new BuildPruner(backend), ONCE_PER_DAY);
  }

  private void schedule(final Runnable t) {
    schedule(t, taskSleep);
  }

  private void schedule(final Runnable t, final int sleep) {
    pool.scheduleWithFixedDelay(t, 0, sleep, TimeUnit.SECONDS);
  }

  private String required(final String sec, final String key) {
    final String r = config.getString(sec, null, key);
    if (r == null || r.length() == 0) {
      System.err.println("error: Missing required config " + sec + "." + key);
      System.exit(1);
    }
    return r;
  }

  private static void configureLogging(final RepositoryConfig config,
      final boolean interactive) {
    final Logger root = Logger.getLogger("");
    for (final Handler h : root.getHandlers())
      root.removeHandler(h);

    OutputStream out = System.out;
    final String logfile = config.getString(SEC_LOG, null, "file");
    if (logfile != null && !interactive) {
      try {
        out = new FileOutputStream(logfile, true);
      } catch (IOException err) {
        System.err.println("error: Cannot append to " + logfile);
        System.err.println("error: " + err.toString());
        System.exit(1);
        throw new ThreadDeath();
      }
    }

    final StreamHandler ch = new StreamHandler(out, new Formatter() {
      private final SimpleDateFormat sdf;
      private final StringWriter stringBuffer = new StringWriter();
      private final PrintWriter p = new PrintWriter(stringBuffer);
      {
        sdf = new SimpleDateFormat("yyyyMMdd.HHmmss");
      }

      @Override
      public String format(final LogRecord record) {
        stringBuffer.getBuffer().setLength(0);
        final String levelName = record.getLevel().getName();
        p.print(sdf.format(new Date(record.getMillis())));
        p.print(' ');
        p.print(levelName);
        for (int cnt = "WARNING".length() - levelName.length(); --cnt >= 0;) {
          p.print(' ');
        }
        p.print(' ');
        p.print(record.getLoggerName());
        p.print(" - ");
        p.print(record.getMessage());
        p.print('\n');
        if (record.getThrown() != null) {
          record.getThrown().printStackTrace(p);
          p.print('\n');
        }
        p.flush();
        return stringBuffer.toString();
      }
    }) {
      @Override
      public synchronized void publish(final LogRecord record) {
        super.publish(record);
        flush();
      }
    };
    root.addHandler(ch);

    final Level levelObj;
    final String levelStr = config.getString(SEC_LOG, null, "level");
    if (levelStr == null || levelStr.length() == 0) {
      levelObj = Level.INFO;
    } else if ("trace".equalsIgnoreCase(levelStr)) {
      levelObj = Level.FINEST;
    } else if ("debug".equalsIgnoreCase(levelStr)) {
      levelObj = Level.FINE;
    } else if ("info".equalsIgnoreCase(levelStr)) {
      levelObj = Level.INFO;
    } else if ("warning".equalsIgnoreCase(levelStr)
        || "warn".equalsIgnoreCase(levelStr)) {
      levelObj = Level.WARNING;
    } else if ("error".equalsIgnoreCase(levelStr)) {
      levelObj = Level.SEVERE;
    } else if ("fatal".equalsIgnoreCase(levelStr)) {
      levelObj = Level.SEVERE;
    } else {
      System.out.println("warning: Bad " + SEC_LOG + ".level " + levelStr
          + "; assuming info");
      levelObj = Level.INFO;
    }
    ch.setLevel(levelObj);
    root.setLevel(levelObj);

    Logger.getLogger("httpclient").setLevel(Level.WARNING);
    Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.WARNING);
  }
}