greenstone.org greenstone wiki greenstone trac planet greenstone

Changeset 15373

Show
Ignore:
Timestamp:
2008-05-09 14:41:48 (5 months ago)
Author:
ak19
Message:

1. FedoraLogin? loops on exception; 2. When using FLI and GS server is not remote, gsdl.xml file is written into Fedora server end telling it the location of the Greenstone collect directory containing the FedoraMETS to be ingested

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gli/trunk/src/org/greenstone/gatherer/Gatherer.java

    r15083 r15373  
    8181                * change the regex in the gs3-release-maker code and in this message 
    8282                */ 
     83 
    8384    static final public String PROGRAM_VERSION = "svn-trunk"; 
    8485 
     
    853854            } 
    854855        } 
     856         
     857        boolean showLogin = true; 
     858  
     859        do { 
     860            if(!dialog.loginRequested()) { // user pressed cancel to exit the FedoraLogin dialog 
     861                System.exit(0); 
     862            } else { 
     863                showLogin = dialog.loginRequested(); 
     864                String hostname = dialog.getHostname(); 
     865                String port     = dialog.getPort(); 
     866                String username = dialog.getUsername(); 
     867                String password = dialog.getPassword(); 
     868                String protocol = dialog.getProtocol(); 
    855869             
    856         if (dialog.loginRequested()) { 
    857  
    858             String hostname = dialog.getHostname(); 
    859             String port     = dialog.getPort(); 
    860             String username = dialog.getUsername(); 
    861             String password = dialog.getPassword(); 
    862             String protocol = dialog.getProtocol(); 
    863  
    864             Configuration.fedora_info.setHostname(hostname); 
    865             Configuration.fedora_info.setPort(port); 
    866             Configuration.fedora_info.setUsername(username); 
    867             Configuration.fedora_info.setPassword(password); 
    868             Configuration.fedora_info.setProtocol(protocol); 
    869  
    870             String ping_url_str = protocol + "://" + hostname + ":" + port + "/fedora"; 
    871             String login_str = username + ":" + password; 
     870                Configuration.fedora_info.setHostname(hostname); 
     871                Configuration.fedora_info.setPort(port); 
     872                Configuration.fedora_info.setUsername(username); 
     873                Configuration.fedora_info.setPassword(password); 
     874                Configuration.fedora_info.setProtocol(protocol); 
    872875             
    873             String login_encoding = new sun.misc.BASE64Encoder().encode(login_str.getBytes()); 
    874  
    875  
    876             try { 
    877                 URL ping_url = new URL(ping_url_str); 
    878                 URLConnection uc = ping_url.openConnection(); 
    879                 uc.setRequestProperty  ("Authorization", "Basic " + login_encoding); 
    880                 // Attempt to access some content ... 
    881                 InputStream content = (InputStream)uc.getInputStream(); 
    882  
    883             } 
    884             catch (Exception exception) { 
    885                 System.err.println("****"); 
    886                 System.err.println("* Failed to connect to Fedora server."); 
    887                 System.err.println("* Server might not be running, or incorrect username and/or password."); 
    888                 System.err.println("***"); 
    889                 DebugStream.printStackTrace(exception); 
    890                 System.exit(1); 
    891             } 
    892         } 
    893         else { 
    894             System.exit(0); 
    895         } 
    896  
    897  
    898         dialog.dispose(); 
    899         dialog = null; 
    900  
    901  
     876                String ping_url_str = protocol + "://" + hostname + ":" + port + "/fedora"; 
     877                String login_str = username + ":" + password; 
     878                 
     879                String login_encoding = new sun.misc.BASE64Encoder().encode(login_str.getBytes()); 
     880                                 
     881                try { 
     882                    URL ping_url = new URL(ping_url_str); 
     883                    URLConnection uc = ping_url.openConnection(); 
     884                    uc.setRequestProperty  ("Authorization", "Basic " + login_encoding); 
     885                    // Attempt to access some content ... 
     886                    InputStream content = (InputStream)uc.getInputStream(); 
     887                     
     888                    // if no exception occurred in the above, we would have come here: 
     889                    showLogin = false; 
     890                    dialog.dispose(); 
     891                } 
     892                catch (Exception exception) { 
     893                    String[] errorMessage = {"Failed to connect to the Fedora server.", "It might not be running, or", 
     894                                             "incorrect username and/or password."}; 
     895                    dialog.setErrorMessage(errorMessage); 
     896                    DebugStream.printStackTrace(exception); 
     897                    // exception occurred, show the dialog again (do this after printing to 
     898                    // debugStream, else the above does not get done for some reason). 
     899                    dialog.setVisible(true); 
     900                } 
     901            }  
     902        } while(showLogin); 
     903 
     904        dialog = null; // no more need of the dialog 
     905         
     906        // Now we are connected. Need to tell fedora where to find the FedoraMETS 
     907        // files for ingestion 
     908 
     909        //Fedora needs an xml file:  
     910        // FEDORA_HOME/tomcat/conf/Catalina/<fedora-hostname OR localhost>/gsdl.xml  
     911        // (which is to be wherever fedora.xml is situated) in order to know where 
     912        // to get the collection contents from.  
     913        writeGsdlXmlFile(); 
     914 
     915    } 
     916 
     917    /** Fedora needs an xml file:  
     918     *  FEDORA_HOME/tomcat/conf/Catalina/[fedora-hostname OR localhost]/gsdl.xml  
     919     * (host folder is the one wherever fedora.xml is found) in order to know where 
     920     * to get the collection contents from. 
     921     * This method generates the necessary gsdl.xml file which points Fedora to the 
     922     * parent of the collect folder. 
     923    */ 
     924    private static void writeGsdlXmlFile() { 
     925        // System.getenv(FEDORA_HOME) is deprecated in Java v4 (but is back in v5).  
     926        // But fedora_info.getHome() will return FEDORA_HOME.  
     927 
     928        // To do the following, we actually need to stop and restart the Fedora server.  
     929        // Why should FLI have to do all this, or even know that it needs doing? 
     930        String execExtension = Utility.isWindows() ? "bat" : "sh"; 
     931        String fedoraProcess = Configuration.fedora_info.getHome()+"tomcat" 
     932            +File.separator+"bin"+File.separator; 
     933        String fedoraStop = fedoraProcess+"shutdown."+execExtension; 
     934        String fedoraStart = fedoraProcess+"startup."+execExtension; 
     935        String errorMessage = ""; 
     936         
     937        try{ 
     938            // (1) stop the fedora server 
     939            errorMessage = "Failed to stop the fedora server."; 
     940 
     941            Process process = Runtime.getRuntime().exec(fedoraStop); 
     942            System.err.println("*****Stopping the fedora server: " + fedoraStop); 
     943            int exitValue = -1; 
     944            if((exitValue = process.waitFor()) != 0) { 
     945                throw new Exception(errorMessage); 
     946            } 
     947            System.out.println("Exit value is: " + exitValue); 
     948                 
     949            // (2) See if the folder for the host contains fedora.xml, if it doesn't,  
     950            // then don't write gsdl.xml in there, try folder "localhost" instead 
     951            String pathPrefix = Configuration.fedora_info.getHome()+"tomcat"+File.separator+"conf" 
     952                +File.separator+"Catalina"; 
     953            String gsdlHostFolderPath = pathPrefix+File.separator+Configuration.fedora_info.getHostname(); 
     954            File fedoraXML = new File(gsdlHostFolderPath,"fedora.xml"); 
     955            if(!fedoraXML.exists()) {  
     956                // prepare the errorMessage just in case we need it 
     957                errorMessage = "Directory " + gsdlHostFolderPath + " is the wrong place to put" 
     958                        + " the crucial gsdl.xml file as it doesn't contain the example file fedora.xml\n"; 
     959                     
     960                // no default fedora.xml file in the folder we want to write gsdl.xml to 
     961                // so we may be in the wrong place 
     962                if(Configuration.fedora_info.getHostname().equals("localhost")) {  
     963                    // if we just tried localhost and then failed 
     964                    // we can't continue 
     965                    fedoraXML = null; 
     966                    throw new Exception(errorMessage);               
     967                } else {  
     968                    System.err.println("*** Folder " + gsdlHostFolderPath  
     969                        +"\nis the wrong location for gsdl.xml. Trying folder localhost."); 
     970                    // we just tried a folder of a different hostname,  
     971                    // let's try "localhost" as a last ditch effort 
     972                    gsdlHostFolderPath = pathPrefix+File.separator+"localhost"; 
     973                    fedoraXML = new File(gsdlHostFolderPath,"fedora.xml"); 
     974                    if(!fedoraXML.exists()){ 
     975                        fedoraXML = null; 
     976                        throw new Exception(errorMessage); 
     977                    } // else we found the location for gsdl.xml 
     978                } 
     979            } 
     980 
     981            // If we made it here, we found the correct folder to write gsdl.xml to 
     982            errorMessage = ""; 
     983            fedoraXML = null; 
     984            File gsdlXMLPath = new File(gsdlHostFolderPath); 
     985            if(!gsdlXMLPath.exists()) { 
     986                // make sure all the intermediate directories exist  
     987                // (should be the case when fedoraXML exists in it) 
     988                gsdlXMLPath.mkdirs(); 
     989            } 
     990            String gsdlXMLFile = gsdlXMLPath+File.separator+"gsdl.xml"; 
     991             
     992            // (3) We write xml out containing the location of the parent folder of the collect dir 
     993            String collectParent = Configuration.gsdl_path; 
     994            if(Gatherer.GS3){ // Collect dir's parent folder is web/sites/localsite, which is GSDL3HOME/sites/localsite 
     995                collectParent = Configuration.gsdl3_path+"sites"+File.separator+"localsite"; 
     996            } 
     997            System.err.println("****CollectParent folder: " + collectParent + "\n****gsdlXMLFile: " + gsdlXMLFile); 
     998             
     999            // The contents we are going to write out to the file: <Context docBase="collectParent" path="/gsdl"></Context> 
     1000            // Does Fedora expect a / in the path to gsdl, does Fedora deal with "/gsdl" or do I need to use a File.separator? 
     1001            String contents = "<?xml version='1.0' encoding='utf-8'?>\n<Context docBase=\"" 
     1002                +collectParent+"\" path=\""+File.separator+"gsdl\"></Context>";  
     1003             
     1004            // even if the file exists, we overwrite it, because we may have changed GS versions  
     1005            // and collect dir would have changed though there'd still be a gsdl.xml file 
     1006            errorMessage =  "Failed to create the essential file: " + gsdlXMLFile; 
     1007            java.io.OutputStream gsdlXMLFileOut = new java.io.FileOutputStream(gsdlXMLFile); 
     1008            gsdlXMLFileOut.write(contents.getBytes()); 
     1009            gsdlXMLFileOut.close(); 
     1010            gsdlXMLFileOut = null; 
     1011             
     1012            // Otherwise, restart the fedora server again 
     1013            errorMessage = "Failed to stop the fedora server."; 
     1014            process = Runtime.getRuntime().exec(fedoraStart); 
     1015            System.err.println("*****Restarting the fedora server: " + fedoraStart); 
     1016            if((exitValue = process.waitFor()) != 0) { 
     1017                throw new Exception(errorMessage); 
     1018            } 
     1019            System.out.println("Exit value is: " + exitValue); 
     1020        }catch(Exception e){ 
     1021            System.err.println("****"); 
     1022            System.err.println("* " + errorMessage + "\n" + e.getMessage()); 
     1023            System.err.println("***"); 
     1024            e.printStackTrace(); 
     1025            DebugStream.printStackTrace(e); 
     1026            // We can't continue, so we exit 
     1027            System.exit(1);              
     1028        }            
    9021029    } 
    9031030