6262 * Utility to start and stop local Google Cloud Datastore process.
6363 */
6464public class LocalGcdHelper {
65-
65+
6666 private static final Logger log = Logger .getLogger (LocalGcdHelper .class .getName ());
6767
6868 private final String projectId ;
@@ -93,7 +93,7 @@ public class LocalGcdHelper {
9393 }
9494 }
9595 }
96-
96+
9797 private static Path installedGcdPath () {
9898 String gcloudExecutableName ;
9999 if (isWindows ()) {
@@ -132,7 +132,7 @@ private static Path installedGcdPath() {
132132 }
133133 return null ;
134134 }
135-
135+
136136 private static String installedGcdVersion () throws IOException , InterruptedException {
137137 CommandWrapper gcloudCommand ;
138138 if (isWindows ()) {
@@ -142,8 +142,8 @@ private static String installedGcdVersion() throws IOException, InterruptedExcep
142142 }
143143 Process process = gcloudCommand .command ("gcloud" , "version" ).redirectErrorStream ().start ();
144144 process .waitFor ();
145- try (BufferedReader reader =
146- new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
145+ try (BufferedReader reader =
146+ new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
147147 for (String line = reader .readLine (); line != null ; line = reader .readLine ()) {
148148 if (line .startsWith (GCD_VERSION_PREFIX )) {
149149 String [] lineComponents = line .split (" " );
@@ -155,7 +155,7 @@ private static String installedGcdVersion() throws IOException, InterruptedExcep
155155 return null ;
156156 }
157157 }
158-
158+
159159 private static Path executablePath (String cmd ) {
160160 String [] paths = System .getenv ("PATH" ).split (Pattern .quote (File .pathSeparator ));
161161 for (String pathString : paths ) {
@@ -180,7 +180,7 @@ private static class ProcessStreamReader extends Thread {
180180 super ("Local GCD InputStream reader" );
181181 setDaemon (true );
182182 this .process = process ;
183- reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
183+ reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
184184 if (!Strings .isNullOrEmpty (blockUntil )) {
185185 String line ;
186186 do {
@@ -212,48 +212,48 @@ public static ProcessStreamReader start(Process process, String blockUntil) thro
212212 return thread ;
213213 }
214214 }
215-
215+
216216 private static class CommandWrapper {
217-
217+
218218 private final List <String > prefix ;
219219 private List <String > command ;
220220 private String nullFilename ;
221221 private boolean redirectOutputToNull ;
222222 private boolean redirectErrorStream ;
223223 private boolean redirectErrorInherit ;
224224 private Path directory ;
225-
225+
226226 private CommandWrapper () {
227227 this .prefix = new ArrayList <>();
228228 }
229-
229+
230230 public CommandWrapper command (String ... command ) {
231231 this .command = new ArrayList <>(command .length + this .prefix .size ());
232232 this .command .addAll (prefix );
233233 this .command .addAll (Arrays .asList (command ));
234234 return this ;
235235 }
236-
236+
237237 public CommandWrapper redirectOutputToNull () {
238238 this .redirectOutputToNull = true ;
239239 return this ;
240240 }
241-
241+
242242 public CommandWrapper redirectErrorStream () {
243243 this .redirectErrorStream = true ;
244244 return this ;
245245 }
246-
246+
247247 public CommandWrapper redirectErrorInherit () {
248248 this .redirectErrorInherit = true ;
249249 return this ;
250250 }
251-
251+
252252 public CommandWrapper directory (Path directory ) {
253253 this .directory = directory ;
254254 return this ;
255255 }
256-
256+
257257 public ProcessBuilder builder () {
258258 ProcessBuilder builder = new ProcessBuilder (command );
259259 if (redirectOutputToNull ) {
@@ -270,19 +270,19 @@ public ProcessBuilder builder() {
270270 }
271271 return builder ;
272272 }
273-
273+
274274 public Process start () throws IOException {
275275 return builder ().start ();
276276 }
277-
277+
278278 public static CommandWrapper cmd () {
279279 CommandWrapper wrapper = new CommandWrapper ();
280280 wrapper .prefix .add ("cmd" );
281281 wrapper .prefix .add ("/C" );
282282 wrapper .nullFilename = "NUL:" ;
283283 return wrapper ;
284284 }
285-
285+
286286 public static CommandWrapper bash () {
287287 CommandWrapper wrapper = new CommandWrapper ();
288288 wrapper .prefix .add ("bash" );
@@ -321,7 +321,7 @@ public void start() throws IOException, InterruptedException {
321321 }
322322 startGcd (gcdExecutablePath );
323323 }
324-
324+
325325 private void downloadGcd () throws IOException {
326326 // check if we already have a local copy of the gcd utility and download it if not.
327327 File gcdZipFile = new File (System .getProperty ("java.io.tmpdir" ), GCD_FILENAME );
@@ -336,7 +336,7 @@ private void downloadGcd() throws IOException {
336336 } else {
337337 if (log .isLoggable (Level .FINE )) {
338338 log .fine ("Using cached datastore emulator" );
339- }
339+ }
340340 }
341341 // unzip the gcd
342342 try (ZipInputStream zipIn = new ZipInputStream (new FileInputStream (gcdZipFile ))) {
@@ -354,14 +354,14 @@ private void downloadGcd() throws IOException {
354354 zipIn .closeEntry ();
355355 entry = zipIn .getNextEntry ();
356356 }
357- }
357+ }
358358 }
359-
359+
360360 private void startGcd (Path executablePath ) throws IOException , InterruptedException {
361361 // cleanup any possible data for the same project
362362 File datasetFolder = new File (gcdPath .toFile (), projectId );
363363 deleteRecurse (datasetFolder .toPath ());
364-
364+
365365 // create command wrappers
366366 CommandWrapper gcdCreateCommand ;
367367 CommandWrapper gcdStartCommand ;
@@ -380,24 +380,26 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
380380 if (log .isLoggable (Level .FINE )) {
381381 log .log (Level .FINE , "Creating datastore for the project: {0}" , projectId );
382382 }
383- Process temp = gcdCreateCommand . command ( gcdAbsolutePath . toString (), "create" , "-p" , projectId ,
384- projectId )
383+ Process createProcess =
384+ gcdCreateCommand . command ( gcdAbsolutePath . toString (), "create" , "-p" , projectId , projectId )
385385 .redirectErrorInherit ()
386386 .directory (gcdPath )
387387 .redirectOutputToNull ()
388388 .start ();
389- temp .waitFor ();
389+ createProcess .waitFor ();
390390
391391 // start the datastore for the project
392392 if (log .isLoggable (Level .FINE )) {
393393 log .log (Level .FINE , "Starting datastore emulator for the project: {0}" , projectId );
394394 }
395- temp = gcdStartCommand .command (gcdAbsolutePath .toString (), "start" , "--testing" ,
396- "--allow_remote_shutdown" , projectId )
395+ Process startProcess =
396+ gcdStartCommand
397+ .command (gcdAbsolutePath .toString (), "start" , "--testing" , "--allow_remote_shutdown" ,
398+ projectId )
397399 .directory (gcdPath )
398400 .redirectErrorStream ()
399401 .start ();
400- processReader = ProcessStreamReader .start (temp , "Dev App Server is now running" );
402+ processReader = ProcessStreamReader .start (startProcess , "Dev App Server is now running" );
401403 }
402404
403405 private static String md5 (File gcdZipFile ) throws IOException {
@@ -410,7 +412,7 @@ private static String md5(File gcdZipFile) throws IOException {
410412 md5 .update (bytes , 0 , len );
411413 }
412414 }
413- return String .format ("%032x" ,new BigInteger (1 , md5 .digest ()));
415+ return String .format ("%032x" , new BigInteger (1 , md5 .digest ()));
414416 } catch (NoSuchAlgorithmException e ) {
415417 throw new IOException (e );
416418 }
@@ -520,7 +522,7 @@ public static boolean isActive(String projectId) {
520522 urlBuilder .append ("/datastore/v1beta2/datasets/" ).append (projectId ).append ("/lookup" );
521523 URL url = new URL (urlBuilder .toString ());
522524 try (BufferedReader reader =
523- new BufferedReader (new InputStreamReader (url .openStream (), UTF_8 ))) {
525+ new BufferedReader (new InputStreamReader (url .openStream (), UTF_8 ))) {
524526 return "Valid RPC" .equals (reader .readLine ());
525527 }
526528 } catch (IOException ignore ) {
0 commit comments