// Copyright (C) 2008 The Android Open Source Project
//
// 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.gerrit.server.ssh;

import com.google.gerrit.client.reviewdb.SystemConfig.LoginType;
import com.google.gerrit.client.rpc.Common;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/** Causes the caches to purge all entries and reload. */
class AdminFlushCaches extends AbstractCommand {
  PrintWriter p;

  @Override
  protected void run(String[] args) throws Failure,
      UnsupportedEncodingException {
    assertIsAdministrator();
    p = toPrintWriter(err);
    try {
      Common.getGroupCache().flush();
      Common.getProjectCache().flush();
      Common.getAccountCache().flush();

      if (Common.getGerritConfig().getLoginType() == LoginType.OPENID) {
        flushCache("openid");
      }

      try {
        getGerritServer().getDiffCache().flush();
      } catch (Throwable e1) {
        p.println("warning: cannot flush cache \"diff\": " + e1);
      }
    } finally {
      p.flush();
    }
  }

  private void flushCache(final String name) {
    try {
      getGerritServer().getCache(name).removeAll();
    } catch (Throwable e1) {
      p.println("warning: cannot flush cache \"" + name + "\": " + e1);
    }
  }
}

@@ -678,82 +678,112 @@
 nt.r
-eviewdb.SystemConfig.LoginType
+pc.Common
 ;%0A
+%0A
 import 
-com.google.gerrit.client.rpc.Common
+net.sf.ehcache.Ehcache;%0Aimport net.sf.ehcache.constructs.blocking.SelfPopulatingCache
 ;%0A%0Ai
@@ -1144,24 +1144,103 @@
 ;%0A    try %7B%0A
+      final SelfPopulatingCache diffCache = getGerritServer().getDiffCache();%0A%0A
       Common
@@ -1356,111 +1356,182 @@
     
-if (Common.getGerritConfig().getLoginType() == LoginType.OPENID) %7B%0A        flushCache(%22openid%22)
+for (final Ehcache c : getGerritServer().getAllCaches()) %7B%0A        final String name = c.getName();%0A        if (diffCache.getName().equals(name)) %7B%0A          continue
 ;%0A      
 %7D%0A%0A 
@@ -1526,19 +1526,22 @@
 ;%0A      
-%7D%0A%0A
+  %7D%0A  
       tr
@@ -1556,46 +1556,21 @@
     
-getGerritServer().getDiffCache().flush
+  c.removeAll
 ();%0A
@@ -1571,24 +1571,26 @@
 ll();%0A      
+ 
+ 
 %7D catch (Thr
@@ -1589,33 +1589,32 @@
 tch (Throwable e
-1
 ) %7B%0A        p.pr
@@ -1605,24 +1605,26 @@
 ) %7B%0A        
+ 
+ 
 p.println(%22w
@@ -1614,39 +1614,37 @@
      p.println(%22
-warning
+error
 : cannot flush c
@@ -1650,20 +1650,28 @@
 cache %5C%22
-diff
+%22 + name + %22
 %5C%22: %22 + 
@@ -1667,25 +1667,24 @@
 + %22%5C%22: %22 + e
-1
 );%0A      %7D%0A 
@@ -1676,26 +1676,66 @@
 + e);%0A      
+ 
+ 
 %7D%0A
+      %7D%0A%0A      saveToDisk(diffCache);%0A
     %7D finall
@@ -1785,98 +1785,61 @@
 oid 
-flushCache(final String name) %7B%0A    try %7B%0A      getGerritServer().getCache(name).removeAll
+saveToDisk(final Ehcache c) %7B%0A    try %7B%0A      c.flush
 ();%0A
@@ -1862,17 +1862,16 @@
 owable e
-1
 ) %7B%0A    
@@ -1891,37 +1891,36 @@
 warning: cannot 
-flush
+save
  cache %5C%22%22 + nam
@@ -1908,36 +1908,43 @@
 ave cache %5C%22%22 + 
-n
+c.getN
 ame
+()
  + %22%5C%22: %22 + e1);
@@ -1944,9 +1944,8 @@
  + e
-1
 );%0A 

Refactor gerrit flush-caches to just flush everything in Ehcache