Changeset 23143


Ignore:
Timestamp:
2010-10-15T20:33:43+13:00 (14 years ago)
Author:
ak19
Message:

More changes to making collectdir movable: 1. When not working with a remote GS or server.exe: if the collecthome set in GLI is not the same as the one in gsdlsite.cfg used by the apache web server, then a dialog pops up on exiting GLI allowing the user to set the collecthome value for both GLI (in the user's GLI config.xml) and the server (in gsdlsite.cfg). 2. Collecthome line in gsdlsite.cfg is removed if it is the default GS collect folder. (Just as a recent commit stores an empty string for the open_collection element in the GLI config.xml file if no collection is left open on exiting GLI and the collect folder is the default GS collect directory.) 3. Some bug fixes.

Location:
main/trunk/gli
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/classes/dictionary.properties

    r22970 r23143  
    710710GUI.Gather:Gather
    711711GUI.Gather_Tooltip:Select the files to include in your collection
     712GUI.CollectHome.title:The collect directory has changed
     713GUI.CollectHome.message:The collect directory value has been changed from:
     714GUI.CollectHome.to:to:
     715GUI.CollectHome.dir:{0}
     716GUI.CollectHome.gli:Do you want the Librarian Interface to remember the new collect directory?
     717GUI.CollectHome.server:Do you want the Greenstone Server to remember the new collect directory?
     718GUI.CollectHome.resetToDefault:No, reset to default
     719GUI.CollectHome.leaveAtDefault:No, leave at default
     720GUI.CollectHome.reset:Reset to default
     721
    712722#***************
    713723#
  • main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r23031 r23143  
    102102    static private String non_standard_collect_directory_path = null;
    103103    static public String open_collection_file_path = null;
     104    static public String gsdlsite_collecthome = "";
    104105    /** A public reference to the FileAssociationManager. */
    105106    static public FileAssociationManager assoc_man;
     
    315316        if (fedora_info.isActive()) {
    316317        popupFedoraInfo();
    317         }   
    318 
     318        }
    319319       
     320        // the no_load flag to GLI is processed at the end of handling open_collection_file_path
    320321        open_collection_file_path = open_collection;
    321322        if (open_collection_file_path == null) {
     
    323324                "general.open_collection"+Configuration.gliPropertyNameSuffix(), true);
    324325        }
     326       
     327        initCollectDirectoryPath(open_collection);
     328       
    325329        if (no_load || (isGsdlRemote && open_collection_file_path.equals(""))) {
    326330        open_collection_file_path = null;
    327         //      collectDirectoryHasChanged(); // make sure gsdlsite.cfg's collecthome defaults to GS' collect
    328         }
    329        
    330         // If opening a collection outside the standard GS collect folder, need
    331         // to open the non-standard collect folder that the collection resides in
    332         if (!isGsdlRemote
    333             && open_collection_file_path != null
    334             && !open_collection_file_path.startsWith(getDefaultGSCollectDirectoryPath(true)))
    335         {
    336             File collectFolder = null;
    337            
    338             if(!open_collection_file_path.equals("")) {
    339            
    340                 if(!open_collection_file_path.endsWith("gli.col")) { // then it's a collect folder
    341                     collectFolder = new File(open_collection_file_path);
    342                 } else {
    343                     // the filepath is a gli.col file. To get the collect folder: the 1st level
    344                     // up is collection folder, 2 two levels up is the containing collect folder
    345                     collectFolder = new File(open_collection_file_path).getParentFile().getParentFile();
    346                 }   
    347            
    348                 // Need to deal with colgroups as well: while there's an etc/collect.cfg in the current
    349                 // collectFolder, move one level up
    350                 String cfg_file = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
    351                 if(new File(collectFolder.getAbsolutePath()+File.separator+cfg_file).exists()) { // colgroup
    352                     collectFolder = collectFolder.getParentFile();         
    353                 }
    354             }
    355            
    356             if(collectFolder == null || !collectFolder.exists()) {
    357                 // if GLI config file specified no collectDir  (open_collection_file_path is "")
    358                 // OR if dealing with a local server but the collectdir no longer exists,
    359                 // use the default greenstone collect directory, and write that to affected files
    360                
    361                 String defaultColDir = getDefaultGSCollectDirectoryPath(false); // no file separator at end
    362                 open_collection_file_path = defaultColDir;
    363                
    364                 setCollectDirectoryPath(defaultColDir); // will ensure the required file separator at end
    365                
    366                 Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(),
    367                         true, "");
    368                
    369                 if(Gatherer.GS3) {  // GS2's gsdlsite.cfg was taken care of when LocalLibraryServer was started up
    370                                     // (LocalLibraryServer would also already have set glisite.cfg to the correct collecthome)
    371                     String gsdlsitecfg = Configuration.gsdl3_path + File.separator
    372                         + "WEB-INF" + File.separator + "cgi" + File.separator + "gsdl3site.cfg";
    373                     Utility.updatePropertyConfigFile(gsdlsitecfg, "collecthome", defaultColDir); // no file separator
    374                             // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
    375                 }
    376             } else { // remote GS, or collect dir exists (all config files will already be set since last GLI session)
    377                 if(collectFolder == null) {
    378                     setCollectDirectoryPath(getDefaultGSCollectDirectoryPath(false)); // no file separator at end
    379                 } else {
    380                     setCollectDirectoryPath(collectFolder.getAbsolutePath()); // now set this as the current collect folder
    381                 }                   
    382             }           
    383331        }
    384332       
     
    711659    // Flush debug
    712660    DebugStream.closeDebugStream();
    713 
     661   
    714662    // If we started a server, we should try to stop it.
    715     if (LocalLibraryServer.isRunning() == true) {
     663    if (LocalLibraryServer.isRunning() == true) {       
    716664        LocalLibraryServer.stop();
    717665    }
     
    751699   
    752700    }
     701   
     702    // if we need to know whether the local server we are running is server.exe vs apache web server
     703    static public boolean isPersistentServer() {
     704        return (!isGsdlRemote && LocalLibraryServer.isPersistentServer());
     705    }
    753706
    754707    /** Returns the path of the Greenstone "collect" directory. */
     
    835788    }
    836789
     790
     791    public static void initCollectDirectoryPath(String open_collection) {       
     792        String defaultColdir = getDefaultGSCollectDirectoryPath(false); // no file separator at end
     793        String coldir = defaultColdir;
     794        // If local GS and opening a collection outside the standard GS collect folder,
     795        // need to open the non-standard collect folder that the collection resides in
     796        if (!isGsdlRemote
     797                && !open_collection_file_path.startsWith(defaultColdir))
     798        {
     799            File collectFolder = null;
     800           
     801            if(!open_collection_file_path.equals("")) {         
     802                if(!open_collection_file_path.endsWith("gli.col")) { // then it's a collect folder
     803                    collectFolder = new File(open_collection_file_path);
     804                } else {
     805                    // the filepath is a gli.col file. To get the collect folder: the 1st level up
     806                    // is the collection folder, 2 two levels up is the containing collect folder
     807                    collectFolder = new File(open_collection_file_path).getParentFile().getParentFile();
     808                }   
     809           
     810                // Need to deal with colgroups as well: while there's an etc/collect.cfg
     811                // in the current collectFolder, move one level up
     812                String cfg_file = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
     813                if(new File(collectFolder.getAbsolutePath()+File.separator+cfg_file).exists()) { // colgroup
     814                    collectFolder = collectFolder.getParentFile();         
     815                }
     816            }       
     817           
     818            if(collectFolder == null || !collectFolder.exists()) {
     819                // if GLI config file specified no collectDir (open_collection_file_path is "")
     820                // OR if dealing with a local server but the collectdir no longer exists,
     821                // use the default greenstone collect directory, and write that to affected files
     822               
     823                open_collection_file_path = defaultColdir;      // default GS collect dir
     824                // Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(), true, "");
     825            } else { // use the coldir value specified in the flags to GLI or from the last GLI session             
     826                coldir = collectFolder.getAbsolutePath();
     827            }           
     828            // set it as the current folder
     829            setCollectDirectoryPath(coldir); // will ensure the required file separator at end
     830        }
     831       
     832        if(!isGsdlRemote) {
     833            // LocalLibraryServer would  already have set glisite.cfg to the correct collecthome for server.exe
     834            // Here we set collecthome in gsdl(3)site.cfg for the GS2 apache web server and GS3 tomcat server
     835            String gsdlsitecfg = getGsdlSiteConfigFile();           
     836            // update the gsdlsite config file and store the old value for use when we exit GLI
     837            if(coldir.equals(defaultColdir)) {             
     838                gsdlsite_collecthome = Utility.updatePropertyConfigFile(
     839                    gsdlsitecfg, "collecthome", null);                     
     840            } else {
     841                gsdlsite_collecthome = Utility.updatePropertyConfigFile(
     842                    gsdlsitecfg, "collecthome", coldir); // no file separator
     843                    // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
     844            }
     845        }       
     846    }
     847   
     848    /** depending on the version of GS being run, return the path to the current GS' installation's gsdl(3)site.cfg */
     849    public static String getGsdlSiteConfigFile() {
     850        if(Gatherer.GS3) { // web/WEB-INF/cgi/gsdl3site.cfg
     851            return Configuration.gsdl3_path + File.separator + "WEB-INF"
     852                + File.separator + "cgi" + File.separator + "gsdl3site.cfg";
     853        } else { // cgi-bin/gsdlsite.cfg
     854            return Configuration.gsdl_path + File.separator
     855                + "cgi-bin" + File.separator + "gsdlsite.cfg";
     856        }
     857    }
    837858
    838859    public static void collectDirectoryHasChanged(
     
    885906                return;
    886907            }   
    887 
    888             String gsdlsitecfg = "";
    889             if(Gatherer.GS3) { // web/WEB-INF/cgi/gsdl3site.cfg
    890                 gsdlsitecfg = Configuration.gsdl3_path + File.separator + "WEB-INF"
    891                     + File.separator + "cgi" + File.separator + "gsdl3site.cfg";
    892             } else { // cgi-bin/gsdlsite.cfg
    893                 gsdlsitecfg = Configuration.gsdl_path + File.separator
    894                     + "cgi-bin" + File.separator + "gsdlsite.cfg";
    895             }
    896 
     908           
    897909            // non-destructive update of gsdl(3)site.cfg (comments preserved)
    898910            String collectDir = Gatherer.getCollectDirectoryPath();
    899911            //collectDir = "\"" + collectDir.substring(0, collectDir.length()-1) + "\""; // remove file separator at end   
    900912            collectDir = collectDir.substring(0, collectDir.length()-1); // remove file separator at end   
    901             Utility.updatePropertyConfigFile(gsdlsitecfg, "collecthome", collectDir);
     913            Utility.updatePropertyConfigFile(getGsdlSiteConfigFile(), "collecthome", collectDir);
    902914                    // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
    903915           
  • main/trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java

    r23015 r23143  
    15581558    public void openCollectionFromLastTime() {
    15591559      // If there was an open collection last session, reopen it.
    1560       // Skip if there was none or only a collect folder instead of any previously-opened collection     
    1561       if (Gatherer.open_collection_file_path != null
    1562             && !(new File(Gatherer.open_collection_file_path).isDirectory())) {
    1563          // Load the collection now
    1564          loadCollection(Gatherer.open_collection_file_path);
    1565       }
     1560      // (this method doesn't get called if there was no open collection or only a collect folder
     1561      // instead of any previously-opened collection)
     1562     
     1563        // Load the collection now
     1564        loadCollection(Gatherer.open_collection_file_path);     
    15661565    }
    15671566
  • main/trunk/gli/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java

    r23015 r23143  
    7070    static private boolean isPersistentServer = false;
    7171
     72    static public boolean isPersistentServer() {
     73        return isPersistentServer;
     74    }
     75   
    7276    static public void addCollection(String collection_name)
    7377    {
     
    395399    if (isPersistentServer) {
    396400        config(QUIT_COMMAND);
    397     } else {
     401    } else {       
    398402        boolean success = sendMessageToServer("QUIT\n");
    399403        try {
     
    697701        // Set the correct collectdir (from last time) before the server is started up
    698702       
    699         //String collectDir = Configuration.getString("general.open_collection_gs2", true);
    700703        String orig_collection_path = Configuration.getString("general.open_collection_gs2", true);
    701         String collectDir = Gatherer.getCollectDirectoryPath();
     704        String collectDir = Gatherer.getCollectDirectoryPath();                 // Gatherer would've set this up
    702705        String defaultColDir = Gatherer.getDefaultGSCollectDirectoryPath(true); // with file separator at end
    703        
    704         // If opening a collection outside the standard GS collect folder, need
    705         // to open the non-standard collect folder that the collection resides in.
    706         // Also, would have been forced to change the loaded collection--and hence collectDir--
    707         // to the default coldir if orig_col_path was "" or led to a collectdir that didn't exist
    708         // (in the last case, gsdlsite.cfg already corrected in Gatherer if coldir was changed).   
    709        
     706               
    710707        if(orig_collection_path.equals("")) { // default GS collect dir path
    711708            return;
    712709        }
    713710       
    714         if (Gatherer.open_collection_file_path != null // may be null if no_load was specified on GLI startup           
    715             && (!orig_collection_path.startsWith(collectDir) // if coldir would've been changed on startup OR if coldir is non-standard collect folder
    716                 || !collectDir.equals(defaultColDir)))
     711        if (!orig_collection_path.startsWith(collectDir) // if coldir would've been changed on startup OR if coldir is non-standard collect folder
     712                || !collectDir.equals(defaultColDir))
    717713        {           
    718714            // if we're running *server.exe* and if the current collect dir at Local Lib's startup is
     
    720716            // dir because the non-standard collectDir didn't exist/wasn't specified in the GLI config
    721717            // file, then write out the new collectDir (minus file separator at end) to the lls site conf in use
     718            // Regardless of what collecthome value was in the llssite/glisite file, we end up resetting it here
    722719            if (isPersistentServer) { // server.exe, so we're dealing with a local GS2         
    723720                put(COLLECTHOME, collectDir);
  • main/trunk/gli/src/org/greenstone/gatherer/gui/GUIManager.java

    r23031 r23143  
    104104    private TabUpdater tab_updater = null;
    105105
    106 
    107 
    108 
    109 
    110106  final static String newline = "\n";
    111107    final static String space = "    ";
     
    502498    public void exit(int exit_status)
    503499    {
     500    boolean collection_open = false;
    504501    // If we have a collection open remember it for next time, then save it and close it
    505502    if (Gatherer.c_man.ready()) {
     
    515512        }
    516513        saveThenCloseCurrentCollection();
     514        collection_open = true;
    517515    }
    518516    else {
     
    528526        }
    529527    }
     528   
     529   
     530    // Basically, if GLI coldir != gsdlsite_colhome, we ask them if they want to save GLI coldir as gsdlsite_colhome
     531    // If gsdlsite_colhome = "" (meaning default GS coldir) and GLI is also set to defaultGScoldir, no saving necessary.
     532    // We ONLY do this for the local included apache web server, since the issue revolves around gsdlsite.cfg containing
     533    // the global collecthome when using the apache web server. This is not an issue for remote GS or server.exe (the latter
     534    // has separate config files specifying collecthome for when GLI is running whereas when just the server.exe is running).
     535    if(!Gatherer.isGsdlRemote && !Gatherer.isPersistentServer()) {     
     536   
     537        String defaultColDir = Gatherer.getDefaultGSCollectDirectoryPath(false); // no filesep at end
     538        String gliColDir = Gatherer.getCollectDirectoryPath();
     539        String serverColDir = Gatherer.gsdlsite_collecthome;
     540       
     541        boolean showSettingsDlg = true;
     542        // if collectdir was changed during GLI session (to something different from what's in gsdlsite), need to ask what to store
     543        if(gliColDir.equals(Gatherer.gsdlsite_collecthome+File.separator)) {   
     544            showSettingsDlg = false;
     545        } else if(Gatherer.gsdlsite_collecthome.equals("") // both set to default coldir
     546                && gliColDir.equals(defaultColDir+File.separator)) {
     547            showSettingsDlg = false;
     548        }
     549        if(showSettingsDlg) {
     550           
     551            // else we will be showing the Collect Directory Settings Dialog
     552            if(gliColDir.endsWith(File.separator)) {
     553                gliColDir = gliColDir.substring(0, gliColDir.length()-1);
     554            }
     555           
     556            if(serverColDir.equals("")) {
     557                serverColDir = defaultColDir;
     558            }   
     559            collectDirSettingsDialog(defaultColDir, serverColDir, gliColDir, collection_open);
     560        }
     561    }
    530562
    531563    // Store the current position and size of the GLI for next time
     
    544576    }
    545577
     578    public void collectDirSettingsDialog(final String defaultColDir,
     579        final String from, final String to, final boolean collection_open)
     580    {   
     581        final JDialog colHomeDialog
     582            = new JDialog(this, Dictionary.get("GUI.CollectHome.title"), true); // this = Gatherer.g_man
     583        colHomeDialog.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
     584       
     585       
     586        JRadioButton gliYes = new JRadioButton(Dictionary.get("General.Yes"), true);
     587                        // the default selection for GLI, collecthome=to
     588        JRadioButton serverYes = new JRadioButton(Dictionary.get("General.Yes")); // not selected for the server       
     589        JRadioButton gliNo = null;
     590        JRadioButton gliThirdOption = null;
     591        JRadioButton serverNo = null;
     592        JRadioButton serverThirdOption = null;
     593       
     594        if(from.equals(defaultColDir)) {
     595            gliNo = new JRadioButton(Dictionary.get("GUI.CollectHome.resetToDefault"));
     596            serverNo = new JRadioButton(Dictionary.get("GUI.CollectHome.leaveAtDefault"), true);
     597                        // default selection for server, collecthome=from
     598        } else {
     599            gliNo = new JRadioButton(Dictionary.get("General.No"));
     600            serverNo = new JRadioButton(Dictionary.get("General.No"), true); // default selection for server
     601            if(!to.equals(defaultColDir)) { // neither from nor to is the default GS collect dir, so give them that as the third option
     602                gliThirdOption = new JRadioButton(Dictionary.get("GUI.CollectHome.reset"));
     603                serverThirdOption = new JRadioButton(Dictionary.get("GUI.CollectHome.reset"));     
     604            }
     605        }
     606       
     607        JPanel gliPanel = new JPanel(); // flowlayout by default
     608        ButtonGroup gliGroup = new ButtonGroup();
     609        JPanel serverPanel = new JPanel(); // flowlayout by default
     610        ButtonGroup serverGroup = new ButtonGroup();
     611       
     612        gliGroup.add(gliYes);
     613        gliPanel.add(gliYes);
     614        serverGroup.add(serverYes);
     615        serverPanel.add(serverYes);     
     616       
     617        gliGroup.add(gliNo);
     618        gliPanel.add(gliNo);
     619        serverGroup.add(serverNo);
     620        serverPanel.add(serverNo);
     621           
     622        if(gliThirdOption != null) {
     623            gliThirdOption = new JRadioButton("Reset to default");
     624            serverThirdOption = new JRadioButton("Reset to default");
     625           
     626            gliGroup.add(gliThirdOption);
     627            gliPanel.add(gliThirdOption);
     628            serverGroup.add(serverThirdOption);
     629            serverPanel.add(serverThirdOption);
     630        }
     631       
     632        // vars need to be final to use them in the actionlistener below
     633        final JRadioButton gli_yes = gliYes;
     634        final JRadioButton gli_no = gliNo;
     635        final JRadioButton gli_optional = gliThirdOption;
     636        final JRadioButton server_yes = serverYes;
     637        final JRadioButton server_no = serverNo;
     638        final JRadioButton server_optional = serverThirdOption;
     639       
     640        JButton okButton = new JButton(Dictionary.get("General.OK"));
     641        okButton.addActionListener(new ActionListener() {
     642            public void actionPerformed(ActionEvent e) {
     643                // store the option chosen for GLI
     644                String gliColDir = to; 
     645                if(gli_optional != null && gli_optional.isSelected()) {
     646                    // defaultColDir
     647                    gliColDir = "";
     648                } else if(gli_yes.isSelected()) {
     649                    gliColDir = to;
     650                } else if (gli_no.isSelected()) {
     651                    gliColDir = from;
     652                }
     653                if(defaultColDir.equals(gliColDir)) {
     654                    gliColDir = "";
     655                }
     656                if(!(collection_open && gli_yes.isSelected())) { // don't overwrite open collections
     657                    Configuration.setString(
     658                        "general.open_collection"+Configuration.gliPropertyNameSuffix(),
     659                        true, gliColDir);
     660                }
     661               
     662                // store the option chosen for the server's settings
     663                String serverColDir = from;
     664                if(server_optional != null && server_optional.isSelected()) {
     665                    // defaultColDir
     666                    serverColDir = null;
     667                } else if(server_yes.isSelected()) {
     668                    serverColDir = to;
     669                } else if (server_no.isSelected()) {
     670                    serverColDir = from;
     671                }
     672                if(serverColDir != null && defaultColDir.equals(serverColDir)) {                   
     673                    serverColDir = null;
     674                }
     675                               
     676                Gatherer.gsdlsite_collecthome = Utility.updatePropertyConfigFile(
     677                    Gatherer.getGsdlSiteConfigFile(), "collecthome", serverColDir);
     678               
     679                colHomeDialog.dispose();
     680            }
     681        });
     682       
     683        //String[] dirs = {from, to};       
     684        //JLabel message = new JLabel(Dictionary.get("GUI.CollectHome.message", dirs));
     685        JLabel message = new JLabel(Dictionary.get("GUI.CollectHome.message"));
     686        JLabel fromDirLine = new JLabel(Dictionary.get("GUI.CollectHome.dir", from));
     687        fromDirLine.setBorder(BorderFactory.createEmptyBorder(0,25,0,0)); // padding
     688        JLabel toLine = new JLabel(Dictionary.get("GUI.CollectHome.to"));
     689        JLabel toDirLine = new JLabel(Dictionary.get("GUI.CollectHome.dir", to));
     690        toDirLine.setBorder(BorderFactory.createEmptyBorder(0,25,0,0)); // padding
     691        JLabel gliOption = new JLabel(Dictionary.get("GUI.CollectHome.gli"));
     692        JLabel serverOption = new JLabel(Dictionary.get("GUI.CollectHome.server"));     
     693       
     694        JPanel mainPanel = new JPanel();
     695        mainPanel.setLayout(new GridLayout(9, 1));
     696        mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); // padding
     697        mainPanel.add(message);
     698        mainPanel.add(fromDirLine);     
     699        mainPanel.add(toLine);
     700        mainPanel.add(toDirLine);
     701        mainPanel.add(gliOption);
     702        mainPanel.add(gliPanel);
     703        mainPanel.add(serverOption);
     704        mainPanel.add(serverPanel);
     705        mainPanel.add(okButton);       
     706        Container c = colHomeDialog.getContentPane();
     707        c.setLayout(new BorderLayout());
     708        c.add(mainPanel, BorderLayout.CENTER);
     709        colHomeDialog.getRootPane().setDefaultButton(okButton);
     710       
     711        colHomeDialog.pack();
     712        colHomeDialog.setVisible(true);
     713    }
     714   
    546715
    547716    /** This method is called when the collection is being built, and is used to disable all controls in all pane which could change the state of the collection.
  • main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r22911 r23143  
    461461
    462462   
    463     /** Write out a property line--a (property, value) pair--to the gsdl(3)site.cfg file.
     463  /** Write out a property line--a (property, value) pair--to the gsdl(3)site.cfg file.
    464464     * If the file already contains the line as-is, it is not re-written.   
    465465     * If the file doesn't contain the line, it is appended.
    466466     * If the file contained a different value for the property, the line is corrected
    467467     * and the file is written out.
     468     * If the propertyValue parameter is null, the property line is removed from the file.
    468469     * Not using the Properties class, as we want to keep the file's contents in the
    469470     * same order and preserve all the comments in as they're meant to help the user.
    470      */
    471     public static void updatePropertyConfigFile(String filename, String propertyName, String propertyValue) {
     471     * Return the old value for the property, if it existed, else "".   
     472     */
     473    public static String updatePropertyConfigFile(
     474        String filename, String propertyName, String propertyValue)
     475    {   
    472476    File propFile = new File(filename);
     477    String oldValue = "";
    473478    if(!propFile.exists()) {
    474479        System.err.println("*** Unable to update property " + propertyName + " in file "
    475480                + filename + "to\n" + propertyValue + ". File does not exist.\n");
    476         return;
     481        return oldValue;
    477482    }   
    478483    BufferedReader fin = null;
    479484    BufferedWriter fout = null;
    480485    StringBuffer contents = new StringBuffer();
    481     String insertLine = propertyName+"\t"+propertyValue+"\n"; // new line after every propertyLine
     486    String insertLine = null;
     487    if(propertyValue != null) {
     488        insertLine = propertyName+"\t"+propertyValue+"\n"; // new line after every propertyLine
     489    }
    482490    boolean found = false;
    483491    try {
     
    485493        String line = "";
    486494        while((line = fin.readLine()) != null) {
    487         if(line.startsWith(propertyName)) { // won't match comment
    488             found = true;
    489             if(line.equals(insertLine)) { // file is already correct, nothing to do
     495            line = line.trim(); // remove any preceding (surrounding) whitespace
     496            if(line.startsWith(propertyName)) { // won't match comment
     497                found = true;
     498                // store the previous value for the property
     499                oldValue = line;
     500                oldValue = oldValue.substring(propertyName.length());
     501                oldValue = oldValue.trim();
     502               
     503                if(propertyValue != null) { // line should be removed if propertyValue == null
     504                    if(line.equals(insertLine)) { // file is already correct, nothing to do
     505                        fin.close();
     506                        fin = null;
     507                        break;
     508                    } else {
     509                        contents.append(insertLine);
     510                    }
     511                }
     512            } else { // any other line
     513                contents.append(line);         
     514                contents.append("\n"); // ensures the required new line at end of file
     515            }
     516        }       
     517
     518        if(fin != null) { // need to write something out to the file
    490519            fin.close();
    491520            fin = null;
    492             break;
    493             } else {
    494             contents.append(insertLine);
    495             }
    496 
    497         } else { // any other line
    498             contents.append(line);         
    499             contents.append("\n"); // ensures the required new line at end of file
    500         }
    501         }
    502 
    503         if(fin != null) { // need to write something out to the file
    504         fin.close();
    505         fin = null;
    506 
    507         // if collecthome wasn't already specified in the file, append it
    508         if(!found) {
    509             fout = new BufferedWriter(new FileWriter(filename, true)); // append mode
    510             fout.write(insertLine, 0, insertLine.length());
    511         } else {
    512            
    513             fout = new BufferedWriter(new FileWriter(filename)); // hopefully this will overwrite
    514             fout.write(contents.toString(), 0, contents.length());
    515         }
    516 
    517         fout.close();
    518         fout = null;
     521
     522            // if collecthome/property wasn't already specified in the file, append it
     523            // but only if we have a value to write out to the file
     524            if(!found && propertyValue != null) {       
     525                fout = new BufferedWriter(new FileWriter(filename, true)); // append mode
     526                fout.write(insertLine, 0, insertLine.length());
     527            } else {           
     528                fout = new BufferedWriter(new FileWriter(filename)); // hopefully this will overwrite
     529                fout.write(contents.toString(), 0, contents.length());
     530            }
     531
     532            fout.close();
     533            fout = null;
     534           
    519535        } // else the file is fine
    520536    } catch(IOException e) {
     
    538554       
    539555    }
    540    
    541     }
    542    
     556        return oldValue;
     557    }
    543558}
Note: See TracChangeset for help on using the changeset viewer.