Changeset 16132 for gli

Show
Ignore:
Timestamp:
24.06.2008 18:04:07 (12 years ago)
Author:
osborn
Message:

Additions for Scheduling Component

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • gli/trunk/src/org/greenstone/gatherer/collection/CollectionManager.java

    r15362 r16132  
    7676import org.w3c.dom.*; 
    7777 
    78 /** This class manages many aspects of the collection, from its creation via scripts, data access via methods and its importing and building into the final collection. It is also resposible for firing appropriate event when significant changes have occured within the collection, and for creating a new metadata set manager as necessary. 
     78/** This class manages many aspects of the collection, from its creation via scripts, data access via methods and its importing and building into the final collection. It is also responsible for firing appropriate event when significant changes have occured within the collection, and for creating a new metadata set manager as necessary. 
    7979 * @author John Thompson 
    8080 * @version 2.3 
     
    8787    /** Are we currently in the process of importing? */ 
    8888    static private boolean importing = false; 
     89    /** Are we currently in the process of scheduling? */  
     90    static private boolean scheduling = false;  
    8991    /** The objects listening for CollectionContentsChanged events. */ 
    9092    static private ArrayList collection_contents_changed_listeners = new ArrayList(); 
     
    9799    /** An inner class listener responsible for noting tree changes and resetting saved when they occur. */ 
    98100    static private FMTreeModelListener fm_tree_model_listener = null; 
    99     /** The monitor resposible for parsing the build process. */ 
     101    /** The monitor responsible for parsing the build process. */ 
    100102    static private GShellProgressMonitor build_monitor = null; 
    101     /** The monitor resposible for parsing the import process. */ 
     103    /** The monitor responsible for parsing the import process. */ 
    102104    static private GShellProgressMonitor import_monitor = null; 
     105    /** The monitor responsible for parsing the scheduler process. */  
     106    static private GShellProgressMonitor schedule_monitor = null;  
    103107 
    104108    /** The name of the standard lock file. */ 
     
    109113    /** Used to indicate the source of the message is the building methods. */ 
    110114    static final public int BUILDING  = 5; 
     115    /** Used to indicate the source of the message is in the scheduling methods...?  */  
     116    static final public int SCHEDULING = 7;  
    111117 
    112118   
     
    116122    this.building = false; 
    117123    this.importing = false; 
     124    this.scheduling = false;  
    118125    this.collection = null; 
    119126 
     
    209216    } 
    210217 
     218    /*probably repeating alot of work, but I want to keep this separate... wendy*/ 
     219    public void scheduleBuild(boolean incremental_build) 
     220    { 
     221 
     222    DebugStream.println("In CollectionManager.scheduleBuild(), incremental_build: " + incremental_build);  
     223    DebugStream.println("Is event dispatch threa: " + SwingUtilities.isEventDispatchThread());  
     224 
     225    ArrayList sched_list = new ArrayList();  
     226    if ((Utility.isWindows()) && (!Gatherer.isGsdlRemote)) { 
     227        sched_list.add(Configuration.perl_path); 
     228        sched_list.add("-S"); 
     229    } 
     230    sched_list.add(LocalGreenstone.getBinScriptDirectoryPath() + "schedule.pl");  
     231    sched_list.add("-colname");  
     232    sched_list.add(collection.getName());  
     233    sched_list.add("-gli");  
     234 
     235    // First, generate the import.pl command, also converting to a string 
     236    // Generate the import.pl command 
     237    ArrayList import_list = new ArrayList(); 
     238    if ((Utility.isWindows()) && (!Gatherer.isGsdlRemote)) { 
     239        import_list.add(Configuration.perl_path); 
     240        import_list.add("-S"); 
     241    } 
     242        import_list.add(LocalGreenstone.getBinScriptDirectoryPath() + "import.pl"); 
     243    import_list.add("-language"); 
     244    import_list.add(Configuration.getLanguage()); 
     245    import_list.add("-collectdir"); 
     246    import_list.add(getCollectDirectory()); 
     247 
     248    String[] import_options = collection.import_options.getValues(); 
     249    int i = 0;  
     250    for (i = 0; i < import_options.length; i++) { 
     251        import_list.add(import_options[i]); 
     252    } 
     253 
     254    import_list.add(collection.getName());   
     255 
     256    String[] import_parts = (String[]) import_list.toArray(new String[0]);  
     257    String command = ""; 
     258    i = 0;  
     259    for (i = 0; i < import_parts.length-1; i++) { 
     260        command = command + import_parts[i] + " ";  
     261    }  
     262    command = command + import_parts[i];  
     263 
     264    sched_list.add("-import");  
     265    sched_list.add("\"" + command + "\"");  
     266 
     267    // Generate the buildcol.pl command, also converting to a string 
     268    ArrayList build_list = new ArrayList(); 
     269 
     270    // i'm not doing this in schedule.pl right now - should i be?  
     271    if ((Utility.isWindows()) && (!Gatherer.isGsdlRemote)) { 
     272        build_list.add(Configuration.perl_path); 
     273        build_list.add("-S"); 
     274    } 
     275    build_list.add(LocalGreenstone.getBinScriptDirectoryPath() + "buildcol.pl"); 
     276    build_list.add("-language"); 
     277    build_list.add(Configuration.getLanguage()); 
     278    build_list.add("-collectdir"); 
     279    build_list.add(getCollectDirectory()); 
     280 
     281    // If the user hasn't manually specified "-keepold" or "-removeold" then pick one based on incremental_build 
     282    if (!collection.build_options.getValueEnabled("keepold") && !collection.build_options.getValueEnabled("removeold")) { 
     283        build_list.add(incremental_build ? "-keepold" : "-removeold"); 
     284    } 
     285 
     286    String[] build_options = collection.build_options.getValues(); 
     287    for (i = 0; i < build_options.length; i++) { 
     288        build_list.add(build_options[i]); 
     289    } 
     290 
     291    build_list.add(collection.getName()); 
     292     
     293    //build actual string 
     294    String[] build_parts = (String[]) build_list.toArray(new String[0]); 
     295    String command2 = ""; 
     296        for(i = 0; i < build_parts.length-1; i++) { 
     297            command2 = command2 + build_parts[i] + " "; 
     298        } 
     299    command2 = command2 + build_parts[i];  
     300 
     301    sched_list.add("-build");  
     302    sched_list.add("\"" + command2 + "\"");  
     303     
     304    //next, the scheduling frequency goes here 
     305    String[] schedule_options = collection.schedule_options.getValues();   
     306    for(i = 0; i < schedule_options.length; i++) {  
     307        sched_list.add(schedule_options[i]);  
     308    } 
     309 
     310    //now, hope it will run. ;)  
     311    String[] sched_parts = (String[]) sched_list.toArray(new String[0]);  
     312 
     313        GShell shell = new GShell(sched_parts, GShell.SCHEDULE, SCHEDULING, this, schedule_monitor, GShell.GSHELL_SCHEDULE); 
     314    shell.addGShellListener(Gatherer.g_man.create_pane); 
     315    shell.addGShellListener(Gatherer.g_man.format_pane); 
     316    shell.start(); 
     317    } 
    211318 
    212319    /** Used to determine whether the currently active collection has been built. 
     
    450557        collection.import_options.setValue("removeold", true, null); 
    451558 
     559        //try to obtain email address of collection owner if it exists...  
     560        String stmp = Configuration.getEmail();  
     561        if(stmp != null) { 
     562        collection.schedule_options.setValue("toaddr", false, Configuration.getEmail()); 
     563        } 
     564 
     565        //try to obtain email address of Greenstone installation webmaster for - used to indicate "sender".  
     566        File mcfg = new File(LocalGreenstone.getDirectoryPath()+"etc" + File.separator + "main.cfg"); 
     567        BufferedReader maincfg = new BufferedReader(new FileReader(mcfg));       
     568        stmp = ""; 
     569        String fromaddr = "";  
     570        while((stmp = maincfg.readLine()) != null) {  
     571        if(stmp.startsWith("maintainer")) { 
     572               fromaddr = stmp.substring(10); //length of MailServer  
     573           fromaddr = fromaddr.trim();   
     574           break;  
     575        } 
     576        } 
     577        maincfg.close();  
     578        if(!fromaddr.equals("NULL") && !fromaddr.equals("null")) { 
     579        collection.schedule_options.setValue("fromaddr", false, fromaddr);       
     580        } 
     581 
     582        //try to obtain an smtp server address from main.cfg. If that fails, 
     583        //try mail.server if an email address exists. If that fails, 
     584        //maybe a message to set attribute in main.cfg?  
     585        //i'm pretty sure there exists functionality to do this, but  
     586        //i'll finish this faster if I just wrote it 
     587 
     588 
     589        maincfg = new BufferedReader(new FileReader(mcfg));        
     590        String smtptmp = "NULL"; 
     591        while((stmp = maincfg.readLine()) != null) {  
     592        if(stmp.startsWith("MailServer")) { 
     593               smtptmp = stmp.substring(10); //length of MailServer  
     594           smtptmp = smtptmp.trim();  
     595           break;  
     596        } 
     597        } 
     598            maincfg.close(); 
     599 
     600            //try if lookup failes 
     601            if(smtptmp.equals("NULL") || smtptmp.equals("null")) { 
     602        String email2=fromaddr;  
     603        if(!email2.equals("NULL") && !email2.equals("null")) { 
     604            int loc = email2.indexOf('@');  
     605            email2 = email2.substring(loc+1);  
     606            smtptmp = "mail."+email2; 
     607            } 
     608            } 
     609        if(!smtptmp.equals("NULL") && !smtptmp.equals("null")) {  
     610        collection.schedule_options.setValue("smtp", false, smtptmp); 
     611        } 
     612 
    452613        MetadataSetManager.clearMetadataSets(); 
    453614        MetadataXMLFileManager.clearMetadataXMLFiles(); 
     
    10381199        } 
    10391200 
     1201        //THIS LOOKS LIKE THE BEST PLACE TO TRY AND UPDATE .col FILES FOR EXISTING COLLECTIONS...Wendy 
     1202  
     1203        //First, see if "Schedule" exists in the XMl File...  
     1204        BufferedReader bir = new BufferedReader(new FileReader(collection_file));  
     1205        boolean flag = false;  
     1206        try { 
     1207        String stmp = new String();  
     1208            
     1209        while((stmp = bir.readLine()) != null) { 
     1210            stmp = stmp.trim();   
     1211            if(stmp.equals("<Schedule>") || stmp.equals("<Schedule/>")) { 
     1212               flag = true;  
     1213               break;  
     1214            } 
     1215        } 
     1216        bir.close();  
     1217 
     1218        } catch (IOException ioe) { 
     1219        //do something here 
     1220        } 
     1221 
     1222        //modify if old .col (i.e. no Schedule exists in XML file) 
     1223        if(!flag) { 
     1224        File new_collection_file = new File(collection_directory.getAbsolutePath() + "/tmp.col"); 
     1225 
     1226 
     1227        BufferedWriter bor = new BufferedWriter(new FileWriter(new_collection_file));  
     1228        bir = new BufferedReader(new FileReader(collection_file));  
     1229 
     1230        try {  
     1231            String stmp = new String();  
     1232            while((stmp = bir.readLine()) != null) { 
     1233            String stmp2 = stmp.trim();  
     1234            if(stmp2.startsWith("<!ELEMENT Argument")) { 
     1235                bor.write("  <!ELEMENT Schedule          (Arguments*)>\n");                 
     1236                } 
     1237                        else if(stmp2.equals("</BuildConfig>")) { 
     1238                               bor.write("      <Schedule/>\n");  
     1239                        } 
     1240 
     1241                        bor.write(stmp + "\n");  
     1242 
     1243            } 
     1244                    bir.close();  
     1245                    bor.close(); 
     1246                } catch (IOException ioe) {   
     1247                  //do something here 
     1248                }   
     1249 
     1250        //copy over tmp.col to replace  
     1251        try {  
     1252        collection_file.delete(); 
     1253        new_collection_file.renameTo(collection_file);  
     1254        } catch (Exception e) {  
     1255            //do something here  
     1256        }  
     1257        } 
     1258 
    10401259        // Open the collection file 
    10411260        this.collection = new Collection(collection_file); 
     
    10481267        throw(new Exception(Dictionary.get("CollectionManager.Missing_Config"))); // this error message does not agree with the error 
    10491268        } 
     1269 
     1270 
     1271        //try to obtain email address of collection owner if it exists...  
     1272        String stmp = Configuration.getEmail();  
     1273        if(stmp != null) { 
     1274        collection.schedule_options.setValue("toaddr", false, Configuration.getEmail()); 
     1275        } 
     1276 
     1277 
     1278 
     1279        //The next few items deal with updating the SMTP server, and the to: and from: addresses 
     1280        //from main.cfg and the collection configuration. if no changes are made, or the  
     1281        //values are result to NULL, any existing values are kept.  
     1282 
     1283        //try to obtain email address of Greenstone installation webmaster for - used to indicate "sender".  
     1284        File mcfg = new File(LocalGreenstone.getDirectoryPath()+"etc" + File.separator + "main.cfg"); 
     1285        BufferedReader maincfg = new BufferedReader(new FileReader(mcfg));       
     1286        stmp = ""; 
     1287        String fromaddr = "";  
     1288        while((stmp = maincfg.readLine()) != null) {  
     1289        if(stmp.startsWith("maintainer")) { 
     1290               fromaddr = stmp.substring(10); //length of MailServer  
     1291           fromaddr = fromaddr.trim();   
     1292           break;  
     1293        } 
     1294        } 
     1295        maincfg.close();  
     1296        if(!fromaddr.equals("NULL") && !fromaddr.equals("null")) { 
     1297        collection.schedule_options.setValue("fromaddr", false, fromaddr);       
     1298        } 
     1299 
     1300        //try to obtain an smtp server address from main.cfg. If that fails, 
     1301        //try mail.server if an email address exists. If that fails, 
     1302        //maybe a message to set attribute in main.cfg?  
     1303        //i'm pretty sure there exists functionality to do this, but  
     1304        //i'll finish this faster if I just wrote it 
     1305 
     1306 
     1307        maincfg = new BufferedReader(new FileReader(mcfg));        
     1308        String smtptmp = "NULL"; 
     1309        while((stmp = maincfg.readLine()) != null) {  
     1310        if(stmp.startsWith("MailServer")) { 
     1311               smtptmp = stmp.substring(10); //length of MailServer  
     1312           smtptmp = smtptmp.trim();  
     1313           break;  
     1314        } 
     1315        } 
     1316            maincfg.close(); 
     1317 
     1318            //try if lookup failes 
     1319            if(smtptmp.equals("NULL") || smtptmp.equals("null")) { 
     1320        String email2=fromaddr;  
     1321        if(!email2.equals("NULL") && !email2.equals("null")) { 
     1322            int loc = email2.indexOf('@');  
     1323            email2 = email2.substring(loc+1);  
     1324            smtptmp = "mail."+email2; 
     1325            } 
     1326            } 
     1327        if(!smtptmp.equals("NULL") && !smtptmp.equals("null")) {  
     1328        collection.schedule_options.setValue("smtp", false, smtptmp); 
     1329        } 
     1330 
    10501331 
    10511332            MetadataSetManager.clearMetadataSets(); 
     
    12031484        buildCollection(false); 
    12041485    } 
     1486    else if(event.getType() == GShell.SCHEDULE && event.getStatus() == GShell.OK ) { 
     1487 
     1488        WarningDialog collection_built_warning_dialog = new WarningDialog("warning.ScheduleBuilt", Dictionary.get("ScheduleBuilt.Title"), Dictionary.get("ScheduleBuilt.Message"), null, false); 
     1489        collection_built_warning_dialog.setMessageOnly(true); // Not a warning 
     1490        collection_built_warning_dialog.display(); 
     1491        collection_built_warning_dialog.dispose(); 
     1492        collection_built_warning_dialog = null;    
     1493    }  
    12051494    // If we were running a build, now is when we move files across. 
    12061495    else if(event.getType() == GShell.BUILD && event.getStatus() == GShell.OK) { 
     
    12571546          JOptionPane.showMessageDialog(Gatherer.g_man, errMsg, Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE); 
    12581547        }  
     1548        else if(event.getType() == GShell.SCHEDULE) {  
     1549        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Schedule_Failed"), Dictionary.get("CollectionManager.Schedule_Ready_Title"), JOptionPane.ERROR_MESSAGE); 
     1550        } 
    12591551        else { 
    12601552        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CollectionManager.Preview_Ready_Failed"), Dictionary.get("CollectionManager.Preview_Ready_Title"), JOptionPane.ERROR_MESSAGE); 
     
    12911583    public void registerImportMonitor(GShellProgressMonitor monitor) { 
    12921584    import_monitor = monitor; 
     1585    } 
     1586 
     1587    public void registerScheduleMonitor(GShellProgressMonitor monitor) {  
     1588    schedule_monitor = monitor;  
    12931589    } 
    12941590