From fd6962b1759007451c05294221e5395e2b38fd14 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Fri, 6 Jul 2018 15:39:30 +0200 Subject: [PATCH] Introduce HexFormat to hold hex formatting previously in IdGenerator.format Change-Id: Id5c4b7c9be320434db48e71d6996c80f18946fb6 --- .../gerrit/server/ioutil/HexFormat.java | 27 +++++++++++++++++ .../server/restapi/config/ListTasks.java | 4 +-- .../restapi/project/GarbageCollect.java | 4 +-- .../gerrit/server/util/IdGenerator.java | 10 ------- java/com/google/gerrit/sshd/SshLog.java | 6 ++-- .../gerrit/sshd/commands/ShowConnections.java | 4 +-- .../gerrit/server/ioutil/HexFormatTest.java | 29 +++++++++++++++++++ .../gerrit/server/util/IdGeneratorTest.java | 7 ----- plugins/BUILD | 1 + plugins/replication | 2 +- 10 files changed, 67 insertions(+), 27 deletions(-) create mode 100644 java/com/google/gerrit/server/ioutil/HexFormat.java create mode 100644 javatests/com/google/gerrit/server/ioutil/HexFormatTest.java diff --git a/java/com/google/gerrit/server/ioutil/HexFormat.java b/java/com/google/gerrit/server/ioutil/HexFormat.java new file mode 100644 index 0000000000..fd9c17ac3e --- /dev/null +++ b/java/com/google/gerrit/server/ioutil/HexFormat.java @@ -0,0 +1,27 @@ +// Copyright (C) 2018 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.server.ioutil; + +public class HexFormat { + public static String fromInt(int id) { + final char[] r = new char[8]; + for (int p = 7; 0 <= p; p--) { + final int h = id & 0xf; + r[p] = h < 10 ? (char) ('0' + h) : (char) ('a' + (h - 10)); + id >>= 4; + } + return new String(r); + } +} diff --git a/java/com/google/gerrit/server/restapi/config/ListTasks.java b/java/com/google/gerrit/server/restapi/config/ListTasks.java index d700028116..7b69831124 100644 --- a/java/com/google/gerrit/server/restapi/config/ListTasks.java +++ b/java/com/google/gerrit/server/restapi/config/ListTasks.java @@ -23,13 +23,13 @@ import com.google.gerrit.server.config.ConfigResource; import com.google.gerrit.server.git.WorkQueue; import com.google.gerrit.server.git.WorkQueue.ProjectTask; import com.google.gerrit.server.git.WorkQueue.Task; +import com.google.gerrit.server.ioutil.HexFormat; import com.google.gerrit.server.permissions.GlobalPermission; import com.google.gerrit.server.permissions.PermissionBackend; import com.google.gerrit.server.permissions.PermissionBackendException; import com.google.gerrit.server.permissions.ProjectPermission; import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectState; -import com.google.gerrit.server.util.IdGenerator; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; @@ -133,7 +133,7 @@ public class ListTasks implements RestReadView { public String queueName; public TaskInfo(Task task) { - this.id = IdGenerator.format(task.getTaskId()); + this.id = HexFormat.fromInt(task.getTaskId()); this.state = task.getState(); this.startTime = new Timestamp(task.getStartTime().getTime()); this.delay = task.getDelay(TimeUnit.MILLISECONDS); diff --git a/java/com/google/gerrit/server/restapi/project/GarbageCollect.java b/java/com/google/gerrit/server/restapi/project/GarbageCollect.java index ea1620be9c..3b7f3e3d74 100644 --- a/java/com/google/gerrit/server/restapi/project/GarbageCollect.java +++ b/java/com/google/gerrit/server/restapi/project/GarbageCollect.java @@ -29,9 +29,9 @@ import com.google.gerrit.server.git.GarbageCollection; import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.LocalDiskRepositoryManager; import com.google.gerrit.server.git.WorkQueue; +import com.google.gerrit.server.ioutil.HexFormat; import com.google.gerrit.server.project.ProjectResource; import com.google.gerrit.server.restapi.project.GarbageCollect.Input; -import com.google.gerrit.server.util.IdGenerator; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; @@ -98,7 +98,7 @@ public class GarbageCollect WorkQueue.Task task = (WorkQueue.Task) workQueue.getDefaultQueue().submit(job); String location = - canonicalUrl.get() + "a/config/server/tasks/" + IdGenerator.format(task.getTaskId()); + canonicalUrl.get() + "a/config/server/tasks/" + HexFormat.fromInt(task.getTaskId()); return Response.accepted(location); } diff --git a/java/com/google/gerrit/server/util/IdGenerator.java b/java/com/google/gerrit/server/util/IdGenerator.java index a9a22d989b..276df06d66 100644 --- a/java/com/google/gerrit/server/util/IdGenerator.java +++ b/java/com/google/gerrit/server/util/IdGenerator.java @@ -22,16 +22,6 @@ import java.util.concurrent.atomic.AtomicInteger; /** Simple class to produce 4 billion keys randomly distributed. */ @Singleton public class IdGenerator { - /** Format an id created by this class as a hex string. */ - public static String format(int id) { - final char[] r = new char[8]; - for (int p = 7; 0 <= p; p--) { - final int h = id & 0xf; - r[p] = h < 10 ? (char) ('0' + h) : (char) ('a' + (h - 10)); - id >>= 4; - } - return new String(r); - } private final AtomicInteger gen; diff --git a/java/com/google/gerrit/sshd/SshLog.java b/java/com/google/gerrit/sshd/SshLog.java index b6c2d19eb2..98c649d3db 100644 --- a/java/com/google/gerrit/sshd/SshLog.java +++ b/java/com/google/gerrit/sshd/SshLog.java @@ -27,7 +27,7 @@ import com.google.gerrit.server.config.ConfigKey; import com.google.gerrit.server.config.ConfigUpdatedEvent; import com.google.gerrit.server.config.GerritConfigListener; import com.google.gerrit.server.config.GerritServerConfig; -import com.google.gerrit.server.util.IdGenerator; +import com.google.gerrit.server.ioutil.HexFormat; import com.google.gerrit.server.util.SystemLog; import com.google.gerrit.sshd.SshScope.Context; import com.google.inject.Inject; @@ -277,7 +277,7 @@ class SshLog implements LifecycleListener, GerritConfigListener { } private static String id(int id) { - return IdGenerator.format(id); + return HexFormat.fromInt(id); } void audit(Context ctx, Object result, String cmd) { @@ -298,7 +298,7 @@ class SshLog implements LifecycleListener, GerritConfigListener { created = TimeUtil.nowMs(); } else { SshSession session = ctx.getSession(); - sessionId = IdGenerator.format(session.getSessionId()); + sessionId = HexFormat.fromInt(session.getSessionId()); currentUser = session.getUser(); created = ctx.created; } diff --git a/java/com/google/gerrit/sshd/commands/ShowConnections.java b/java/com/google/gerrit/sshd/commands/ShowConnections.java index b30799bb0d..9b517c6a24 100644 --- a/java/com/google/gerrit/sshd/commands/ShowConnections.java +++ b/java/com/google/gerrit/sshd/commands/ShowConnections.java @@ -22,7 +22,7 @@ import com.google.gerrit.common.data.GlobalCapability; import com.google.gerrit.extensions.annotations.RequiresCapability; import com.google.gerrit.server.CurrentUser; import com.google.gerrit.server.IdentifiedUser; -import com.google.gerrit.server.util.IdGenerator; +import com.google.gerrit.server.ioutil.HexFormat; import com.google.gerrit.sshd.CommandMetaData; import com.google.gerrit.sshd.SshCommand; import com.google.gerrit.sshd.SshDaemon; @@ -168,7 +168,7 @@ final class ShowConnections extends SshCommand { } private static String id(SshSession sd) { - return sd != null ? IdGenerator.format(sd.getSessionId()) : ""; + return sd != null ? HexFormat.fromInt(sd.getSessionId()) : ""; } private static String time(long now, long time) { diff --git a/javatests/com/google/gerrit/server/ioutil/HexFormatTest.java b/javatests/com/google/gerrit/server/ioutil/HexFormatTest.java new file mode 100644 index 0000000000..9bb6951501 --- /dev/null +++ b/javatests/com/google/gerrit/server/ioutil/HexFormatTest.java @@ -0,0 +1,29 @@ +// Copyright (C) 2018 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.server.ioutil; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class HexFormatTest { + + @Test + public void fromInt() { + assertEquals("0000000f", HexFormat.fromInt(0xf)); + assertEquals("801234ab", HexFormat.fromInt(0x801234ab)); + assertEquals("deadbeef", HexFormat.fromInt(0xdeadbeef)); + } +} diff --git a/javatests/com/google/gerrit/server/util/IdGeneratorTest.java b/javatests/com/google/gerrit/server/util/IdGeneratorTest.java index 39afcacb9a..808eca8691 100644 --- a/javatests/com/google/gerrit/server/util/IdGeneratorTest.java +++ b/javatests/com/google/gerrit/server/util/IdGeneratorTest.java @@ -34,11 +34,4 @@ public class IdGeneratorTest { assertEquals(0xdeadbeef, IdGenerator.unmix(IdGenerator.mix(0xdeadbeef))); assertEquals(0x0b966b11, IdGenerator.unmix(IdGenerator.mix(0x0b966b11))); } - - @Test - public void format() { - assertEquals("0000000f", IdGenerator.format(0xf)); - assertEquals("801234ab", IdGenerator.format(0x801234ab)); - assertEquals("deadbeef", IdGenerator.format(0xdeadbeef)); - } } diff --git a/plugins/BUILD b/plugins/BUILD index 4cc982a5ea..b6b79a7bfa 100644 --- a/plugins/BUILD +++ b/plugins/BUILD @@ -19,6 +19,7 @@ genrule2( PLUGIN_API = [ "//java/com/google/gerrit/server", + "//java/com/google/gerrit/server/ioutil", "//java/com/google/gerrit/server/restapi", "//java/com/google/gerrit/pgm/init/api", "//java/com/google/gerrit/httpd", diff --git a/plugins/replication b/plugins/replication index 3a47f8c11e..1086faccd0 160000 --- a/plugins/replication +++ b/plugins/replication @@ -1 +1 @@ -Subproject commit 3a47f8c11ebdbcbc65fc6a58c35d18f1f3c3a74b +Subproject commit 1086faccd0cf2aa53977854767fdc77f048b0253