Dynamically load plugins in new private injectors
Each plugin is given its own Guice injector that is isolated from the main server, and from each other. Explicit bindings in the main server are copied down into the plugin's private environment, making any object that is bound in a module (e.g. GerritGlobalModule) automatically available, but hiding anything that is loaded by a just-in-time implicit binding. These private injectors ensure plugins can't accidentally load a just-in-time binding into the sysInjector and cause them to be unable to garbage collect, or to confuse another plugin with a bogus binding. Change-Id: I7bc54c84fba30381cfb58d24b88871b2714c335a
This commit is contained in:

committed by
gerrit code review

parent
b4992582d6
commit
4c847cf912
@@ -0,0 +1,57 @@
|
||||
// Copyright (C) 2012 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.sshd;
|
||||
|
||||
import com.google.gerrit.server.plugins.Plugin;
|
||||
import com.google.gerrit.server.plugins.StartPluginListener;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.apache.sshd.server.Command;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@Singleton
|
||||
class SshPluginStarterCallback implements StartPluginListener {
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SshPluginStarterCallback.class);
|
||||
|
||||
private final DispatchCommandProvider root;
|
||||
|
||||
@Inject
|
||||
SshPluginStarterCallback(
|
||||
@CommandName(Commands.ROOT) DispatchCommandProvider root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartPlugin(Plugin plugin) {
|
||||
if (plugin.getSshInjector() != null) {
|
||||
Key<Command> key = Commands.key(plugin.getName());
|
||||
Provider<Command> cmd;
|
||||
try {
|
||||
cmd = plugin.getSshInjector().getProvider(key);
|
||||
} catch (RuntimeException err) {
|
||||
log.warn(String.format("Plugin %s does not define command",
|
||||
plugin.getName()), err);
|
||||
return;
|
||||
}
|
||||
plugin.add(root.register(Commands.named(plugin.getName()), cmd));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user