]> source.dussan.org Git - gitblit.git/commitdiff
Merge branch 'issue-182' of git://git.quelltextlich.at/gitblit into issue182
authorJames Moger <james.moger@gitblit.com>
Thu, 28 Mar 2013 15:40:29 +0000 (11:40 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 28 Mar 2013 15:40:29 +0000 (11:40 -0400)
1  2 
src/main/java/com/gitblit/GitBlit.java

index 4cfd61e46638a5a31defa960d983ed2bc342b095,0000000000000000000000000000000000000000..0452e1da49c89a55f8027abca5fa3e160c3c0de4
mode 100644,000000..100644
--- /dev/null
@@@ -1,3396 -1,0 +1,3399 @@@
 +/*\r
 + * Copyright 2011 gitblit.com.\r
 + *\r
 + * Licensed under the Apache License, Version 2.0 (the "License");\r
 + * you may not use this file except in compliance with the License.\r
 + * You may obtain a copy of the License at\r
 + *\r
 + *     http://www.apache.org/licenses/LICENSE-2.0\r
 + *\r
 + * Unless required by applicable law or agreed to in writing, software\r
 + * distributed under the License is distributed on an "AS IS" BASIS,\r
 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
 + * See the License for the specific language governing permissions and\r
 + * limitations under the License.\r
 + */\r
 +package com.gitblit;\r
 +\r
 +import java.io.BufferedReader;\r
 +import java.io.File;\r
 +import java.io.FileFilter;\r
 +import java.io.IOException;\r
 +import java.io.InputStream;\r
 +import java.io.InputStreamReader;\r
 +import java.lang.reflect.Field;\r
 +import java.net.URI;\r
 +import java.net.URISyntaxException;\r
 +import java.nio.charset.Charset;\r
 +import java.security.Principal;\r
 +import java.text.MessageFormat;\r
 +import java.text.SimpleDateFormat;\r
 +import java.util.ArrayList;\r
 +import java.util.Arrays;\r
 +import java.util.Calendar;\r
 +import java.util.Collection;\r
 +import java.util.Collections;\r
 +import java.util.Date;\r
 +import java.util.HashMap;\r
 +import java.util.HashSet;\r
 +import java.util.LinkedHashMap;\r
 +import java.util.LinkedHashSet;\r
 +import java.util.List;\r
 +import java.util.Map;\r
 +import java.util.Map.Entry;\r
 +import java.util.Set;\r
 +import java.util.TimeZone;\r
 +import java.util.TreeMap;\r
 +import java.util.TreeSet;\r
 +import java.util.concurrent.ConcurrentHashMap;\r
 +import java.util.concurrent.Executors;\r
 +import java.util.concurrent.ScheduledExecutorService;\r
 +import java.util.concurrent.TimeUnit;\r
 +import java.util.concurrent.atomic.AtomicInteger;\r
 +import java.util.concurrent.atomic.AtomicReference;\r
 +\r
 +import javax.mail.Message;\r
 +import javax.mail.MessagingException;\r
 +import javax.servlet.ServletContext;\r
 +import javax.servlet.ServletContextEvent;\r
 +import javax.servlet.ServletContextListener;\r
 +import javax.servlet.http.Cookie;\r
 +import javax.servlet.http.HttpServletRequest;\r
 +\r
 +import org.apache.wicket.RequestCycle;\r
 +import org.apache.wicket.protocol.http.WebResponse;\r
 +import org.apache.wicket.resource.ContextRelativeResource;\r
 +import org.apache.wicket.util.resource.ResourceStreamNotFoundException;\r
 +import org.eclipse.jgit.lib.Repository;\r
 +import org.eclipse.jgit.lib.RepositoryCache;\r
 +import org.eclipse.jgit.lib.RepositoryCache.FileKey;\r
 +import org.eclipse.jgit.lib.StoredConfig;\r
 +import org.eclipse.jgit.storage.file.FileBasedConfig;\r
 +import org.eclipse.jgit.storage.file.WindowCache;\r
 +import org.eclipse.jgit.storage.file.WindowCacheConfig;\r
 +import org.eclipse.jgit.util.FS;\r
 +import org.eclipse.jgit.util.FileUtils;\r
 +import org.slf4j.Logger;\r
 +import org.slf4j.LoggerFactory;\r
 +\r
 +import com.gitblit.Constants.AccessPermission;\r
 +import com.gitblit.Constants.AccessRestrictionType;\r
 +import com.gitblit.Constants.AuthenticationType;\r
 +import com.gitblit.Constants.AuthorizationControl;\r
 +import com.gitblit.Constants.FederationRequest;\r
 +import com.gitblit.Constants.FederationStrategy;\r
 +import com.gitblit.Constants.FederationToken;\r
 +import com.gitblit.Constants.PermissionType;\r
 +import com.gitblit.Constants.RegistrantType;\r
 +import com.gitblit.fanout.FanoutNioService;\r
 +import com.gitblit.fanout.FanoutService;\r
 +import com.gitblit.fanout.FanoutSocketService;\r
 +import com.gitblit.models.FederationModel;\r
 +import com.gitblit.models.FederationProposal;\r
 +import com.gitblit.models.FederationSet;\r
 +import com.gitblit.models.ForkModel;\r
 +import com.gitblit.models.Metric;\r
 +import com.gitblit.models.ProjectModel;\r
 +import com.gitblit.models.RegistrantAccessPermission;\r
 +import com.gitblit.models.RepositoryModel;\r
 +import com.gitblit.models.SearchResult;\r
 +import com.gitblit.models.ServerSettings;\r
 +import com.gitblit.models.ServerStatus;\r
 +import com.gitblit.models.SettingModel;\r
 +import com.gitblit.models.TeamModel;\r
 +import com.gitblit.models.UserModel;\r
 +import com.gitblit.utils.ArrayUtils;\r
 +import com.gitblit.utils.Base64;\r
 +import com.gitblit.utils.ByteFormat;\r
 +import com.gitblit.utils.ContainerUtils;\r
 +import com.gitblit.utils.DeepCopier;\r
 +import com.gitblit.utils.FederationUtils;\r
 +import com.gitblit.utils.HttpUtils;\r
 +import com.gitblit.utils.JGitUtils;\r
 +import com.gitblit.utils.JsonUtils;\r
 +import com.gitblit.utils.MetricUtils;\r
 +import com.gitblit.utils.ObjectCache;\r
 +import com.gitblit.utils.StringUtils;\r
 +import com.gitblit.utils.TimeUtils;\r
 +import com.gitblit.utils.X509Utils.X509Metadata;\r
 +import com.gitblit.wicket.GitBlitWebSession;\r
 +import com.gitblit.wicket.WicketUtils;\r
 +\r
 +/**\r
 + * GitBlit is the servlet context listener singleton that acts as the core for\r
 + * the web ui and the servlets. This class is either directly instantiated by\r
 + * the GitBlitServer class (Gitblit GO) or is reflectively instantiated from the\r
 + * definition in the web.xml file (Gitblit WAR).\r
 + * \r
 + * This class is the central logic processor for Gitblit. All settings, user\r
 + * object, and repository object operations pass through this class.\r
 + * \r
 + * Repository Resolution. There are two pathways for finding repositories. One\r
 + * pathway, for web ui display and repository authentication & authorization, is\r
 + * within this class. The other pathway is through the standard GitServlet.\r
 + * \r
 + * @author James Moger\r
 + * \r
 + */\r
 +public class GitBlit implements ServletContextListener {\r
 +\r
 +      private static GitBlit gitblit;\r
 +      \r
 +      private final Logger logger = LoggerFactory.getLogger(GitBlit.class);\r
 +\r
 +      private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(5);\r
 +\r
 +      private final List<FederationModel> federationRegistrations = Collections\r
 +                      .synchronizedList(new ArrayList<FederationModel>());\r
 +\r
 +      private final Map<String, FederationModel> federationPullResults = new ConcurrentHashMap<String, FederationModel>();\r
 +\r
 +      private final ObjectCache<Long> repositorySizeCache = new ObjectCache<Long>();\r
 +\r
 +      private final ObjectCache<List<Metric>> repositoryMetricsCache = new ObjectCache<List<Metric>>();\r
 +      \r
 +      private final Map<String, RepositoryModel> repositoryListCache = new ConcurrentHashMap<String, RepositoryModel>();\r
 +      \r
 +      private final Map<String, ProjectModel> projectCache = new ConcurrentHashMap<String, ProjectModel>();\r
 +      \r
 +      private final AtomicReference<String> repositoryListSettingsChecksum = new AtomicReference<String>("");\r
 +      \r
 +      private final ObjectCache<String> projectMarkdownCache = new ObjectCache<String>();\r
 +      \r
 +      private final ObjectCache<String> projectRepositoriesMarkdownCache = new ObjectCache<String>();\r
 +\r
 +      private ServletContext servletContext;\r
 +      \r
 +      private File baseFolder;\r
 +\r
 +      private File repositoriesFolder;\r
 +\r
 +      private IUserService userService;\r
 +\r
 +      private IStoredSettings settings;\r
 +\r
 +      private ServerSettings settingsModel;\r
 +\r
 +      private ServerStatus serverStatus;\r
 +\r
 +      private MailExecutor mailExecutor;\r
 +      \r
 +      private LuceneExecutor luceneExecutor;\r
 +      \r
 +      private GCExecutor gcExecutor;\r
 +      \r
 +      private TimeZone timezone;\r
 +      \r
 +      private FileBasedConfig projectConfigs;\r
 +      \r
 +      private FanoutService fanoutService;\r
 +\r
 +      public GitBlit() {\r
 +              if (gitblit == null) {\r
 +                      // set the static singleton reference\r
 +                      gitblit = this;\r
 +              }\r
 +      }\r
 +\r
 +      public GitBlit(final IUserService userService) {\r
 +              this.userService = userService;\r
 +              gitblit = this;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the Gitblit singleton.\r
 +       * \r
 +       * @return gitblit singleton\r
 +       */\r
 +      public static GitBlit self() {\r
 +              if (gitblit == null) {\r
 +                      new GitBlit();\r
 +              }\r
 +              return gitblit;\r
 +      }\r
 +\r
 +      /**\r
 +       * Determine if this is the GO variant of Gitblit.\r
 +       * \r
 +       * @return true if this is the GO variant of Gitblit.\r
 +       */\r
 +      public static boolean isGO() {\r
 +              return self().settings instanceof FileSettings;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the preferred timezone for the Gitblit instance.\r
 +       * \r
 +       * @return a timezone\r
 +       */\r
 +      public static TimeZone getTimezone() {\r
 +              if (self().timezone == null) {\r
 +                      String tzid = getString("web.timezone", null);\r
 +                      if (StringUtils.isEmpty(tzid)) {\r
 +                              self().timezone = TimeZone.getDefault();\r
 +                              return self().timezone;\r
 +                      }\r
 +                      self().timezone = TimeZone.getTimeZone(tzid);\r
 +              }\r
 +              return self().timezone;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the user-defined blob encodings.\r
 +       * \r
 +       * @return an array of encodings, may be empty\r
 +       */\r
 +      public static String [] getEncodings() {\r
 +              return getStrings(Keys.web.blobEncodings).toArray(new String[0]);\r
 +      }\r
 +      \r
 +\r
 +      /**\r
 +       * Returns the boolean value for the specified key. If the key does not\r
 +       * exist or the value for the key can not be interpreted as a boolean, the\r
 +       * defaultValue is returned.\r
 +       * \r
 +       * @see IStoredSettings.getBoolean(String, boolean)\r
 +       * @param key\r
 +       * @param defaultValue\r
 +       * @return key value or defaultValue\r
 +       */\r
 +      public static boolean getBoolean(String key, boolean defaultValue) {\r
 +              return self().settings.getBoolean(key, defaultValue);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the integer value for the specified key. If the key does not\r
 +       * exist or the value for the key can not be interpreted as an integer, the\r
 +       * defaultValue is returned.\r
 +       * \r
 +       * @see IStoredSettings.getInteger(String key, int defaultValue)\r
 +       * @param key\r
 +       * @param defaultValue\r
 +       * @return key value or defaultValue\r
 +       */\r
 +      public static int getInteger(String key, int defaultValue) {\r
 +              return self().settings.getInteger(key, defaultValue);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the value in bytes for the specified key. If the key does not\r
 +       * exist or the value for the key can not be interpreted as an integer, the\r
 +       * defaultValue is returned.\r
 +       * \r
 +       * @see IStoredSettings.getFilesize(String key, int defaultValue)\r
 +       * @param key\r
 +       * @param defaultValue\r
 +       * @return key value or defaultValue\r
 +       */\r
 +      public static int getFilesize(String key, int defaultValue) {\r
 +              return self().settings.getFilesize(key, defaultValue);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the value in bytes for the specified key. If the key does not\r
 +       * exist or the value for the key can not be interpreted as a long, the\r
 +       * defaultValue is returned.\r
 +       * \r
 +       * @see IStoredSettings.getFilesize(String key, long defaultValue)\r
 +       * @param key\r
 +       * @param defaultValue\r
 +       * @return key value or defaultValue\r
 +       */\r
 +      public static long getFilesize(String key, long defaultValue) {\r
 +              return self().settings.getFilesize(key, defaultValue);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the char value for the specified key. If the key does not exist\r
 +       * or the value for the key can not be interpreted as a character, the\r
 +       * defaultValue is returned.\r
 +       * \r
 +       * @see IStoredSettings.getChar(String key, char defaultValue)\r
 +       * @param key\r
 +       * @param defaultValue\r
 +       * @return key value or defaultValue\r
 +       */\r
 +      public static char getChar(String key, char defaultValue) {\r
 +              return self().settings.getChar(key, defaultValue);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the string value for the specified key. If the key does not exist\r
 +       * or the value for the key can not be interpreted as a string, the\r
 +       * defaultValue is returned.\r
 +       * \r
 +       * @see IStoredSettings.getString(String key, String defaultValue)\r
 +       * @param key\r
 +       * @param defaultValue\r
 +       * @return key value or defaultValue\r
 +       */\r
 +      public static String getString(String key, String defaultValue) {\r
 +              return self().settings.getString(key, defaultValue);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns a list of space-separated strings from the specified key.\r
 +       * \r
 +       * @see IStoredSettings.getStrings(String key)\r
 +       * @param name\r
 +       * @return list of strings\r
 +       */\r
 +      public static List<String> getStrings(String key) {\r
 +              return self().settings.getStrings(key);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns a map of space-separated key-value pairs from the specified key.\r
 +       * \r
 +       * @see IStoredSettings.getStrings(String key)\r
 +       * @param name\r
 +       * @return map of string, string\r
 +       */\r
 +      public static Map<String, String> getMap(String key) {\r
 +              return self().settings.getMap(key);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of keys whose name starts with the specified prefix. If\r
 +       * the prefix is null or empty, all key names are returned.\r
 +       * \r
 +       * @see IStoredSettings.getAllKeys(String key)\r
 +       * @param startingWith\r
 +       * @return list of keys\r
 +       */\r
 +\r
 +      public static List<String> getAllKeys(String startingWith) {\r
 +              return self().settings.getAllKeys(startingWith);\r
 +      }\r
 +\r
 +      /**\r
 +       * Is Gitblit running in debug mode?\r
 +       * \r
 +       * @return true if Gitblit is running in debug mode\r
 +       */\r
 +      public static boolean isDebugMode() {\r
 +              return self().settings.getBoolean(Keys.web.debugMode, false);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the file object for the specified configuration key.\r
 +       * \r
 +       * @return the file\r
 +       */\r
 +      public static File getFileOrFolder(String key, String defaultFileOrFolder) {\r
 +              String fileOrFolder = GitBlit.getString(key, defaultFileOrFolder);\r
 +              return getFileOrFolder(fileOrFolder);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the file object which may have it's base-path determined by\r
 +       * environment variables for running on a cloud hosting service. All Gitblit\r
 +       * file or folder retrievals are (at least initially) funneled through this\r
 +       * method so it is the correct point to globally override/alter filesystem\r
 +       * access based on environment or some other indicator.\r
 +       * \r
 +       * @return the file\r
 +       */\r
 +      public static File getFileOrFolder(String fileOrFolder) {\r
 +              return com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$,\r
 +                              self().baseFolder, fileOrFolder);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the path of the repositories folder. This method checks to see if\r
 +       * Gitblit is running on a cloud service and may return an adjusted path.\r
 +       * \r
 +       * @return the repositories folder path\r
 +       */\r
 +      public static File getRepositoriesFolder() {\r
 +              return getFileOrFolder(Keys.git.repositoriesFolder, "${baseFolder}/git");\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the path of the proposals folder. This method checks to see if\r
 +       * Gitblit is running on a cloud service and may return an adjusted path.\r
 +       * \r
 +       * @return the proposals folder path\r
 +       */\r
 +      public static File getProposalsFolder() {\r
 +              return getFileOrFolder(Keys.federation.proposalsFolder, "${baseFolder}/proposals");\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the path of the Groovy folder. This method checks to see if\r
 +       * Gitblit is running on a cloud service and may return an adjusted path.\r
 +       * \r
 +       * @return the Groovy scripts folder path\r
 +       */\r
 +      public static File getGroovyScriptsFolder() {\r
 +              return getFileOrFolder(Keys.groovy.scriptsFolder, "${baseFolder}/groovy");\r
 +      }\r
 +      \r
 +      /**\r
 +       * Updates the list of server settings.\r
 +       * \r
 +       * @param settings\r
 +       * @return true if the update succeeded\r
 +       */\r
 +      public boolean updateSettings(Map<String, String> updatedSettings) {\r
 +              return settings.saveSettings(updatedSettings);\r
 +      }\r
 +\r
 +      public ServerStatus getStatus() {\r
 +              // update heap memory status\r
 +              serverStatus.heapAllocated = Runtime.getRuntime().totalMemory();\r
 +              serverStatus.heapFree = Runtime.getRuntime().freeMemory();\r
 +              return serverStatus;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of non-Gitblit clone urls. This allows Gitblit to\r
 +       * advertise alternative urls for Git client repository access.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @return list of non-gitblit clone urls\r
 +       */\r
 +      public List<String> getOtherCloneUrls(String repositoryName) {\r
 +              List<String> cloneUrls = new ArrayList<String>();\r
 +              for (String url : settings.getStrings(Keys.web.otherUrls)) {\r
 +                      cloneUrls.add(MessageFormat.format(url, repositoryName));\r
 +              }\r
 +              return cloneUrls;\r
 +      }\r
 +\r
 +      /**\r
 +       * Set the user service. The user service authenticates all users and is\r
 +       * responsible for managing user permissions.\r
 +       * \r
 +       * @param userService\r
 +       */\r
 +      public void setUserService(IUserService userService) {\r
 +              logger.info("Setting up user service " + userService.toString());\r
 +              this.userService = userService;\r
 +              this.userService.setup(settings);\r
 +      }\r
 +      \r
 +      public boolean supportsAddUser() {\r
 +              return supportsCredentialChanges(new UserModel(""));\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns true if the user's credentials can be changed.\r
 +       * \r
 +       * @param user\r
 +       * @return true if the user service supports credential changes\r
 +       */\r
 +      public boolean supportsCredentialChanges(UserModel user) {\r
 +              return (user != null && user.isLocalAccount()) || userService.supportsCredentialChanges();\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns true if the user's display name can be changed.\r
 +       * \r
 +       * @param user\r
 +       * @return true if the user service supports display name changes\r
 +       */\r
 +      public boolean supportsDisplayNameChanges(UserModel user) {\r
 +              return (user != null && user.isLocalAccount()) || userService.supportsDisplayNameChanges();\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns true if the user's email address can be changed.\r
 +       * \r
 +       * @param user\r
 +       * @return true if the user service supports email address changes\r
 +       */\r
 +      public boolean supportsEmailAddressChanges(UserModel user) {\r
 +              return (user != null && user.isLocalAccount()) || userService.supportsEmailAddressChanges();\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns true if the user's team memberships can be changed.\r
 +       * \r
 +       * @param user\r
 +       * @return true if the user service supports team membership changes\r
 +       */\r
 +      public boolean supportsTeamMembershipChanges(UserModel user) {\r
 +              return (user != null && user.isLocalAccount()) || userService.supportsTeamMembershipChanges();\r
 +      }\r
 +\r
 +      /**\r
 +       * Authenticate a user based on a username and password.\r
 +       * \r
 +       * @see IUserService.authenticate(String, char[])\r
 +       * @param username\r
 +       * @param password\r
 +       * @return a user object or null\r
 +       */\r
 +      public UserModel authenticate(String username, char[] password) {\r
 +              if (StringUtils.isEmpty(username)) {\r
 +                      // can not authenticate empty username\r
 +                      return null;\r
 +              }\r
 +              String pw = new String(password);\r
 +              if (StringUtils.isEmpty(pw)) {\r
 +                      // can not authenticate empty password\r
 +                      return null;\r
 +              }\r
 +\r
 +              // check to see if this is the federation user\r
 +              if (canFederate()) {\r
 +                      if (username.equalsIgnoreCase(Constants.FEDERATION_USER)) {\r
 +                              List<String> tokens = getFederationTokens();\r
 +                              if (tokens.contains(pw)) {\r
 +                                      // the federation user is an administrator\r
 +                                      UserModel federationUser = new UserModel(Constants.FEDERATION_USER);\r
 +                                      federationUser.canAdmin = true;\r
 +                                      return federationUser;\r
 +                              }\r
 +                      }\r
 +              }\r
 +\r
 +              // delegate authentication to the user service\r
 +              if (userService == null) {\r
 +                      return null;\r
 +              }\r
 +              return userService.authenticate(username, password);\r
 +      }\r
 +\r
 +      /**\r
 +       * Authenticate a user based on their cookie.\r
 +       * \r
 +       * @param cookies\r
 +       * @return a user object or null\r
 +       */\r
 +      protected UserModel authenticate(Cookie[] cookies) {\r
 +              if (userService == null) {\r
 +                      return null;\r
 +              }\r
 +              if (userService.supportsCookies()) {\r
 +                      if (cookies != null && cookies.length > 0) {\r
 +                              for (Cookie cookie : cookies) {\r
 +                                      if (cookie.getName().equals(Constants.NAME)) {\r
 +                                              String value = cookie.getValue();\r
 +                                              return userService.authenticate(value.toCharArray());\r
 +                                      }\r
 +                              }\r
 +                      }\r
 +              }\r
 +              return null;\r
 +      }\r
 +\r
 +      /**\r
 +       * Authenticate a user based on HTTP request parameters.\r
 +       * \r
 +       * Authentication by X509Certificate is tried first and then by cookie.\r
 +       * \r
 +       * @param httpRequest\r
 +       * @return a user object or null\r
 +       */\r
 +      public UserModel authenticate(HttpServletRequest httpRequest) {\r
 +              return authenticate(httpRequest, false);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Authenticate a user based on HTTP request parameters.\r
 +       * \r
 +       * Authentication by X509Certificate, servlet container principal, cookie,\r
 +       * and BASIC header.\r
 +       * \r
 +       * @param httpRequest\r
 +       * @param requiresCertificate\r
 +       * @return a user object or null\r
 +       */\r
 +      public UserModel authenticate(HttpServletRequest httpRequest, boolean requiresCertificate) {\r
 +              // try to authenticate by certificate\r
 +              boolean checkValidity = settings.getBoolean(Keys.git.enforceCertificateValidity, true);\r
 +              String [] oids = getStrings(Keys.git.certificateUsernameOIDs).toArray(new String[0]);\r
 +              UserModel model = HttpUtils.getUserModelFromCertificate(httpRequest, checkValidity, oids);\r
 +              if (model != null) {\r
 +                      // grab real user model and preserve certificate serial number\r
 +                      UserModel user = getUserModel(model.username);\r
 +                      X509Metadata metadata = HttpUtils.getCertificateMetadata(httpRequest);\r
 +                      if (user != null) {\r
 +                              flagWicketSession(AuthenticationType.CERTIFICATE);\r
 +                              logger.info(MessageFormat.format("{0} authenticated by client certificate {1} from {2}",\r
 +                                              user.username, metadata.serialNumber, httpRequest.getRemoteAddr()));\r
 +                              return user;\r
 +                      } else {\r
 +                              logger.warn(MessageFormat.format("Failed to find UserModel for {0}, attempted client certificate ({1}) authentication from {2}",\r
 +                                              model.username, metadata.serialNumber, httpRequest.getRemoteAddr()));\r
 +                      }\r
 +              }\r
 +              \r
 +              if (requiresCertificate) {\r
 +                      // caller requires client certificate authentication (e.g. git servlet)\r
 +                      return null;\r
 +              }\r
 +              \r
 +              // try to authenticate by servlet container principal\r
 +              Principal principal = httpRequest.getUserPrincipal();\r
 +              if (principal != null) {\r
 +                      UserModel user = getUserModel(principal.getName());\r
 +                      if (user != null) {\r
 +                              flagWicketSession(AuthenticationType.CONTAINER);\r
 +                              logger.info(MessageFormat.format("{0} authenticated by servlet container principal from {1}",\r
 +                                              user.username, httpRequest.getRemoteAddr()));\r
 +                              return user;\r
 +                      } else {\r
 +                              logger.warn(MessageFormat.format("Failed to find UserModel for {0}, attempted servlet container authentication from {1}",\r
 +                                              principal.getName(), httpRequest.getRemoteAddr()));\r
 +                      }\r
 +              }\r
 +              \r
 +              // try to authenticate by cookie\r
 +              if (allowCookieAuthentication()) {\r
 +                      UserModel user = authenticate(httpRequest.getCookies());\r
 +                      if (user != null) {\r
 +                              flagWicketSession(AuthenticationType.COOKIE);\r
 +                              logger.info(MessageFormat.format("{0} authenticated by cookie from {1}",\r
 +                                              user.username, httpRequest.getRemoteAddr()));\r
 +                              return user;\r
 +                      }\r
 +              }\r
 +              \r
 +              // try to authenticate by BASIC\r
 +              final String authorization = httpRequest.getHeader("Authorization");\r
 +              if (authorization != null && authorization.startsWith("Basic")) {\r
 +                      // Authorization: Basic base64credentials\r
 +                      String base64Credentials = authorization.substring("Basic".length()).trim();\r
 +                      String credentials = new String(Base64.decode(base64Credentials),\r
 +                                      Charset.forName("UTF-8"));\r
 +                      // credentials = username:password\r
 +                      final String[] values = credentials.split(":",2);\r
 +\r
 +                      if (values.length == 2) {\r
 +                              String username = values[0];\r
 +                              char[] password = values[1].toCharArray();\r
 +                              UserModel user = authenticate(username, password);\r
 +                              if (user != null) {\r
 +                                      flagWicketSession(AuthenticationType.CREDENTIALS);\r
 +                                      logger.info(MessageFormat.format("{0} authenticated by BASIC request header from {1}",\r
 +                                                      user.username, httpRequest.getRemoteAddr()));\r
 +                                      return user;\r
 +                              } else {\r
 +                                      logger.warn(MessageFormat.format("Failed login attempt for {0}, invalid credentials ({1}) from {2}", \r
 +                                                      username, credentials, httpRequest.getRemoteAddr()));\r
 +                              }\r
 +                      }\r
 +              }\r
 +              return null;\r
 +      }\r
 +      \r
 +      protected void flagWicketSession(AuthenticationType authenticationType) {\r
 +              RequestCycle requestCycle = RequestCycle.get();\r
 +              if (requestCycle != null) {\r
 +                      // flag the Wicket session, if this is a Wicket request\r
 +                      GitBlitWebSession session = GitBlitWebSession.get();\r
 +                      session.authenticationType = authenticationType;\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Open a file resource using the Servlet container.\r
 +       * @param file to open\r
 +       * @return InputStream of the opened file\r
 +       * @throws ResourceStreamNotFoundException\r
 +       */\r
 +      public InputStream getResourceAsStream(String file) throws ResourceStreamNotFoundException {\r
 +              ContextRelativeResource res = WicketUtils.getResource(file);\r
 +              return res.getResourceStream().getInputStream();\r
 +      }\r
 +\r
 +      /**\r
 +       * Sets a cookie for the specified user.\r
 +       * \r
 +       * @param response\r
 +       * @param user\r
 +       */\r
 +      public void setCookie(WebResponse response, UserModel user) {\r
 +              if (userService == null) {\r
 +                      return;\r
 +              }\r
 +              if (userService.supportsCookies()) {\r
 +                      Cookie userCookie;\r
 +                      if (user == null) {\r
 +                              // clear cookie for logout\r
 +                              userCookie = new Cookie(Constants.NAME, "");\r
 +                      } else {\r
 +                              // set cookie for login\r
 +                              String cookie = userService.getCookie(user);\r
 +                              if (StringUtils.isEmpty(cookie)) {\r
 +                                      // create empty cookie\r
 +                                      userCookie = new Cookie(Constants.NAME, "");\r
 +                              } else {\r
 +                                      // create real cookie\r
 +                                      userCookie = new Cookie(Constants.NAME, cookie);\r
 +                                      userCookie.setMaxAge(Integer.MAX_VALUE);\r
 +                              }\r
 +                      }\r
 +                      userCookie.setPath("/");\r
 +                      response.addCookie(userCookie);\r
 +              }\r
 +      }\r
 +      \r
 +      /**\r
 +       * Logout a user.\r
 +       * \r
 +       * @param user\r
 +       */\r
 +      public void logout(UserModel user) {\r
 +              if (userService == null) {\r
 +                      return;\r
 +              }\r
 +              userService.logout(user);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of all users available to the login service.\r
 +       * \r
 +       * @see IUserService.getAllUsernames()\r
 +       * @return list of all usernames\r
 +       */\r
 +      public List<String> getAllUsernames() {\r
 +              List<String> names = new ArrayList<String>(userService.getAllUsernames());\r
 +              return names;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of all users available to the login service.\r
 +       * \r
 +       * @see IUserService.getAllUsernames()\r
 +       * @return list of all usernames\r
 +       */\r
 +      public List<UserModel> getAllUsers() {\r
 +              List<UserModel> users = userService.getAllUsers();\r
 +              return users;\r
 +      }\r
 +\r
 +      /**\r
 +       * Delete the user object with the specified username\r
 +       * \r
 +       * @see IUserService.deleteUser(String)\r
 +       * @param username\r
 +       * @return true if successful\r
 +       */\r
 +      public boolean deleteUser(String username) {\r
 +              if (StringUtils.isEmpty(username)) {\r
 +                      return false;\r
 +              }\r
 +              return userService.deleteUser(username);\r
 +      }\r
 +\r
 +      /**\r
 +       * Retrieve the user object for the specified username.\r
 +       * \r
 +       * @see IUserService.getUserModel(String)\r
 +       * @param username\r
 +       * @return a user object or null\r
 +       */\r
 +      public UserModel getUserModel(String username) {\r
 +              if (StringUtils.isEmpty(username)) {\r
 +                      return null;\r
 +              }\r
 +              UserModel user = userService.getUserModel(username);            \r
 +              return user;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the effective list of permissions for this user, taking into account\r
 +       * team memberships, ownerships.\r
 +       * \r
 +       * @param user\r
 +       * @return the effective list of permissions for the user\r
 +       */\r
 +      public List<RegistrantAccessPermission> getUserAccessPermissions(UserModel user) {\r
 +              if (StringUtils.isEmpty(user.username)) {\r
 +                      // new user\r
 +                      return new ArrayList<RegistrantAccessPermission>();\r
 +              }\r
 +              Set<RegistrantAccessPermission> set = new LinkedHashSet<RegistrantAccessPermission>();\r
 +              set.addAll(user.getRepositoryPermissions());\r
 +              // Flag missing repositories\r
 +              for (RegistrantAccessPermission permission : set) {\r
 +                      if (permission.mutable && PermissionType.EXPLICIT.equals(permission.permissionType)) {\r
 +                              RepositoryModel rm = GitBlit.self().getRepositoryModel(permission.registrant);\r
 +                              if (rm == null) {\r
 +                                      permission.permissionType = PermissionType.MISSING;\r
 +                                      permission.mutable = false;\r
 +                                      continue;\r
 +                              }\r
 +                      }\r
 +              }\r
 +\r
 +              // TODO reconsider ownership as a user property\r
 +              // manually specify personal repository ownerships\r
 +              for (RepositoryModel rm : repositoryListCache.values()) {\r
 +                      if (rm.isUsersPersonalRepository(user.username) || rm.isOwner(user.username)) {\r
 +                              RegistrantAccessPermission rp = new RegistrantAccessPermission(rm.name, AccessPermission.REWIND,\r
 +                                              PermissionType.OWNER, RegistrantType.REPOSITORY, null, false);\r
 +                              // user may be owner of a repository to which they've inherited\r
 +                              // a team permission, replace any existing perm with owner perm\r
 +                              set.remove(rp);\r
 +                              set.add(rp);\r
 +                      }\r
 +              }\r
 +              \r
 +              List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>(set);\r
 +              Collections.sort(list);\r
 +              return list;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of users and their access permissions for the specified\r
 +       * repository including permission source information such as the team or\r
 +       * regular expression which sets the permission.\r
 +       * \r
 +       * @param repository\r
 +       * @return a list of RegistrantAccessPermissions\r
 +       */\r
 +      public List<RegistrantAccessPermission> getUserAccessPermissions(RepositoryModel repository) {\r
 +              List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();\r
 +              if (AccessRestrictionType.NONE.equals(repository.accessRestriction)) {\r
 +                      // no permissions needed, REWIND for everyone!\r
 +                      return list;\r
 +              }\r
 +              if (AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl)) {\r
 +                      // no permissions needed, REWIND for authenticated!\r
 +                      return list;\r
 +              }\r
 +              // NAMED users and teams\r
 +              for (UserModel user : userService.getAllUsers()) {\r
 +                      RegistrantAccessPermission ap = user.getRepositoryPermission(repository);\r
 +                      if (ap.permission.exceeds(AccessPermission.NONE)) {\r
 +                              list.add(ap);\r
 +                      }\r
 +              }\r
 +              return list;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Sets the access permissions to the specified repository for the specified users.\r
 +       * \r
 +       * @param repository\r
 +       * @param permissions\r
 +       * @return true if the user models have been updated\r
 +       */\r
 +      public boolean setUserAccessPermissions(RepositoryModel repository, Collection<RegistrantAccessPermission> permissions) {\r
 +              List<UserModel> users = new ArrayList<UserModel>();\r
 +              for (RegistrantAccessPermission up : permissions) {\r
 +                      if (up.mutable) {\r
 +                              // only set editable defined permissions\r
 +                              UserModel user = userService.getUserModel(up.registrant);\r
 +                              user.setRepositoryPermission(repository.name, up.permission);\r
 +                              users.add(user);\r
 +                      }\r
 +              }\r
 +              return userService.updateUserModels(users);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the list of all users who have an explicit access permission\r
 +       * for the specified repository.\r
 +       * \r
 +       * @see IUserService.getUsernamesForRepositoryRole(String)\r
 +       * @param repository\r
 +       * @return list of all usernames that have an access permission for the repository\r
 +       */\r
 +      public List<String> getRepositoryUsers(RepositoryModel repository) {\r
 +              return userService.getUsernamesForRepositoryRole(repository.name);\r
 +      }\r
 +\r
 +      /**\r
 +       * Sets the list of all uses who are allowed to bypass the access\r
 +       * restriction placed on the specified repository.\r
 +       * \r
 +       * @see IUserService.setUsernamesForRepositoryRole(String, List<String>)\r
 +       * @param repository\r
 +       * @param usernames\r
 +       * @return true if successful\r
 +       */\r
 +      @Deprecated\r
 +      public boolean setRepositoryUsers(RepositoryModel repository, List<String> repositoryUsers) {\r
 +              // rejects all changes since 1.2.0 because this would elevate\r
 +              // all discrete access permissions to RW+\r
 +              return false;\r
 +      }\r
 +\r
 +      /**\r
 +       * Adds/updates a complete user object keyed by username. This method allows\r
 +       * for renaming a user.\r
 +       * \r
 +       * @see IUserService.updateUserModel(String, UserModel)\r
 +       * @param username\r
 +       * @param user\r
 +       * @param isCreate\r
 +       * @throws GitBlitException\r
 +       */\r
 +      public void updateUserModel(String username, UserModel user, boolean isCreate)\r
 +                      throws GitBlitException {\r
 +              if (!username.equalsIgnoreCase(user.username)) {\r
 +                      if (userService.getUserModel(user.username) != null) {\r
 +                              throw new GitBlitException(MessageFormat.format(\r
 +                                              "Failed to rename ''{0}'' because ''{1}'' already exists.", username,\r
 +                                              user.username));\r
 +                      }\r
 +                      \r
 +                      // rename repositories and owner fields for all repositories\r
 +                      for (RepositoryModel model : getRepositoryModels(user)) {\r
 +                              if (model.isUsersPersonalRepository(username)) {\r
 +                                      // personal repository\r
 +                                      model.addOwner(user.username);\r
 +                                      String oldRepositoryName = model.name;\r
 +                                      model.name = "~" + user.username + model.name.substring(model.projectPath.length());\r
 +                                      model.projectPath = "~" + user.username;\r
 +                                      updateRepositoryModel(oldRepositoryName, model, false);\r
 +                              } else if (model.isOwner(username)) {\r
 +                                      // common/shared repo\r
 +                                      model.addOwner(user.username);\r
 +                                      updateRepositoryModel(model.name, model, false);\r
 +                              }\r
 +                      }\r
 +              }\r
 +              if (!userService.updateUserModel(username, user)) {\r
 +                      throw new GitBlitException(isCreate ? "Failed to add user!" : "Failed to update user!");\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of available teams that a user or repository may be\r
 +       * assigned to.\r
 +       * \r
 +       * @return the list of teams\r
 +       */\r
 +      public List<String> getAllTeamnames() {\r
 +              List<String> teams = new ArrayList<String>(userService.getAllTeamNames());\r
 +              return teams;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of available teams that a user or repository may be\r
 +       * assigned to.\r
 +       * \r
 +       * @return the list of teams\r
 +       */\r
 +      public List<TeamModel> getAllTeams() {\r
 +              List<TeamModel> teams = userService.getAllTeams();\r
 +              return teams;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the TeamModel object for the specified name.\r
 +       * \r
 +       * @param teamname\r
 +       * @return a TeamModel object or null\r
 +       */\r
 +      public TeamModel getTeamModel(String teamname) {\r
 +              return userService.getTeamModel(teamname);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the list of teams and their access permissions for the specified\r
 +       * repository including the source of the permission such as the admin flag\r
 +       * or a regular expression.\r
 +       * \r
 +       * @param repository\r
 +       * @return a list of RegistrantAccessPermissions\r
 +       */\r
 +      public List<RegistrantAccessPermission> getTeamAccessPermissions(RepositoryModel repository) {\r
 +              List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();\r
 +              for (TeamModel team : userService.getAllTeams()) {\r
 +                      RegistrantAccessPermission ap = team.getRepositoryPermission(repository);\r
 +                      if (ap.permission.exceeds(AccessPermission.NONE)) {\r
 +                              list.add(ap);\r
 +                      }\r
 +              }\r
 +              Collections.sort(list);\r
 +              return list;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Sets the access permissions to the specified repository for the specified teams.\r
 +       * \r
 +       * @param repository\r
 +       * @param permissions\r
 +       * @return true if the team models have been updated\r
 +       */\r
 +      public boolean setTeamAccessPermissions(RepositoryModel repository, Collection<RegistrantAccessPermission> permissions) {\r
 +              List<TeamModel> teams = new ArrayList<TeamModel>();\r
 +              for (RegistrantAccessPermission tp : permissions) {\r
 +                      if (tp.mutable) {\r
 +                              // only set explicitly defined access permissions\r
 +                              TeamModel team = userService.getTeamModel(tp.registrant);\r
 +                              team.setRepositoryPermission(repository.name, tp.permission);\r
 +                              teams.add(team);\r
 +                      }\r
 +              }\r
 +              return userService.updateTeamModels(teams);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the list of all teams who have an explicit access permission for\r
 +       * the specified repository.\r
 +       * \r
 +       * @see IUserService.getTeamnamesForRepositoryRole(String)\r
 +       * @param repository\r
 +       * @return list of all teamnames with explicit access permissions to the repository\r
 +       */\r
 +      public List<String> getRepositoryTeams(RepositoryModel repository) {\r
 +              return userService.getTeamnamesForRepositoryRole(repository.name);\r
 +      }\r
 +\r
 +      /**\r
 +       * Sets the list of all uses who are allowed to bypass the access\r
 +       * restriction placed on the specified repository.\r
 +       * \r
 +       * @see IUserService.setTeamnamesForRepositoryRole(String, List<String>)\r
 +       * @param repository\r
 +       * @param teamnames\r
 +       * @return true if successful\r
 +       */\r
 +      @Deprecated\r
 +      public boolean setRepositoryTeams(RepositoryModel repository, List<String> repositoryTeams) {\r
 +              // rejects all changes since 1.2.0 because this would elevate\r
 +              // all discrete access permissions to RW+\r
 +              return false;\r
 +      }\r
 +\r
 +      /**\r
 +       * Updates the TeamModel object for the specified name.\r
 +       * \r
 +       * @param teamname\r
 +       * @param team\r
 +       * @param isCreate\r
 +       */\r
 +      public void updateTeamModel(String teamname, TeamModel team, boolean isCreate)\r
 +                      throws GitBlitException {\r
 +              if (!teamname.equalsIgnoreCase(team.name)) {\r
 +                      if (userService.getTeamModel(team.name) != null) {\r
 +                              throw new GitBlitException(MessageFormat.format(\r
 +                                              "Failed to rename ''{0}'' because ''{1}'' already exists.", teamname,\r
 +                                              team.name));\r
 +                      }\r
 +              }\r
 +              if (!userService.updateTeamModel(teamname, team)) {\r
 +                      throw new GitBlitException(isCreate ? "Failed to add team!" : "Failed to update team!");\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Delete the team object with the specified teamname\r
 +       * \r
 +       * @see IUserService.deleteTeam(String)\r
 +       * @param teamname\r
 +       * @return true if successful\r
 +       */\r
 +      public boolean deleteTeam(String teamname) {\r
 +              return userService.deleteTeam(teamname);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Adds the repository to the list of cached repositories if Gitblit is\r
 +       * configured to cache the repository list.\r
 +       * \r
 +       * @param model\r
 +       */\r
 +      private void addToCachedRepositoryList(RepositoryModel model) {\r
 +              if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) {\r
 +                      repositoryListCache.put(model.name.toLowerCase(), model);\r
 +                      \r
 +                      // update the fork origin repository with this repository clone\r
 +                      if (!StringUtils.isEmpty(model.originRepository)) {\r
 +                              if (repositoryListCache.containsKey(model.originRepository)) {\r
 +                                      RepositoryModel origin = repositoryListCache.get(model.originRepository);\r
 +                                      origin.addFork(model.name);\r
 +                              }\r
 +                      }\r
 +              }\r
 +      }\r
 +      \r
 +      /**\r
 +       * Removes the repository from the list of cached repositories.\r
 +       * \r
 +       * @param name\r
 +       * @return the model being removed\r
 +       */\r
 +      private RepositoryModel removeFromCachedRepositoryList(String name) {\r
 +              if (StringUtils.isEmpty(name)) {\r
 +                      return null;\r
 +              }\r
 +              return repositoryListCache.remove(name.toLowerCase());\r
 +      }\r
 +\r
 +      /**\r
 +       * Clears all the cached metadata for the specified repository.\r
 +       * \r
 +       * @param repositoryName\r
 +       */\r
 +      private void clearRepositoryMetadataCache(String repositoryName) {\r
 +              repositorySizeCache.remove(repositoryName);\r
 +              repositoryMetricsCache.remove(repositoryName);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Resets the repository list cache.\r
 +       * \r
 +       */\r
 +      public void resetRepositoryListCache() {\r
 +              logger.info("Repository cache manually reset");\r
 +              repositoryListCache.clear();\r
 +      }\r
 +      \r
 +      /**\r
 +       * Calculate the checksum of settings that affect the repository list cache.\r
 +       * @return a checksum\r
 +       */\r
 +      private String getRepositoryListSettingsChecksum() {\r
 +              StringBuilder ns = new StringBuilder();\r
 +              ns.append(settings.getString(Keys.git.cacheRepositoryList, "")).append('\n');\r
 +              ns.append(settings.getString(Keys.git.onlyAccessBareRepositories, "")).append('\n');\r
 +              ns.append(settings.getString(Keys.git.searchRepositoriesSubfolders, "")).append('\n');\r
 +              ns.append(settings.getString(Keys.git.searchRecursionDepth, "")).append('\n');\r
 +              ns.append(settings.getString(Keys.git.searchExclusions, "")).append('\n');\r
 +              String checksum = StringUtils.getSHA1(ns.toString());\r
 +              return checksum;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Compare the last repository list setting checksum to the current checksum.\r
 +       * If different then clear the cache so that it may be rebuilt.\r
 +       * \r
 +       * @return true if the cached repository list is valid since the last check\r
 +       */\r
 +      private boolean isValidRepositoryList() {\r
 +              String newChecksum = getRepositoryListSettingsChecksum();\r
 +              boolean valid = newChecksum.equals(repositoryListSettingsChecksum.get());\r
 +              repositoryListSettingsChecksum.set(newChecksum);\r
 +              if (!valid && settings.getBoolean(Keys.git.cacheRepositoryList,  true)) {\r
 +                      logger.info("Repository list settings have changed. Clearing repository list cache.");\r
 +                      repositoryListCache.clear();\r
 +              }\r
 +              return valid;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of all repositories available to Gitblit. This method\r
 +       * does not consider user access permissions.\r
 +       * \r
 +       * @return list of all repositories\r
 +       */\r
 +      public List<String> getRepositoryList() {\r
 +              if (repositoryListCache.size() == 0 || !isValidRepositoryList()) {\r
 +                      // we are not caching OR we have not yet cached OR the cached list is invalid\r
 +                      long startTime = System.currentTimeMillis();\r
 +                      List<String> repositories = JGitUtils.getRepositoryList(repositoriesFolder, \r
 +                                      settings.getBoolean(Keys.git.onlyAccessBareRepositories, false),\r
 +                                      settings.getBoolean(Keys.git.searchRepositoriesSubfolders, true),\r
 +                                      settings.getInteger(Keys.git.searchRecursionDepth, -1),\r
 +                                      settings.getStrings(Keys.git.searchExclusions));\r
 +\r
 +                      if (!settings.getBoolean(Keys.git.cacheRepositoryList,  true)) {\r
 +                              // we are not caching\r
 +                              StringUtils.sortRepositorynames(repositories);\r
 +                              return repositories;\r
 +                      } else {\r
 +                              // we are caching this list\r
 +                              String msg = "{0} repositories identified in {1} msecs";\r
 +\r
 +                              // optionally (re)calculate repository sizes\r
 +                              if (getBoolean(Keys.web.showRepositorySizes, true)) {\r
 +                                      msg = "{0} repositories identified with calculated folder sizes in {1} msecs";\r
 +                                      for (String repository : repositories) {\r
 +                                              RepositoryModel model = getRepositoryModel(repository);\r
 +                                              if (!model.skipSizeCalculation) {\r
 +                                                      calculateSize(model);\r
 +                                              }\r
 +                                      }\r
 +                              } else {\r
 +                                      // update cache\r
 +                                      for (String repository : repositories) {\r
 +                                              getRepositoryModel(repository);\r
 +                                      }\r
 +                              }\r
 +                              \r
 +                              // rebuild fork networks\r
 +                              for (RepositoryModel model : repositoryListCache.values()) {\r
 +                                      if (!StringUtils.isEmpty(model.originRepository)) {\r
 +                                              if (repositoryListCache.containsKey(model.originRepository)) {\r
 +                                                      RepositoryModel origin = repositoryListCache.get(model.originRepository);\r
 +                                                      origin.addFork(model.name);\r
 +                                              }\r
 +                                      }\r
 +                              }\r
 +                              \r
 +                              long duration = System.currentTimeMillis() - startTime;\r
 +                              logger.info(MessageFormat.format(msg, repositoryListCache.size(), duration));\r
 +                      }\r
 +              }\r
 +              \r
 +              // return sorted copy of cached list\r
 +              List<String> list = new ArrayList<String>(repositoryListCache.keySet());                \r
 +              StringUtils.sortRepositorynames(list);\r
 +              return list;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the JGit repository for the specified name.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @return repository or null\r
 +       */\r
 +      public Repository getRepository(String repositoryName) {\r
 +              return getRepository(repositoryName, true);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the JGit repository for the specified name.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @param logError\r
 +       * @return repository or null\r
 +       */\r
 +      public Repository getRepository(String repositoryName, boolean logError) {\r
 +              if (isCollectingGarbage(repositoryName)) {\r
 +                      logger.warn(MessageFormat.format("Rejecting request for {0}, busy collecting garbage!", repositoryName));\r
 +                      return null;\r
 +              }\r
 +\r
 +              File dir = FileKey.resolve(new File(repositoriesFolder, repositoryName), FS.DETECTED);\r
 +              if (dir == null)\r
 +                      return null;\r
 +              \r
 +              Repository r = null;\r
 +              try {\r
 +                      FileKey key = FileKey.exact(dir, FS.DETECTED);\r
 +                      r = RepositoryCache.open(key, true);\r
 +              } catch (IOException e) {\r
 +                      if (logError) {\r
 +                              logger.error("GitBlit.getRepository(String) failed to find "\r
 +                                              + new File(repositoriesFolder, repositoryName).getAbsolutePath());\r
 +                      }\r
 +              }\r
 +              return r;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of repository models that are accessible to the user.\r
 +       * \r
 +       * @param user\r
 +       * @return list of repository models accessible to user\r
 +       */\r
 +      public List<RepositoryModel> getRepositoryModels(UserModel user) {\r
 +              long methodStart = System.currentTimeMillis();\r
 +              List<String> list = getRepositoryList();\r
 +              List<RepositoryModel> repositories = new ArrayList<RepositoryModel>();\r
 +              for (String repo : list) {\r
 +                      RepositoryModel model = getRepositoryModel(user, repo);\r
 +                      if (model != null) {\r
 +                              repositories.add(model);\r
 +                      }\r
 +              }\r
 +              if (getBoolean(Keys.web.showRepositorySizes, true)) {\r
 +                      int repoCount = 0;\r
 +                      long startTime = System.currentTimeMillis();\r
 +                      ByteFormat byteFormat = new ByteFormat();\r
 +                      for (RepositoryModel model : repositories) {\r
 +                              if (!model.skipSizeCalculation) {\r
 +                                      repoCount++;\r
 +                                      model.size = byteFormat.format(calculateSize(model));\r
 +                              }\r
 +                      }\r
 +                      long duration = System.currentTimeMillis() - startTime;\r
 +                      if (duration > 250) {\r
 +                              // only log calcualtion time if > 250 msecs\r
 +                              logger.info(MessageFormat.format("{0} repository sizes calculated in {1} msecs",\r
 +                                      repoCount, duration));\r
 +                      }\r
 +              }\r
 +              long duration = System.currentTimeMillis() - methodStart;\r
 +              logger.info(MessageFormat.format("{0} repository models loaded for {1} in {2} msecs",\r
 +                              repositories.size(), user == null ? "anonymous" : user.username, duration));\r
 +              return repositories;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns a repository model if the repository exists and the user may\r
 +       * access the repository.\r
 +       * \r
 +       * @param user\r
 +       * @param repositoryName\r
 +       * @return repository model or null\r
 +       */\r
 +      public RepositoryModel getRepositoryModel(UserModel user, String repositoryName) {\r
 +              RepositoryModel model = getRepositoryModel(repositoryName);\r
 +              if (model == null) {\r
 +                      return null;\r
 +              }\r
 +              if (user == null) {\r
 +                      user = UserModel.ANONYMOUS;\r
 +              }\r
 +              if (user.canView(model)) {\r
 +                      return model;\r
 +              }\r
 +              return null;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the repository model for the specified repository. This method\r
 +       * does not consider user access permissions.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @return repository model or null\r
 +       */\r
 +      public RepositoryModel getRepositoryModel(String repositoryName) {\r
 +              if (!repositoryListCache.containsKey(repositoryName)) {\r
 +                      RepositoryModel model = loadRepositoryModel(repositoryName);\r
 +                      if (model == null) {\r
 +                              return null;\r
 +                      }\r
 +                      addToCachedRepositoryList(model);\r
 +                      return model;\r
 +              }\r
 +              \r
 +              // cached model\r
 +              RepositoryModel model = repositoryListCache.get(repositoryName.toLowerCase());\r
 +\r
 +              if (gcExecutor.isCollectingGarbage(model.name)) {\r
 +                      // Gitblit is busy collecting garbage, use our cached model\r
 +                      RepositoryModel rm = DeepCopier.copy(model);\r
 +                      rm.isCollectingGarbage = true;\r
 +                      return rm;\r
 +              }\r
 +\r
 +              // check for updates\r
 +              Repository r = getRepository(model.name);\r
 +              if (r == null) {\r
 +                      // repository is missing\r
 +                      removeFromCachedRepositoryList(repositoryName);\r
 +                      logger.error(MessageFormat.format("Repository \"{0}\" is missing! Removing from cache.", repositoryName));\r
 +                      return null;\r
 +              }\r
 +              \r
 +              FileBasedConfig config = (FileBasedConfig) getRepositoryConfig(r);\r
 +              if (config.isOutdated()) {\r
 +                      // reload model\r
 +                      logger.info(MessageFormat.format("Config for \"{0}\" has changed. Reloading model and updating cache.", repositoryName));\r
 +                      model = loadRepositoryModel(model.name);\r
 +                      removeFromCachedRepositoryList(model.name);\r
 +                      addToCachedRepositoryList(model);\r
 +              } else {\r
 +                      // update a few repository parameters \r
 +                      if (!model.hasCommits) {\r
 +                              // update hasCommits, assume a repository only gains commits :)\r
 +                              model.hasCommits = JGitUtils.hasCommits(r);\r
 +                      }\r
 +\r
 +                      model.lastChange = JGitUtils.getLastChange(r);\r
 +              }\r
 +              r.close();\r
 +              \r
 +              // return a copy of the cached model\r
 +              return DeepCopier.copy(model);\r
 +      }\r
 +      \r
 +      \r
 +      /**\r
 +       * Returns the map of project config.  This map is cached and reloaded if\r
 +       * the underlying projects.conf file changes.\r
 +       * \r
 +       * @return project config map\r
 +       */\r
 +      private Map<String, ProjectModel> getProjectConfigs() {\r
 +              if (projectCache.isEmpty() || projectConfigs.isOutdated()) {\r
 +                      \r
 +                      try {\r
 +                              projectConfigs.load();\r
 +                      } catch (Exception e) {\r
 +                      }\r
 +\r
 +                      // project configs\r
 +                      String rootName = GitBlit.getString(Keys.web.repositoryRootGroupName, "main");\r
 +                      ProjectModel rootProject = new ProjectModel(rootName, true);\r
 +\r
 +                      Map<String, ProjectModel> configs = new HashMap<String, ProjectModel>();\r
 +                      // cache the root project under its alias and an empty path\r
 +                      configs.put("", rootProject);\r
 +                      configs.put(rootProject.name.toLowerCase(), rootProject);\r
 +\r
 +                      for (String name : projectConfigs.getSubsections("project")) {\r
 +                              ProjectModel project;\r
 +                              if (name.equalsIgnoreCase(rootName)) {\r
 +                                      project = rootProject;\r
 +                              } else {\r
 +                                      project = new ProjectModel(name);\r
 +                              }\r
 +                              project.title = projectConfigs.getString("project", name, "title");\r
 +                              project.description = projectConfigs.getString("project", name, "description");\r
 +                              \r
 +                              // project markdown\r
 +                              File pmkd = new File(getRepositoriesFolder(), (project.isRoot ? "" : name) + "/project.mkd");\r
 +                              if (pmkd.exists()) {\r
 +                                      Date lm = new Date(pmkd.lastModified());\r
 +                                      if (!projectMarkdownCache.hasCurrent(name, lm)) {\r
 +                                              String mkd = com.gitblit.utils.FileUtils.readContent(pmkd,  "\n");\r
 +                                              projectMarkdownCache.updateObject(name, lm, mkd);\r
 +                                      }\r
 +                                      project.projectMarkdown = projectMarkdownCache.getObject(name);\r
 +                              }\r
 +                              \r
 +                              // project repositories markdown\r
 +                              File rmkd = new File(getRepositoriesFolder(), (project.isRoot ? "" : name) + "/repositories.mkd");\r
 +                              if (rmkd.exists()) {\r
 +                                      Date lm = new Date(rmkd.lastModified());\r
 +                                      if (!projectRepositoriesMarkdownCache.hasCurrent(name, lm)) {\r
 +                                              String mkd = com.gitblit.utils.FileUtils.readContent(rmkd,  "\n");\r
 +                                              projectRepositoriesMarkdownCache.updateObject(name, lm, mkd);\r
 +                                      }\r
 +                                      project.repositoriesMarkdown = projectRepositoriesMarkdownCache.getObject(name);\r
 +                              }\r
 +                              \r
 +                              configs.put(name.toLowerCase(), project);\r
 +                      }\r
 +                      projectCache.clear();\r
 +                      projectCache.putAll(configs);\r
 +              }\r
 +              return projectCache;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns a list of project models for the user.\r
 +       * \r
 +       * @param user\r
 +       * @param includeUsers\r
 +       * @return list of projects that are accessible to the user\r
 +       */\r
 +      public List<ProjectModel> getProjectModels(UserModel user, boolean includeUsers) {\r
 +              Map<String, ProjectModel> configs = getProjectConfigs();\r
 +\r
 +              // per-user project lists, this accounts for security and visibility\r
 +              Map<String, ProjectModel> map = new TreeMap<String, ProjectModel>();\r
 +              // root project\r
 +              map.put("", configs.get(""));\r
 +              \r
 +              for (RepositoryModel model : getRepositoryModels(user)) {\r
 +                      String rootPath = StringUtils.getRootPath(model.name).toLowerCase();                    \r
 +                      if (!map.containsKey(rootPath)) {\r
 +                              ProjectModel project;\r
 +                              if (configs.containsKey(rootPath)) {\r
 +                                      // clone the project model because it's repository list will\r
 +                                      // be tailored for the requesting user\r
 +                                      project = DeepCopier.copy(configs.get(rootPath));\r
 +                              } else {\r
 +                                      project = new ProjectModel(rootPath);\r
 +                              }\r
 +                              map.put(rootPath, project);\r
 +                      }\r
 +                      map.get(rootPath).addRepository(model);\r
 +              }\r
 +              \r
 +              // sort projects, root project first\r
 +              List<ProjectModel> projects;\r
 +              if (includeUsers) {\r
 +                      // all projects\r
 +                      projects = new ArrayList<ProjectModel>(map.values());\r
 +                      Collections.sort(projects);\r
 +                      projects.remove(map.get(""));\r
 +                      projects.add(0, map.get(""));\r
 +              } else {\r
 +                      // all non-user projects\r
 +                      projects = new ArrayList<ProjectModel>();\r
 +                      ProjectModel root = map.remove("");\r
 +                      for (ProjectModel model : map.values()) {\r
 +                              if (!model.isUserProject()) {\r
 +                                      projects.add(model);\r
 +                              }\r
 +                      }\r
 +                      Collections.sort(projects);\r
 +                      projects.add(0, root);\r
 +              }\r
 +              return projects;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the project model for the specified user.\r
 +       * \r
 +       * @param name\r
 +       * @param user\r
 +       * @return a project model, or null if it does not exist\r
 +       */\r
 +      public ProjectModel getProjectModel(String name, UserModel user) {\r
 +              for (ProjectModel project : getProjectModels(user, true)) {\r
 +                      if (project.name.equalsIgnoreCase(name)) {\r
 +                              return project;\r
 +                      }\r
 +              }\r
 +              return null;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns a project model for the Gitblit/system user.\r
 +       * \r
 +       * @param name a project name\r
 +       * @return a project model or null if the project does not exist\r
 +       */\r
 +      public ProjectModel getProjectModel(String name) {\r
 +              Map<String, ProjectModel> configs = getProjectConfigs();\r
 +              ProjectModel project = configs.get(name.toLowerCase());\r
 +              if (project == null) {\r
 +                      project = new ProjectModel(name);\r
 +                      if (name.length() > 0 && name.charAt(0) == '~') {\r
 +                              UserModel user = getUserModel(name.substring(1));\r
 +                              if (user != null) {\r
 +                                      project.title = user.getDisplayName();\r
 +                                      project.description = "personal repositories";\r
 +                              }\r
 +                      }\r
 +              } else {\r
 +                      // clone the object\r
 +                      project = DeepCopier.copy(project);\r
 +              }\r
 +              if (StringUtils.isEmpty(name)) {\r
 +                      // get root repositories\r
 +                      for (String repository : getRepositoryList()) {\r
 +                              if (repository.indexOf('/') == -1) {\r
 +                                      project.addRepository(repository);\r
 +                              }\r
 +                      }\r
 +              } else {\r
 +                      // get repositories in subfolder\r
 +                      String folder = name.toLowerCase() + "/";\r
 +                      for (String repository : getRepositoryList()) {\r
 +                              if (repository.toLowerCase().startsWith(folder)) {\r
 +                                      project.addRepository(repository);\r
 +                              }\r
 +                      }\r
 +              }\r
 +              if (project.repositories.size() == 0) {\r
 +                      // no repositories == no project\r
 +                      return null;\r
 +              }\r
 +              return project;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the list of project models that are referenced by the supplied\r
 +       * repository model     list.  This is an alternative method exists to ensure\r
 +       * Gitblit does not call getRepositoryModels(UserModel) twice in a request.\r
 +       * \r
 +       * @param repositoryModels\r
 +       * @param includeUsers\r
 +       * @return a list of project models\r
 +       */\r
 +      public List<ProjectModel> getProjectModels(List<RepositoryModel> repositoryModels, boolean includeUsers) {\r
 +              Map<String, ProjectModel> projects = new LinkedHashMap<String, ProjectModel>();\r
 +              for (RepositoryModel repository : repositoryModels) {\r
 +                      if (!includeUsers && repository.isPersonalRepository()) {\r
 +                              // exclude personal repositories\r
 +                              continue;\r
 +                      }\r
 +                      if (!projects.containsKey(repository.projectPath)) {\r
 +                              ProjectModel project = getProjectModel(repository.projectPath);\r
 +                              if (project == null) {\r
 +                                      logger.warn(MessageFormat.format("excluding project \"{0}\" from project list because it is empty!",\r
 +                                                      repository.projectPath));\r
 +                                      continue;\r
 +                              }\r
 +                              projects.put(repository.projectPath, project);\r
 +                              // clear the repo list in the project because that is the system\r
 +                              // list, not the user-accessible list and start building the\r
 +                              // user-accessible list\r
 +                              project.repositories.clear();\r
 +                              project.repositories.add(repository.name);\r
 +                              project.lastChange = repository.lastChange;\r
 +                      } else {\r
 +                              // update the user-accessible list\r
 +                              // this is used for repository count\r
 +                              ProjectModel project = projects.get(repository.projectPath);\r
 +                              project.repositories.add(repository.name);\r
 +                              if (project.lastChange.before(repository.lastChange)) {\r
 +                                      project.lastChange = repository.lastChange;\r
 +                              }\r
 +                      }\r
 +              }\r
 +              return new ArrayList<ProjectModel>(projects.values());\r
 +      }\r
 +      \r
 +      /**\r
 +       * Workaround JGit.  I need to access the raw config object directly in order\r
 +       * to see if the config is dirty so that I can reload a repository model.\r
 +       * If I use the stock JGit method to get the config it already reloads the\r
 +       * config.  If the config changes are made within Gitblit this is fine as\r
 +       * the returned config will still be flagged as dirty.  BUT... if the config\r
 +       * is manipulated outside Gitblit then it fails to recognize this as dirty.\r
 +       *  \r
 +       * @param r\r
 +       * @return a config\r
 +       */\r
 +      private StoredConfig getRepositoryConfig(Repository r) {\r
 +              try {\r
 +                      Field f = r.getClass().getDeclaredField("repoConfig");\r
 +                      f.setAccessible(true);\r
 +                      StoredConfig config = (StoredConfig) f.get(r);\r
 +                      return config;\r
 +              } catch (Exception e) {\r
 +                      logger.error("Failed to retrieve \"repoConfig\" via reflection", e);\r
 +              }\r
 +              return r.getConfig();\r
 +      }\r
 +      \r
 +      /**\r
 +       * Create a repository model from the configuration and repository data.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @return a repositoryModel or null if the repository does not exist\r
 +       */\r
 +      private RepositoryModel loadRepositoryModel(String repositoryName) {\r
 +              Repository r = getRepository(repositoryName);\r
 +              if (r == null) {\r
 +                      return null;\r
 +              }\r
 +              RepositoryModel model = new RepositoryModel();\r
 +              model.isBare = r.isBare();\r
 +              File basePath = getFileOrFolder(Keys.git.repositoriesFolder, "${baseFolder}/git");\r
 +              if (model.isBare) {\r
 +                      model.name = com.gitblit.utils.FileUtils.getRelativePath(basePath, r.getDirectory());\r
 +              } else {\r
 +                      model.name = com.gitblit.utils.FileUtils.getRelativePath(basePath, r.getDirectory().getParentFile());\r
 +              }\r
 +              model.hasCommits = JGitUtils.hasCommits(r);\r
 +              model.lastChange = JGitUtils.getLastChange(r);\r
 +              model.projectPath = StringUtils.getFirstPathElement(repositoryName);\r
 +              \r
 +              StoredConfig config = r.getConfig();\r
 +              boolean hasOrigin = !StringUtils.isEmpty(config.getString("remote", "origin", "url"));\r
 +              \r
 +              if (config != null) {\r
 +                      model.description = getConfig(config, "description", "");\r
 +                      model.addOwners(ArrayUtils.fromString(getConfig(config, "owner", "")));\r
 +                      model.useTickets = getConfig(config, "useTickets", false);\r
 +                      model.useDocs = getConfig(config, "useDocs", false);\r
 +                      model.allowForks = getConfig(config, "allowForks", true);\r
 +                      model.accessRestriction = AccessRestrictionType.fromName(getConfig(config,\r
 +                                      "accessRestriction", settings.getString(Keys.git.defaultAccessRestriction, null)));\r
 +                      model.authorizationControl = AuthorizationControl.fromName(getConfig(config,\r
 +                                      "authorizationControl", settings.getString(Keys.git.defaultAuthorizationControl, null)));\r
 +                      model.verifyCommitter = getConfig(config, "verifyCommitter", false);\r
 +                      model.showRemoteBranches = getConfig(config, "showRemoteBranches", hasOrigin);\r
 +                      model.isFrozen = getConfig(config, "isFrozen", false);\r
 +                      model.showReadme = getConfig(config, "showReadme", false);\r
 +                      model.skipSizeCalculation = getConfig(config, "skipSizeCalculation", false);\r
 +                      model.skipSummaryMetrics = getConfig(config, "skipSummaryMetrics", false);\r
 +                      model.federationStrategy = FederationStrategy.fromName(getConfig(config,\r
 +                                      "federationStrategy", null));\r
 +                      model.federationSets = new ArrayList<String>(Arrays.asList(config.getStringList(\r
 +                                      Constants.CONFIG_GITBLIT, null, "federationSets")));\r
 +                      model.isFederated = getConfig(config, "isFederated", false);\r
 +                      model.gcThreshold = getConfig(config, "gcThreshold", settings.getString(Keys.git.defaultGarbageCollectionThreshold, "500KB"));\r
 +                      model.gcPeriod = getConfig(config, "gcPeriod", settings.getInteger(Keys.git.defaultGarbageCollectionPeriod, 7));\r
 +                      try {\r
 +                              model.lastGC = new SimpleDateFormat(Constants.ISO8601).parse(getConfig(config, "lastGC", "1970-01-01'T'00:00:00Z"));\r
 +                      } catch (Exception e) {\r
 +                              model.lastGC = new Date(0);\r
 +                      }\r
 +                      model.maxActivityCommits = getConfig(config, "maxActivityCommits", settings.getInteger(Keys.web.maxActivityCommits, 0));\r
 +                      model.origin = config.getString("remote", "origin", "url");\r
 +                      if (model.origin != null) {\r
 +                              model.origin = model.origin.replace('\\', '/');\r
 +                      }\r
 +                      model.preReceiveScripts = new ArrayList<String>(Arrays.asList(config.getStringList(\r
 +                                      Constants.CONFIG_GITBLIT, null, "preReceiveScript")));\r
 +                      model.postReceiveScripts = new ArrayList<String>(Arrays.asList(config.getStringList(\r
 +                                      Constants.CONFIG_GITBLIT, null, "postReceiveScript")));\r
 +                      model.mailingLists = new ArrayList<String>(Arrays.asList(config.getStringList(\r
 +                                      Constants.CONFIG_GITBLIT, null, "mailingList")));\r
 +                      model.indexedBranches = new ArrayList<String>(Arrays.asList(config.getStringList(\r
 +                                      Constants.CONFIG_GITBLIT, null, "indexBranch")));\r
 +                      \r
 +                      // Custom defined properties\r
 +                      model.customFields = new LinkedHashMap<String, String>();\r
 +                      for (String aProperty : config.getNames(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS)) {\r
 +                              model.customFields.put(aProperty, config.getString(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS, aProperty));\r
 +                      }\r
 +              }\r
 +              model.HEAD = JGitUtils.getHEADRef(r);\r
 +              model.availableRefs = JGitUtils.getAvailableHeadTargets(r);\r
 +              model.sparkleshareId = JGitUtils.getSparkleshareId(r);\r
 +              r.close();\r
 +              \r
 +              if (model.origin != null && model.origin.startsWith("file://")) {\r
 +                      // repository was cloned locally... perhaps as a fork\r
 +                      try {\r
 +                              File folder = new File(new URI(model.origin));\r
 +                              String originRepo = com.gitblit.utils.FileUtils.getRelativePath(getRepositoriesFolder(), folder);\r
 +                              if (!StringUtils.isEmpty(originRepo)) {\r
 +                                      // ensure origin still exists\r
 +                                      File repoFolder = new File(getRepositoriesFolder(), originRepo);\r
 +                                      if (repoFolder.exists()) {\r
 +                                              model.originRepository = originRepo.toLowerCase();\r
 +                                      }\r
 +                              }\r
 +                      } catch (URISyntaxException e) {\r
 +                              logger.error("Failed to determine fork for " + model, e);\r
 +                      }\r
 +              }\r
 +              return model;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Determines if this server has the requested repository.\r
 +       * \r
 +       * @param name\r
 +       * @return true if the repository exists\r
 +       */\r
 +      public boolean hasRepository(String repositoryName) {\r
 +              return hasRepository(repositoryName, false);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Determines if this server has the requested repository.\r
 +       * \r
 +       * @param name\r
 +       * @param caseInsensitive\r
 +       * @return true if the repository exists\r
 +       */\r
 +      public boolean hasRepository(String repositoryName, boolean caseSensitiveCheck) {\r
 +              if (!caseSensitiveCheck && settings.getBoolean(Keys.git.cacheRepositoryList, true)) {\r
 +                      // if we are caching use the cache to determine availability\r
 +                      // otherwise we end up adding a phantom repository to the cache\r
 +                      return repositoryListCache.containsKey(repositoryName.toLowerCase());\r
 +              }               \r
 +              Repository r = getRepository(repositoryName, false);\r
 +              if (r == null) {\r
 +                      return false;\r
 +              }\r
 +              r.close();\r
 +              return true;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Determines if the specified user has a fork of the specified origin\r
 +       * repository.\r
 +       * \r
 +       * @param username\r
 +       * @param origin\r
 +       * @return true the if the user has a fork\r
 +       */\r
 +      public boolean hasFork(String username, String origin) {\r
 +              return getFork(username, origin) != null;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Gets the name of a user's fork of the specified origin\r
 +       * repository.\r
 +       * \r
 +       * @param username\r
 +       * @param origin\r
 +       * @return the name of the user's fork, null otherwise\r
 +       */\r
 +      public String getFork(String username, String origin) {\r
 +              String userProject = "~" + username.toLowerCase();\r
 +              if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) {\r
 +                      String userPath = userProject + "/";\r
 +\r
 +                      // collect all origin nodes in fork network\r
 +                      Set<String> roots = new HashSet<String>();\r
 +                      roots.add(origin);\r
 +                      RepositoryModel originModel = repositoryListCache.get(origin);\r
 +                      while (originModel != null) {\r
 +                              if (!ArrayUtils.isEmpty(originModel.forks)) {\r
 +                                      for (String fork : originModel.forks) {\r
 +                                              if (!fork.startsWith(userPath)) {\r
 +                                                      roots.add(fork);\r
 +                                              }\r
 +                                      }\r
 +                              }\r
 +                              \r
 +                              if (originModel.originRepository != null) {\r
 +                                      roots.add(originModel.originRepository);\r
 +                                      originModel = repositoryListCache.get(originModel.originRepository);\r
 +                              } else {\r
 +                                      // break\r
 +                                      originModel = null;\r
 +                              }\r
 +                      }\r
 +                      \r
 +                      for (String repository : repositoryListCache.keySet()) {\r
 +                              if (repository.startsWith(userPath)) {\r
 +                                      RepositoryModel model = repositoryListCache.get(repository);\r
 +                                      if (!StringUtils.isEmpty(model.originRepository)) {\r
 +                                              if (roots.contains(model.originRepository)) {\r
 +                                                      // user has a fork in this graph\r
 +                                                      return model.name;\r
 +                                              }\r
 +                                      }\r
 +                              }\r
 +                      }\r
 +              } else {\r
 +                      // not caching\r
 +                      ProjectModel project = getProjectModel(userProject);\r
++                      if (project == null) {\r
++                              return null;\r
++                      }\r
 +                      for (String repository : project.repositories) {\r
 +                              if (repository.startsWith(userProject)) {\r
 +                                      RepositoryModel model = getRepositoryModel(repository);\r
 +                                      if (model.originRepository.equalsIgnoreCase(origin)) {\r
 +                                              // user has a fork\r
 +                                              return model.name;\r
 +                                      }\r
 +                              }\r
 +                      }\r
 +              }\r
 +              // user does not have a fork\r
 +              return null;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the fork network for a repository by traversing up the fork graph\r
 +       * to discover the root and then down through all children of the root node.\r
 +       * \r
 +       * @param repository\r
 +       * @return a ForkModel\r
 +       */\r
 +      public ForkModel getForkNetwork(String repository) {\r
 +              if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) {\r
 +                      // find the root, cached\r
 +                      RepositoryModel model = repositoryListCache.get(repository.toLowerCase());\r
 +                      while (model.originRepository != null) {\r
 +                              model = repositoryListCache.get(model.originRepository);\r
 +                      }\r
 +                      ForkModel root = getForkModelFromCache(model.name);\r
 +                      return root;\r
 +              } else {\r
 +                      // find the root, non-cached\r
 +                      RepositoryModel model = getRepositoryModel(repository.toLowerCase());\r
 +                      while (model.originRepository != null) {\r
 +                              model = getRepositoryModel(model.originRepository);\r
 +                      }\r
 +                      ForkModel root = getForkModel(model.name);\r
 +                      return root;\r
 +              }\r
 +      }\r
 +      \r
 +      private ForkModel getForkModelFromCache(String repository) {\r
 +              RepositoryModel model = repositoryListCache.get(repository.toLowerCase());\r
 +              if (model == null) {\r
 +                      return null;\r
 +              }\r
 +              ForkModel fork = new ForkModel(model);\r
 +              if (!ArrayUtils.isEmpty(model.forks)) {\r
 +                      for (String aFork : model.forks) {\r
 +                              ForkModel fm = getForkModelFromCache(aFork);\r
 +                              if (fm != null) {\r
 +                                      fork.forks.add(fm);\r
 +                              }\r
 +                      }\r
 +              }\r
 +              return fork;\r
 +      }\r
 +      \r
 +      private ForkModel getForkModel(String repository) {\r
 +              RepositoryModel model = getRepositoryModel(repository.toLowerCase());\r
 +              if (model == null) {\r
 +                      return null;\r
 +              }\r
 +              ForkModel fork = new ForkModel(model);\r
 +              if (!ArrayUtils.isEmpty(model.forks)) {\r
 +                      for (String aFork : model.forks) {\r
 +                              ForkModel fm = getForkModel(aFork);\r
 +                              if (fm != null) {\r
 +                                      fork.forks.add(fm);\r
 +                              }\r
 +                      }\r
 +              }\r
 +              return fork;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the size in bytes of the repository. Gitblit caches the\r
 +       * repository sizes to reduce the performance penalty of recursive\r
 +       * calculation. The cache is updated if the repository has been changed\r
 +       * since the last calculation.\r
 +       * \r
 +       * @param model\r
 +       * @return size in bytes\r
 +       */\r
 +      public long calculateSize(RepositoryModel model) {\r
 +              if (repositorySizeCache.hasCurrent(model.name, model.lastChange)) {\r
 +                      return repositorySizeCache.getObject(model.name);\r
 +              }\r
 +              File gitDir = FileKey.resolve(new File(repositoriesFolder, model.name), FS.DETECTED);\r
 +              long size = com.gitblit.utils.FileUtils.folderSize(gitDir);\r
 +              repositorySizeCache.updateObject(model.name, model.lastChange, size);\r
 +              return size;\r
 +      }\r
 +\r
 +      /**\r
 +       * Ensure that a cached repository is completely closed and its resources\r
 +       * are properly released.\r
 +       * \r
 +       * @param repositoryName\r
 +       */\r
 +      private void closeRepository(String repositoryName) {\r
 +              Repository repository = getRepository(repositoryName);\r
 +              if (repository == null) {\r
 +                      return;\r
 +              }\r
 +              RepositoryCache.close(repository);\r
 +\r
 +              // assume 2 uses in case reflection fails\r
 +              int uses = 2;\r
 +              try {\r
 +                      // The FileResolver caches repositories which is very useful\r
 +                      // for performance until you want to delete a repository.\r
 +                      // I have to use reflection to call close() the correct\r
 +                      // number of times to ensure that the object and ref databases\r
 +                      // are properly closed before I can delete the repository from\r
 +                      // the filesystem.\r
 +                      Field useCnt = Repository.class.getDeclaredField("useCnt");\r
 +                      useCnt.setAccessible(true);\r
 +                      uses = ((AtomicInteger) useCnt.get(repository)).get();\r
 +              } catch (Exception e) {\r
 +                      logger.warn(MessageFormat\r
 +                                      .format("Failed to reflectively determine use count for repository {0}",\r
 +                                                      repositoryName), e);\r
 +              }\r
 +              if (uses > 0) {\r
 +                      logger.info(MessageFormat\r
 +                                      .format("{0}.useCnt={1}, calling close() {2} time(s) to close object and ref databases",\r
 +                                                      repositoryName, uses, uses));\r
 +                      for (int i = 0; i < uses; i++) {\r
 +                              repository.close();\r
 +                      }\r
 +              }\r
 +              \r
 +              // close any open index writer/searcher in the Lucene executor\r
 +              luceneExecutor.close(repositoryName);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the metrics for the default branch of the specified repository.\r
 +       * This method builds a metrics cache. The cache is updated if the\r
 +       * repository is updated. A new copy of the metrics list is returned on each\r
 +       * call so that modifications to the list are non-destructive.\r
 +       * \r
 +       * @param model\r
 +       * @param repository\r
 +       * @return a new array list of metrics\r
 +       */\r
 +      public List<Metric> getRepositoryDefaultMetrics(RepositoryModel model, Repository repository) {\r
 +              if (repositoryMetricsCache.hasCurrent(model.name, model.lastChange)) {\r
 +                      return new ArrayList<Metric>(repositoryMetricsCache.getObject(model.name));\r
 +              }\r
 +              List<Metric> metrics = MetricUtils.getDateMetrics(repository, null, true, null, getTimezone());\r
 +              repositoryMetricsCache.updateObject(model.name, model.lastChange, metrics);\r
 +              return new ArrayList<Metric>(metrics);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the gitblit string value for the specified key. If key is not\r
 +       * set, returns defaultValue.\r
 +       * \r
 +       * @param config\r
 +       * @param field\r
 +       * @param defaultValue\r
 +       * @return field value or defaultValue\r
 +       */\r
 +      private String getConfig(StoredConfig config, String field, String defaultValue) {\r
 +              String value = config.getString(Constants.CONFIG_GITBLIT, null, field);\r
 +              if (StringUtils.isEmpty(value)) {\r
 +                      return defaultValue;\r
 +              }\r
 +              return value;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the gitblit boolean value for the specified key. If key is not\r
 +       * set, returns defaultValue.\r
 +       * \r
 +       * @param config\r
 +       * @param field\r
 +       * @param defaultValue\r
 +       * @return field value or defaultValue\r
 +       */\r
 +      private boolean getConfig(StoredConfig config, String field, boolean defaultValue) {\r
 +              return config.getBoolean(Constants.CONFIG_GITBLIT, field, defaultValue);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns the gitblit string value for the specified key. If key is not\r
 +       * set, returns defaultValue.\r
 +       * \r
 +       * @param config\r
 +       * @param field\r
 +       * @param defaultValue\r
 +       * @return field value or defaultValue\r
 +       */\r
 +      private int getConfig(StoredConfig config, String field, int defaultValue) {\r
 +              String value = config.getString(Constants.CONFIG_GITBLIT, null, field);\r
 +              if (StringUtils.isEmpty(value)) {\r
 +                      return defaultValue;\r
 +              }\r
 +              try {\r
 +                      return Integer.parseInt(value);\r
 +              } catch (Exception e) {\r
 +              }\r
 +              return defaultValue;\r
 +      }\r
 +\r
 +      /**\r
 +       * Creates/updates the repository model keyed by reopsitoryName. Saves all\r
 +       * repository settings in .git/config. This method allows for renaming\r
 +       * repositories and will update user access permissions accordingly.\r
 +       * \r
 +       * All repositories created by this method are bare and automatically have\r
 +       * .git appended to their names, which is the standard convention for bare\r
 +       * repositories.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @param repository\r
 +       * @param isCreate\r
 +       * @throws GitBlitException\r
 +       */\r
 +      public void updateRepositoryModel(String repositoryName, RepositoryModel repository,\r
 +                      boolean isCreate) throws GitBlitException {\r
 +              if (gcExecutor.isCollectingGarbage(repositoryName)) {\r
 +                      throw new GitBlitException(MessageFormat.format("sorry, Gitblit is busy collecting garbage in {0}",\r
 +                                      repositoryName));\r
 +              }\r
 +              Repository r = null;\r
 +              String projectPath = StringUtils.getFirstPathElement(repository.name);\r
 +              if (!StringUtils.isEmpty(projectPath)) {\r
 +                      if (projectPath.equalsIgnoreCase(getString(Keys.web.repositoryRootGroupName, "main"))) {\r
 +                              // strip leading group name\r
 +                              repository.name = repository.name.substring(projectPath.length() + 1);\r
 +                      }\r
 +              }\r
 +              if (isCreate) {\r
 +                      // ensure created repository name ends with .git\r
 +                      if (!repository.name.toLowerCase().endsWith(org.eclipse.jgit.lib.Constants.DOT_GIT_EXT)) {\r
 +                              repository.name += org.eclipse.jgit.lib.Constants.DOT_GIT_EXT;\r
 +                      }\r
 +                      if (hasRepository(repository.name)) {\r
 +                              throw new GitBlitException(MessageFormat.format(\r
 +                                              "Can not create repository ''{0}'' because it already exists.",\r
 +                                              repository.name));\r
 +                      }\r
 +                      // create repository\r
 +                      logger.info("create repository " + repository.name);\r
 +                      r = JGitUtils.createRepository(repositoriesFolder, repository.name);\r
 +              } else {\r
 +                      // rename repository\r
 +                      if (!repositoryName.equalsIgnoreCase(repository.name)) {\r
 +                              if (!repository.name.toLowerCase().endsWith(\r
 +                                              org.eclipse.jgit.lib.Constants.DOT_GIT_EXT)) {\r
 +                                      repository.name += org.eclipse.jgit.lib.Constants.DOT_GIT_EXT;\r
 +                              }\r
 +                              if (new File(repositoriesFolder, repository.name).exists()) {\r
 +                                      throw new GitBlitException(MessageFormat.format(\r
 +                                                      "Failed to rename ''{0}'' because ''{1}'' already exists.",\r
 +                                                      repositoryName, repository.name));\r
 +                              }\r
 +                              closeRepository(repositoryName);\r
 +                              File folder = new File(repositoriesFolder, repositoryName);\r
 +                              File destFolder = new File(repositoriesFolder, repository.name);\r
 +                              if (destFolder.exists()) {\r
 +                                      throw new GitBlitException(\r
 +                                                      MessageFormat\r
 +                                                                      .format("Can not rename repository ''{0}'' to ''{1}'' because ''{1}'' already exists.",\r
 +                                                                                      repositoryName, repository.name));\r
 +                              }\r
 +                              File parentFile = destFolder.getParentFile();\r
 +                              if (!parentFile.exists() && !parentFile.mkdirs()) {\r
 +                                      throw new GitBlitException(MessageFormat.format(\r
 +                                                      "Failed to create folder ''{0}''", parentFile.getAbsolutePath()));\r
 +                              }\r
 +                              if (!folder.renameTo(destFolder)) {\r
 +                                      throw new GitBlitException(MessageFormat.format(\r
 +                                                      "Failed to rename repository ''{0}'' to ''{1}''.", repositoryName,\r
 +                                                      repository.name));\r
 +                              }\r
 +                              // rename the roles\r
 +                              if (!userService.renameRepositoryRole(repositoryName, repository.name)) {\r
 +                                      throw new GitBlitException(MessageFormat.format(\r
 +                                                      "Failed to rename repository permissions ''{0}'' to ''{1}''.",\r
 +                                                      repositoryName, repository.name));\r
 +                              }\r
 +                              \r
 +                              // rename fork origins in their configs\r
 +                              if (!ArrayUtils.isEmpty(repository.forks)) {\r
 +                                      for (String fork : repository.forks) {\r
 +                                              Repository rf = getRepository(fork);\r
 +                                              try {\r
 +                                                      StoredConfig config = rf.getConfig();\r
 +                                                      String origin = config.getString("remote", "origin", "url");\r
 +                                                      origin = origin.replace(repositoryName, repository.name);\r
 +                                                      config.setString("remote", "origin", "url", origin);\r
 +                                                      config.save();\r
 +                                              } catch (Exception e) {\r
 +                                                      logger.error("Failed to update repository fork config for " + fork, e);\r
 +                                              }\r
 +                                              rf.close();\r
 +                                      }\r
 +                              }\r
 +                              \r
 +                              // remove this repository from any origin model's fork list\r
 +                              if (!StringUtils.isEmpty(repository.originRepository)) {\r
 +                                      RepositoryModel origin = repositoryListCache.get(repository.originRepository);\r
 +                                      if (origin != null && !ArrayUtils.isEmpty(origin.forks)) {\r
 +                                              origin.forks.remove(repositoryName);\r
 +                                      }\r
 +                              }\r
 +\r
 +                              // clear the cache\r
 +                              clearRepositoryMetadataCache(repositoryName);\r
 +                              repository.resetDisplayName();\r
 +                      }\r
 +\r
 +                      // load repository\r
 +                      logger.info("edit repository " + repository.name);\r
 +                      r = getRepository(repository.name);\r
 +              }\r
 +\r
 +              // update settings\r
 +              if (r != null) {\r
 +                      updateConfiguration(r, repository);\r
 +                      // only update symbolic head if it changes\r
 +                      String currentRef = JGitUtils.getHEADRef(r);\r
 +                      if (!StringUtils.isEmpty(repository.HEAD) && !repository.HEAD.equals(currentRef)) {\r
 +                              logger.info(MessageFormat.format("Relinking {0} HEAD from {1} to {2}", \r
 +                                              repository.name, currentRef, repository.HEAD));\r
 +                              if (JGitUtils.setHEADtoRef(r, repository.HEAD)) {\r
 +                                      // clear the cache\r
 +                                      clearRepositoryMetadataCache(repository.name);\r
 +                              }\r
 +                      }\r
 +\r
 +                      // close the repository object\r
 +                      r.close();\r
 +              }\r
 +              \r
 +              // update repository cache\r
 +              removeFromCachedRepositoryList(repositoryName);\r
 +              // model will actually be replaced on next load because config is stale\r
 +              addToCachedRepositoryList(repository);\r
 +      }\r
 +      \r
 +      /**\r
 +       * Updates the Gitblit configuration for the specified repository.\r
 +       * \r
 +       * @param r\r
 +       *            the Git repository\r
 +       * @param repository\r
 +       *            the Gitblit repository model\r
 +       */\r
 +      public void updateConfiguration(Repository r, RepositoryModel repository) {\r
 +              StoredConfig config = r.getConfig();\r
 +              config.setString(Constants.CONFIG_GITBLIT, null, "description", repository.description);\r
 +              config.setString(Constants.CONFIG_GITBLIT, null, "owner", ArrayUtils.toString(repository.owners));\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "useTickets", repository.useTickets);\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "useDocs", repository.useDocs);\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "allowForks", repository.allowForks);\r
 +              config.setString(Constants.CONFIG_GITBLIT, null, "accessRestriction", repository.accessRestriction.name());\r
 +              config.setString(Constants.CONFIG_GITBLIT, null, "authorizationControl", repository.authorizationControl.name());\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "verifyCommitter", repository.verifyCommitter);\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "showRemoteBranches", repository.showRemoteBranches);\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "isFrozen", repository.isFrozen);\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "showReadme", repository.showReadme);\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "skipSizeCalculation", repository.skipSizeCalculation);\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "skipSummaryMetrics", repository.skipSummaryMetrics);\r
 +              config.setString(Constants.CONFIG_GITBLIT, null, "federationStrategy",\r
 +                              repository.federationStrategy.name());\r
 +              config.setBoolean(Constants.CONFIG_GITBLIT, null, "isFederated", repository.isFederated);\r
 +              config.setString(Constants.CONFIG_GITBLIT, null, "gcThreshold", repository.gcThreshold);\r
 +              if (repository.gcPeriod == settings.getInteger(Keys.git.defaultGarbageCollectionPeriod, 7)) {\r
 +                      // use default from config\r
 +                      config.unset(Constants.CONFIG_GITBLIT, null, "gcPeriod");\r
 +              } else {\r
 +                      config.setInt(Constants.CONFIG_GITBLIT, null, "gcPeriod", repository.gcPeriod);\r
 +              }\r
 +              if (repository.lastGC != null) {\r
 +                      config.setString(Constants.CONFIG_GITBLIT, null, "lastGC", new SimpleDateFormat(Constants.ISO8601).format(repository.lastGC));\r
 +              }\r
 +              if (repository.maxActivityCommits == settings.getInteger(Keys.web.maxActivityCommits, 0)) {\r
 +                      // use default from config\r
 +                      config.unset(Constants.CONFIG_GITBLIT, null, "maxActivityCommits");\r
 +              } else {\r
 +                      config.setInt(Constants.CONFIG_GITBLIT, null, "maxActivityCommits", repository.maxActivityCommits);\r
 +              }\r
 +\r
 +              updateList(config, "federationSets", repository.federationSets);\r
 +              updateList(config, "preReceiveScript", repository.preReceiveScripts);\r
 +              updateList(config, "postReceiveScript", repository.postReceiveScripts);\r
 +              updateList(config, "mailingList", repository.mailingLists);\r
 +              updateList(config, "indexBranch", repository.indexedBranches);\r
 +              \r
 +              // User Defined Properties\r
 +              if (repository.customFields != null) {\r
 +                      if (repository.customFields.size() == 0) {\r
 +                              // clear section\r
 +                              config.unsetSection(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS);\r
 +                      } else {\r
 +                              for (Entry<String, String> property : repository.customFields.entrySet()) {\r
 +                                      // set field\r
 +                                      String key = property.getKey();\r
 +                                      String value = property.getValue();\r
 +                                      config.setString(Constants.CONFIG_GITBLIT, Constants.CONFIG_CUSTOM_FIELDS, key, value);\r
 +                              }\r
 +                      }\r
 +              }\r
 +\r
 +              try {\r
 +                      config.save();\r
 +              } catch (IOException e) {\r
 +                      logger.error("Failed to save repository config!", e);\r
 +              }\r
 +      }\r
 +      \r
 +      private void updateList(StoredConfig config, String field, List<String> list) {\r
 +              // a null list is skipped, not cleared\r
 +              // this is for RPC administration where an older manager might be used\r
 +              if (list == null) {\r
 +                      return;\r
 +              }\r
 +              if (ArrayUtils.isEmpty(list)) {\r
 +                      config.unset(Constants.CONFIG_GITBLIT, null, field);\r
 +              } else {\r
 +                      config.setStringList(Constants.CONFIG_GITBLIT, null, field, list);\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Deletes the repository from the file system and removes the repository\r
 +       * permission from all repository users.\r
 +       * \r
 +       * @param model\r
 +       * @return true if successful\r
 +       */\r
 +      public boolean deleteRepositoryModel(RepositoryModel model) {\r
 +              return deleteRepository(model.name);\r
 +      }\r
 +\r
 +      /**\r
 +       * Deletes the repository from the file system and removes the repository\r
 +       * permission from all repository users.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @return true if successful\r
 +       */\r
 +      public boolean deleteRepository(String repositoryName) {\r
 +              try {\r
 +                      closeRepository(repositoryName);\r
 +                      // clear the repository cache\r
 +                      clearRepositoryMetadataCache(repositoryName);\r
 +                      \r
 +                      RepositoryModel model = removeFromCachedRepositoryList(repositoryName);\r
 +                      if (model != null && !ArrayUtils.isEmpty(model.forks)) {\r
 +                              resetRepositoryListCache();\r
 +                      }\r
 +\r
 +                      File folder = new File(repositoriesFolder, repositoryName);\r
 +                      if (folder.exists() && folder.isDirectory()) {\r
 +                              FileUtils.delete(folder, FileUtils.RECURSIVE | FileUtils.RETRY);\r
 +                              if (userService.deleteRepositoryRole(repositoryName)) {\r
 +                                      logger.info(MessageFormat.format("Repository \"{0}\" deleted", repositoryName));\r
 +                                      return true;\r
 +                              }\r
 +                      }\r
 +              } catch (Throwable t) {\r
 +                      logger.error(MessageFormat.format("Failed to delete repository {0}", repositoryName), t);\r
 +              }\r
 +              return false;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns an html version of the commit message with any global or\r
 +       * repository-specific regular expression substitution applied.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @param text\r
 +       * @return html version of the commit message\r
 +       */\r
 +      public String processCommitMessage(String repositoryName, String text) {\r
 +              String html = StringUtils.breakLinesForHtml(text);\r
 +              Map<String, String> map = new HashMap<String, String>();\r
 +              // global regex keys\r
 +              if (settings.getBoolean(Keys.regex.global, false)) {\r
 +                      for (String key : settings.getAllKeys(Keys.regex.global)) {\r
 +                              if (!key.equals(Keys.regex.global)) {\r
 +                                      String subKey = key.substring(key.lastIndexOf('.') + 1);\r
 +                                      map.put(subKey, settings.getString(key, ""));\r
 +                              }\r
 +                      }\r
 +              }\r
 +\r
 +              // repository-specific regex keys\r
 +              List<String> keys = settings.getAllKeys(Keys.regex._ROOT + "."\r
 +                              + repositoryName.toLowerCase());\r
 +              for (String key : keys) {\r
 +                      String subKey = key.substring(key.lastIndexOf('.') + 1);\r
 +                      map.put(subKey, settings.getString(key, ""));\r
 +              }\r
 +\r
 +              for (Entry<String, String> entry : map.entrySet()) {\r
 +                      String definition = entry.getValue().trim();\r
 +                      String[] chunks = definition.split("!!!");\r
 +                      if (chunks.length == 2) {\r
 +                              html = html.replaceAll(chunks[0], chunks[1]);\r
 +                      } else {\r
 +                              logger.warn(entry.getKey()\r
 +                                              + " improperly formatted.  Use !!! to separate match from replacement: "\r
 +                                              + definition);\r
 +                      }\r
 +              }\r
 +              return html;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns Gitblit's scheduled executor service for scheduling tasks.\r
 +       * \r
 +       * @return scheduledExecutor\r
 +       */\r
 +      public ScheduledExecutorService executor() {\r
 +              return scheduledExecutor;\r
 +      }\r
 +\r
 +      public static boolean canFederate() {\r
 +              String passphrase = getString(Keys.federation.passphrase, "");\r
 +              return !StringUtils.isEmpty(passphrase);\r
 +      }\r
 +\r
 +      /**\r
 +       * Configures this Gitblit instance to pull any registered federated gitblit\r
 +       * instances.\r
 +       */\r
 +      private void configureFederation() {\r
 +              boolean validPassphrase = true;\r
 +              String passphrase = settings.getString(Keys.federation.passphrase, "");\r
 +              if (StringUtils.isEmpty(passphrase)) {\r
 +                      logger.warn("Federation passphrase is blank! This server can not be PULLED from.");\r
 +                      validPassphrase = false;\r
 +              }\r
 +              if (validPassphrase) {\r
 +                      // standard tokens\r
 +                      for (FederationToken tokenType : FederationToken.values()) {\r
 +                              logger.info(MessageFormat.format("Federation {0} token = {1}", tokenType.name(),\r
 +                                              getFederationToken(tokenType)));\r
 +                      }\r
 +\r
 +                      // federation set tokens\r
 +                      for (String set : settings.getStrings(Keys.federation.sets)) {\r
 +                              logger.info(MessageFormat.format("Federation Set {0} token = {1}", set,\r
 +                                              getFederationToken(set)));\r
 +                      }\r
 +              }\r
 +\r
 +              // Schedule the federation executor\r
 +              List<FederationModel> registrations = getFederationRegistrations();\r
 +              if (registrations.size() > 0) {\r
 +                      FederationPullExecutor executor = new FederationPullExecutor(registrations, true);\r
 +                      scheduledExecutor.schedule(executor, 1, TimeUnit.MINUTES);\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of federated gitblit instances that this instance will\r
 +       * try to pull.\r
 +       * \r
 +       * @return list of registered gitblit instances\r
 +       */\r
 +      public List<FederationModel> getFederationRegistrations() {\r
 +              if (federationRegistrations.isEmpty()) {\r
 +                      federationRegistrations.addAll(FederationUtils.getFederationRegistrations(settings));\r
 +              }\r
 +              return federationRegistrations;\r
 +      }\r
 +\r
 +      /**\r
 +       * Retrieve the specified federation registration.\r
 +       * \r
 +       * @param name\r
 +       *            the name of the registration\r
 +       * @return a federation registration\r
 +       */\r
 +      public FederationModel getFederationRegistration(String url, String name) {\r
 +              // check registrations\r
 +              for (FederationModel r : getFederationRegistrations()) {\r
 +                      if (r.name.equals(name) && r.url.equals(url)) {\r
 +                              return r;\r
 +                      }\r
 +              }\r
 +\r
 +              // check the results\r
 +              for (FederationModel r : getFederationResultRegistrations()) {\r
 +                      if (r.name.equals(name) && r.url.equals(url)) {\r
 +                              return r;\r
 +                      }\r
 +              }\r
 +              return null;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of federation sets.\r
 +       * \r
 +       * @return list of federation sets\r
 +       */\r
 +      public List<FederationSet> getFederationSets(String gitblitUrl) {\r
 +              List<FederationSet> list = new ArrayList<FederationSet>();\r
 +              // generate standard tokens\r
 +              for (FederationToken type : FederationToken.values()) {\r
 +                      FederationSet fset = new FederationSet(type.toString(), type, getFederationToken(type));\r
 +                      fset.repositories = getRepositories(gitblitUrl, fset.token);\r
 +                      list.add(fset);\r
 +              }\r
 +              // generate tokens for federation sets\r
 +              for (String set : settings.getStrings(Keys.federation.sets)) {\r
 +                      FederationSet fset = new FederationSet(set, FederationToken.REPOSITORIES,\r
 +                                      getFederationToken(set));\r
 +                      fset.repositories = getRepositories(gitblitUrl, fset.token);\r
 +                      list.add(fset);\r
 +              }\r
 +              return list;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of possible federation tokens for this Gitblit instance.\r
 +       * \r
 +       * @return list of federation tokens\r
 +       */\r
 +      public List<String> getFederationTokens() {\r
 +              List<String> tokens = new ArrayList<String>();\r
 +              // generate standard tokens\r
 +              for (FederationToken type : FederationToken.values()) {\r
 +                      tokens.add(getFederationToken(type));\r
 +              }\r
 +              // generate tokens for federation sets\r
 +              for (String set : settings.getStrings(Keys.federation.sets)) {\r
 +                      tokens.add(getFederationToken(set));\r
 +              }\r
 +              return tokens;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the specified federation token for this Gitblit instance.\r
 +       * \r
 +       * @param type\r
 +       * @return a federation token\r
 +       */\r
 +      public String getFederationToken(FederationToken type) {\r
 +              return getFederationToken(type.name());\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the specified federation token for this Gitblit instance.\r
 +       * \r
 +       * @param value\r
 +       * @return a federation token\r
 +       */\r
 +      public String getFederationToken(String value) {\r
 +              String passphrase = settings.getString(Keys.federation.passphrase, "");\r
 +              return StringUtils.getSHA1(passphrase + "-" + value);\r
 +      }\r
 +\r
 +      /**\r
 +       * Compares the provided token with this Gitblit instance's tokens and\r
 +       * determines if the requested permission may be granted to the token.\r
 +       * \r
 +       * @param req\r
 +       * @param token\r
 +       * @return true if the request can be executed\r
 +       */\r
 +      public boolean validateFederationRequest(FederationRequest req, String token) {\r
 +              String all = getFederationToken(FederationToken.ALL);\r
 +              String unr = getFederationToken(FederationToken.USERS_AND_REPOSITORIES);\r
 +              String jur = getFederationToken(FederationToken.REPOSITORIES);\r
 +              switch (req) {\r
 +              case PULL_REPOSITORIES:\r
 +                      return token.equals(all) || token.equals(unr) || token.equals(jur);\r
 +              case PULL_USERS:\r
 +              case PULL_TEAMS:\r
 +                      return token.equals(all) || token.equals(unr);\r
 +              case PULL_SETTINGS:\r
 +              case PULL_SCRIPTS:\r
 +                      return token.equals(all);\r
 +              default:\r
 +                      break;\r
 +              }\r
 +              return false;\r
 +      }\r
 +\r
 +      /**\r
 +       * Acknowledge and cache the status of a remote Gitblit instance.\r
 +       * \r
 +       * @param identification\r
 +       *            the identification of the pulling Gitblit instance\r
 +       * @param registration\r
 +       *            the registration from the pulling Gitblit instance\r
 +       * @return true if acknowledged\r
 +       */\r
 +      public boolean acknowledgeFederationStatus(String identification, FederationModel registration) {\r
 +              // reset the url to the identification of the pulling Gitblit instance\r
 +              registration.url = identification;\r
 +              String id = identification;\r
 +              if (!StringUtils.isEmpty(registration.folder)) {\r
 +                      id += "-" + registration.folder;\r
 +              }\r
 +              federationPullResults.put(id, registration);\r
 +              return true;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of registration results.\r
 +       * \r
 +       * @return the list of registration results\r
 +       */\r
 +      public List<FederationModel> getFederationResultRegistrations() {\r
 +              return new ArrayList<FederationModel>(federationPullResults.values());\r
 +      }\r
 +\r
 +      /**\r
 +       * Submit a federation proposal. The proposal is cached locally and the\r
 +       * Gitblit administrator(s) are notified via email.\r
 +       * \r
 +       * @param proposal\r
 +       *            the proposal\r
 +       * @param gitblitUrl\r
 +       *            the url of your gitblit instance to send an email to\r
 +       *            administrators\r
 +       * @return true if the proposal was submitted\r
 +       */\r
 +      public boolean submitFederationProposal(FederationProposal proposal, String gitblitUrl) {\r
 +              // convert proposal to json\r
 +              String json = JsonUtils.toJsonString(proposal);\r
 +\r
 +              try {\r
 +                      // make the proposals folder\r
 +                      File proposalsFolder = getProposalsFolder();\r
 +                      proposalsFolder.mkdirs();\r
 +\r
 +                      // cache json to a file\r
 +                      File file = new File(proposalsFolder, proposal.token + Constants.PROPOSAL_EXT);\r
 +                      com.gitblit.utils.FileUtils.writeContent(file, json);\r
 +              } catch (Exception e) {\r
 +                      logger.error(MessageFormat.format("Failed to cache proposal from {0}", proposal.url), e);\r
 +              }\r
 +\r
 +              // send an email, if possible\r
 +              try {\r
 +                      Message message = mailExecutor.createMessageForAdministrators();\r
 +                      if (message != null) {\r
 +                              message.setSubject("Federation proposal from " + proposal.url);\r
 +                              message.setText("Please review the proposal @ " + gitblitUrl + "/proposal/"\r
 +                                              + proposal.token);\r
 +                              mailExecutor.queue(message);\r
 +                      }\r
 +              } catch (Throwable t) {\r
 +                      logger.error("Failed to notify administrators of proposal", t);\r
 +              }\r
 +              return true;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of pending federation proposals\r
 +       * \r
 +       * @return list of federation proposals\r
 +       */\r
 +      public List<FederationProposal> getPendingFederationProposals() {\r
 +              List<FederationProposal> list = new ArrayList<FederationProposal>();\r
 +              File folder = getProposalsFolder();\r
 +              if (folder.exists()) {\r
 +                      File[] files = folder.listFiles(new FileFilter() {\r
 +                              @Override\r
 +                              public boolean accept(File file) {\r
 +                                      return file.isFile()\r
 +                                                      && file.getName().toLowerCase().endsWith(Constants.PROPOSAL_EXT);\r
 +                              }\r
 +                      });\r
 +                      for (File file : files) {\r
 +                              String json = com.gitblit.utils.FileUtils.readContent(file, null);\r
 +                              FederationProposal proposal = JsonUtils.fromJsonString(json,\r
 +                                              FederationProposal.class);\r
 +                              list.add(proposal);\r
 +                      }\r
 +              }\r
 +              return list;\r
 +      }\r
 +\r
 +      /**\r
 +       * Get repositories for the specified token.\r
 +       * \r
 +       * @param gitblitUrl\r
 +       *            the base url of this gitblit instance\r
 +       * @param token\r
 +       *            the federation token\r
 +       * @return a map of <cloneurl, RepositoryModel>\r
 +       */\r
 +      public Map<String, RepositoryModel> getRepositories(String gitblitUrl, String token) {\r
 +              Map<String, String> federationSets = new HashMap<String, String>();\r
 +              for (String set : getStrings(Keys.federation.sets)) {\r
 +                      federationSets.put(getFederationToken(set), set);\r
 +              }\r
 +\r
 +              // Determine the Gitblit clone url\r
 +              StringBuilder sb = new StringBuilder();\r
 +              sb.append(gitblitUrl);\r
 +              sb.append(Constants.GIT_PATH);\r
 +              sb.append("{0}");\r
 +              String cloneUrl = sb.toString();\r
 +\r
 +              // Retrieve all available repositories\r
 +              UserModel user = new UserModel(Constants.FEDERATION_USER);\r
 +              user.canAdmin = true;\r
 +              List<RepositoryModel> list = getRepositoryModels(user);\r
 +\r
 +              // create the [cloneurl, repositoryModel] map\r
 +              Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();\r
 +              for (RepositoryModel model : list) {\r
 +                      // by default, setup the url for THIS repository\r
 +                      String url = MessageFormat.format(cloneUrl, model.name);\r
 +                      switch (model.federationStrategy) {\r
 +                      case EXCLUDE:\r
 +                              // skip this repository\r
 +                              continue;\r
 +                      case FEDERATE_ORIGIN:\r
 +                              // federate the origin, if it is defined\r
 +                              if (!StringUtils.isEmpty(model.origin)) {\r
 +                                      url = model.origin;\r
 +                              }\r
 +                              break;\r
 +                      default:\r
 +                              break;\r
 +                      }\r
 +\r
 +                      if (federationSets.containsKey(token)) {\r
 +                              // include repositories only for federation set\r
 +                              String set = federationSets.get(token);\r
 +                              if (model.federationSets.contains(set)) {\r
 +                                      repositories.put(url, model);\r
 +                              }\r
 +                      } else {\r
 +                              // standard federation token for ALL\r
 +                              repositories.put(url, model);\r
 +                      }\r
 +              }\r
 +              return repositories;\r
 +      }\r
 +\r
 +      /**\r
 +       * Creates a proposal from the token.\r
 +       * \r
 +       * @param gitblitUrl\r
 +       *            the url of this Gitblit instance\r
 +       * @param token\r
 +       * @return a potential proposal\r
 +       */\r
 +      public FederationProposal createFederationProposal(String gitblitUrl, String token) {\r
 +              FederationToken tokenType = FederationToken.REPOSITORIES;\r
 +              for (FederationToken type : FederationToken.values()) {\r
 +                      if (token.equals(getFederationToken(type))) {\r
 +                              tokenType = type;\r
 +                              break;\r
 +                      }\r
 +              }\r
 +              Map<String, RepositoryModel> repositories = getRepositories(gitblitUrl, token);\r
 +              FederationProposal proposal = new FederationProposal(gitblitUrl, tokenType, token,\r
 +                              repositories);\r
 +              return proposal;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the proposal identified by the supplied token.\r
 +       * \r
 +       * @param token\r
 +       * @return the specified proposal or null\r
 +       */\r
 +      public FederationProposal getPendingFederationProposal(String token) {\r
 +              List<FederationProposal> list = getPendingFederationProposals();\r
 +              for (FederationProposal proposal : list) {\r
 +                      if (proposal.token.equals(token)) {\r
 +                              return proposal;\r
 +                      }\r
 +              }\r
 +              return null;\r
 +      }\r
 +\r
 +      /**\r
 +       * Deletes a pending federation proposal.\r
 +       * \r
 +       * @param a\r
 +       *            proposal\r
 +       * @return true if the proposal was deleted\r
 +       */\r
 +      public boolean deletePendingFederationProposal(FederationProposal proposal) {\r
 +              File folder = getProposalsFolder();\r
 +              File file = new File(folder, proposal.token + Constants.PROPOSAL_EXT);\r
 +              return file.delete();\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of all Groovy push hook scripts. Script files must have\r
 +       * .groovy extension\r
 +       * \r
 +       * @return list of available hook scripts\r
 +       */\r
 +      public List<String> getAllScripts() {\r
 +              File groovyFolder = getGroovyScriptsFolder();\r
 +              File[] files = groovyFolder.listFiles(new FileFilter() {\r
 +                      @Override\r
 +                      public boolean accept(File pathname) {\r
 +                              return pathname.isFile() && pathname.getName().endsWith(".groovy");\r
 +                      }\r
 +              });\r
 +              List<String> scripts = new ArrayList<String>();\r
 +              if (files != null) {\r
 +                      for (File file : files) {\r
 +                              String script = file.getName().substring(0, file.getName().lastIndexOf('.'));\r
 +                              scripts.add(script);\r
 +                      }\r
 +              }\r
 +              return scripts;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of pre-receive scripts the repository inherited from the\r
 +       * global settings and team affiliations.\r
 +       * \r
 +       * @param repository\r
 +       *            if null only the globally specified scripts are returned\r
 +       * @return a list of scripts\r
 +       */\r
 +      public List<String> getPreReceiveScriptsInherited(RepositoryModel repository) {\r
 +              Set<String> scripts = new LinkedHashSet<String>();\r
 +              // Globals\r
 +              for (String script : getStrings(Keys.groovy.preReceiveScripts)) {\r
 +                      if (script.endsWith(".groovy")) {\r
 +                              scripts.add(script.substring(0, script.lastIndexOf('.')));\r
 +                      } else {\r
 +                              scripts.add(script);\r
 +                      }\r
 +              }\r
 +\r
 +              // Team Scripts\r
 +              if (repository != null) {\r
 +                      for (String teamname : userService.getTeamnamesForRepositoryRole(repository.name)) {\r
 +                              TeamModel team = userService.getTeamModel(teamname);\r
 +                              scripts.addAll(team.preReceiveScripts);\r
 +                      }\r
 +              }\r
 +              return new ArrayList<String>(scripts);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of all available Groovy pre-receive push hook scripts\r
 +       * that are not already inherited by the repository. Script files must have\r
 +       * .groovy extension\r
 +       * \r
 +       * @param repository\r
 +       *            optional parameter\r
 +       * @return list of available hook scripts\r
 +       */\r
 +      public List<String> getPreReceiveScriptsUnused(RepositoryModel repository) {\r
 +              Set<String> inherited = new TreeSet<String>(getPreReceiveScriptsInherited(repository));\r
 +\r
 +              // create list of available scripts by excluding inherited scripts\r
 +              List<String> scripts = new ArrayList<String>();\r
 +              for (String script : getAllScripts()) {\r
 +                      if (!inherited.contains(script)) {\r
 +                              scripts.add(script);\r
 +                      }\r
 +              }\r
 +              return scripts;\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of post-receive scripts the repository inherited from\r
 +       * the global settings and team affiliations.\r
 +       * \r
 +       * @param repository\r
 +       *            if null only the globally specified scripts are returned\r
 +       * @return a list of scripts\r
 +       */\r
 +      public List<String> getPostReceiveScriptsInherited(RepositoryModel repository) {\r
 +              Set<String> scripts = new LinkedHashSet<String>();\r
 +              // Global Scripts\r
 +              for (String script : getStrings(Keys.groovy.postReceiveScripts)) {\r
 +                      if (script.endsWith(".groovy")) {\r
 +                              scripts.add(script.substring(0, script.lastIndexOf('.')));\r
 +                      } else {\r
 +                              scripts.add(script);\r
 +                      }\r
 +              }\r
 +              // Team Scripts\r
 +              if (repository != null) {\r
 +                      for (String teamname : userService.getTeamnamesForRepositoryRole(repository.name)) {\r
 +                              TeamModel team = userService.getTeamModel(teamname);\r
 +                              scripts.addAll(team.postReceiveScripts);\r
 +                      }\r
 +              }\r
 +              return new ArrayList<String>(scripts);\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the list of unused Groovy post-receive push hook scripts that are\r
 +       * not already inherited by the repository. Script files must have .groovy\r
 +       * extension\r
 +       * \r
 +       * @param repository\r
 +       *            optional parameter\r
 +       * @return list of available hook scripts\r
 +       */\r
 +      public List<String> getPostReceiveScriptsUnused(RepositoryModel repository) {\r
 +              Set<String> inherited = new TreeSet<String>(getPostReceiveScriptsInherited(repository));\r
 +\r
 +              // create list of available scripts by excluding inherited scripts\r
 +              List<String> scripts = new ArrayList<String>();\r
 +              for (String script : getAllScripts()) {\r
 +                      if (!inherited.contains(script)) {\r
 +                              scripts.add(script);\r
 +                      }\r
 +              }\r
 +              return scripts;\r
 +      }\r
 +      \r
 +      /**\r
 +       * Search the specified repositories using the Lucene query.\r
 +       * \r
 +       * @param query\r
 +       * @param page\r
 +       * @param pageSize\r
 +       * @param repositories\r
 +       * @return\r
 +       */\r
 +      public List<SearchResult> search(String query, int page, int pageSize, List<String> repositories) {             \r
 +              List<SearchResult> srs = luceneExecutor.search(query, page, pageSize, repositories);\r
 +              return srs;\r
 +      }\r
 +\r
 +      /**\r
 +       * Notify the administrators by email.\r
 +       * \r
 +       * @param subject\r
 +       * @param message\r
 +       */\r
 +      public void sendMailToAdministrators(String subject, String message) {\r
 +              try {\r
 +                      Message mail = mailExecutor.createMessageForAdministrators();\r
 +                      if (mail != null) {\r
 +                              mail.setSubject(subject);\r
 +                              mail.setText(message);\r
 +                              mailExecutor.queue(mail);\r
 +                      }\r
 +              } catch (MessagingException e) {\r
 +                      logger.error("Messaging error", e);\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Notify users by email of something.\r
 +       * \r
 +       * @param subject\r
 +       * @param message\r
 +       * @param toAddresses\r
 +       */\r
 +      public void sendMail(String subject, String message, Collection<String> toAddresses) {\r
 +              this.sendMail(subject, message, toAddresses.toArray(new String[0]));\r
 +      }\r
 +\r
 +      /**\r
 +       * Notify users by email of something.\r
 +       * \r
 +       * @param subject\r
 +       * @param message\r
 +       * @param toAddresses\r
 +       */\r
 +      public void sendMail(String subject, String message, String... toAddresses) {\r
 +              try {\r
 +                      Message mail = mailExecutor.createMessage(toAddresses);\r
 +                      if (mail != null) {\r
 +                              mail.setSubject(subject);\r
 +                              mail.setText(message);\r
 +                              mailExecutor.queue(mail);\r
 +                      }\r
 +              } catch (MessagingException e) {\r
 +                      logger.error("Messaging error", e);\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Notify users by email of something.\r
 +       * \r
 +       * @param subject\r
 +       * @param message\r
 +       * @param toAddresses\r
 +       */\r
 +      public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {\r
 +              this.sendHtmlMail(subject, message, toAddresses.toArray(new String[0]));\r
 +      }\r
 +\r
 +      /**\r
 +       * Notify users by email of something.\r
 +       * \r
 +       * @param subject\r
 +       * @param message\r
 +       * @param toAddresses\r
 +       */\r
 +      public void sendHtmlMail(String subject, String message, String... toAddresses) {\r
 +              try {\r
 +                      Message mail = mailExecutor.createMessage(toAddresses);\r
 +                      if (mail != null) {\r
 +                              mail.setSubject(subject);\r
 +                              mail.setContent(message, "text/html");\r
 +                              mailExecutor.queue(mail);\r
 +                      }\r
 +              } catch (MessagingException e) {\r
 +                      logger.error("Messaging error", e);\r
 +              }\r
 +      }\r
 +\r
 +      /**\r
 +       * Returns the descriptions/comments of the Gitblit config settings.\r
 +       * \r
 +       * @return SettingsModel\r
 +       */\r
 +      public ServerSettings getSettingsModel() {\r
 +              // ensure that the current values are updated in the setting models\r
 +              for (String key : settings.getAllKeys(null)) {\r
 +                      SettingModel setting = settingsModel.get(key);\r
 +                      if (setting == null) {\r
 +                              // unreferenced setting, create a setting model\r
 +                              setting = new SettingModel();\r
 +                              setting.name = key;\r
 +                              settingsModel.add(setting);\r
 +                      }\r
 +                      setting.currentValue = settings.getString(key, "");                     \r
 +              }\r
 +              settingsModel.pushScripts = getAllScripts();\r
 +              return settingsModel;\r
 +      }\r
 +\r
 +      /**\r
 +       * Parse the properties file and aggregate all the comments by the setting\r
 +       * key. A setting model tracks the current value, the default value, the\r
 +       * description of the setting and and directives about the setting.\r
 +       * @param referencePropertiesInputStream\r
 +       * \r
 +       * @return Map<String, SettingModel>\r
 +       */\r
 +      private ServerSettings loadSettingModels(InputStream referencePropertiesInputStream) {\r
 +              ServerSettings settingsModel = new ServerSettings();\r
 +              settingsModel.supportsCredentialChanges = userService.supportsCredentialChanges();\r
 +              settingsModel.supportsDisplayNameChanges = userService.supportsDisplayNameChanges();\r
 +              settingsModel.supportsEmailAddressChanges = userService.supportsEmailAddressChanges();\r
 +              settingsModel.supportsTeamMembershipChanges = userService.supportsTeamMembershipChanges();\r
 +              try {\r
 +                      // Read bundled Gitblit properties to extract setting descriptions.\r
 +                      // This copy is pristine and only used for populating the setting\r
 +                      // models map.\r
 +                      InputStream is = referencePropertiesInputStream;\r
 +                      BufferedReader propertiesReader = new BufferedReader(new InputStreamReader(is));\r
 +                      StringBuilder description = new StringBuilder();\r
 +                      SettingModel setting = new SettingModel();\r
 +                      String line = null;\r
 +                      while ((line = propertiesReader.readLine()) != null) {\r
 +                              if (line.length() == 0) {\r
 +                                      description.setLength(0);\r
 +                                      setting = new SettingModel();\r
 +                              } else {\r
 +                                      if (line.charAt(0) == '#') {\r
 +                                              if (line.length() > 1) {\r
 +                                                      String text = line.substring(1).trim();\r
 +                                                      if (SettingModel.CASE_SENSITIVE.equals(text)) {\r
 +                                                              setting.caseSensitive = true;\r
 +                                                      } else if (SettingModel.RESTART_REQUIRED.equals(text)) {\r
 +                                                              setting.restartRequired = true;\r
 +                                                      } else if (SettingModel.SPACE_DELIMITED.equals(text)) {\r
 +                                                              setting.spaceDelimited = true;\r
 +                                                      } else if (text.startsWith(SettingModel.SINCE)) {\r
 +                                                              try {\r
 +                                                                      setting.since = text.split(" ")[1];\r
 +                                                              } catch (Exception e) {\r
 +                                                                      setting.since = text;\r
 +                                                              }\r
 +                                                      } else {\r
 +                                                              description.append(text);\r
 +                                                              description.append('\n');\r
 +                                                      }\r
 +                                              }\r
 +                                      } else {\r
 +                                              String[] kvp = line.split("=", 2);\r
 +                                              String key = kvp[0].trim();\r
 +                                              setting.name = key;\r
 +                                              setting.defaultValue = kvp[1].trim();\r
 +                                              setting.currentValue = setting.defaultValue;\r
 +                                              setting.description = description.toString().trim();\r
 +                                              settingsModel.add(setting);\r
 +                                              description.setLength(0);\r
 +                                              setting = new SettingModel();\r
 +                                      }\r
 +                              }\r
 +                      }\r
 +                      propertiesReader.close();\r
 +              } catch (NullPointerException e) {\r
 +                      logger.error("Failed to find resource copy of gitblit.properties");\r
 +              } catch (IOException e) {\r
 +                      logger.error("Failed to load resource copy of gitblit.properties");\r
 +              }\r
 +              return settingsModel;\r
 +      }\r
 +\r
 +      /**\r
 +       * Configure the Gitblit singleton with the specified settings source. This\r
 +       * source may be file settings (Gitblit GO) or may be web.xml settings\r
 +       * (Gitblit WAR).\r
 +       * \r
 +       * @param settings\r
 +       */\r
 +      public void configureContext(IStoredSettings settings, File folder, boolean startFederation) {\r
 +              this.settings = settings;\r
 +              this.baseFolder = folder;\r
 +\r
 +              repositoriesFolder = getRepositoriesFolder();\r
 +\r
 +              logger.info("Gitblit base folder     = " + folder.getAbsolutePath());\r
 +              logger.info("Git repositories folder = " + repositoriesFolder.getAbsolutePath());\r
 +              logger.info("Gitblit settings        = " + settings.toString());\r
 +\r
 +              // prepare service executors\r
 +              mailExecutor = new MailExecutor(settings);\r
 +              luceneExecutor = new LuceneExecutor(settings, repositoriesFolder);\r
 +              gcExecutor = new GCExecutor(settings);\r
 +              \r
 +              // calculate repository list settings checksum for future config changes\r
 +              repositoryListSettingsChecksum.set(getRepositoryListSettingsChecksum());\r
 +\r
 +              // build initial repository list\r
 +              if (settings.getBoolean(Keys.git.cacheRepositoryList,  true)) {\r
 +                      logger.info("Identifying available repositories...");\r
 +                      getRepositoryList();\r
 +              }\r
 +              \r
 +              logTimezone("JVM", TimeZone.getDefault());\r
 +              logTimezone(Constants.NAME, getTimezone());\r
 +\r
 +              serverStatus = new ServerStatus(isGO());\r
 +\r
 +              if (this.userService == null) {\r
 +                      String realm = settings.getString(Keys.realm.userService, "${baseFolder}/users.properties");\r
 +                      IUserService loginService = null;\r
 +                      try {\r
 +                              // check to see if this "file" is a login service class\r
 +                              Class<?> realmClass = Class.forName(realm);\r
 +                              loginService = (IUserService) realmClass.newInstance();\r
 +                      } catch (Throwable t) {\r
 +                              loginService = new GitblitUserService();\r
 +                      }\r
 +                      setUserService(loginService);\r
 +              }\r
 +              \r
 +              // load and cache the project metadata\r
 +              projectConfigs = new FileBasedConfig(getFileOrFolder(Keys.web.projectsFile, "${baseFolder}/projects.conf"), FS.detect());\r
 +              getProjectConfigs();\r
 +              \r
 +              // schedule mail engine\r
 +              if (mailExecutor.isReady()) {\r
 +                      logger.info("Mail executor is scheduled to process the message queue every 2 minutes.");\r
 +                      scheduledExecutor.scheduleAtFixedRate(mailExecutor, 1, 2, TimeUnit.MINUTES);\r
 +              } else {\r
 +                      logger.warn("Mail server is not properly configured.  Mail services disabled.");\r
 +              }\r
 +              \r
 +              // schedule lucene engine\r
 +              enableLuceneIndexing();\r
 +\r
 +              \r
 +              // schedule gc engine\r
 +              if (gcExecutor.isReady()) {\r
 +                      logger.info("GC executor is scheduled to scan repositories every 24 hours.");\r
 +                      Calendar c = Calendar.getInstance();\r
 +                      c.set(Calendar.HOUR_OF_DAY, settings.getInteger(Keys.git.garbageCollectionHour, 0));\r
 +                      c.set(Calendar.MINUTE, 0);\r
 +                      c.set(Calendar.SECOND, 0);\r
 +                      c.set(Calendar.MILLISECOND, 0);\r
 +                      Date cd = c.getTime();\r
 +                      Date now = new Date();\r
 +                      int delay = 0;\r
 +                      if (cd.before(now)) {\r
 +                              c.add(Calendar.DATE, 1);\r
 +                              cd = c.getTime();\r
 +                      }\r
 +                      delay = (int) ((cd.getTime() - now.getTime())/TimeUtils.MIN);\r
 +                      String when = delay + " mins";\r
 +                      if (delay > 60) {\r
 +                              when = MessageFormat.format("{0,number,0.0} hours", ((float)delay)/60f);\r
 +                      }\r
 +                      logger.info(MessageFormat.format("Next scheculed GC scan is in {0}", when));\r
 +                      scheduledExecutor.scheduleAtFixedRate(gcExecutor, delay, 60*24, TimeUnit.MINUTES);\r
 +              }\r
 +              \r
 +              if (startFederation) {\r
 +                      configureFederation();\r
 +              }\r
 +              \r
 +              // Configure JGit\r
 +              WindowCacheConfig cfg = new WindowCacheConfig();\r
 +              \r
 +              cfg.setPackedGitWindowSize(settings.getFilesize(Keys.git.packedGitWindowSize, cfg.getPackedGitWindowSize()));\r
 +              cfg.setPackedGitLimit(settings.getFilesize(Keys.git.packedGitLimit, cfg.getPackedGitLimit()));\r
 +              cfg.setDeltaBaseCacheLimit(settings.getFilesize(Keys.git.deltaBaseCacheLimit, cfg.getDeltaBaseCacheLimit()));\r
 +              cfg.setPackedGitOpenFiles(settings.getFilesize(Keys.git.packedGitOpenFiles, cfg.getPackedGitOpenFiles()));\r
 +              cfg.setStreamFileThreshold(settings.getFilesize(Keys.git.streamFileThreshold, cfg.getStreamFileThreshold()));\r
 +              cfg.setPackedGitMMAP(settings.getBoolean(Keys.git.packedGitMmap, cfg.isPackedGitMMAP()));\r
 +              \r
 +              try {\r
 +                      WindowCache.reconfigure(cfg);\r
 +                      logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitWindowSize, cfg.getPackedGitWindowSize()));\r
 +                      logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitLimit, cfg.getPackedGitLimit()));\r
 +                      logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.deltaBaseCacheLimit, cfg.getDeltaBaseCacheLimit()));\r
 +                      logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitOpenFiles, cfg.getPackedGitOpenFiles()));\r
 +                      logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.streamFileThreshold, cfg.getStreamFileThreshold()));\r
 +                      logger.debug(MessageFormat.format("{0} = {1}", Keys.git.packedGitMmap, cfg.isPackedGitMMAP()));\r
 +              } catch (IllegalArgumentException e) {\r
 +                      logger.error("Failed to configure JGit parameters!", e);\r
 +              }\r
 +\r
 +              ContainerUtils.CVE_2007_0450.test();\r
 +              \r
 +              // startup Fanout PubSub service\r
 +              if (settings.getInteger(Keys.fanout.port, 0) > 0) {\r
 +                      String bindInterface = settings.getString(Keys.fanout.bindInterface, null);\r
 +                      int port = settings.getInteger(Keys.fanout.port, FanoutService.DEFAULT_PORT);\r
 +                      boolean useNio = settings.getBoolean(Keys.fanout.useNio, true);\r
 +                      int limit = settings.getInteger(Keys.fanout.connectionLimit, 0);\r
 +                      \r
 +                      if (useNio) {\r
 +                              if (StringUtils.isEmpty(bindInterface)) {\r
 +                                      fanoutService = new FanoutNioService(port);\r
 +                              } else {\r
 +                                      fanoutService = new FanoutNioService(bindInterface, port);\r
 +                              }\r
 +                      } else {\r
 +                              if (StringUtils.isEmpty(bindInterface)) {\r
 +                                      fanoutService = new FanoutSocketService(port);\r
 +                              } else {\r
 +                                      fanoutService = new FanoutSocketService(bindInterface, port);\r
 +                              }\r
 +                      }\r
 +                      \r
 +                      fanoutService.setConcurrentConnectionLimit(limit);\r
 +                      fanoutService.setAllowAllChannelAnnouncements(false);\r
 +                      fanoutService.start();\r
 +              }\r
 +      }\r
 +      \r
 +      protected void enableLuceneIndexing() {\r
 +              scheduledExecutor.scheduleAtFixedRate(luceneExecutor, 1, 2,  TimeUnit.MINUTES);\r
 +              logger.info("Lucene executor is scheduled to process indexed branches every 2 minutes.");\r
 +      }\r
 +      \r
 +      protected final Logger getLogger() {\r
 +              return logger;\r
 +      }\r
 +      \r
 +      protected final ScheduledExecutorService getScheduledExecutor() {\r
 +              return scheduledExecutor;\r
 +      }\r
 +\r
 +      protected final LuceneExecutor getLuceneExecutor() {\r
 +              return luceneExecutor;\r
 +      }\r
 +      \r
 +      private void logTimezone(String type, TimeZone zone) {\r
 +              SimpleDateFormat df = new SimpleDateFormat("z Z");\r
 +              df.setTimeZone(zone);\r
 +              String offset = df.format(new Date());\r
 +              logger.info(type + " timezone is " + zone.getID() + " (" + offset + ")");\r
 +      }\r
 +\r
 +      /**\r
 +       * Configure Gitblit from the web.xml, if no configuration has already been\r
 +       * specified.\r
 +       * \r
 +       * @see ServletContextListener.contextInitialize(ServletContextEvent)\r
 +       */\r
 +      @Override\r
 +      public void contextInitialized(ServletContextEvent contextEvent) {\r
 +              contextInitialized(contextEvent, contextEvent.getServletContext().getResourceAsStream("/WEB-INF/reference.properties"));\r
 +      }\r
 +\r
 +      public void contextInitialized(ServletContextEvent contextEvent, InputStream referencePropertiesInputStream) {\r
 +              servletContext = contextEvent.getServletContext();\r
 +              if (settings == null) {\r
 +                      // Gitblit is running in a servlet container\r
 +                      ServletContext context = contextEvent.getServletContext();\r
 +                      WebXmlSettings webxmlSettings = new WebXmlSettings(context);\r
 +                      File contextFolder = new File(context.getRealPath("/"));\r
 +                      String openShift = System.getenv("OPENSHIFT_DATA_DIR");\r
 +                      \r
 +                      if (!StringUtils.isEmpty(openShift)) {\r
 +                              // Gitblit is running in OpenShift/JBoss\r
 +                              File base = new File(openShift);\r
 +\r
 +                              // gitblit.properties setting overrides\r
 +                              File overrideFile = new File(base, "gitblit.properties");\r
 +                              webxmlSettings.applyOverrides(overrideFile);\r
 +                              \r
 +                              // Copy the included scripts to the configured groovy folder\r
 +                              File localScripts = new File(base, webxmlSettings.getString(Keys.groovy.scriptsFolder, "groovy"));\r
 +                              if (!localScripts.exists()) {\r
 +                                      File warScripts = new File(contextFolder, "/WEB-INF/data/groovy");\r
 +                                      if (!warScripts.equals(localScripts)) {\r
 +                                              try {\r
 +                                                      com.gitblit.utils.FileUtils.copy(localScripts, warScripts.listFiles());\r
 +                                              } catch (IOException e) {\r
 +                                                      logger.error(MessageFormat.format(\r
 +                                                                      "Failed to copy included Groovy scripts from {0} to {1}",\r
 +                                                                      warScripts, localScripts));\r
 +                                              }\r
 +                                      }\r
 +                              }\r
 +                              \r
 +                              // configure context using the web.xml\r
 +                              configureContext(webxmlSettings, base, true);\r
 +                      } else {\r
 +                              // Gitblit is running in a standard servlet container\r
 +                              logger.info("WAR contextFolder is " + contextFolder.getAbsolutePath());\r
 +                              \r
 +                              String path = webxmlSettings.getString(Constants.baseFolder, Constants.contextFolder$ + "/WEB-INF/data");\r
 +                              File base = com.gitblit.utils.FileUtils.resolveParameter(Constants.contextFolder$, contextFolder, path);\r
 +                              base.mkdirs();\r
 +                              \r
 +                              // try to copy the data folder contents to the baseFolder\r
 +                              File localSettings = new File(base, "gitblit.properties");\r
 +                              if (!localSettings.exists()) {\r
 +                                      File contextData = new File(contextFolder, "/WEB-INF/data");\r
 +                                      if (!base.equals(contextData)) {\r
 +                                              try {\r
 +                                                      com.gitblit.utils.FileUtils.copy(base, contextData.listFiles());\r
 +                                              } catch (IOException e) {\r
 +                                                      logger.error(MessageFormat.format(\r
 +                                                                      "Failed to copy included data from {0} to {1}",\r
 +                                                              contextData, base));\r
 +                                              }\r
 +                                      }\r
 +                              }\r
 +                              \r
 +                              // delegate all config to baseFolder/gitblit.properties file\r
 +                              FileSettings settings = new FileSettings(localSettings.getAbsolutePath());                              \r
 +                              configureContext(settings, base, true);\r
 +                      }\r
 +              }\r
 +              \r
 +              settingsModel = loadSettingModels(referencePropertiesInputStream);\r
 +              serverStatus.servletContainer = servletContext.getServerInfo();\r
 +      }\r
 +\r
 +      /**\r
 +       * Gitblit is being shutdown either because the servlet container is\r
 +       * shutting down or because the servlet container is re-deploying Gitblit.\r
 +       */\r
 +      @Override\r
 +      public void contextDestroyed(ServletContextEvent contextEvent) {\r
 +              logger.info("Gitblit context destroyed by servlet container.");\r
 +              scheduledExecutor.shutdownNow();\r
 +              luceneExecutor.close();\r
 +              gcExecutor.close();\r
 +              if (fanoutService != null) {\r
 +                      fanoutService.stop();\r
 +              }\r
 +      }\r
 +      \r
 +      /**\r
 +       * \r
 +       * @return true if we are running the gc executor\r
 +       */\r
 +      public boolean isCollectingGarbage() {\r
 +              return gcExecutor.isRunning();\r
 +      }\r
 +      \r
 +      /**\r
 +       * Returns true if Gitblit is actively collecting garbage in this repository.\r
 +       * \r
 +       * @param repositoryName\r
 +       * @return true if actively collecting garbage\r
 +       */\r
 +      public boolean isCollectingGarbage(String repositoryName) {\r
 +              return gcExecutor.isCollectingGarbage(repositoryName);\r
 +      }\r
 +\r
 +      /**\r
 +       * Creates a personal fork of the specified repository. The clone is view\r
 +       * restricted by default and the owner of the source repository is given\r
 +       * access to the clone. \r
 +       * \r
 +       * @param repository\r
 +       * @param user\r
 +       * @return the repository model of the fork, if successful\r
 +       * @throws GitBlitException\r
 +       */\r
 +      public RepositoryModel fork(RepositoryModel repository, UserModel user) throws GitBlitException {\r
 +              String cloneName = MessageFormat.format("~{0}/{1}.git", user.username, StringUtils.stripDotGit(StringUtils.getLastPathElement(repository.name)));\r
 +              String fromUrl = MessageFormat.format("file://{0}/{1}", repositoriesFolder.getAbsolutePath(), repository.name);\r
 +\r
 +              // clone the repository\r
 +              try {\r
 +                      JGitUtils.cloneRepository(repositoriesFolder, cloneName, fromUrl, true, null);\r
 +              } catch (Exception e) {\r
 +                      throw new GitBlitException(e);\r
 +              }\r
 +\r
 +              // create a Gitblit repository model for the clone\r
 +              RepositoryModel cloneModel = repository.cloneAs(cloneName);\r
 +              // owner has REWIND/RW+ permissions\r
 +              cloneModel.addOwner(user.username);\r
 +              updateRepositoryModel(cloneName, cloneModel, false);\r
 +\r
 +              // add the owner of the source repository to the clone's access list\r
 +              if (!ArrayUtils.isEmpty(repository.owners)) {\r
 +                      for (String owner : repository.owners) {\r
 +                              UserModel originOwner = getUserModel(owner);\r
 +                              if (originOwner != null) {\r
 +                                      originOwner.setRepositoryPermission(cloneName, AccessPermission.CLONE);\r
 +                                      updateUserModel(originOwner.username, originOwner, false);\r
 +                              }\r
 +                      }\r
 +              }\r
 +\r
 +              // grant origin's user list clone permission to fork\r
 +              List<String> users = getRepositoryUsers(repository);\r
 +              List<UserModel> cloneUsers = new ArrayList<UserModel>();\r
 +              for (String name : users) {\r
 +                      if (!name.equalsIgnoreCase(user.username)) {\r
 +                              UserModel cloneUser = getUserModel(name);\r
 +                              if (cloneUser.canClone(repository)) {\r
 +                                      // origin user can clone origin, grant clone access to fork\r
 +                                      cloneUser.setRepositoryPermission(cloneName, AccessPermission.CLONE);\r
 +                              }\r
 +                              cloneUsers.add(cloneUser);\r
 +                      }\r
 +              }\r
 +              userService.updateUserModels(cloneUsers);\r
 +\r
 +              // grant origin's team list clone permission to fork\r
 +              List<String> teams = getRepositoryTeams(repository);\r
 +              List<TeamModel> cloneTeams = new ArrayList<TeamModel>();\r
 +              for (String name : teams) {\r
 +                      TeamModel cloneTeam = getTeamModel(name);\r
 +                      if (cloneTeam.canClone(repository)) {\r
 +                              // origin team can clone origin, grant clone access to fork\r
 +                              cloneTeam.setRepositoryPermission(cloneName, AccessPermission.CLONE);\r
 +                      }\r
 +                      cloneTeams.add(cloneTeam);\r
 +              }\r
 +              userService.updateTeamModels(cloneTeams);                       \r
 +\r
 +              // add this clone to the cached model\r
 +              addToCachedRepositoryList(cloneModel);\r
 +              return cloneModel;\r
 +      }\r
 +\r
 +      /**\r
 +       * Allow to understand if GitBlit supports and is configured to allow\r
 +       * cookie-based authentication.\r
 +       * \r
 +       * @return status of Cookie authentication enablement.\r
 +       */\r
 +      public boolean allowCookieAuthentication() {\r
 +              return GitBlit.getBoolean(Keys.web.allowCookieAuthentication, true) && userService.supportsCookies();\r
 +      }\r
 +}\r