Move the SchemaFactory<ReviewDb> into Common

Some day when we have a pure JS offline mode client we'll need
access to our schema factory for the offline versions of our
service implementations.  Moving it to Common as a static field
cleans up a lot of our code, as we don't have to pass around the
GerritServer instance quite as much.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-01-06 16:44:04 -08:00
parent 9b26a425c7
commit ab79bcb54b
29 changed files with 51 additions and 96 deletions

View File

@@ -24,7 +24,6 @@ import com.google.gerrit.client.rpc.NoSuchEntityException;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtjsonrpc.client.VoidResult;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import com.google.gwtorm.client.Transaction;
import java.util.ArrayList;
@@ -35,10 +34,6 @@ import java.util.Set;
public class AccountServiceImpl extends BaseServiceImplementation implements
AccountService {
public AccountServiceImpl(final SchemaFactory<ReviewDb> rdf) {
super(rdf);
}
public void myAccount(final AsyncCallback<Account> callback) {
run(callback, new Action<Account>() {
public Account run(ReviewDb db) throws OrmException {

View File

@@ -26,14 +26,9 @@ import com.google.gerrit.client.rpc.NoSuchEntityException;
import com.google.gerrit.client.workflow.RightRule;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
public class ChangeDetailServiceImpl extends BaseServiceImplementation
implements ChangeDetailService {
public ChangeDetailServiceImpl(final SchemaFactory<ReviewDb> rdf) {
super(rdf);
}
public void changeDetail(final Change.Id id,
final AsyncCallback<ChangeDetail> callback) {
run(callback, new Action<ChangeDetail>() {

View File

@@ -31,7 +31,6 @@ import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtjsonrpc.client.VoidResult;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.ResultSet;
import com.google.gwtorm.client.SchemaFactory;
import com.google.gwtorm.client.Transaction;
import java.util.ArrayList;
@@ -43,10 +42,6 @@ import java.util.Set;
public class ChangeListServiceImpl extends BaseServiceImplementation implements
ChangeListService {
public ChangeListServiceImpl(final SchemaFactory<ReviewDb> rdf) {
super(rdf);
}
public void forAccount(final Account.Id id,
final AsyncCallback<AccountDashboardInfo> callback) {
final Account.Id me = Common.getAccountId();

View File

@@ -19,8 +19,8 @@ import com.google.gerrit.client.reviewdb.AccountGroup;
import com.google.gerrit.client.reviewdb.AccountGroupMember;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.reviewdb.SystemConfig;
import com.google.gerrit.client.rpc.Common;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import java.util.Collections;
import java.util.HashSet;
@@ -30,7 +30,6 @@ import java.util.Set;
/** Cache of group information, including account memberships. */
public class GroupCache {
private final SchemaFactory<ReviewDb> schema;
private AccountGroup.Id adminGroupId;
private AccountGroup.Id anonymousGroupId;
private AccountGroup.Id registeredGroupId;
@@ -44,8 +43,7 @@ public class GroupCache {
}
};
public GroupCache(final SchemaFactory<ReviewDb> rdf, final SystemConfig cfg) {
schema = rdf;
public GroupCache(final SystemConfig cfg) {
adminGroupId = cfg.adminGroupId;
anonymousGroupId = cfg.anonymousGroupId;
registeredGroupId = cfg.registeredGroupId;
@@ -178,7 +176,7 @@ public class GroupCache {
m = new HashSet<AccountGroup.Id>();
try {
final ReviewDb db = schema.open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
for (final AccountGroupMember g : db.accountGroupMembers().byAccount(
accountId)) {

View File

@@ -17,16 +17,9 @@ package com.google.gerrit.client.rpc;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
/** Support for services which require a {@link ReviewDb} instance. */
public class BaseServiceImplementation {
protected final SchemaFactory<ReviewDb> schema;
protected BaseServiceImplementation(final SchemaFactory<ReviewDb> rdf) {
schema = rdf;
}
/**
* Executes <code>action.run</code> with an active ReviewDb connection.
* <p>
@@ -40,7 +33,7 @@ public class BaseServiceImplementation {
*/
protected <T> void run(final AsyncCallback<T> callback, final Action<T> action) {
try {
final ReviewDb db = schema.open();
final ReviewDb db = Common.getSchemaFactory().open();
final T r;
try {
r = action.run(db);

View File

@@ -18,11 +18,14 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.data.GerritConfig;
import com.google.gerrit.client.data.GroupCache;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gwt.core.client.GWT;
import com.google.gwtorm.client.SchemaFactory;
public class Common {
public static final RpcConstants C;
private static GerritConfig config;
private static SchemaFactory<ReviewDb> schema;
private static GroupCache groupCache;
private static CurrentAccountImpl caImpl;
@@ -62,6 +65,19 @@ public class Common {
groupCache = imp;
}
/**
* Get the schema factory for this instance.
* <p>
* <b>Note: this is likely only available on the server side.</b>
*/
public static SchemaFactory<ReviewDb> getSchemaFactory() {
return schema;
}
public static void setSchemaFactory(final SchemaFactory<ReviewDb> imp) {
schema = imp;
}
/** Get the unique id for this account; null if there is no account. */
public static Account.Id getAccountId() {
return caImpl.getAccountId();

View File

@@ -23,7 +23,6 @@ import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.BaseServiceImplementation;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -31,10 +30,6 @@ import java.util.List;
public class SuggestServiceImpl extends BaseServiceImplementation implements
SuggestService {
public SuggestServiceImpl(final SchemaFactory<ReviewDb> rdf) {
super(rdf);
}
public void suggestProjectNameKey(final String query, final int limit,
final AsyncCallback<List<Project.NameKey>> callback) {
run(callback, new Action<List<Project.NameKey>>() {

View File

@@ -72,7 +72,7 @@ public class ImportGerrit1 {
XsrfException, SQLException, IOException, InvalidRepositoryException {
final ProgressMonitor pm = new TextProgressMonitor();
gs = GerritServer.getInstance();
db = gs.getDatabase().open();
db = Common.getSchemaFactory().open();
sql = ((JdbcSchema) db).getConnection();
try {
verifyCategory = db.approvalCategories().byName("Verified");

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.pgm;
import com.google.gerrit.client.reviewdb.Change;
import com.google.gerrit.client.reviewdb.PatchSet;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gerrit.git.InvalidRepositoryException;
import com.google.gerrit.git.PatchSetImporter;
import com.google.gerrit.server.GerritServer;
@@ -62,7 +63,7 @@ public class ReimportPatchSets {
todo.add(PatchSet.Id.parse(line.replace('|', ',')));
}
final ReviewDb db = gs.getDatabase().open();
final ReviewDb db = Common.getSchemaFactory().open();
final ProgressMonitor pm = new TextProgressMonitor();
try {
pm.start(1);

View File

@@ -29,7 +29,6 @@ import com.google.gerrit.server.ssh.SshUtil;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtjsonrpc.client.VoidResult;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import com.google.gwtorm.client.Transaction;
import java.security.NoSuchAlgorithmException;
@@ -41,10 +40,6 @@ import java.util.Set;
public class AccountSecurityImpl extends BaseServiceImplementation implements
AccountSecurity {
public AccountSecurityImpl(final SchemaFactory<ReviewDb> rdf) {
super(rdf);
}
public void mySshKeys(final AsyncCallback<List<AccountSshKey>> callback) {
run(callback, new Action<List<AccountSshKey>>() {
public List<AccountSshKey> run(ReviewDb db) throws OrmException {

View File

@@ -19,6 +19,6 @@ package com.google.gerrit.server;
public class AccountSecuritySrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
return new AccountSecurityImpl(GerritServer.getInstance().getDatabase());
return new AccountSecurityImpl();
}
}

View File

@@ -20,6 +20,6 @@ import com.google.gerrit.client.account.AccountServiceImpl;
public class AccountServiceSrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
return new AccountServiceImpl(GerritServer.getInstance().getDatabase());
return new AccountServiceImpl();
}
}

View File

@@ -15,15 +15,11 @@
package com.google.gerrit.server;
import com.google.gerrit.client.changes.ChangeDetailServiceImpl;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gwtorm.client.SchemaFactory;
/** Publishes {@link ChangeDetailServiceImpl} over JSON. */
public class ChangeDetailServiceSrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
final GerritServer gs = GerritServer.getInstance();
final SchemaFactory<ReviewDb> rdf = gs.getDatabase();
return new ChangeDetailServiceImpl(rdf);
return new ChangeDetailServiceImpl();
}
}

View File

@@ -20,6 +20,6 @@ import com.google.gerrit.client.changes.ChangeListServiceImpl;
public class ChangeListServiceSrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
return new ChangeListServiceImpl(GerritServer.getInstance().getDatabase());
return new ChangeListServiceImpl();
}
}

View File

@@ -98,7 +98,8 @@ public class GerritServer {
repositories = null;
}
Common.setGroupCache(new GroupCache(db, sConfig));
Common.setSchemaFactory(db);
Common.setGroupCache(new GroupCache(sConfig));
}
private Database<ReviewDb> createDatabase() throws OrmException {
@@ -289,11 +290,6 @@ public class GerritServer {
Common.setGerritConfig(r);
}
/** Get the {@link ReviewDb} schema factory for the server. */
public Database<ReviewDb> getDatabase() {
return db;
}
/** Time (in seconds) that user sessions stay "signed in". */
public int getSessionAge() {
return sConfig.maxSessionAge;

View File

@@ -40,10 +40,6 @@ import java.util.Set;
public class GroupAdminServiceImpl extends BaseServiceImplementation implements
GroupAdminService {
public GroupAdminServiceImpl(final GerritServer server) {
super(server.getDatabase());
}
public void ownedGroups(final AsyncCallback<List<AccountGroup>> callback) {
run(callback, new Action<List<AccountGroup>>() {
public List<AccountGroup> run(ReviewDb db) throws OrmException {

View File

@@ -19,6 +19,6 @@ package com.google.gerrit.server;
public class GroupAdminServiceSrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
return new GroupAdminServiceImpl(GerritServer.getInstance());
return new GroupAdminServiceImpl();
}
}

View File

@@ -22,6 +22,7 @@ import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.AccountExternalId;
import com.google.gerrit.client.reviewdb.AccountExternalIdAccess;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gwt.user.server.rpc.RPCServletUtils;
import com.google.gwtjsonrpc.server.JsonServlet;
import com.google.gwtjsonrpc.server.ValidToken;
@@ -236,7 +237,7 @@ public class LoginServlet extends HttpServlet {
Account account = null;
if (user != null) {
try {
final ReviewDb d = server.getDatabase().open();
final ReviewDb d = Common.getSchemaFactory().open();
try {
switch (mode) {
case SIGN_IN:
@@ -465,7 +466,7 @@ public class LoginServlet extends HttpServlet {
Account account;
try {
final ReviewDb db = server.getDatabase().open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
account = db.accounts().get(id);
} finally {

View File

@@ -40,10 +40,6 @@ import java.util.Set;
public class ProjectAdminServiceImpl extends BaseServiceImplementation
implements ProjectAdminService {
public ProjectAdminServiceImpl(final GerritServer server) {
super(server.getDatabase());
}
public void ownedProjects(final AsyncCallback<List<Project>> callback) {
run(callback, new Action<List<Project>>() {
public List<Project> run(ReviewDb db) throws OrmException {

View File

@@ -19,6 +19,6 @@ package com.google.gerrit.server;
public class ProjectAdminServiceSrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
return new ProjectAdminServiceImpl(GerritServer.getInstance());
return new ProjectAdminServiceImpl();
}
}

View File

@@ -20,6 +20,6 @@ import com.google.gerrit.client.ui.SuggestServiceImpl;
public class SuggestServiceSrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
return new SuggestServiceImpl(GerritServer.getInstance().getDatabase());
return new SuggestServiceImpl();
}
}

View File

@@ -25,12 +25,6 @@ import com.google.gwtorm.client.OrmException;
import java.util.List;
public class SystemInfoServiceImpl implements SystemInfoService {
private final GerritServer server;
public SystemInfoServiceImpl(final GerritServer server) {
this.server = server;
}
public void loadGerritConfig(final AsyncCallback<GerritConfig> callback) {
callback.onSuccess(Common.getGerritConfig());
}
@@ -38,7 +32,7 @@ public class SystemInfoServiceImpl implements SystemInfoService {
public void contributorAgreements(
final AsyncCallback<List<ContributorAgreement>> callback) {
try {
final ReviewDb db = server.getDatabase().open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
callback.onSuccess(db.contributorAgreements().active().toList());
} finally {

View File

@@ -19,6 +19,6 @@ package com.google.gerrit.server;
public class SystemInfoServiceSrv extends GerritJsonServlet {
@Override
protected Object createServiceHandle() throws Exception {
return new SystemInfoServiceImpl(GerritServer.getInstance());
return new SystemInfoServiceImpl();
}
}

View File

@@ -20,6 +20,7 @@ import com.google.gerrit.client.reviewdb.Change;
import com.google.gerrit.client.reviewdb.PatchSet;
import com.google.gerrit.client.reviewdb.RevId;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException;
@@ -72,12 +73,11 @@ public class UrlRewriteFilter implements Filter {
}
private FilterConfig config;
private GerritServer server;
public void init(final FilterConfig config) throws ServletException {
this.config = config;
try {
server = GerritServer.getInstance();
GerritServer.getInstance();
} catch (OrmException e) {
throw new ServletException("Cannot initialize GerritServer", e);
} catch (XsrfException e) {
@@ -181,7 +181,7 @@ public class UrlRewriteFilter implements Filter {
final RevId id = new RevId(rev);
final List<PatchSet> patches;
try {
final ReviewDb db = server.getDatabase().open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
if (id.isComplete()) {
patches = db.patchSets().byRevision(id).toList();
@@ -222,7 +222,7 @@ public class UrlRewriteFilter implements Filter {
final String email = cleanEmail(m.group(1));
final List<Account> people;
try {
final ReviewDb db = server.getDatabase().open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
people = db.accounts().byPreferredEmail(email).toList();
} finally {

View File

@@ -28,7 +28,6 @@ public class PatchDetailServiceImpl extends BaseServiceImplementation implements
private final GerritServer server;
public PatchDetailServiceImpl(final GerritServer gs) {
super(gs.getDatabase());
server = gs;
}

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.ssh;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gerrit.git.RepositoryCache;
import com.google.gerrit.server.GerritServer;
import com.google.gwtjsonrpc.server.XsrfException;
@@ -82,7 +83,7 @@ abstract class AbstractCommand implements Command, SessionAware {
protected ReviewDb openReviewDb() throws Failure {
try {
return getGerritServer().getDatabase().open();
return Common.getSchemaFactory().open();
} catch (OrmException e) {
throw new Failure(1, "fatal: Gerrit database is offline");
}

View File

@@ -16,8 +16,8 @@ package com.google.gerrit.server.ssh;
import com.google.gerrit.client.reviewdb.AccountSshKey;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import org.apache.sshd.server.PublickeyAuthenticator;
import org.apache.sshd.server.session.ServerSession;
@@ -36,17 +36,11 @@ import java.util.Collections;
* account as authorized keys are permitted to access the account.
*/
class DatabasePubKeyAuth implements PublickeyAuthenticator {
private final SchemaFactory<ReviewDb> schema;
DatabasePubKeyAuth(final SchemaFactory<ReviewDb> rdf) {
schema = rdf;
}
public boolean hasKey(final String username, final PublicKey inkey,
final ServerSession session) {
AccountSshKey matched = null;
for (final AccountSshKey k : SshUtil.keysFor(schema, username)) {
for (final AccountSshKey k : SshUtil.keysFor(username)) {
if (match(username, k, inkey)) {
if (matched == null) {
matched = k;
@@ -92,7 +86,7 @@ class DatabasePubKeyAuth implements PublickeyAuthenticator {
private void markInvalid(final String username, final AccountSshKey k) {
try {
final ReviewDb db = schema.open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
k.setInvalid();
db.accountSshKeys().update(Collections.singleton(k));
@@ -108,7 +102,7 @@ class DatabasePubKeyAuth implements PublickeyAuthenticator {
private void updateLastUsed(final AccountSshKey k) {
try {
final ReviewDb db = schema.open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
k.setLastUsedOn();
db.accountSshKeys().update(Collections.singleton(k));

View File

@@ -76,7 +76,7 @@ public class SshServlet extends HttpServlet {
new File(srv.getSitePath(), "ssh_host_dsa_key").getAbsolutePath()}));
sshd.setUserAuthFactories(Arrays
.<NamedFactory<UserAuth>> asList(new UserAuthPublicKey.Factory()));
sshd.setPublickeyAuthenticator(new DatabasePubKeyAuth(srv.getDatabase()));
sshd.setPublickeyAuthenticator(new DatabasePubKeyAuth());
sshd.setCommandFactory(new GerritCommandFactory());
sshd.setShellFactory(new NoShell());

View File

@@ -17,8 +17,8 @@ package com.google.gerrit.server.ssh;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.AccountSshKey;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import org.apache.commons.codec.binary.Base64;
import org.apache.sshd.common.KeyPairProvider;
@@ -158,8 +158,7 @@ public class SshUtil {
}
/** Locate keys for the requested account whose email matches the name given. */
public static List<AccountSshKey> keysFor(final SchemaFactory<ReviewDb> rdf,
final String username) {
public static List<AccountSshKey> keysFor(final String username) {
synchronized (keys) {
final List<AccountSshKey> r = keys.get(username);
if (r != null) {
@@ -169,7 +168,7 @@ public class SshUtil {
List<AccountSshKey> kl;
try {
final ReviewDb db = rdf.open();
final ReviewDb db = Common.getSchemaFactory().open();
try {
final List<Account> matches =
db.accounts().bySshUserName(username).toList();