Changeset 30513


Ignore:
Timestamp:
2016-05-06T18:51:26+12:00 (8 years ago)
Author:
ak19
Message:

Corrected annoying tabbing inconsistent with emacs, ahead of some commits

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/bin/script/activate.pl

    r30499 r30513  
    4343    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
    4444   
    45     # Adding cpan in, adds in its auto subfolder which conflicts with ActivePerl on Windows
    46     # The auto folder has been moved into a perl-5.8 folder, and this will now be included
    47     # only if the current version of perl is 5.8 (and not ActivePerl).
    48     my $perl_dir;
     45    # Adding cpan in, adds in its auto subfolder which conflicts with ActivePerl on Windows
     46    # The auto folder has been moved into a perl-5.8 folder, and this will now be included
     47    # only if the current version of perl is 5.8 (and not ActivePerl).
     48    my $perl_dir;
    4949
    5050    # Note: $] encodes the version number of perl
     
    6969    $perl_dir = "perl-5.8";
    7070    }
    71 
     71   
    7272    #if ($ENV{'GSDLOS'} !~ /^windows$/i) {
    73         # Use push to put this on the end, so an existing XML::Parser will be used by default
     73    # Use push to put this on the end, so an existing XML::Parser will be used by default
    7474        if (-d "$ENV{'GSDLHOME'}/perllib/cpan/$perl_dir-mt" && $Config{usethreads}){
    7575            push (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/$perl_dir-mt");
     
    112112
    113113sub print_task_msg {
    114     my ($task_msg, $verbosity_setting) = @_;
    115    
    116     $verbosity_setting = $default_verbosity unless $verbosity_setting;
    117     #$verbosity_setting = 1 unless defined $verbosity;
    118     if($verbosity_setting >= 1) {
    119         print STDERR "\n";
    120         print STDERR "************************\n";
    121         print STDERR "* $task_msg\n";
    122         print STDERR "************************\n";
    123     }
     114    my ($task_msg, $verbosity_setting) = @_;
     115   
     116    $verbosity_setting = $default_verbosity unless $verbosity_setting;
     117    #$verbosity_setting = 1 unless defined $verbosity;
     118    if($verbosity_setting >= 1) {
     119    print STDERR "\n";
     120    print STDERR "************************\n";
     121    print STDERR "* $task_msg\n";
     122    print STDERR "************************\n";
     123    }
    124124}
    125125
    126126# Prints messages if the verbosity is right. Does not add new lines.
    127127sub print_msg {
    128     my ($msg, $min_verbosity, $verbosity_setting) = @_;
    129    
    130     # only display error messages if the current
    131     # verbosity setting >= the minimum verbosity level
    132     # needed for that message to be displayed.
    133    
    134     $verbosity_setting = $default_verbosity unless defined $verbosity_setting;
    135     $min_verbosity = 1 unless defined $min_verbosity;
    136     if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
    137         print STDERR "$msg";
    138     }
     128    my ($msg, $min_verbosity, $verbosity_setting) = @_;
     129   
     130    # only display error messages if the current
     131    # verbosity setting >= the minimum verbosity level
     132    # needed for that message to be displayed.
     133   
     134    $verbosity_setting = $default_verbosity unless defined $verbosity_setting;
     135    $min_verbosity = 1 unless defined $min_verbosity;
     136    if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
     137    print STDERR "$msg";
     138    }
    139139}
    140140
     
    143143# and is-persistent (is-persistent only implemented for GS2).
    144144sub config {
    145     my ($library_url, $command, $check_message_against_regex, $site, $expected_error_code, $silent) = @_;
    146             # Gatherer.java's configGS3Server doesn't use the site variable
    147             # so we don't have to either
    148    
    149     # for GS2, getting the HTTP status isn't enough, we need to read the output
    150     # since this is what CollectionManager.config() stipulates.
    151     # Using LWP::UserAgent::get($url) for this 
    152    
    153     if(!defined $library_url) {
    154         return 0;
     145    my ($library_url, $command, $check_message_against_regex, $site, $expected_error_code, $silent) = @_;
     146    # Gatherer.java's configGS3Server doesn't use the site variable
     147    # so we don't have to either
     148   
     149    # for GS2, getting the HTTP status isn't enough, we need to read the output
     150    # since this is what CollectionManager.config() stipulates.
     151    # Using LWP::UserAgent::get($url) for this 
     152   
     153    if(!defined $library_url) {
     154    return 0;
     155    }
     156    else {
     157    $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
     158    # Need to set this, else it takes I don't know how long to timeout
     159    # http://www.perlmonks.org/?node_id=618534
     160   
     161    # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
     162    # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
     163   
     164    my $response_obj = $ua->get( $library_url.$command);
     165   
     166    # $response_obj->content stores the content and $response_obj->code the HTTP response code
     167    my $response_code = $response_obj->code();
     168   
     169    if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
     170        &print_msg("*** Command $library_url$command\n", 3);
     171        &print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
     172       
     173        # check the page content is as expected
     174        my $response_content = $response_obj->content;
     175        my $resultstr = $response_content;
     176        $resultstr =~ s@.*gs_content\"\>@@s;       
     177        $resultstr =~ s@</div>.*@@s;
     178       
     179        if($response_content =~ m/$check_message_against_regex/) {
     180        &print_msg(" Response as expected.\n", 3);
     181        &print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
     182        return 1;
     183        } else {
     184        # if we expect the collection to be inactive, then we'd be in silent mode: if so,
     185        # don't print out the "ping did not succeed" response, but print out any other messages
     186       
     187        # So we only suppress the ping col "did not succeed" response if we're in silent mode
     188        # But if any message other than ping "did not succeed" is returned, we always print it
     189        if($response_content !~ m/did not succeed/ || !$silent) {
     190            &print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
     191            &print_msg("*** Got message:\n$response_content.\n", 4);
     192            &print_msg("*** Got result:\n$resultstr\n", 3);
     193        }
     194        return 0; # ping on a collection may "not succeed."
     195        }
     196    }
     197    elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
     198        # check the page content is as expected
     199        if(defined $expected_error_code && $response_code == $expected_error_code) {
     200        &print_msg(" Response status $response_code as expected.\n", 3);
     201        } else {
     202        &print_msg("*** Command $library_url$command\n");
     203        &print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
     204        }
     205        return 0; # return false, since the response_code was an error, expected or not
    155206    }
    156207    else {
    157         $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
    158             # Need to set this, else it takes I don't know how long to timeout
    159             # http://www.perlmonks.org/?node_id=618534
    160        
    161         # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
    162         # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
    163        
    164         my $response_obj = $ua->get( $library_url.$command);
    165        
    166         # $response_obj->content stores the content and $response_obj->code the HTTP response code
    167         my $response_code = $response_obj->code();
    168        
    169         if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
    170             &print_msg("*** Command $library_url$command\n", 3);
    171             &print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
    172            
    173             # check the page content is as expected
    174             my $response_content = $response_obj->content;
    175             my $resultstr = $response_content;
    176             $resultstr =~ s@.*gs_content\"\>@@s;       
    177             $resultstr =~ s@</div>.*@@s;
    178            
    179             if($response_content =~ m/$check_message_against_regex/) {
    180                 &print_msg(" Response as expected.\n", 3);
    181                 &print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
    182                 return 1;
    183             } else {
    184                 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
    185                 # don't print out the "ping did not succeed" response, but print out any other messages
    186 
    187                 # So we only suppress the ping col "did not succeed" response if we're in silent mode
    188                 # But if any message other than ping "did not succeed" is returned, we always print it
    189                 if($response_content !~ m/did not succeed/ || !$silent) {
    190                 &print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
    191                 &print_msg("*** Got message:\n$response_content.\n", 4);
    192                 &print_msg("*** Got result:\n$resultstr\n", 3);
    193                 }
    194                 return 0; # ping on a collection may "not succeed."
    195             }
    196         }
    197         elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
    198             # check the page content is as expected
    199             if(defined $expected_error_code && $response_code == $expected_error_code) {
    200             &print_msg(" Response status $response_code as expected.\n", 3);
    201             } else {
    202             &print_msg("*** Command $library_url$command\n");
    203             &print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
    204             }
    205             return 0; # return false, since the response_code was an error, expected or not
    206         }
    207         else {
    208             &print_msg("*** Command $library_url$command\n");
    209             &print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
    210             return 0;
    211         }
    212     }   
     208        &print_msg("*** Command $library_url$command\n");
     209        &print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
     210        return 0;
     211    }
     212    }   
    213213}
    214214
    215215sub deactivate_collection {
    216     my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
    217    
    218     if($gs_mode eq "gs2") {
    219         my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
    220         my $check_message_against_regex = q/configured release-collection/;
    221         config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
    222     }
    223     elsif ($gs_mode eq "gs3") {
    224         my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
    225         my $check_message_against_regex = "collection: $qualified_collection deactivated";
    226         config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
    227     }   
     216    my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
     217   
     218    if($gs_mode eq "gs2") {
     219    my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
     220    my $check_message_against_regex = q/configured release-collection/;
     221    config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
     222    }
     223    elsif ($gs_mode eq "gs3") {
     224    my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
     225    my $check_message_against_regex = "collection: $qualified_collection deactivated";
     226    config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
     227    }   
    228228}
    229229
    230230sub activate_collection {
    231     my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
    232    
    233     if($gs_mode eq "gs2") {
    234         my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
    235         my $check_message_against_regex = q/configured add-collection/;
    236         config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
    237     }
    238     elsif ($gs_mode eq "gs3") {
    239         my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
    240         my $check_message_against_regex = "collection: $qualified_collection activated";
    241         config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
    242     }   
     231    my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
     232   
     233    if($gs_mode eq "gs2") {
     234    my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
     235    my $check_message_against_regex = q/configured add-collection/;
     236    config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
     237    }
     238    elsif ($gs_mode eq "gs3") {
     239    my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
     240    my $check_message_against_regex = "collection: $qualified_collection activated";
     241    config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
     242    }   
    243243}
    244244
    245245sub ping {
    246     my ($library_url, $command, $gs_mode, $site, $silent) = @_;
    247 
    248     # If the GS server is not running, we *expect* to see a "500" status code.
    249     # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
    250     # When pinging an inactive collection, it will say it did "not succeed". This is
    251     # a message of interest to return.
    252     my $check_responsemsg_against_regex = q/(succeeded)/;
    253     my $expected_error_code = 500;
    254    
    255     &print_msg("*** COMMAND WAS: |$command|***\n", 4);
    256    
    257     return config($library_url, $command, $check_responsemsg_against_regex, $site, $expected_error_code, $silent);
     246    my ($library_url, $command, $gs_mode, $site, $silent) = @_;
     247   
     248    # If the GS server is not running, we *expect* to see a "500" status code.
     249    # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
     250    # When pinging an inactive collection, it will say it did "not succeed". This is
     251    # a message of interest to return.
     252    my $check_responsemsg_against_regex = q/(succeeded)/;
     253    my $expected_error_code = 500;
     254   
     255    &print_msg("*** COMMAND WAS: |$command|***\n", 4);
     256   
     257    return config($library_url, $command, $check_responsemsg_against_regex, $site, $expected_error_code, $silent);
    258258}
    259259
    260260# send a pingaction to the GS library. General server-level ping.
    261261sub ping_library {
    262     my ($library_url, $gs_mode, $site) = @_;
    263    
    264     my $command = "";
    265     if($gs_mode eq "gs2") {     
    266         $command = "?a=ping";       
    267     }
    268     elsif ($gs_mode eq "gs3") {     
    269         $command = "?a=s&sa=ping";
    270     }
    271     return &ping($library_url, $command, $gs_mode, $site);
     262    my ($library_url, $gs_mode, $site) = @_;
     263   
     264    my $command = "";
     265    if($gs_mode eq "gs2") {     
     266    $command = "?a=ping";       
     267    }
     268    elsif ($gs_mode eq "gs3") {     
     269    $command = "?a=s&sa=ping";
     270    }
     271    return &ping($library_url, $command, $gs_mode, $site);
    272272}
    273273
     
    275275# send a pingaction to a collection in GS library to check if it's active
    276276sub ping_library_collection {
    277     my ($library_url, $gs_mode, $qualified_collection, $site, $silent) = @_;
    278    
    279     my $command = "";
    280     if($gs_mode eq "gs2") {     
    281         $command = "?a=ping&c=$qualified_collection";
    282     }
    283     elsif ($gs_mode eq "gs3") {     
    284         $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";       
    285     }
    286     return &ping($library_url, $command, $gs_mode, $site, $silent);
     277    my ($library_url, $gs_mode, $qualified_collection, $site, $silent) = @_;
     278   
     279    my $command = "";
     280    if($gs_mode eq "gs2") {     
     281    $command = "?a=ping&c=$qualified_collection";
     282    }
     283    elsif ($gs_mode eq "gs3") {     
     284    $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";       
     285    }
     286    return &ping($library_url, $command, $gs_mode, $site, $silent);
    287287}
    288288
     
    290290# this is only for GS2, since the GS3 server is always persistent
    291291sub is_persistent {
    292     my ($library_url, $gs_mode) = @_;
    293    
    294     if($gs_mode eq "gs3") { # GS3 server is always persistent
    295         return 1;
    296     }
    297    
    298     my $command = "?a=is-persistent";   
    299     my $check_responsemsg_against_regex = q/true/;  # isPersistent: true versus isPersistent: false     
    300     return config($library_url, $command, $check_responsemsg_against_regex);
    301 }
    302    
     292    my ($library_url, $gs_mode) = @_;
     293   
     294    if($gs_mode eq "gs3") { # GS3 server is always persistent
     295    return 1;
     296    }
     297   
     298    my $command = "?a=is-persistent";   
     299    my $check_responsemsg_against_regex = q/true/;  # isPersistent: true versus isPersistent: false     
     300    return config($library_url, $command, $check_responsemsg_against_regex);
     301}
     302
    303303sub get_library_URL {
    304     my ($gs_mode, $lib_name) = @_; # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
    305    
    306     # If we get here, we are dealing with a server included with GS.
    307     # For GS3, we ask ant for the library URL.
    308     # For GS2, we derive the URL from the llssite.cfg file.
    309    
    310     my $url = undef;   
    311    
    312     if($gs_mode eq "gs2") {     
    313         my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
     304    my ($gs_mode, $lib_name) = @_; # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
     305   
     306    # If we get here, we are dealing with a server included with GS.
     307    # For GS3, we ask ant for the library URL.
     308    # For GS2, we derive the URL from the llssite.cfg file.
     309   
     310    my $url = undef;   
     311   
     312    if($gs_mode eq "gs2") {     
     313    my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
     314   
     315    if(-f $llssite_cfg) {
     316        # check llssite.cfg for line with url property
     317        # for server.exe also need to use portnumber and enterlib properties           
     318       
     319        # Read in the entire contents of the file in one hit
     320        if (!open (FIN, $llssite_cfg)) {
     321        &print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
     322        return undef;
     323        }
     324       
     325        my $contents;
     326        sysread(FIN, $contents, -s FIN);           
     327        close(FIN);
     328       
     329        my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
     330        my $enterlib = "";
     331        my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
     332       
     333        foreach my $line (@lines) {             
     334        if($line =~ m/^url=(.*)$/) {
     335            $url = $1;                 
     336        } elsif($line =~ m/^enterlib=(.*)$/) {
     337            $enterlib = $1;                 
     338        } elsif($line =~ m/^portnumber=(.*)$/) {
     339            $portnumber = $1;                   
     340        }   
     341        }
     342       
     343        if(!$url) {
     344        return undef;
     345        }
     346        elsif($url eq "URL_pending") { # library is not running
     347        # do not process url=URL_pending in the file, since for server.exe
     348        # this just means the Enter Library button hasn't been pressed yet             
     349        $url = undef;
     350        }
     351        else {
     352        # In the case of server.exe, need to do extra work to get the proper URL
     353        # But first, need to know whether we're indeed dealing with server.exe:
    314354       
    315         if(-f $llssite_cfg) {
    316             # check llssite.cfg for line with url property
    317             # for server.exe also need to use portnumber and enterlib properties           
    318            
    319             # Read in the entire contents of the file in one hit
    320             if (!open (FIN, $llssite_cfg)) {
    321                 &print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
    322                 return undef;
    323             }
    324            
    325             my $contents;
    326             sysread(FIN, $contents, -s FIN);           
    327             close(FIN);
    328            
    329             my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
    330             my $enterlib = "";
    331             my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
    332            
    333             foreach my $line (@lines) {             
    334                 if($line =~ m/^url=(.*)$/) {
    335                     $url = $1;                 
    336                 } elsif($line =~ m/^enterlib=(.*)$/) {
    337                     $enterlib = $1;                 
    338                 } elsif($line =~ m/^portnumber=(.*)$/) {
    339                     $portnumber = $1;                   
    340                 }   
    341             }
    342            
    343             if(!$url) {
    344                 return undef;
    345             }
    346             elsif($url eq "URL_pending") { # library is not running
    347                 # do not process url=URL_pending in the file, since for server.exe
    348                 # this just means the Enter Library button hasn't been pressed yet             
    349                 $url = undef;
    350             }
    351             else {
    352                 # In the case of server.exe, need to do extra work to get the proper URL
    353                 # But first, need to know whether we're indeed dealing with server.exe:
     355        # compare the URL's domain to the full URL
     356        # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
     357        my $uri = URI->new( $url );
     358        my $host = $uri->host;
     359        #print STDERR "@@@@@ host: $host\n";
     360        if($url =~ m/http:\/\/$host(\/)?$/) {
     361            #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
     362            # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)                 
     363            # If the domain with http:// prefix is completely the same as the URL, assume server.exe
     364            # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
     365            $url = $url.":".$portnumber.$enterlib;         
     366        } # else, apache web server         
    354367               
    355                 # compare the URL's domain to the full URL
    356                 # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
    357                 my $uri = URI->new( $url );
    358                 my $host = $uri->host;
    359                 #print STDERR "@@@@@ host: $host\n";
    360                 if($url =~ m/http:\/\/$host(\/)?$/) {
    361                     #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
    362                     # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)                 
    363                     # If the domain with http:// prefix is completely the same as the URL, assume server.exe
    364                     # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
    365                     $url = $url.":".$portnumber.$enterlib;         
    366                 } # else, apache web server         
    367                
    368             }           
    369         }
    370     } elsif($gs_mode eq "gs3") {
    371         # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
    372         # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
    373        
    374         # Or, run the new target get-default-servlet-url
    375         # the output can look like:
    376         #
    377         # Buildfile: build.xml
    378         #   [echo] os.name: Windows Vista
    379         #
    380         # get-default-servlet-url:
    381         #   [echo] http://localhost:8383/greenstone3/library
    382         # BUILD SUCCESSFUL
    383         # Total time: 0 seconds
    384        
    385         #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
    386             # see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
    387        
    388             # The get-default-servlet-url ant target can be run from anywhere by specifying the
    389             # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
    390             # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
    391             # to run the activate.pl script in the first place
    392         my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
    393        
    394         if (open(PIN, "$perl_command |")) {
    395             while (defined (my $perl_output_line = <PIN>)) {
    396                 if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
    397                     $url="http://".$1;
    398                 }
    399             }
    400             close(PIN);
    401         } else {
    402             &print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
    403         }
    404         if(defined $lib_name) {
    405             # replace the servlet_name portion of the url found, with the given library_name
    406             $url =~ s@/[^/]*$@/$lib_name@;
    407         }
    408     }
    409    
    410     # either the url is still undef or it is now set
    411     #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;     
    412     #print STDERR "\n@@@@@ URL still undef\n" if !$url;
    413     return $url;
     368        }           
     369    }
     370    } elsif($gs_mode eq "gs3") {
     371    # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
     372    # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
     373   
     374    # Or, run the new target get-default-servlet-url
     375    # the output can look like:
     376    #
     377    # Buildfile: build.xml
     378    #   [echo] os.name: Windows Vista
     379    #
     380    # get-default-servlet-url:
     381    #   [echo] http://localhost:8383/greenstone3/library
     382    # BUILD SUCCESSFUL
     383    # Total time: 0 seconds
     384   
     385    #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
     386    # see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
     387   
     388    # The get-default-servlet-url ant target can be run from anywhere by specifying the
     389    # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
     390    # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
     391    # to run the activate.pl script in the first place
     392    my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
     393   
     394    if (open(PIN, "$perl_command |")) {
     395        while (defined (my $perl_output_line = <PIN>)) {
     396        if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
     397            $url="http://".$1;
     398        }
     399        }
     400        close(PIN);
     401    } else {
     402        &print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
     403    }
     404    if(defined $lib_name) {
     405        # replace the servlet_name portion of the url found, with the given library_name
     406        $url =~ s@/[^/]*$@/$lib_name@;
     407    }
     408    }
     409   
     410    # either the url is still undef or it is now set
     411    #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;     
     412    #print STDERR "\n@@@@@ URL still undef\n" if !$url;
     413    return $url;
    414414}
    415415
     
    421421# and http://www.perlmonks.org/?node_id=618534
    422422sub is_URL_active {
    423     my $url = shift(@_); # gs3 or gs2 URL   
    424    
    425     my $status = 0;
    426     if(defined $url) {
    427         $ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
    428             # Need to set this, else it takes I don't know how long to timeout
    429             # http://www.perlmonks.org/?node_id=618534
    430        
    431         $status = LWP::Simple::head($url); # returns empty list of headers if it fails
    432                 # LWP::Simple::get($url) is more intensive, so don't need to do that
    433         #print STDERR "**** $url is alive.\n" if $status;
    434     }
    435     return $status;
     423    my $url = shift(@_); # gs3 or gs2 URL   
     424   
     425    my $status = 0;
     426    if(defined $url) {
     427    $ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
     428    # Need to set this, else it takes I don't know how long to timeout
     429    # http://www.perlmonks.org/?node_id=618534
     430   
     431    $status = LWP::Simple::head($url); # returns empty list of headers if it fails
     432    # LWP::Simple::get($url) is more intensive, so don't need to do that
     433    #print STDERR "**** $url is alive.\n" if $status;
     434    }
     435    return $status;
    436436}
    437437
    438438# Pinging seems to always return true, so this method doesn't work
    439439sub pingHost {
    440     my $url = shift(@_); # gs3 or gs2 URL
    441    
    442     my $status = 0;
    443     if(defined $url) {
    444         # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
    445         # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
    446        
    447         #my $host = $url;       
    448         #$host =~ s@^http:\/\/(www.)?@@;       
    449         #$host =~ s@\/.*@@;
    450         #print STDERR "**** HOST: $host\n";
    451        
    452         # More robust way
    453         # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
    454         my $uri = URI->new( $url );
    455         my $host = $uri->host;
    456        
    457         # Ping the host. http://perldoc.perl.org/Net/Ping.html 
    458         my $p = Net::Ping->new();       
    459         $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
    460         print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
    461         $p->close();       
    462     }
    463     # return whether pinging was a success or failure
    464     return $status;
     440    my $url = shift(@_); # gs3 or gs2 URL
     441   
     442    my $status = 0;
     443    if(defined $url) {
     444    # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
     445    # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
     446   
     447    #my $host = $url;       
     448    #$host =~ s@^http:\/\/(www.)?@@;       
     449    #$host =~ s@\/.*@@;
     450    #print STDERR "**** HOST: $host\n";
     451   
     452    # More robust way
     453    # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
     454    my $uri = URI->new( $url );
     455    my $host = $uri->host;
     456   
     457    # Ping the host. http://perldoc.perl.org/Net/Ping.html 
     458    my $p = Net::Ping->new();       
     459    $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
     460    print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
     461    $p->close();       
     462    }
     463    # return whether pinging was a success or failure
     464    return $status;
    465465}
    466466
     
    478478
    479479    if (($argc==0)  || (($argc==1) && ($argv[0] =~ m/^--?h(elp)?$/))) {
    480         my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
    481 
    482 
    483         print STDERR "\n";
    484         print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -skipactivation -removeold -keepold -verbosity v\n";
    485         print STDERR "\t-library_url URL -library_name n] <[colgroup/]collection>\n";
    486         print STDERR "\n";
    487 
    488         exit(-1);
    489     }
    490    
    491     # get the collection details
     480    my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
     481   
     482   
     483    print STDERR "\n";
     484    print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -skipactivation -removeold -keepold -verbosity v\n";
     485    print STDERR "\t-library_url URL -library_name n] <[colgroup/]collection>\n";
     486    print STDERR "\n";
     487   
     488    exit(-1);
     489    }
     490   
     491    # get the collection details
    492492    my $qualified_collection = pop @argv; # qualified collection
    493    
     493   
    494494    my $collect_dir = undef; #"collect"; # can't be "collect" when only -site is provided for GS3
    495495    my $build_dir = undef;
    496     my $index_dir = undef;
    497     my $site = undef;
    498    
    499     # if run from server (java code), it will handle deactivation and activation to prevent open file handles when java launches this script and exits:
    500     my $skipactivation = 0;
    501     my $removeold = 0;
    502     my $keepold = 0;
    503     my $incremental = 0; # used by solr
    504 
    505     my $library_url = $ENV{'GSDL_LIBRARY_URL'} || undef; # to be specified on the cmdline if not using a GS-included web server
    506         # the GSDL_LIBRARY_URL env var is useful when running cmdline buildcol.pl in the linux package manager versions of GS3
    507    
     496    my $index_dir = undef;
     497    my $site = undef;
     498   
     499    # if run from server (java code), it will handle deactivation and activation to prevent open file handles when java launches this script and exits:
     500    my $skipactivation = 0;
     501    my $removeold = 0;
     502    my $keepold = 0;
     503    my $incremental = 0; # used by solr
     504   
     505    my $library_url = $ENV{'GSDL_LIBRARY_URL'} || undef; # to be specified on the cmdline if not using a GS-included web server
     506    # the GSDL_LIBRARY_URL env var is useful when running cmdline buildcol.pl in the linux package manager versions of GS3
     507   
    508508    my $library_name = undef;
    509 
     509   
    510510    while (my $arg = shift @argv) {
    511         if ($arg eq "-collectdir") {
    512             $collect_dir = shift @argv;
    513         }
    514         elsif ($arg eq "-builddir") {
    515             $build_dir = shift @argv;
    516         }
    517         elsif ($arg eq "-indexdir") {
    518             $index_dir = shift @argv;
    519         }
    520         elsif ($arg eq "-site") {
    521             $site = shift @argv;
    522         }
    523         elsif ($arg eq "-skipactivation") {
    524             $skipactivation = 1;
    525         }
    526         elsif ($arg eq "-removeold") {
    527             $removeold = 1;
    528         }
    529         elsif ($arg eq "-keepold") {
    530             $keepold = 1;
    531         }
    532         elsif ($arg eq "-incremental") {
    533             $incremental = 1;
    534         }
    535         elsif ($arg eq "-library_url") {
    536             $library_url = shift @argv;
    537         }
    538         elsif ($arg eq "-library_name") {
    539             $library_name = shift @argv;
    540         }
    541         elsif ($arg eq "-verbosity") {
    542             $default_verbosity = shift @argv; # global variable
    543            
    544             # ensure we're working with ints not strings (int context not str context), in case verbosity=0
    545             # http://stackoverflow.com/questions/288900/how-can-i-convert-a-string-to-a-number-in-perl
    546             $default_verbosity = int($default_verbosity || 0); ### is this the best way?
    547         }
    548     }
    549    
    550     # work out the building and index dirs
    551     my $collection_dir = &util::resolve_collection_dir($collect_dir, $qualified_collection, $site);
    552     $build_dir = &FileUtils::filenameConcatenate($collection_dir, "building") unless (defined $build_dir);
    553     $index_dir = &FileUtils::filenameConcatenate($collection_dir, "index") unless (defined $index_dir);
    554    
    555     &print_task_msg("Running  Collection  Activation  Stage");
     511    if ($arg eq "-collectdir") {
     512        $collect_dir = shift @argv;
     513    }
     514    elsif ($arg eq "-builddir") {
     515        $build_dir = shift @argv;
     516    }
     517    elsif ($arg eq "-indexdir") {
     518        $index_dir = shift @argv;
     519    }
     520    elsif ($arg eq "-site") {
     521        $site = shift @argv;
     522    }
     523    elsif ($arg eq "-skipactivation") {
     524        $skipactivation = 1;
     525    }
     526    elsif ($arg eq "-removeold") {
     527        $removeold = 1;
     528    }
     529    elsif ($arg eq "-keepold") {
     530        $keepold = 1;
     531    }
     532    elsif ($arg eq "-incremental") {
     533        $incremental = 1;
     534    }
     535    elsif ($arg eq "-library_url") {
     536        $library_url = shift @argv;
     537    }
     538    elsif ($arg eq "-library_name") {
     539        $library_name = shift @argv;
     540    }
     541    elsif ($arg eq "-verbosity") {
     542        $default_verbosity = shift @argv; # global variable
     543       
     544        # ensure we're working with ints not strings (int context not str context), in case verbosity=0
     545        # http://stackoverflow.com/questions/288900/how-can-i-convert-a-string-to-a-number-in-perl
     546        $default_verbosity = int($default_verbosity || 0); ### is this the best way?
     547    }
     548    }
     549   
     550    # work out the building and index dirs
     551    my $collection_dir = &util::resolve_collection_dir($collect_dir, $qualified_collection, $site);
     552    $build_dir = &FileUtils::filenameConcatenate($collection_dir, "building") unless (defined $build_dir);
     553    $index_dir = &FileUtils::filenameConcatenate($collection_dir, "index") unless (defined $index_dir);
     554   
     555    &print_task_msg("Running  Collection  Activation  Stage");
    556556   
    557557    # get and check the collection name
    558558    if ((&colcfg::use_collection($site, $qualified_collection, $collect_dir)) eq "") {
    559         &print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
    560         exit -1;
    561     }
    562    
     559    &print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
     560    exit -1;
     561    }
     562   
    563563    # Read in the collection configuration file.
    564564    # Beware: Only if $site is specified in the cmdline does collectionConfig.xml get
     
    570570    my $collect_cfg_filename = &colcfg::get_collect_cfg_name(STDERR, $gs_mode);
    571571    my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);
    572 
     572   
    573573    # look for build.cfg/buildConfig.xml
    574574    my $build_cfg_filename ="";
    575    
     575   
    576576    if ($gs_mode eq "gs2") {
    577         $build_cfg_filename = &FileUtils::filenameConcatenate($build_dir,"build.cfg");
     577    $build_cfg_filename = &FileUtils::filenameConcatenate($build_dir,"build.cfg");
    578578    } else {
    579         $build_cfg_filename = &FileUtils::filenameConcatenate($build_dir, "buildConfig.xml");
    580         # gs_mode is GS3. Set the site now if this was not specified as cmdline argument
    581         #$site = "localsite" unless defined $site;
    582     }
    583    
     579    $build_cfg_filename = &FileUtils::filenameConcatenate($build_dir, "buildConfig.xml");
     580    # gs_mode is GS3. Set the site now if this was not specified as cmdline argument
     581    #$site = "localsite" unless defined $site;
     582    }
     583   
    584584    # We need to know the buildtype for Solr.
    585585    # Any change of indexers is already detected and handled by the calling code (buildcol or
    586586    # full-rebuild), so that at this stage the config file's buildtype reflects the actual buildtype.
    587 
     587   
    588588    # From buildcol.pl we use searchtype for determining buildtype, but for old versions, use buildtype
    589589    my $buildtype;
     
    607607    }
    608608   
    609     # Now the logic in GLI's CollectionManager.java (processComplete()
    610     # and installCollection()) and Gatherer.configGS3Server().
    611 
    612     # 1. Get library URL
    613    
    614     # For web servers that are external to a Greenstone installation,
    615     # the user can pass in their web server's library URL.
    616     # For web servers included with GS (like tomcat for GS3 and server.exe
    617     # and apache for GS2), we work out the library URL:
    618     if(!$library_url) {
    619         $library_url = &get_library_URL($gs_mode, $library_name); # returns undef if no server is running
    620     }
    621    
    622     # CollectionManager's installCollection phase in GLI
    623     # 2. Ping the library URL, and if it's a persistent server and running, release the collection
    624    
    625     my $is_persistent_server = undef;
    626     if(!$skipactivation && $library_url) { # undef if no valid server URL
    627 
    628         &print_msg("Pinging $library_url\n");       
    629         if (&ping_library($library_url, $gs_mode, $site)) { # server running
     609    # Now the logic in GLI's CollectionManager.java (processComplete()
     610    # and installCollection()) and Gatherer.configGS3Server().
     611   
     612    # 1. Get library URL
     613   
     614    # For web servers that are external to a Greenstone installation,
     615    # the user can pass in their web server's library URL.
     616    # For web servers included with GS (like tomcat for GS3 and server.exe
     617    # and apache for GS2), we work out the library URL:
     618    if(!$library_url) {
     619    $library_url = &get_library_URL($gs_mode, $library_name); # returns undef if no server is running
     620    }
     621   
     622    # CollectionManager's installCollection phase in GLI
     623    # 2. Ping the library URL, and if it's a persistent server and running, release the collection
     624   
     625    my $is_persistent_server = undef;
     626    if(!$skipactivation && $library_url) { # undef if no valid server URL
     627   
     628    &print_msg("Pinging $library_url\n");       
     629    if (&ping_library($library_url, $gs_mode, $site)) { # server running
     630       
     631        # server is running, so release the collection if
     632        # the server is persistent and the collection is active
     633        &print_msg("Checking if Greenstone server is persistent\n");
     634        $is_persistent_server = &is_persistent($library_url, $gs_mode);         
     635       
     636        if ($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
    630637       
    631             # server is running, so release the collection if
    632             # the server is persistent and the collection is active
    633             &print_msg("Checking if Greenstone server is persistent\n");
    634             $is_persistent_server = &is_persistent($library_url, $gs_mode);         
    635            
    636             if ($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
    637                
    638                 &print_msg("Checking if the collection $qualified_collection is already active\n");
    639                 my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
    640 
    641                 if ($collection_active) {
    642                 &print_msg("De-activating collection $qualified_collection\n");
    643                 &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
    644                 }
    645                 else {
    646                 &print_msg("Collection is not active => No need to deactivate\n");
    647                 }
    648             }
    649             else {
    650                 &print_msg("Server is not persistent => No need to deactivate collection\n");
    651             }
     638        &print_msg("Checking if the collection $qualified_collection is already active\n");
     639        my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
     640       
     641        if ($collection_active) {
     642            &print_msg("De-activating collection $qualified_collection\n");
     643            &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
    652644        }
    653645        else {
    654             &print_msg("No response to Ping => Taken to mean server is not running\n");
    655         }
    656 
    657     }
    658 
     646            &print_msg("Collection is not active => No need to deactivate\n");
     647        }
     648        }
     649        else {
     650        &print_msg("Server is not persistent => No need to deactivate collection\n");
     651        }
     652    }
     653    else {
     654        &print_msg("No response to Ping => Taken to mean server is not running\n");
     655    }
     656   
     657    }
     658   
    659659    # 2b. If we're working with a solr collection, then start up the solrserver now.
    660660    my $solr_server;
     
    681681    $solr_server = new solrserver($build_dir);
    682682    $solr_server->start();
    683 
     683   
    684684    # We'll be moving building to index. For solr collection, there's further
    685685    # special processing to make a corresponding change to the solr.xml
     
    688688
    689689
    690     # 3. Do all the moving building to index stuff now 
    691 
    692     # If removeold: replace index dir with building dir.
    693     # If keepold: move building's contents into index, where only duplicates will get deleted.
    694     # removeold and keepold can't both be on at the same time
    695         # incremental becomes relevant for solr, though it was irrelevant to what activate.pl does (moving building to index)
    696     my $incremental_mode;
    697     ($removeold, $keepold, $incremental, $incremental_mode) = &scriptutil::check_removeold_and_keepold($removeold, $keepold,
     690    # 3. Do all the moving building to index stuff now 
     691   
     692    # If removeold: replace index dir with building dir.
     693    # If keepold: move building's contents into index, where only duplicates will get deleted.
     694    # removeold and keepold can't both be on at the same time
     695    # incremental becomes relevant for solr, though it was irrelevant to what activate.pl does (moving building to index)
     696    my $incremental_mode;
     697    ($removeold, $keepold, $incremental, $incremental_mode) = &scriptutil::check_removeold_and_keepold($removeold, $keepold,
    698698                           $incremental,
    699699                           $build_dir, # checkdir. Usually archives or export to be deleted. activate.pl deletes building
    700700                           $collectcfg);
    701701   
    702     if($removeold) {
    703        
    704         if(&FileUtils::directoryExists($index_dir)) {
    705             &print_task_msg("Removing \"index\"");
    706 
    707             if ($buildtype eq "solr") {
    708                 # if solr, remove any cores that are using the index_dir before deleting this dir
    709                 foreach my $corename (@corenames) {
    710                 $solr_server->admin_unload_core($corename);
    711                 }
    712             }   
    713 
    714             &FileUtils::removeFilesRecursive($index_dir);
    715            
    716             # Wait for a couple of seconds, just for luck
    717             sleep 2;
    718            
    719             if (&FileUtils::directoryExists($index_dir)) {
    720                 &print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
    721             }
    722         }
    723        
    724         # if remote GS server: gliserver.pl would call activate.pl to activate
    725         # the collection at this point since activate.pl lives on the server side
    726        
    727         if ($buildtype eq "solr") {
    728             # if solr, remove any cores that are using the building_dir before moving this dir onto index
    729             foreach my $corename (@corenames) {
    730             $solr_server->admin_unload_core("building-$corename");
    731             }
    732         }
    733 
    734         # Move the building directory to become the new index directory
    735         &print_task_msg("Moving \"building\" -> \"index\"");
    736         &FileUtils::moveFiles($build_dir, $index_dir);
    737         if(&FileUtils::directoryExists($build_dir) || !&FileUtils::directoryExists($index_dir)) {           
    738             &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
    739         }
    740     }
    741     elsif ($keepold || $incremental) {
    742             if ($buildtype eq "solr") {
    743             # if solr, remove any cores that may be using the building_dir before moving this dir onto index
    744             foreach my $corename (@corenames) {         
    745             $solr_server->admin_unload_core("building-$corename") if $solr_server->admin_ping_core("building-$corename");
    746             }
    747         }
    748        
    749         if($build_dir eq $index_dir) { # building_dir can have been set to "index" folder, see incremental-buildcol.pl
    750             &print_task_msg("building folder is index folder, not moving");
    751         } else {
    752             # Copy just the contents of building dir into the index dir, overwriting
    753             # existing files, but don't replace index with building.
    754             &print_task_msg("Moving \"building\" -> \"index\"");       
    755             &FileUtils::moveDirectoryContents($build_dir, $index_dir);
    756         }
    757     }
    758 
     702    if($removeold) {
     703   
     704    if(&FileUtils::directoryExists($index_dir)) {
     705        &print_task_msg("Removing \"index\"");
     706       
     707        if ($buildtype eq "solr") {
     708        # if solr, remove any cores that are using the index_dir before deleting this dir
     709        foreach my $corename (@corenames) {
     710            $solr_server->admin_unload_core($corename);
     711        }
     712        }   
     713       
     714        &FileUtils::removeFilesRecursive($index_dir);
     715       
     716        # Wait for a couple of seconds, just for luck
     717        sleep 2;
     718       
     719        if (&FileUtils::directoryExists($index_dir)) {
     720        &print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
     721        }
     722    }
     723   
     724    # if remote GS server: gliserver.pl would call activate.pl to activate
     725    # the collection at this point since activate.pl lives on the server side
     726   
     727    if ($buildtype eq "solr") {
     728        # if solr, remove any cores that are using the building_dir before moving this dir onto index
     729        foreach my $corename (@corenames) {
     730        $solr_server->admin_unload_core("building-$corename");
     731        }
     732    }
     733   
     734    # Move the building directory to become the new index directory
     735    &print_task_msg("Moving \"building\" -> \"index\"");
     736    &FileUtils::moveFiles($build_dir, $index_dir);
     737    if(&FileUtils::directoryExists($build_dir) || !&FileUtils::directoryExists($index_dir)) {           
     738        &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
     739    }
     740    }
     741    elsif ($keepold || $incremental) {
     742    if ($buildtype eq "solr") {
     743        # if solr, remove any cores that may be using the building_dir before moving this dir onto index
     744        foreach my $corename (@corenames) {         
     745        $solr_server->admin_unload_core("building-$corename") if $solr_server->admin_ping_core("building-$corename");
     746        }
     747    }
     748   
     749    if($build_dir eq $index_dir) { # building_dir can have been set to "index" folder, see incremental-buildcol.pl
     750        &print_task_msg("building folder is index folder, not moving");
     751    } else {
     752        # Copy just the contents of building dir into the index dir, overwriting
     753        # existing files, but don't replace index with building.
     754        &print_task_msg("Moving \"building\" -> \"index\"");       
     755        &FileUtils::moveDirectoryContents($build_dir, $index_dir);
     756    }
     757    }
     758   
    759759    if ($buildtype eq "solr") {
    760760    # Call CREATE action to get the old cores pointing to the index folder
     
    775775        }
    776776    }
    777 
     777   
    778778    # regenerate the solr.xml.in from solr.xml in case we are working off a dvd.
    779779    $solr_server->solr_xml_to_solr_xml_in();
    780780    }
    781 
    782     # 4. Ping the library URL, and if it's a persistent server and running, activate the collection again   
    783    
    784     # Check for success: if building does not exist OR is empty OR if building is index (in which case there was no move)   
    785     if($build_dir eq $index_dir || !&FileUtils::directoryExists($build_dir) || &FileUtils::isDirectoryEmpty($build_dir)) {
    786    
    787         if(!$skipactivation && $library_url) { # undef if no valid server URL
     781   
     782    # 4. Ping the library URL, and if it's a persistent server and running, activate the collection again   
     783   
     784    # Check for success: if building does not exist OR is empty OR if building is index (in which case there was no move)   
     785    if($build_dir eq $index_dir || !&FileUtils::directoryExists($build_dir) || &FileUtils::isDirectoryEmpty($build_dir)) {
     786   
     787    if(!$skipactivation && $library_url) { # undef if no valid server URL
     788       
     789        &print_msg("Pinging $library_url\n");
     790        if (&ping_library($library_url, $gs_mode, $site)) { # server running
    788791       
    789             &print_msg("Pinging $library_url\n");
    790             if (&ping_library($library_url, $gs_mode, $site)) { # server running
     792        # don't need to work out persistency of server more than once, since the libraryURL hasn't changed             
     793        if (!defined $is_persistent_server) {
     794            &print_msg("Checking if Greenstone server is persistent\n");
     795            $is_persistent_server = &is_persistent($library_url, $gs_mode);
     796        }
     797       
     798       
     799        if ($is_persistent_server) { # persistent server, so can try activating collection
     800           
     801            &print_msg("Checking if the collection $qualified_collection is not already active\n");
     802           
     803            # Since we could have deactivated the collection at this point,
     804            # it is likely that it is not yet active. When pinging the collection
     805            # a "ping did not succeed" message is expected, therefore tell the ping
     806            # to proceed silently
     807            my $silent = 1;
     808            my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site, $silent);
     809           
     810            if (!$collection_active) {
     811            &print_msg(" Collection is not active.\n");
     812            &print_msg("Activating collection $qualified_collection\n");
     813            &activate_collection($library_url, $gs_mode, $qualified_collection, $site);
    791814           
    792                 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed             
    793                 if (!defined $is_persistent_server) {
    794                     &print_msg("Checking if Greenstone server is persistent\n");
    795                     $is_persistent_server = &is_persistent($library_url, $gs_mode);
    796                 }
    797                
    798                    
    799                 if ($is_persistent_server) { # persistent server, so can try activating collection
    800 
    801                     &print_msg("Checking if the collection $qualified_collection is not already active\n");
    802 
    803                     # Since we could have deactivated the collection at this point,
    804                     # it is likely that it is not yet active. When pinging the collection
    805                     # a "ping did not succeed" message is expected, therefore tell the ping
    806                     # to proceed silently
    807                     my $silent = 1;
    808                     my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site, $silent);
    809 
    810                     if (!$collection_active) {
    811                     &print_msg(" Collection is not active.\n");
    812                     &print_msg("Activating collection $qualified_collection\n");
    813                     &activate_collection($library_url, $gs_mode, $qualified_collection, $site);
    814                    
    815                     # unless an error occurred, the collection should now be active:
    816                     $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site); # not silent if ping did not succeed
    817                     if(!$collection_active) {
    818                         &print_msg("ERROR: collection $qualified_collection did not get activated\n");
    819                     }
    820                     }
    821                     else {
    822                     &print_msg("Collection is already active => No need to activate\n");
    823                     }
    824                 }
    825                 else {
    826                     &print_msg("Server is not persistent => No need to activate collection\n");
    827                 }
     815            # unless an error occurred, the collection should now be active:
     816            $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site); # not silent if ping did not succeed
     817            if(!$collection_active) {
     818                &print_msg("ERROR: collection $qualified_collection did not get activated\n");
    828819            }
    829             else {
    830                 &print_msg("No response to Ping => Taken to mean server is not running\n");
    831             }
    832         }
    833     } else { # installcollection failed     
    834         #CollectionManager.Preview_Ready_Failed
    835         &print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
    836     }
    837 
     820            }
     821            else {
     822            &print_msg("Collection is already active => No need to activate\n");
     823            }
     824        }
     825        else {
     826            &print_msg("Server is not persistent => No need to activate collection\n");
     827        }
     828        }
     829        else {
     830        &print_msg("No response to Ping => Taken to mean server is not running\n");
     831        }
     832    }
     833    } else { # installcollection failed     
     834    #CollectionManager.Preview_Ready_Failed
     835    &print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
     836    }
     837   
    838838    &print_msg("\n");
    839 
     839   
    840840    if($buildtype eq "solr") {
    841841    if ($solr_server->explicitly_started()) {
Note: See TracChangeset for help on using the changeset viewer.