Ignore:
Timestamp:
2011-11-17T09:33:36+13:00 (12 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/build/GS2PerlConstructor.java

    r6270 r24816  
    33// greenstome classes
    44import org.greenstone.gsdl3.util.*;
    5 
    65
    76// xml classes
     
    1514import java.util.Vector;
    1615
    17 /** CollectionConstructor class for greenstone 2 compatible building
    18  * it uses the perl scripts to do the building stuff */
    19 public class GS2PerlConstructor
    20     extends CollectionConstructor {
    21 
    22     public static final int NEW = 0;
    23     public static final int IMPORT = 1;
    24     public static final int BUILD = 2;
    25     public static final int ACTIVATE = 3;
    26 
    27     /** gsdlhome for greenstone 2 - we use the perl modules and building
    28      * scripts from there */
    29     protected String gsdl2home = null;
    30     /** gsdlhome for gsdl3 - shouldn't need this eventually ??*/
    31     protected String gsdl3home = null;
    32     /** gsdlos for greenstone 2 */
    33     protected String gsdlos = null;
    34     /** the path environment variable */
    35     protected String path = null;
    36 
    37     public GS2PerlConstructor(String name) {
    38     super(name);
    39     }
    40 
    41     /** retrieves the necessary environment variables */
    42     public boolean configure() {
    43     // try to get the environment variables
    44     this.gsdl2home = System.getProperty("GSDLHOME");
    45     this.gsdl3home = System.getProperty("GSDL3HOME");
    46     this.gsdlos = System.getProperty("GSDLOS");
    47     this.path = System.getProperty("PATH");
    48     if (this.gsdl2home ==null ) {
    49         System.err.println("You must have greenstone2 installed, and GSDLHOME set for GS2Perl building to work!!");
    50         return false;
    51     }
    52     if (this.gsdl3home == null|| this.gsdlos == null) {
    53        System.err.println("You must have GSDL3HOME and GSDLOS set for GS2Perl building to work!!");
    54         return false;
    55     } 
    56     if (this.path==null) {
    57         System.err.println("You must have the PATH set for GS2Perl building to work!!");
    58         return false;
    59     }
    60     return true;
    61     }
    62 
    63     public void run() {
    64     String msg;
    65     ConstructionEvent evt;
    66     if (this.process_type == -1) {
    67         msg = "Error: you must set the action type";
    68         evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
    69         sendMessage(evt);
    70         return;
    71     }
    72     if (this.site_home==null) {
    73         msg = "Error: you must set site_home";
    74         evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
    75         sendMessage(evt);
    76         return;
    77     }
    78     if (this.process_type != NEW && this.collection_name ==null) {
    79         msg = "Error: you must set collection_name";
    80         evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
    81         sendMessage(evt);
    82         return;
    83     }
    84    
    85     switch (this.process_type) {
    86     case NEW:
    87         newCollection();
    88         break;
    89     case IMPORT:
    90         importCollection();
    91         break;
    92     case BUILD:
    93         buildCollection();
    94         break;
    95     case ACTIVATE:
    96         activateCollection();
    97         break;
    98     default:
    99         msg = "wrong type of action specified!";
    100         evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
    101         sendMessage(evt);
    102         break;
    103     }
    104 
    105     }
    106 
    107     protected void newCollection() {
    108     sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: new collection."));
    109     Vector command = new Vector();
    110     command.add("gs2_mkcol.pl");
    111     command.add("-site");
    112     command.add(this.site_home);
    113     command.add("-collectdir");
    114     command.add(GSFile.collectDir(this.site_home));
    115     command.addAll(extractParameters(this.process_params));
    116     command.add(this.collection_name);
    117     String [] command_str = {};
    118     command_str = (String [])command.toArray(command_str);
    119     if (runPerlCommand(command_str)) {
    120         // success!! - need to send the final completed message
    121         sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
    122     } // else an error message has already been sent, do nothing
    123    
    124     }
    125 
    126     protected void importCollection() {
    127     sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: import collection."));
    128     Vector command = new Vector();
    129     command.add("import.pl");
    130     command.add("-collectdir");
    131     command.add(GSFile.collectDir(this.site_home));
    132     command.addAll(extractParameters(this.process_params));
    133     command.add(this.collection_name);
    134     String [] command_str = {};
    135     command_str = (String [])command.toArray(command_str);
    136 
    137     if (runPerlCommand(command_str)) {
    138         // success!! - need to send the final completed message
    139         sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
    140     } // else an error message has already been sent, do nothing
    141     }
    142 
    143      protected void buildCollection() {
    144     sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: build collection."));
    145     Vector command = new Vector();
    146     command.add("buildcol.pl");
    147     command.add("-collectdir");
    148     command.add(GSFile.collectDir(this.site_home));
    149     command.addAll(extractParameters(this.process_params));
    150     command.add(this.collection_name);
    151     String [] command_str = {};
    152     command_str = (String [])command.toArray(command_str);
    153    
    154     if (runPerlCommand(command_str)) {
    155         // success!! - need to send the final completed message
    156         sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
    157     }// else an error message has already been sent, do nothing
    158 //         
    159 
    160      }
    161     protected void activateCollection() {
    162     sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: activate collection."));
    163 
    164     // first check that we have a building directory
    165     File build_dir = new File(GSFile.collectionBuildDir(this.site_home, this.collection_name));
    166     if (!build_dir.exists()) {
    167         sendMessage(new ConstructionEvent(this, GSStatus.ERROR, "build dir doesn't exist!"));
    168         return;
    169     }
    170 
    171     // move building to index
    172     File index_dir = new File(GSFile.collectionIndexDir(this.site_home, this.collection_name));
    173     if (index_dir.exists()) {
    174         sendMessage(new ConstructionEvent(this, GSStatus.INFO, "deleting index directory"));
    175         index_dir.delete();
    176         if (index_dir.exists()) {
    177         sendMessage(new ConstructionEvent(this, GSStatus.INFO, "index directory still exists!"));
    178         }
    179     }
    180 
    181     GSFile.moveDirectory(build_dir, index_dir);
    182     if (!index_dir.exists()) {
    183         sendMessage(new ConstructionEvent(this, GSStatus.ERROR, "index dir wasn't created!"));
    184     }
    185 
    186     // run the convert coll script to convert the gs2 coll to gs3
    187     Vector command = new Vector();
    188     command.add("convert_coll_from_gs2.pl");
    189     command.add("-collectdir");
    190     command.add(GSFile.collectDir(this.site_home));
    191     command.addAll(extractParameters(this.process_params));
    192     command.add(this.collection_name);
    193     String [] command_str = {};
    194     command_str = (String [])command.toArray(command_str);
    195    
    196     if (!runPerlCommand(command_str)) {
    197         // an error has occurred, dont carry on
    198         return;
    199     }
    200 
    201     // success!!  - need to send the final completed message
    202     sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
    203     }
    204 
    205     /** extracts all the args from the xml and returns them in a Vector */
    206     protected Vector extractParameters(Element param_list) {
    207 
    208     Vector args = new Vector();
    209     if (param_list==null) {
    210         return args; // return an empty vector
    211     }
    212     NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
    213    
    214     for (int i=0; i<params.getLength(); i++) {
    215         Element p = (Element)params.item(i);
    216         String name = p.getAttribute(GSXML.NAME_ATT);
    217         String value=p.getAttribute(GSXML.VALUE_ATT);
    218         if (!name.equals("")) {
    219         args.add("-"+name);
    220         if (!value.equals("")) {
    221             args.add(value);
    222         }
    223         }
    224     }
    225        
    226     return args;
    227     }
    228     /** returns true if completed correctly, false otherwise */
    229     protected boolean runPerlCommand(String[] command) {
    230     String msg;
    231     ConstructionEvent evt;
    232    
    233     String args[] = {"GSDLHOME="+this.gsdl2home, "GSDL3HOME="+this.gsdl3home, "GSDLOS="+this.gsdlos, "PATH="+this.path};
    234     String command_str = "";
    235     for(int i = 0; i < command.length; i++) {
    236         command_str = command_str + command[i] + " ";
    237     }
    238     sendMessage(new ConstructionEvent(this, GSStatus.INFO, "command = "+command_str));
    239     try {
    240         Runtime rt = Runtime.getRuntime();
    241         sendProcessBegun(new ConstructionEvent(this, GSStatus.ACCEPTED, "starting"));
    242         Process prcs = rt.exec(command, args);
    243 
    244         InputStreamReader eisr = new InputStreamReader( prcs.getErrorStream() );
    245         InputStreamReader stdinisr = new InputStreamReader( prcs.getInputStream() );
    246         BufferedReader ebr = new BufferedReader( eisr );
    247         BufferedReader stdinbr = new BufferedReader( stdinisr );
    248         // Captures the std err of a program and pipes it into
    249         // std in of java
    250         String eline = null;
    251         String stdinline = null;
    252         while (((eline = ebr.readLine()) != null || (stdinline = stdinbr.readLine()) != null) && !this.cancel) {
    253         if(eline != null) {
    254             sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, eline));
    255         }
    256         if(stdinline != null) {
    257             sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, stdinline));
    258         }
    259         }
    260         if(!this.cancel) {
    261         // Now display final message based on exit value
    262         prcs.waitFor();
    263        
    264         if(prcs.exitValue() == 0) {
    265             //status = OK;
    266             sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, "Success"));
    267            
    268         } else {
    269             //status = ERROR;
    270             sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Failure"));
    271            
    272             return false;
    273            
    274         }
    275         }
    276         else {
    277         // I need to somehow kill the child process. Unfortunately Thread.stop() and Process.destroy() both fail to do this. But now, thankx to the magic of Michaels 'close the stream suggestion', it works fine.
    278         sendProcessStatus(new ConstructionEvent(this, GSStatus.HALTED, "killing the process"));
    279         prcs.getOutputStream().close();
    280         prcs.destroy();
    281         //status = ERROR;
    282         return false;
    283         }
    284        
    285     } catch (Exception e) {
    286         sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Exception occurred "+e.toString()));
    287     }
    288 
    289     // we're done, but we dont send a process complete message here cos there ight be stuff to do after this has finished.
    290     return true;
    291 
    292     }
    293    
    294 
    295 
     16/**
     17 * CollectionConstructor class for greenstone 2 compatible building it uses the
     18 * perl scripts to do the building stuff
     19 */
     20public class GS2PerlConstructor extends CollectionConstructor
     21{
     22
     23    public static final int NEW = 0;
     24    public static final int IMPORT = 1;
     25    public static final int BUILD = 2;
     26    public static final int ACTIVATE = 3;
     27
     28    /**
     29     * gsdlhome for greenstone 2 - we use the perl modules and building scripts
     30     * from there
     31     */
     32    protected String gsdl2home = null;
     33    /** gsdlhome for gsdl3 - shouldn't need this eventually ?? */
     34    protected String gsdl3home = null;
     35    /** gsdlos for greenstone 2 */
     36    protected String gsdlos = null;
     37    /** the path environment variable */
     38    protected String path = null;
     39
     40    public GS2PerlConstructor(String name)
     41    {
     42        super(name);
     43    }
     44
     45    /** retrieves the necessary environment variables */
     46    public boolean configure()
     47    {
     48        // try to get the environment variables
     49        this.gsdl2home = System.getProperty("GSDLHOME");
     50        this.gsdl3home = System.getProperty("GSDL3HOME");
     51        this.gsdlos = System.getProperty("GSDLOS");
     52        this.path = System.getProperty("PATH");
     53        if (this.gsdl2home == null)
     54        {
     55            System.err.println("You must have greenstone2 installed, and GSDLHOME set for GS2Perl building to work!!");
     56            return false;
     57        }
     58        if (this.gsdl3home == null || this.gsdlos == null)
     59        {
     60            System.err.println("You must have GSDL3HOME and GSDLOS set for GS2Perl building to work!!");
     61            return false;
     62        }
     63        if (this.path == null)
     64        {
     65            System.err.println("You must have the PATH set for GS2Perl building to work!!");
     66            return false;
     67        }
     68        return true;
     69    }
     70
     71    public void run()
     72    {
     73        String msg;
     74        ConstructionEvent evt;
     75        if (this.process_type == -1)
     76        {
     77            msg = "Error: you must set the action type";
     78            evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
     79            sendMessage(evt);
     80            return;
     81        }
     82        if (this.site_home == null)
     83        {
     84            msg = "Error: you must set site_home";
     85            evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
     86            sendMessage(evt);
     87            return;
     88        }
     89        if (this.process_type != NEW && this.collection_name == null)
     90        {
     91            msg = "Error: you must set collection_name";
     92            evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
     93            sendMessage(evt);
     94            return;
     95        }
     96
     97        switch (this.process_type)
     98        {
     99        case NEW:
     100            newCollection();
     101            break;
     102        case IMPORT:
     103            importCollection();
     104            break;
     105        case BUILD:
     106            buildCollection();
     107            break;
     108        case ACTIVATE:
     109            activateCollection();
     110            break;
     111        default:
     112            msg = "wrong type of action specified!";
     113            evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
     114            sendMessage(evt);
     115            break;
     116        }
     117
     118    }
     119
     120    protected void newCollection()
     121    {
     122        sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: new collection."));
     123        Vector command = new Vector();
     124        command.add("gs2_mkcol.pl");
     125        command.add("-site");
     126        command.add(this.site_home);
     127        command.add("-collectdir");
     128        command.add(GSFile.collectDir(this.site_home));
     129        command.addAll(extractParameters(this.process_params));
     130        command.add(this.collection_name);
     131        String[] command_str = {};
     132        command_str = (String[]) command.toArray(command_str);
     133        if (runPerlCommand(command_str))
     134        {
     135            // success!! - need to send the final completed message
     136            sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
     137        } // else an error message has already been sent, do nothing
     138
     139    }
     140
     141    protected void importCollection()
     142    {
     143        sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: import collection."));
     144        Vector command = new Vector();
     145        command.add("import.pl");
     146        command.add("-collectdir");
     147        command.add(GSFile.collectDir(this.site_home));
     148        command.addAll(extractParameters(this.process_params));
     149        command.add(this.collection_name);
     150        String[] command_str = {};
     151        command_str = (String[]) command.toArray(command_str);
     152
     153        if (runPerlCommand(command_str))
     154        {
     155            // success!! - need to send the final completed message
     156            sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
     157        } // else an error message has already been sent, do nothing
     158    }
     159
     160    protected void buildCollection()
     161    {
     162        sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: build collection."));
     163        Vector command = new Vector();
     164        command.add("buildcol.pl");
     165        command.add("-collectdir");
     166        command.add(GSFile.collectDir(this.site_home));
     167        command.addAll(extractParameters(this.process_params));
     168        command.add(this.collection_name);
     169        String[] command_str = {};
     170        command_str = (String[]) command.toArray(command_str);
     171
     172        if (runPerlCommand(command_str))
     173        {
     174            // success!! - need to send the final completed message
     175            sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
     176        }// else an error message has already been sent, do nothing
     177            //         
     178
     179    }
     180
     181    protected void activateCollection()
     182    {
     183        sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: activate collection."));
     184
     185        // first check that we have a building directory
     186        File build_dir = new File(GSFile.collectionBuildDir(this.site_home, this.collection_name));
     187        if (!build_dir.exists())
     188        {
     189            sendMessage(new ConstructionEvent(this, GSStatus.ERROR, "build dir doesn't exist!"));
     190            return;
     191        }
     192
     193        // move building to index
     194        File index_dir = new File(GSFile.collectionIndexDir(this.site_home, this.collection_name));
     195        if (index_dir.exists())
     196        {
     197            sendMessage(new ConstructionEvent(this, GSStatus.INFO, "deleting index directory"));
     198            index_dir.delete();
     199            if (index_dir.exists())
     200            {
     201                sendMessage(new ConstructionEvent(this, GSStatus.INFO, "index directory still exists!"));
     202            }
     203        }
     204
     205        GSFile.moveDirectory(build_dir, index_dir);
     206        if (!index_dir.exists())
     207        {
     208            sendMessage(new ConstructionEvent(this, GSStatus.ERROR, "index dir wasn't created!"));
     209        }
     210
     211        // run the convert coll script to convert the gs2 coll to gs3
     212        Vector command = new Vector();
     213        command.add("convert_coll_from_gs2.pl");
     214        command.add("-collectdir");
     215        command.add(GSFile.collectDir(this.site_home));
     216        command.addAll(extractParameters(this.process_params));
     217        command.add(this.collection_name);
     218        String[] command_str = {};
     219        command_str = (String[]) command.toArray(command_str);
     220
     221        if (!runPerlCommand(command_str))
     222        {
     223            // an error has occurred, dont carry on
     224            return;
     225        }
     226
     227        // success!!  - need to send the final completed message
     228        sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
     229    }
     230
     231    /** extracts all the args from the xml and returns them in a Vector */
     232    protected Vector extractParameters(Element param_list)
     233    {
     234
     235        Vector args = new Vector();
     236        if (param_list == null)
     237        {
     238            return args; // return an empty vector
     239        }
     240        NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
     241
     242        for (int i = 0; i < params.getLength(); i++)
     243        {
     244            Element p = (Element) params.item(i);
     245            String name = p.getAttribute(GSXML.NAME_ATT);
     246            String value = p.getAttribute(GSXML.VALUE_ATT);
     247            if (!name.equals(""))
     248            {
     249                args.add("-" + name);
     250                if (!value.equals(""))
     251                {
     252                    args.add(value);
     253                }
     254            }
     255        }
     256
     257        return args;
     258    }
     259
     260    /** returns true if completed correctly, false otherwise */
     261    protected boolean runPerlCommand(String[] command)
     262    {
     263        String msg;
     264        ConstructionEvent evt;
     265
     266        String args[] = { "GSDLHOME=" + this.gsdl2home, "GSDL3HOME=" + this.gsdl3home, "GSDLOS=" + this.gsdlos, "PATH=" + this.path };
     267        String command_str = "";
     268        for (int i = 0; i < command.length; i++)
     269        {
     270            command_str = command_str + command[i] + " ";
     271        }
     272        sendMessage(new ConstructionEvent(this, GSStatus.INFO, "command = " + command_str));
     273        try
     274        {
     275            Runtime rt = Runtime.getRuntime();
     276            sendProcessBegun(new ConstructionEvent(this, GSStatus.ACCEPTED, "starting"));
     277            Process prcs = rt.exec(command, args);
     278
     279            InputStreamReader eisr = new InputStreamReader(prcs.getErrorStream());
     280            InputStreamReader stdinisr = new InputStreamReader(prcs.getInputStream());
     281            BufferedReader ebr = new BufferedReader(eisr);
     282            BufferedReader stdinbr = new BufferedReader(stdinisr);
     283            // Captures the std err of a program and pipes it into
     284            // std in of java
     285            String eline = null;
     286            String stdinline = null;
     287            while (((eline = ebr.readLine()) != null || (stdinline = stdinbr.readLine()) != null) && !this.cancel)
     288            {
     289                if (eline != null)
     290                {
     291                    sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, eline));
     292                }
     293                if (stdinline != null)
     294                {
     295                    sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, stdinline));
     296                }
     297            }
     298            if (!this.cancel)
     299            {
     300                // Now display final message based on exit value
     301                prcs.waitFor();
     302
     303                if (prcs.exitValue() == 0)
     304                {
     305                    //status = OK;
     306                    sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, "Success"));
     307
     308                }
     309                else
     310                {
     311                    //status = ERROR;
     312                    sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Failure"));
     313
     314                    return false;
     315
     316                }
     317            }
     318            else
     319            {
     320                // I need to somehow kill the child process. Unfortunately Thread.stop() and Process.destroy() both fail to do this. But now, thankx to the magic of Michaels 'close the stream suggestion', it works fine.
     321                sendProcessStatus(new ConstructionEvent(this, GSStatus.HALTED, "killing the process"));
     322                prcs.getOutputStream().close();
     323                prcs.destroy();
     324                //status = ERROR;
     325                return false;
     326            }
     327
     328        }
     329        catch (Exception e)
     330        {
     331            sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Exception occurred " + e.toString()));
     332        }
     333
     334        // we're done, but we dont send a process complete message here cos there ight be stuff to do after this has finished.
     335        return true;
     336
     337    }
    296338
    297339}
Note: See TracChangeset for help on using the changeset viewer.