View Javadoc
1   package com.atlassian.activeobjects.servlet;
2   
3   import com.google.common.base.Predicate;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.util.Map;
8   
9   import static com.google.common.base.Preconditions.checkNotNull;
10  import static com.google.common.collect.Maps.filterEntries;
11  
12  public final class AdminUi {
13      private static final Logger log = LoggerFactory.getLogger(AdminUi.class);
14  
15      private final Map<String, Object> essentials;
16  
17      public AdminUi(Map<String, Object> essentials) {
18          this.essentials = checkNotNull(essentials);
19      }
20  
21      boolean isEnabled() {
22          final Map<String, Object> unavailable = filterEntries(essentials, new UnavailableServicePredicate());
23  
24          if (!unavailable.isEmpty()) {
25              log.debug("The admin UI is disabled because of the following services not being available:\n{}", unavailable.keySet());
26          }
27          return unavailable.isEmpty();
28      }
29  
30      static boolean isDevModeEnabled() {
31          return Boolean.getBoolean("atlassian.dev.mode");
32      }
33  
34      private static class UnavailableServicePredicate implements Predicate<Map.Entry<String, Object>> {
35          @Override
36          public boolean apply(Map.Entry<String, Object> entry) {
37              try {
38                  entry.getValue().toString(); // try toString as it shouldn't have side effects
39                  return false;
40              } catch (RuntimeException e) {
41                  if (e.getClass().getSimpleName().equals("ServiceUnavailableException")) {
42                      if (isDevModeEnabled()) {
43                          log.warn("Service is unavailable, admin UI will be disabled.", e);
44                      }
45                      return true;
46                  }
47                  throw e;
48              }
49          }
50      }
51  }