diff options
author | James Moger <james.moger@gitblit.com> | 2014-06-09 14:10:51 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-06-09 14:10:51 -0400 |
commit | ca4d98678c20e4033fdaca09ecbbf0f5952e0b84 (patch) | |
tree | 30b4700846978f90bf8b6d5820e3d5f68d05290d /src/main/java/com | |
parent | a8d9888516fe052d5879b293a91854e41365fd0c (diff) | |
download | gitblit-ca4d98678c20e4033fdaca09ecbbf0f5952e0b84.tar.gz gitblit-ca4d98678c20e4033fdaca09ecbbf0f5952e0b84.zip |
Add repository and user/team lifecycle listener extension points
Diffstat (limited to 'src/main/java/com')
9 files changed, 267 insertions, 16 deletions
diff --git a/src/main/java/com/gitblit/DaggerModule.java b/src/main/java/com/gitblit/DaggerModule.java index b89f8c44..6ad3fe63 100644 --- a/src/main/java/com/gitblit/DaggerModule.java +++ b/src/main/java/com/gitblit/DaggerModule.java @@ -91,8 +91,11 @@ public class DaggerModule { return new NotificationManager(settings); } - @Provides @Singleton IUserManager provideUserManager(IRuntimeManager runtimeManager) { - return new UserManager(runtimeManager); + @Provides @Singleton IUserManager provideUserManager( + IRuntimeManager runtimeManager, + IPluginManager pluginManager) { + + return new UserManager(runtimeManager, pluginManager); } @Provides @Singleton IAuthenticationManager provideAuthenticationManager( @@ -131,10 +134,12 @@ public class DaggerModule { @Provides @Singleton IRepositoryManager provideRepositoryManager( IRuntimeManager runtimeManager, + IPluginManager pluginManager, IUserManager userManager) { return new RepositoryManager( runtimeManager, + pluginManager, userManager); } diff --git a/src/main/java/com/gitblit/FederationClient.java b/src/main/java/com/gitblit/FederationClient.java index cd06c3cb..29cdefe6 100644 --- a/src/main/java/com/gitblit/FederationClient.java +++ b/src/main/java/com/gitblit/FederationClient.java @@ -94,8 +94,8 @@ public class FederationClient { // configure the Gitblit singleton for minimal, non-server operation
RuntimeManager runtime = new RuntimeManager(settings, baseFolder).start();
NoopNotificationManager notifications = new NoopNotificationManager().start();
- UserManager users = new UserManager(runtime).start();
- RepositoryManager repositories = new RepositoryManager(runtime, users).start();
+ UserManager users = new UserManager(runtime, null).start();
+ RepositoryManager repositories = new RepositoryManager(runtime, null, users).start();
FederationManager federation = new FederationManager(runtime, notifications, repositories).start();
IGitblit gitblit = new GitblitManager(runtime, null, notifications, users, null, null, repositories, null, federation);
diff --git a/src/main/java/com/gitblit/MigrateTickets.java b/src/main/java/com/gitblit/MigrateTickets.java index b6d72376..ad1c63ea 100644 --- a/src/main/java/com/gitblit/MigrateTickets.java +++ b/src/main/java/com/gitblit/MigrateTickets.java @@ -135,7 +135,7 @@ public class MigrateTickets { settings.overrideSetting(ITicketService.SETTING_UPDATE_DIFFSTATS, false); IRuntimeManager runtimeManager = new RuntimeManager(settings, baseFolder).start(); - IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, null).start(); + IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, null, null).start(); String inputServiceName = settings.getString(Keys.tickets.service, BranchTicketService.class.getSimpleName()); if (StringUtils.isEmpty(inputServiceName)) { diff --git a/src/main/java/com/gitblit/ReindexTickets.java b/src/main/java/com/gitblit/ReindexTickets.java index 51ca1657..5a614481 100644 --- a/src/main/java/com/gitblit/ReindexTickets.java +++ b/src/main/java/com/gitblit/ReindexTickets.java @@ -127,7 +127,7 @@ public class ReindexTickets { settings.overrideSetting(Keys.web.activityCacheDays, 0); IRuntimeManager runtimeManager = new RuntimeManager(settings, baseFolder).start(); - IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, null).start(); + IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, null, null).start(); String serviceName = settings.getString(Keys.tickets.service, BranchTicketService.class.getSimpleName()); if (StringUtils.isEmpty(serviceName)) { diff --git a/src/main/java/com/gitblit/extensions/RepositoryLifeCycleListener.java b/src/main/java/com/gitblit/extensions/RepositoryLifeCycleListener.java new file mode 100644 index 00000000..5ef03af7 --- /dev/null +++ b/src/main/java/com/gitblit/extensions/RepositoryLifeCycleListener.java @@ -0,0 +1,45 @@ +/* + * Copyright 2014 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.extensions; + +import ro.fortsoft.pf4j.ExtensionPoint; + +import com.gitblit.models.RepositoryModel; + +/** + * Extension point to allow plugins to listen to major repository lifecycle events. + * + * @author James Moger + * @since 1.6.0 + */ +public abstract class RepositoryLifeCycleListener implements ExtensionPoint { + + /** + * Called after a repository has been created. + * + * @param repository + * @since 1.6.0 + */ + public abstract void onCreation(RepositoryModel repository); + + /** + * Called after a repository has been deleted. + * + * @param repository + * @since 1.6.0 + */ + public abstract void onDeletion(RepositoryModel repository); +} diff --git a/src/main/java/com/gitblit/extensions/UserTeamLifeCycleListener.java b/src/main/java/com/gitblit/extensions/UserTeamLifeCycleListener.java new file mode 100644 index 00000000..6f4cd9ba --- /dev/null +++ b/src/main/java/com/gitblit/extensions/UserTeamLifeCycleListener.java @@ -0,0 +1,62 @@ +/* + * Copyright 2014 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.extensions; + +import ro.fortsoft.pf4j.ExtensionPoint; + +import com.gitblit.models.TeamModel; +import com.gitblit.models.UserModel; + +/** + * Extension point to allow plugins to listen to major user and team lifecycle events. + * + * @author James Moger + * @since 1.6.0 + */ +public abstract class UserTeamLifeCycleListener implements ExtensionPoint { + + /** + * Called after a user has been created. + * + * @param user + * @since 1.6.0 + */ + public abstract void onCreation(UserModel user); + + /** + * Called after a user has been deleted. + * + * @param user + * @since 1.6.0 + */ + public abstract void onDeletion(UserModel user); + + /** + * Called after a team has been created. + * + * @param team + * @since 1.6.0 + */ + public abstract void onCreation(TeamModel team); + + /** + * Called after a team has been deleted. + * + * @param team + * @since 1.6.0 + */ + public abstract void onDeletion(TeamModel team); +} diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index 31d6b341..e0721c7c 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -66,6 +66,7 @@ import com.gitblit.Constants.RegistrantType; import com.gitblit.GitBlitException; import com.gitblit.IStoredSettings; import com.gitblit.Keys; +import com.gitblit.extensions.RepositoryLifeCycleListener; import com.gitblit.models.ForkModel; import com.gitblit.models.Metric; import com.gitblit.models.RefModel; @@ -114,6 +115,8 @@ public class RepositoryManager implements IRepositoryManager { private final IRuntimeManager runtimeManager; + private final IPluginManager pluginManager; + private final IUserManager userManager; private final File repositoriesFolder; @@ -126,10 +129,12 @@ public class RepositoryManager implements IRepositoryManager { public RepositoryManager( IRuntimeManager runtimeManager, + IPluginManager pluginManager, IUserManager userManager) { this.settings = runtimeManager.getSettings(); this.runtimeManager = runtimeManager; + this.pluginManager = pluginManager; this.userManager = userManager; this.repositoriesFolder = runtimeManager.getFileOrFolder(Keys.git.repositoriesFolder, "${baseFolder}/git"); } @@ -1420,6 +1425,16 @@ public class RepositoryManager implements IRepositoryManager { removeFromCachedRepositoryList(repositoryName); // model will actually be replaced on next load because config is stale addToCachedRepositoryList(repository); + + if (isCreate && pluginManager != null) { + for (RepositoryLifeCycleListener listener : pluginManager.getExtensions(RepositoryLifeCycleListener.class)) { + try { + listener.onCreation(repository); + } catch (Throwable t) { + logger.error(String.format("failed to call plugin onCreation %s", repositoryName), t); + } + } + } } /** @@ -1588,6 +1603,16 @@ public class RepositoryManager implements IRepositoryManager { FileUtils.delete(folder, FileUtils.RECURSIVE | FileUtils.RETRY); if (userManager.deleteRepositoryRole(repositoryName)) { logger.info(MessageFormat.format("Repository \"{0}\" deleted", repositoryName)); + + if (pluginManager != null) { + for (RepositoryLifeCycleListener listener : pluginManager.getExtensions(RepositoryLifeCycleListener.class)) { + try { + listener.onDeletion(repository); + } catch (Throwable t) { + logger.error(String.format("failed to call plugin onDeletion %s", repositoryName), t); + } + } + } return true; } } diff --git a/src/main/java/com/gitblit/manager/UserManager.java b/src/main/java/com/gitblit/manager/UserManager.java index 67b1d68f..2b82ffb6 100644 --- a/src/main/java/com/gitblit/manager/UserManager.java +++ b/src/main/java/com/gitblit/manager/UserManager.java @@ -32,6 +32,7 @@ import com.gitblit.Constants; import com.gitblit.IStoredSettings; import com.gitblit.IUserService; import com.gitblit.Keys; +import com.gitblit.extensions.UserTeamLifeCycleListener; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; @@ -50,13 +51,16 @@ public class UserManager implements IUserManager { private final IRuntimeManager runtimeManager; + private final IPluginManager pluginManager; + private final Map<String, String> legacyBackingServices; private IUserService userService; - public UserManager(IRuntimeManager runtimeManager) { + public UserManager(IRuntimeManager runtimeManager, IPluginManager pluginManager) { this.settings = runtimeManager.getSettings(); this.runtimeManager = runtimeManager; + this.pluginManager = pluginManager; // map of legacy realm backing user services legacyBackingServices = new HashMap<String, String>(); @@ -209,7 +213,14 @@ public class UserManager implements IUserManager { */ @Override public boolean updateUserModel(UserModel model) { - return userService.updateUserModel(model); + final boolean isCreate = null == userService.getUserModel(model.username); + if (userService.updateUserModel(model)) { + if (isCreate) { + callCreateUserListeners(model); + } + return true; + } + return false; } /** @@ -236,7 +247,14 @@ public class UserManager implements IUserManager { */ @Override public boolean updateUserModel(String username, UserModel model) { - return userService.updateUserModel(username, model); + final boolean isCreate = null == userService.getUserModel(username); + if (userService.updateUserModel(username, model)) { + if (isCreate) { + callCreateUserListeners(model); + } + return true; + } + return false; } /** @@ -247,7 +265,11 @@ public class UserManager implements IUserManager { */ @Override public boolean deleteUserModel(UserModel model) { - return userService.deleteUserModel(model); + if (userService.deleteUserModel(model)) { + callDeleteUserListeners(model); + return true; + } + return false; } /** @@ -262,7 +284,12 @@ public class UserManager implements IUserManager { return false; } String usernameDecoded = StringUtils.decodeUsername(username); - return userService.deleteUser(usernameDecoded); + UserModel user = getUserModel(usernameDecoded); + if (userService.deleteUser(usernameDecoded)) { + callDeleteUserListeners(user); + return true; + } + return false; } /** @@ -349,7 +376,14 @@ public class UserManager implements IUserManager { */ @Override public boolean updateTeamModel(TeamModel model) { - return userService.updateTeamModel(model); + final boolean isCreate = null == userService.getTeamModel(model.name); + if (userService.updateTeamModel(model)) { + if (isCreate) { + callCreateTeamListeners(model); + } + return true; + } + return false; } /** @@ -377,7 +411,14 @@ public class UserManager implements IUserManager { */ @Override public boolean updateTeamModel(String teamname, TeamModel model) { - return userService.updateTeamModel(teamname, model); + final boolean isCreate = null == userService.getTeamModel(teamname); + if (userService.updateTeamModel(teamname, model)) { + if (isCreate) { + callCreateTeamListeners(model); + } + return true; + } + return false; } /** @@ -389,7 +430,11 @@ public class UserManager implements IUserManager { */ @Override public boolean deleteTeamModel(TeamModel model) { - return userService.deleteTeamModel(model); + if (userService.deleteTeamModel(model)) { + callDeleteTeamListeners(model); + return true; + } + return false; } /** @@ -401,7 +446,12 @@ public class UserManager implements IUserManager { */ @Override public boolean deleteTeam(String teamname) { - return userService.deleteTeam(teamname); + TeamModel team = userService.getTeamModel(teamname); + if (userService.deleteTeam(teamname)) { + callDeleteTeamListeners(team); + return true; + } + return false; } /** @@ -440,4 +490,60 @@ public class UserManager implements IUserManager { public boolean deleteRepositoryRole(String role) { return userService.deleteRepositoryRole(role); } + + protected void callCreateUserListeners(UserModel user) { + if (pluginManager == null || user == null) { + return; + } + + for (UserTeamLifeCycleListener listener : pluginManager.getExtensions(UserTeamLifeCycleListener.class)) { + try { + listener.onCreation(user); + } catch (Throwable t) { + logger.error(String.format("failed to call plugin.onCreation%s", user.username), t); + } + } + } + + protected void callCreateTeamListeners(TeamModel team) { + if (pluginManager == null || team == null) { + return; + } + + for (UserTeamLifeCycleListener listener : pluginManager.getExtensions(UserTeamLifeCycleListener.class)) { + try { + listener.onCreation(team); + } catch (Throwable t) { + logger.error(String.format("failed to call plugin.onCreation %s", team.name), t); + } + } + } + + protected void callDeleteUserListeners(UserModel user) { + if (pluginManager == null || user == null) { + return; + } + + for (UserTeamLifeCycleListener listener : pluginManager.getExtensions(UserTeamLifeCycleListener.class)) { + try { + listener.onDeletion(user); + } catch (Throwable t) { + logger.error(String.format("failed to call plugin.onDeletion %s", user.username), t); + } + } + } + + protected void callDeleteTeamListeners(TeamModel team) { + if (pluginManager == null || team == null) { + return; + } + + for (UserTeamLifeCycleListener listener : pluginManager.getExtensions(UserTeamLifeCycleListener.class)) { + try { + listener.onDeletion(team); + } catch (Throwable t) { + logger.error(String.format("failed to call plugin.onDeletion %s", team.name), t); + } + } + } } diff --git a/src/main/java/com/gitblit/servlet/GitblitContext.java b/src/main/java/com/gitblit/servlet/GitblitContext.java index 50f22d5a..d5b4092c 100644 --- a/src/main/java/com/gitblit/servlet/GitblitContext.java +++ b/src/main/java/com/gitblit/servlet/GitblitContext.java @@ -175,6 +175,9 @@ public class GitblitContext extends DaggerContext { runtime.start(); managers.add(runtime); + // create the plugin manager instance but do not start it + loadManager(injector, IPluginManager.class); + // start all other managers startManager(injector, INotificationManager.class); startManager(injector, IUserManager.class); @@ -215,9 +218,14 @@ public class GitblitContext extends DaggerContext { return null; } + protected <X extends IManager> X loadManager(ObjectGraph injector, Class<X> clazz) { + X x = injector.get(clazz); + return x; + } + protected <X extends IManager> X startManager(ObjectGraph injector, Class<X> clazz) { + X x = loadManager(injector, clazz); logManager(clazz); - X x = injector.get(clazz); x.start(); managers.add(x); return x; |