Changeset 15892


Ignore:
Timestamp:
2008-06-06T11:13:01+12:00 (16 years ago)
Author:
oranfry
Message:

change to search4j: using libraries and headers the proper way

Location:
release-kits/shared/search4j
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • release-kits/shared/search4j/compile.sh

    r15103 r15892  
    1 g++ search4j.cpp -o search4j
     1g++ -c libsearch4j.cpp
     2g++ -c search4j.cpp
     3g++ search4j.o libsearch4j.o -o search4j
     4
  • release-kits/shared/search4j/libsearch4j.cpp

    r15690 r15892  
    33*/
    44
    5 #include <iostream>
    6 #include <fstream>
    7 #include <string>
    8 #include <sstream>
    9 using namespace std;
     5#include "libsearch4j.h"
     6
    107
    118#ifdef WINDOWS
     9
    1210#include <windows.h>
    1311//minimal launch4j stuff
     
    1917#define pclose _pclose
    2018#define strcmp _stricmp
     19#define ID_TIMER 1
     20
    2121#endif /* WINDOWS */
    2222
     
    3232}
    3333
    34 int process( string command, string &output ) {
     34
     35int process( string command, bool render ) {
     36
     37    #ifdef WINDOWS
     38
     39    STARTUPINFO si;
     40    PROCESS_INFORMATION pi;
     41    memset(&pi, 0, sizeof(pi));
     42    memset(&si, 0, sizeof(si));
     43    si.cb = sizeof(si);
     44
     45    DWORD dwExitCode = -1;
     46    char* cmd = (char*)command.c_str();
     47
     48    bool result;
     49    if ( render ) {
     50        result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
     51    } else {
     52        result = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
     53    }
     54
     55    if ( result ) {
     56        WaitForSingleObject(pi.hProcess, INFINITE);
     57        GetExitCodeProcess(pi.hProcess, &dwExitCode);
     58        CloseHandle(pi.hThread);
     59        CloseHandle(pi.hProcess);
     60    }
     61    return dwExitCode;
     62
     63    #else
     64
     65    return system( command.c_str() );
     66
     67    #endif
     68}
     69
     70
     71
     72int process_and_catch_output( string command, string &output ) {
     73   
     74    #ifdef WINDOWS
     75
     76    char cmd[1024] = "cmd.exe /c ";
     77    strcat(cmd, command.c_str());
     78    strcat(cmd, " > cmd_output.txt 2>&1");
     79    //cerr << "command: " << cmd << endl;
     80    int code = process( cmd, false );
     81    if ( code!= 0 )
     82        return code;
     83   
     84    string line;
     85    ifstream myfile("cmd_output.txt");
     86    if ( !myfile.is_open() ) {
     87        return -1;
     88    }
     89
     90    while ( !myfile.eof() ) {
     91        getline(myfile,line);
     92        output.append( line );
     93    }
     94
     95    myfile.close();
     96   
     97    _unlink( "cmd_output.txt" );
     98   
     99    return code;
     100
     101    #else
    35102   
    36103    FILE *pPipe;
    37104
    38     /* Run command so that it writes its output to a pipe. Open this
    39     * pipe with read text attribute so that we can read it
    40     * like a text file. */
    41     char cmd[1024]; strcpy(cmd, command.c_str());
    42    
    43     #ifdef WINDOWS
    44     if( (pPipe = popen( cmd, "rt" )) == NULL ) {
    45         //if ( verbose ) cout << "could not start process" << endl;
    46         pclose( pPipe );
    47         return -1;
    48     }
    49     #endif
    50 
    51     #ifndef WINDOWS
     105    char cmd[1024];
     106    strcpy(cmd, command.c_str());
     107    strcat(cmd, " 2>&1" );
     108
     109    //if ( verbose ) cout << "process(): running '" << command << "'" << endl;
    52110    if( (pPipe = popen( cmd, "r" )) == NULL ) {
    53111        //if ( verbose ) cout << "could not start process" << endl;
     
    55113        return -1;
    56114    }
    57     #endif
     115
     116    //if ( verbose ) cout << "started process" << endl;
    58117
    59118    /* Read pipe until end of file. */
     
    74133    /* Close pipe and return return value of pPipe. */
    75134    int code = pclose( pPipe );
     135
    76136    return code;
    77 }
    78 
    79 class Jvm {
    80 
    81     string javaHome;
    82    
    83     // version string. era.major.minor_update. eg: 1.4.2_02
    84     int era;
    85     int major;
    86     int minor;
    87     int update;
    88 
    89     bool isJdk;
    90     bool healthy;   
    91 
    92 
    93     public:
    94     Jvm() {
     137
     138    #endif
     139
     140}
     141
     142//Jvm class methods
     143
     144Jvm::Jvm() {
     145    healthy = false;
     146}
     147
     148void Jvm::setJavaHome( string jh ) {
     149    healthy = true;
     150    javaHome = jh;
     151    setVersionFromExeOuput();
     152}
     153
     154string Jvm::getJavaHome() {
     155    return javaHome;
     156}
     157
     158string Jvm::getExecutable() {
     159    string exec = "";
     160    exec.append( javaHome );
     161   
     162    #ifdef WINDOWS
     163    exec.append( "\\bin\\java.exe" );
     164    #endif
     165
     166    #ifndef WINDOWS
     167    exec.append( "/bin/java" );
     168    #endif
     169   
     170    return exec;
     171}
     172
     173#ifdef WINDOWS
     174string Jvm::getWinExecutable() {
     175    string exec = "";
     176    exec.append( javaHome );
     177    exec.append( "\\bin\\javaw.exe" );
     178    return exec;
     179}
     180#endif
     181
     182
     183string Jvm::getVersion() {
     184    stringstream ss;
     185    ss << era << "." << major << "." << minor << "_" << update;
     186    return ss.str();
     187}
     188
     189bool Jvm::check() {
     190    return healthy;
     191}
     192
     193bool Jvm::setVersionFromString( string version ) {
     194    era = atoi( version.substr(0,1).c_str() );
     195    major = atoi( version.substr(2,1).c_str() );
     196    minor = atoi( version.substr(4,1).c_str() );
     197    update = atoi( version.substr(6,2).c_str() );
     198    return true;
     199}
     200
     201int Jvm::compare( Jvm otherJvm ) {
     202    //era
     203    if ( era > otherJvm.getEra() )
     204        return 1;
     205    else if ( era < otherJvm.getEra() )
     206        return -1;
     207   
     208    //major
     209    if ( major > otherJvm.getMajor() )
     210        return 1;
     211    else if ( major < otherJvm.getMajor() )
     212        return -1;
     213       
     214    //minor
     215    if ( minor > otherJvm.getMinor() )
     216        return 1;
     217    else if ( minor < otherJvm.getMinor() )
     218        return -1;
     219
     220    //update
     221    if ( update > otherJvm.getUpdate() )
     222        return 1;
     223    else if ( update < otherJvm.getUpdate() )
     224        return -1;
     225
     226    //all the same so far, must be exactly the same
     227    return 0;
     228
     229}
     230
     231int Jvm::getEra() { return era; }
     232int Jvm::getMajor() { return major; }
     233int Jvm::getMinor() { return minor; }
     234int Jvm::getUpdate() { return update; }
     235
     236void Jvm::setVersionFromExeOuput() {
     237    string command = "", output = "";
     238    command.append( "\"" );
     239    command.append( getExecutable() );
     240    command.append( "\"" );
     241    command.append(" -version");
     242
     243    //cerr << "command: " << command << endl;
     244    int result = process_and_catch_output( command, output );
     245    //cerr << "output: " << output << endl;
     246   
     247    if ( result == 0 ) {
     248        if ( strcmp( output.substr( 0, 12 ).c_str() , "java version" ) == 0 || true ) {
     249            era = atoi( output.substr(14,1).c_str() );
     250            major = atoi( output.substr(16,1).c_str() );
     251            minor = atoi( output.substr(18,1).c_str() );
     252            update = atoi( output.substr(20,2).c_str() );
     253        } else {
     254            healthy=false;
     255        }
     256    } else {
    95257        healthy = false;
    96258    }
    97 
    98     void setJavaHome( string jh ) {
    99         healthy = true;
    100         javaHome = jh;
    101         setVersionFromExeOuput();
    102     }
    103 
    104     string getJavaHome() {
    105         return javaHome;
    106     }
    107 
    108     string getExecutable() {
    109         string exec = "";
    110         exec.append( javaHome );
    111        
    112         #ifdef WINDOWS
    113         exec.append( "\\bin\\java.exe" );
    114         #endif
    115 
    116         #ifndef WINDOWS
    117         exec.append( "/bin/java" );
    118         #endif
    119        
    120         return exec;
    121     }
    122 
    123     #ifdef WINDOWS
    124     string getWinExecutable() {
    125         string exec = "";
    126         exec.append( javaHome );
    127         exec.append( "\\bin\\javaw.exe" );
    128         return exec;
    129     }
    130     #endif
    131 
    132    
    133     string getVersion() {
    134         stringstream ss;
    135         ss << era << "." << major << "." << minor << "_" << update;
    136         return ss.str();
    137     }
    138    
    139     bool check() {
    140         return healthy;
    141     }
    142 
    143     bool setVersionFromString( string version ) {
    144         era = atoi( version.substr(0,1).c_str() );
    145         major = atoi( version.substr(2,1).c_str() );
    146         minor = atoi( version.substr(4,1).c_str() );
    147         update = atoi( version.substr(6,2).c_str() );
    148         return true;
    149     }
    150    
    151     int compare( Jvm otherJvm ) {
    152         //era
    153         if ( era > otherJvm.getEra() )
    154             return 1;
    155         else if ( era < otherJvm.getEra() )
    156             return -1;
    157        
    158         //major
    159         if ( major > otherJvm.getMajor() )
    160             return 1;
    161         else if ( major < otherJvm.getMajor() )
    162             return -1;
    163            
    164         //minor
    165         if ( minor > otherJvm.getMinor() )
    166             return 1;
    167         else if ( minor < otherJvm.getMinor() )
    168             return -1;
    169 
    170         //update
    171         if ( update > otherJvm.getUpdate() )
    172             return 1;
    173         else if ( update < otherJvm.getUpdate() )
    174             return -1;
    175 
    176         //all the same so far, must be exactly the same
    177         return 0;
    178 
    179     }
    180    
    181     int getEra() { return era; }
    182     int getMajor() { return major; }
    183     int getMinor() { return minor; }
    184     int getUpdate() { return update; }
    185    
    186     private:
    187    
    188     void setVersionFromExeOuput() {
    189         string command = "", output = "";
    190         command.append( "\"" );
    191         command.append( getExecutable() );
    192         command.append( "\"" );
    193         command.append(" -version 2>&1");
    194        
    195         int result = process( command, output );
    196        
    197         if ( result == 0 ) {
    198             if ( strcmp( output.substr( 0, 12 ).c_str() , "java version" ) == 0 || true ) {
    199                 era = atoi( output.substr(14,1).c_str() );
    200                 major = atoi( output.substr(16,1).c_str() );
    201                 minor = atoi( output.substr(18,1).c_str() );
    202                 update = atoi( output.substr(20,2).c_str() );
    203             } else {
    204                 healthy=false;
    205             }
    206         } else {
    207             healthy = false;
    208         }
    209     }
    210    
    211    
    212 };
     259}
     260   
     261//end of Jvm class methods
     262
    213263
    214264#ifdef WINDOWS
     
    248298   
    249299    if ( verbose ) cout << "Searching for a JVM" << endl;
     300   
    250301    char *javaHome = "";
    251302    bool jvmFound = false;
    252303   
    253304    if ( !jvmFound ) {
     305       
    254306        //try JAVA_HOME
    255307        if ( verbose ) cout << " - trying JAVA_HOME: ";
     
    272324   
    273325    if ( !jvmFound ) {
     326       
    274327        //try JRE_HOME
    275328        if ( verbose ) cout << " - trying JRE_HOME: ";
     
    293346    #ifdef WINDOWS
    294347    if ( !jvmFound ) {
     348
    295349        //try the registry - this code based on launch4j code
    296         char foundJavaVer[128] = {0};
     350        char foundJavaVer[8192] = {0};
    297351        int foundJava = NO_JAVA_FOUND;
    298352       
    299         if ( verbose ) cout << " - trying the registry: ";
     353        if ( verbose ) cout << " - trying the registry: "; cout.flush();
    300354        HKEY hKey;
    301355        const char jre[] = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
     
    322376                strcpy(keyBuffer, sdk);
    323377            }
     378           
    324379            strcat(keyBuffer, "\\");
    325380            strcat(keyBuffer, foundJavaVer);
     381           
    326382            if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(keyBuffer), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
    327383                unsigned char buffer[1024] = {0};
     
    334390                        strcat(path, "\\jre");
    335391                    }
     392                    if ( verbose ) cerr << "path: " << path << endl ;
    336393                    jvm.setJavaHome( path );
    337394                    if ( jvm.check() ) {
     
    353410
    354411    if ( !jvmFound ) {
     412       
    355413        //try the hint
    356414        if ( verbose ) cout << " - trying hint: ";
  • release-kits/shared/search4j/search4j.cpp

    r15690 r15892  
    144144            cout << foundJvm.getVersion() << endl;
    145145        } else if ( jvmProperty == Executable ) {
     146            #ifdef WINDOWS
    146147            if ( useJavaw ) {
    147148                cout << foundJvm.getWinExecutable() << endl;
     
    149150                cout << foundJvm.getExecutable() << endl;
    150151            }
     152            #else
     153            cout << foundJvm.getExecutable() << endl;
     154            #endif
    151155        } else {
    152156            return -1; //should never happen
     
    179183       
    180184        string cmd = "\"", output = "";
     185        #ifdef WINDOWS
    181186        if ( useJavaw ) {
    182187            cmd.append( foundJvm.getWinExecutable() );
     
    184189            cmd.append( foundJvm.getExecutable() );
    185190        }
     191        #else
     192        cmd.append( foundJvm.getExecutable() );
     193        #endif
    186194        cmd.append( "\" -jar " );
    187195        cmd.append( arg1 );
Note: See TracChangeset for help on using the changeset viewer.