3535import java .math .BigInteger ;
3636import java .net .HttpURLConnection ;
3737import java .net .MalformedURLException ;
38- import java .net .ServerSocket ;
3938import java .net .URL ;
4039import java .nio .channels .Channels ;
4140import java .nio .channels .ReadableByteChannel ;
5049import java .security .NoSuchAlgorithmException ;
5150import java .util .ArrayList ;
5251import java .util .Arrays ;
53- import java .util .HashMap ;
5452import java .util .List ;
5553import java .util .Locale ;
56- import java .util .Map ;
5754import java .util .regex .Pattern ;
5855import java .util .zip .ZipEntry ;
5956import java .util .zip .ZipInputStream ;
@@ -71,10 +68,9 @@ public class LocalGcdHelper {
7168 private final String projectId ;
7269 private Path gcdPath ;
7370 private ProcessStreamReader processReader ;
74- private final int port ;
7571
7672 public static final String DEFAULT_PROJECT_ID = "projectid1" ;
77- public static final int DEFAULT_PORT = 8080 ;
73+ public static final int PORT = 8080 ;
7874 private static final String GCD_VERSION = "v1beta2" ;
7975 private static final String GCD_BUILD = "rev1-2.1.2b" ;
8076 private static final String GCD_BASENAME = "gcd-" + GCD_VERSION + "-" + GCD_BUILD ;
@@ -98,14 +94,6 @@ public class LocalGcdHelper {
9894 }
9995 }
10096
101- public static int findAvailablePort (int defaultPort ) {
102- try (ServerSocket tempSocket = new ServerSocket (0 )) {
103- return tempSocket .getLocalPort ();
104- } catch (IOException e ) {
105- return defaultPort ;
106- }
107- }
108-
10997 private static Path installedGcdPath () {
11098 String gcloudExecutableName ;
11199 if (isWindows ()) {
@@ -295,9 +283,8 @@ public static CommandWrapper create() {
295283 }
296284 }
297285
298- public LocalGcdHelper (String projectId , int port ) {
286+ public LocalGcdHelper (String projectId ) {
299287 this .projectId = projectId ;
300- this .port = port ;
301288 }
302289
303290 /**
@@ -310,7 +297,7 @@ public LocalGcdHelper(String projectId, int port) {
310297 */
311298 public void start () throws IOException , InterruptedException {
312299 // send a quick request in case we have a hanging process from a previous run
313- sendQuitRequest (port );
300+ sendQuitRequest ();
314301 // Each run is associated with its own folder that is deleted once test completes.
315302 gcdPath = Files .createTempDirectory ("gcd" );
316303 File gcdFolder = gcdPath .toFile ();
@@ -392,12 +379,13 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
392379 if (log .isLoggable (Level .FINE )) {
393380 log .log (Level .FINE , "Starting datastore emulator for the project: {0}" , projectId );
394381 }
395- Process startProcess = CommandWrapper .create ()
396- .command (gcdAbsolutePath .toString (), "start" , "--testing" , "--allow_remote_shutdown" ,
397- "--port=" + Integer .toString (port ), projectId )
398- .directory (gcdPath )
399- .redirectErrorStream ()
400- .start ();
382+ Process startProcess =
383+ CommandWrapper .create ()
384+ .command (gcdAbsolutePath .toString (), "start" , "--testing" , "--allow_remote_shutdown" ,
385+ projectId )
386+ .directory (gcdPath )
387+ .redirectErrorStream ()
388+ .start ();
401389 processReader = ProcessStreamReader .start (startProcess , "Dev App Server is now running" );
402390 }
403391
@@ -431,9 +419,9 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx
431419 }
432420 }
433421
434- public static void sendQuitRequest (int port ) {
422+ public static void sendQuitRequest () {
435423 try {
436- URL url = new URL ("http" , "localhost" , port , "/_ah/admin/quit" );
424+ URL url = new URL ("http" , "localhost" , PORT , "/_ah/admin/quit" );
437425 HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
438426 con .setRequestMethod ("POST" );
439427 con .setDoOutput (true );
@@ -451,7 +439,7 @@ public static void sendQuitRequest(int port) {
451439 }
452440
453441 public void stop () throws IOException , InterruptedException {
454- sendQuitRequest (port );
442+ sendQuitRequest ();
455443 if (processReader != null ) {
456444 processReader .terminate ();
457445 }
@@ -480,70 +468,44 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
480468 });
481469 }
482470
483- public static LocalGcdHelper start (String projectId , int port )
484- throws IOException , InterruptedException {
485- LocalGcdHelper helper = new LocalGcdHelper (projectId , port );
471+ public static LocalGcdHelper start (String projectId ) throws IOException , InterruptedException {
472+ LocalGcdHelper helper = new LocalGcdHelper (projectId );
486473 helper .start ();
487474 return helper ;
488475 }
489476
490477 public static void main (String ... args ) throws IOException , InterruptedException {
491- Map <String , String > parsedArgs = parseArgs (args );
492- String action = parsedArgs .get ("action" );
493- int port = (parsedArgs .get ("port" ) == null ) ? DEFAULT_PORT
494- : Integer .parseInt (parsedArgs .get ("port" ));
495- switch (action ) {
496- case "START" :
497- if (!isActive (DEFAULT_PROJECT_ID , port )) {
498- LocalGcdHelper helper = start (DEFAULT_PROJECT_ID , port );
499- try (FileWriter writer = new FileWriter (".local_gcd_helper" )) {
500- writer .write (
501- helper .gcdPath .toAbsolutePath ().toString () + System .lineSeparator ());
502- writer .write (Integer .toString (port ));
478+ if (args .length == 1 ) {
479+ switch (args [0 ]) {
480+ case "START" :
481+ if (!isActive (DEFAULT_PROJECT_ID )) {
482+ LocalGcdHelper helper = start (DEFAULT_PROJECT_ID );
483+ try (FileWriter writer = new FileWriter (".local_gcd_helper" )) {
484+ writer .write (helper .gcdPath .toAbsolutePath ().toString ());
485+ }
503486 }
504- }
505- return ;
506- case "STOP" :
507- File file = new File (".local_gcd_helper" );
508- String path = null ;
509- boolean fileExists = file . exists ();
510- if ( fileExists ) {
511- try ( BufferedReader reader = new BufferedReader ( new FileReader ( file ))) {
512- path = reader . readLine ();
513- port = Integer . parseInt ( reader . readLine () );
487+ return ;
488+ case "STOP" :
489+ sendQuitRequest ();
490+ File file = new File (".local_gcd_helper" );
491+ if ( file . exists ()) {
492+ try ( BufferedReader reader = new BufferedReader ( new FileReader ( file ))) {
493+ String path = reader . readLine ();
494+ deleteRecurse ( Paths . get ( path ));
495+ }
496+ file . delete ( );
514497 }
515- }
516- sendQuitRequest (port );
517- if (fileExists ) {
518- deleteRecurse (Paths .get (path ));
519- file .delete ();
520- }
521- return ;
522- default :
523- break ;
524- }
525- }
526-
527- private static Map <String , String > parseArgs (String [] args ) {
528- Map <String , String > parsedArgs = new HashMap <String , String >();
529- for (String arg : args ) {
530- if (arg .startsWith ("--port=" )) {
531- parsedArgs .put ("port" , arg .substring ("--port=" .length ()));
532- } else if (arg .equals ("START" ) || arg .equals ("STOP" )) {
533- parsedArgs .put ("action" , arg );
534- } else {
535- throw new RuntimeException ("Only accepts START, STOP, and --port=<port #> as arguments" );
498+ return ;
499+ default :
500+ break ;
536501 }
537502 }
538- if (parsedArgs .get ("action" ) == null ) {
539- throw new RuntimeException ("EXPECTING START | STOP" );
540- }
541- return parsedArgs ;
503+ throw new RuntimeException ("expecting only START | STOP" );
542504 }
543505
544- public static boolean isActive (String projectId , int port ) {
506+ public static boolean isActive (String projectId ) {
545507 try {
546- StringBuilder urlBuilder = new StringBuilder ("http://localhost:" ).append (port );
508+ StringBuilder urlBuilder = new StringBuilder ("http://localhost:" ).append (PORT );
547509 urlBuilder .append ("/datastore/v1beta2/datasets/" ).append (projectId ).append ("/lookup" );
548510 URL url = new URL (urlBuilder .toString ());
549511 try (BufferedReader reader =
0 commit comments