- Timestamp:
- 2009-05-18T16:10:45+12:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/trunk/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java
r19508 r19513 140 140 static public boolean isRunning() 141 141 { 142 if (!running) return false; // if the url is pending, then running would also be false (server not started up yet) 143 142 if (!running) return false; 144 143 llssite_cfg_file.load(true); 145 String url = llssite_cfg_file.getURL(); 146 if (url == null) return false; 147 148 // Called by Gatherer to check whether we need to stop the server. 149 // if the url is pending, then the GSI hasn't started the server up yet 150 // Already covered in !running 151 //if (url.equals(LLSSiteConfig.URL_PENDING)) return false; 152 144 if (llssite_cfg_file.getURL() == null) return false; 153 145 return true; 154 146 } … … 192 184 193 185 llssite_cfg_file = new LLSSiteConfig(local_library_server_file); 194 195 // If the user launched the GSI independent of GLI, but user has not pressed 196 // Enter Library yet, then we will obtain the previewURL later. 197 if(LocalLibraryServer.isURLPending()) { 198 // running is still false when the URL is pending because only GSI is running, not the server 199 return; 200 } 201 186 202 187 // Spawn local library server process 203 188 String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file); … … 251 236 static public void stop() 252 237 { 253 if (!running) { 254 // also the case if the URL is pending in an independently launched GSI 255 return; 256 } 257 258 // don't (can't) shutdown the GSI/server if it was launched independent of GLI 259 if(llssite_cfg_file.isIndependentGSI()) { 260 System.err.println("**** EXITING. Config file exists: " + llssite_cfg_file.exists()); 238 if (running == false) { 261 239 return; 262 240 } … … 277 255 llssite_cfg_file.load(false); 278 256 int attempt_count = 0; 279 String url = llssite_cfg_file.getURL(); 280 while (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) { // if pending, the server is already stopped (not running) 257 while (llssite_cfg_file.getURL() != null) { 281 258 new OneSecondWait(); // Wait one second (give or take) 282 259 llssite_cfg_file.load(false); … … 292 269 attempt_count = 0; 293 270 } 294 // read the url again to see if it's updated295 url = llssite_cfg_file.getURL();296 271 } 297 272 … … 300 275 301 276 // If the local server is still running then our changed values will get overwritten. 302 url = llssite_cfg_file.getURL(); 303 if (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) { 277 if (llssite_cfg_file.getURL() != null) { 304 278 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 305 279 } … … 372 346 } 373 347 374 /** Returns true if we're waiting on the user to click on Enter Library button 375 * in an independently launched GSI (one not launched by GLI). */ 376 static public boolean isURLPending() { 377 if(Configuration.library_url != null) { 378 return false; // don't need to do anything as we already have the url 379 } 380 348 static public void checkServerRunning() { 349 if (!running) return; // don't worry about it if its not supposed to be running 381 350 llssite_cfg_file.load(true); // don't force reload, load only if modified 382 351 383 String url = llssite_cfg_file.getURL();384 System.err.println("**** url is: " + url);385 386 if(url != null) {387 if(url.equals(LLSSiteConfig.URL_PENDING)) {388 running = false; // imagine if they restarted an external GSI389 return true;390 } else {391 // a valid URL at last392 try {393 Configuration.library_url = new URL(url);394 System.err.println("**** set the config.library url to: " + Configuration.library_url);395 running = true;396 }397 catch (MalformedURLException exception) {398 exception.printStackTrace();399 DebugStream.printStackTrace(exception);400 }401 return false;402 }403 }404 405 // If either the URL is null--which means no independent GSI (Greenstone server interface406 // app) was launched--or if the 'URL' doesn't say it's pending, then we're not waiting407 return false;408 }409 410 static public void checkServerRunning() {411 if (!running) return; // don't worry about it if it's not supposed to be running412 llssite_cfg_file.load(true); // don't force reload, load only if modified413 414 352 String url = llssite_cfg_file.getURL(); 415 353 if(url != null) { … … 422 360 } else { 423 361 // need to restart the server again 424 425 // if we were running an independently launched GSI before, but the GSI has been 426 // exited, it would need to be relaunched, but from within GLI (dependent) this time. 427 if(llssite_cfg_file.isIndependentGSI()) { 428 // first save the current configFile (llssite.cfg), then set to use glisite.cfg 429 llssite_cfg_file.save(); 430 llssite_cfg_file.relaunchAsDependentGSI(local_library_server_file); 431 } else { 432 // We were using glisite.cfg before, don't need to swap config files 433 llssite_cfg_file.set(); 434 } 362 llssite_cfg_file.set(); 363 435 364 // Spawn local library server process 436 365 String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file); … … 462 391 463 392 static public class LLSSiteConfig 464 extends LinkedHashMap 465 { 466 private File configFile; 467 393 extends LinkedHashMap { 468 394 private File llssite_cfg; 469 395 private File glisite_cfg; … … 488 414 static final private String URL = "url"; 489 415 490 static final public String URL_PENDING = "URL_pending";491 492 416 public LLSSiteConfig(File server_exe) { 493 417 debug("New LLSSiteConfig for: " + server_exe.getAbsolutePath()); … … 496 420 glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG); 497 421 498 configFile = null; 499 500 autoenter_initial = null; 501 start_browser_initial = null; 502 503 // first test if server was started independently of GLI 504 // if so, the config file we'd be using would be llssite.cfg 505 if(!usingLLS_configFile()) { // if we were using llssite_cfg, this would have loaded it in 506 // else we try using the glisite configfile 507 useGLISiteCfg(server_exe); 508 } 509 510 if(configFile != null && configFile.exists()) { 511 lastModified = configFile.lastModified(); 512 } 513 } 514 515 /** Tries to get a glisite.cfg file and then loads it, setting it as the configFile */ 516 public void useGLISiteCfg(File server_exe) { 422 File configFile = null; 517 423 if(!glisite_cfg.exists()) { // create it from the templates or the llssite.cfg file 518 424 519 425 File llssite_cfg_in = new File(server_exe.getParentFile(), LLSSITE_CFG+".in"); 520 426 File glisite_cfg_in = new File(server_exe.getParentFile(), GLISITE_CFG+".in"); 521 427 522 428 // need to generate glisite_cfg from glisite_cfg_in, llssite_cfg or llssite.cfg.in 523 429 if(glisite_cfg_in.exists()) { … … 535 441 } 536 442 } 537 // use the config file now 443 538 444 if(glisite_cfg.exists()) { 539 445 configFile = glisite_cfg; 540 load(false); // force reload541 446 } 542 } 543 544 /** Tests whether the server interface is up, running independently of GLI 545 * If so, we don't need to launch the server interface. 546 * The server interface may not have started up the server itself though 547 * (in which case the server URL would be URL_PENDING). 548 * This method returns true if the server interface has already started 549 * and, if so, it would have loaded in the llssite_cfg configFile. 550 */ 551 private boolean usingLLS_configFile() { 552 if(!llssite_cfg.exists()) { 553 return false; 554 } 555 556 // check if the configfile contains the URL line 557 configFile = llssite_cfg; 558 load(false); // force load 559 560 if(getURL() == null) { 561 configFile = null; 562 clear(); // we're not using llssite_cfg, so clear the values we just read 563 return false; 564 } 565 566 System.err.println("***** we're using llssite_configfile"); 567 return true; 568 } 569 570 /** @return true if GSI was started up independently and outside of GLI. 571 * In such a case, GLI would be using llssite_cfg. */ 572 public boolean isIndependentGSI() { 573 return (configFile == llssite_cfg); 574 } 575 576 /** Call this when an independently launched GSI server has been stopped 577 * (one using llssite_cfg) and GLI needs to next launch another server. 578 * In such a case, since GLI itself is relaunching the GSI, we use glisite_cfg. 579 */ 580 public void relaunchAsDependentGSI(File server_exe) { 581 useGLISiteCfg(server_exe); 582 } 583 584 public boolean exists() { 585 return configFile.exists(); 586 } 587 588 public String getLocalHostURL() { 589 StringBuffer url = new StringBuffer(LOCAL_HOST); 590 url.append(COLON); 591 url.append((String)get(PORTNUMBER)); 592 String enterlib = (String)get(ENTERLIB); 593 if(enterlib == null || enterlib.length() == 0) { 594 // Use the default /gsdl and hope for the best. 595 url.append(SEPARATOR); 596 url.append(GSDL); 597 } 598 else { 599 if(!enterlib.startsWith(SEPARATOR)) { 600 url.append(SEPARATOR); 601 } 602 url.append(enterlib); 603 } 604 enterlib = null; 605 debug("Found Local Library Address: " + url.toString()); 606 return url.toString(); 607 } 608 609 /** @return the cmd-line parameter for the configfile used to launch 610 * the server through GLI: --config <glisite.cfg/llssite.cfg file path>. */ 611 public String getSiteConfigFilename() { 612 return SPECIFIC_CONFIG + configFile.getAbsolutePath(); 613 } 614 615 public String getURL() { 616 // URL is made from url and portnumber 617 String url = (String) get(URL); 618 619 // server interface is already up, independent of GLI 620 // but it has not started the server (hence URL is pending) 621 if(url != null && url.equals(URL_PENDING)) { 622 return url; 623 } 624 625 if(!Utility.isWindows()) { 626 return url; 627 } 628 629 if(url != null) { 630 StringBuffer temp = new StringBuffer(url); 631 temp.append(COLON); 632 temp.append((String)get(PORTNUMBER)); 633 String enterlib = (String)get(ENTERLIB); 634 if(enterlib == null || enterlib.length() == 0) { 635 // Use the default /greenstone prefix and hope for the best. 636 temp.append(SEPARATOR); 637 temp.append(GSDL); 638 } 639 else { 640 if(!enterlib.startsWith(SEPARATOR)) { 641 temp.append(SEPARATOR); 642 } 643 temp.append(enterlib); 644 } 645 enterlib = null; 646 url = temp.toString(); 647 } 648 debug("Found Local Library Address: " + url); 649 return url; 650 } 651 652 public boolean isModified() { 653 return (lastModified != configFile.lastModified()); 654 } 655 656 public void load(boolean reloadOnlyIfModified) { 657 658 if(configFile == null) { 659 debug(configFile.getAbsolutePath() + " cannot be found!"); 660 } 661 662 if(isModified()) { 663 lastModified = configFile.lastModified(); 664 } else if(reloadOnlyIfModified) { 665 return; // asked to reload only if modified. Don't reload since not modified 666 } 667 668 if(configFile.exists()) { 447 448 autoenter_initial = null; 449 start_browser_initial = null; 450 if(configFile != null) { 669 451 debug("Load: " + configFile.getAbsolutePath()); 670 452 clear(); … … 691 473 } 692 474 } 475 476 if(glisite_cfg.exists()) { 477 lastModified = glisite_cfg.lastModified(); 478 } 479 } 480 481 public boolean exists() { 482 return llssite_cfg.exists(); 483 } 484 485 public String getLocalHostURL() { 486 StringBuffer url = new StringBuffer(LOCAL_HOST); 487 url.append(COLON); 488 url.append((String)get(PORTNUMBER)); 489 String enterlib = (String)get(ENTERLIB); 490 if(enterlib == null || enterlib.length() == 0) { 491 // Use the default /gsdl and hope for the best. 492 url.append(SEPARATOR); 493 url.append(GSDL); 494 } 693 495 else { 694 debug(configFile.getAbsolutePath() + " cannot be found!"); 496 if(!enterlib.startsWith(SEPARATOR)) { 497 url.append(SEPARATOR); 498 } 499 url.append(enterlib); 500 } 501 enterlib = null; 502 debug("Found Local Library Address: " + url.toString()); 503 return url.toString(); 504 } 505 506 public String getSiteConfigFilename() { 507 return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath(); 508 } 509 510 public String getURL() { 511 // URL is made from url and portnumber 512 String url = (String) get(URL); 513 514 if(!Utility.isWindows()) { 515 return url; 516 } 517 518 if(url != null) { 519 StringBuffer temp = new StringBuffer(url); 520 temp.append(COLON); 521 temp.append((String)get(PORTNUMBER)); 522 String enterlib = (String)get(ENTERLIB); 523 if(enterlib == null || enterlib.length() == 0) { 524 // Use the default /gsdl and hope for the best. 525 temp.append(SEPARATOR); 526 temp.append(GSDL); 527 } 528 else { 529 if(!enterlib.startsWith(SEPARATOR)) { 530 temp.append(SEPARATOR); 531 } 532 temp.append(enterlib); 533 } 534 enterlib = null; 535 url = temp.toString(); 536 } 537 debug("Found Local Library Address: " + url); 538 return url; 539 } 540 541 public boolean isModified() { 542 return (lastModified != glisite_cfg.lastModified()); 543 } 544 545 public void load(boolean reloadOnlyIfModified) { 546 547 if(isModified()) { 548 lastModified = glisite_cfg.lastModified(); 549 } else if(reloadOnlyIfModified) { 550 return; // asked to reload only if modified. Don't reload since not modified 551 } 552 553 if(glisite_cfg.exists()) { 554 debug("Load: " + glisite_cfg.getAbsolutePath()); 555 clear(); 556 try { 557 BufferedReader in = new BufferedReader(new FileReader(glisite_cfg)); 558 String line = null; 559 while((line = in.readLine()) != null) { 560 String key = null; 561 String value = null; 562 int index = -1; 563 if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) { 564 key = line.substring(0, index); 565 value = line.substring(index + 1); 566 } 567 else { 568 key = line; 569 } 570 put(key, value); 571 } 572 in.close(); 573 } 574 catch (Exception error) { 575 error.printStackTrace(); 576 } 577 } 578 else { 579 debug("No glisite.cfg file can be found!"); 695 580 } 696 581 } … … 698 583 /** Restore the autoenter value to its initial value, and remove url if present. */ 699 584 public void restore() { 700 if( configFile!= null) {585 if(glisite_cfg != null) { 701 586 // Delete the file 702 configFile.delete();587 glisite_cfg.delete(); 703 588 } 704 589 else { … … 732 617 private void save() { 733 618 //debug("Save: " + llssite_cfg.getAbsolutePath()); 734 debug("Save: " + configFile.getAbsolutePath());619 debug("Save: " + glisite_cfg.getAbsolutePath()); 735 620 try { 736 621 //BufferedWriter out = new BufferedWriter(new FileWriter(llssite_cfg, false)); 737 BufferedWriter out = new BufferedWriter(new FileWriter( configFile, false));622 BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false)); 738 623 for(Iterator keys = keySet().iterator(); keys.hasNext(); ) { 739 624 String key = (String) keys.next(); … … 742 627 if(value != null) { 743 628 out.write('='); 744 745 // if the server is using llssite.cfg, don't overwrite its default 746 // autoenter and startbrowser values 747 if(configFile == llssite_cfg && (key == AUTOENTER || key == STARTBROWSER)) { 748 if(key == AUTOENTER) { 749 out.write(autoenter_initial, 0, autoenter_initial.length()); 750 } else { // STARTBROWSER 751 out.write(start_browser_initial, 0, start_browser_initial.length()); 752 } 753 } else { 754 out.write(value, 0, value.length()); 755 } 629 out.write(value, 0, value.length()); 756 630 } 757 631 out.newLine();
Note:
See TracChangeset
for help on using the changeset viewer.