fixed mercurial PreReceiveRepositoryHooks

The problem seems to be that guice had multiple options for injecting
HgContext. HgContextProvider bound via Module and HgContext bound void
RequestScoped annotation. It looks like that Guice 4 injects randomly
the one or the other, in SCMv1 (Guice 3) everything works as expected.

To fix the problem we have created a new class annotated with
RequestScoped, which holds an instance of HgContext. This way only the
HgContextProvider is used for injection.
This commit is contained in:
Sebastian Sdorra
2019-02-13 12:30:40 +01:00
parent 093019a918
commit 352bfe7f5a
4 changed files with 22 additions and 14 deletions

View File

@@ -35,13 +35,10 @@ package sonia.scm.repository;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.inject.servlet.RequestScoped;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
@RequestScoped
public class HgContext public class HgContext
{ {

View File

@@ -63,22 +63,17 @@ public class HgContextProvider implements Provider<HgContext>
* @return * @return
*/ */
@Override @Override
public HgContext get() public HgContext get() {
{ if (contextRequestStore == null) {
HgContext ctx = context;
if (ctx == null)
{
ctx = new HgContext();
logger.trace("context is null, we are probably out of request scope"); logger.trace("context is null, we are probably out of request scope");
return new HgContext();
} }
logger.trace("return HgContext from request store");
return ctx; return contextRequestStore.get();
} }
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */
@Inject(optional = true) @Inject(optional = true)
private HgContext context; private HgContextRequestStore contextRequestStore;
} }

View File

@@ -0,0 +1,14 @@
package sonia.scm.repository;
import com.google.inject.servlet.RequestScoped;
@RequestScoped
public class HgContextRequestStore {
private HgContext context = new HgContext();
public HgContext get() {
return context;
}
}

View File

@@ -134,6 +134,8 @@ public final class HgUtil
repoConfiguration.setHgBin(handler.getConfig().getHgBinary()); repoConfiguration.setHgBin(handler.getConfig().getHgBinary());
logger.debug("open hg repository {}: encoding: {}, pending: {}", directory, enc, pending);
return Repository.open(repoConfiguration, directory); return Repository.open(repoConfiguration, directory);
} }