Changeset 25540


Ignore:
Timestamp:
2012-05-08T09:54:33+12:00 (12 years ago)
Author:
sjm84
Message:

Updated the import service so that a manifest file can be included, added a build AND activate call so that the client does not have to trigger both. Also added in a very basic task queuing mechanism

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Construct.java

    r25067 r25540  
    2121import org.greenstone.gsdl3.util.*;
    2222import org.greenstone.gsdl3.build.*;
     23import org.greenstone.util.GlobalProperties;
    2324
    2425import org.w3c.dom.Document;
     
    3334import java.util.List;
    3435import java.util.ArrayList;
     36import java.io.BufferedWriter;
    3537import java.io.File;
     38import java.io.FileWriter;
     39import java.lang.Thread.State;
    3640import java.util.Locale;
     41
     42import java.util.Timer;
     43import java.util.TimerTask;
    3744
    3845import org.apache.log4j.*;
     
    5663    private static final String BUILD_SERVICE = "BuildCollection";
    5764    private static final String ACTIVATE_SERVICE = "ActivateCollection";
     65    private static final String BUILD_AND_ACTIVATE_SERVICE = "BuildAndActivateCollection";
    5866    private static final String DELETE_SERVICE = "DeleteCollection";
    5967    private static final String RELOAD_SERVICE = "ReloadCollection";
     
    7583    // set of listeners for any construction commands
    7684    protected Map listeners = null;
     85    protected HashMap<String, Boolean> collectionOperationMap = new HashMap<String, Boolean>();
    7786
    7887    public GS2Construct()
    7988    {
    8089        this.listeners = Collections.synchronizedMap(new HashMap());
    81 
    8290    }
    8391
     
    155163    }
    156164
     165    protected Element processBuildAndActivateCollection(Element request)
     166    {
     167        waitUntilReady(request);
     168        Element buildResponse = processBuildCollection(request);
     169        if(buildResponse.getElementsByTagName(GSXML.ERROR_ELEM).getLength() > 0)
     170        {
     171            return buildResponse;
     172        }
     173       
     174        Element statusElem = (Element) buildResponse.getElementsByTagName(GSXML.STATUS_ELEM).item(0);
     175        String id = statusElem.getAttribute("pid");
     176
     177        GS2PerlListener currentListener = (GS2PerlListener) this.listeners.get(id);
     178        int statusCode = currentListener.getStatus();
     179        while (!GSStatus.isCompleted(statusCode))
     180        {
     181            // wait for the process, and keep checking the status code
     182            // there is probably a better way to do this.
     183            try
     184            {
     185                Thread.currentThread().sleep(100);
     186            }
     187            catch (Exception e)
     188            { // ignore
     189            }
     190            statusCode = currentListener.getStatus();
     191        }
     192
     193        Element activateResponse = processActivateCollection(request);
     194        signalReady(request);
     195        return activateResponse;
     196    }
     197
    157198    protected Element processImportCollection(Element request)
    158199    {
     200        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     201        HashMap params = GSXML.extractParams(param_list, false);
     202
     203        //If we have been requested to only build certain documents then we need to create a manifest file
     204        String documentsParam = (String) params.get("documents");
     205        if (documentsParam != null && !documentsParam.equals(""))
     206        {
     207            String s = File.separator;
     208            String manifestFolderPath = this.site_home + s + "collect" + s + params.get(COL_PARAM) + s + "manifests";
     209            String manifestFilePath = manifestFolderPath + File.separator + "tempManifest.xml";
     210
     211            File manifestFolderFile = new File(manifestFolderPath);
     212            if (!manifestFolderFile.exists())
     213            {
     214                manifestFolderFile.mkdirs();
     215            }
     216
     217            File manifestFile = new File(manifestFilePath);
     218            if (!manifestFile.exists())
     219            {
     220                try
     221                {
     222                    manifestFile.createNewFile();
     223                }
     224                catch (Exception ex)
     225                {
     226                    ex.printStackTrace();
     227                    return null; //Probably should return an actual error
     228                }
     229            }
     230            String[] docList = documentsParam.split(",");
     231
     232            try
     233            {
     234                BufferedWriter bw = new BufferedWriter(new FileWriter(manifestFile));
     235                bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
     236                bw.write("<Manifest>\n");
     237                bw.write("  <Index>\n");
     238                for (int j = 0; j < docList.length; j++)
     239                {
     240                    bw.write("    <Filename>" + docList[j] + "</Filename>\n");
     241                }
     242                bw.write("  </Index>\n");
     243                bw.write("</Manifest>\n");
     244                bw.close();
     245            }
     246            catch (Exception ex)
     247            {
     248                ex.printStackTrace();
     249                return null; //Probably should return an actual error
     250            }
     251        }
     252
    159253        return runCommand(request, GS2PerlConstructor.IMPORT);
    160254    }
     
    182276            return response;
    183277        }
    184        
     278
    185279        UserContext userContext = new UserContext(request);
    186280        systemRequest("delete", coll_name, null, userContext);
     
    422516        e = this.doc.createElement(GSXML.SERVICE_ELEM);
    423517        e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
     518        e.setAttribute(GSXML.NAME_ATT, BUILD_AND_ACTIVATE_SERVICE);
     519        this.short_service_info.appendChild(e);
     520
     521        e = this.doc.createElement(GSXML.SERVICE_ELEM);
     522        e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
    424523        e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
    425524        this.short_service_info.appendChild(e);
     
    498597            coll_name = (String) params.get(COL_PARAM);
    499598        }
    500         logger.info("Coll name = " + coll_name);
     599
    501600        // makes a paramList of the relevant params
    502601        Element other_params = extractOtherParams(params, type);
     
    512611        }
    513612
    514         GS2PerlListener listener = new GS2PerlListener();
    515613        constructor.setSiteHome(this.site_home);
    516614        constructor.setCollectionName(coll_name);
    517615        constructor.setActionType(type);
    518616        constructor.setProcessParams(other_params);
    519 
     617        if (type == GS2PerlConstructor.IMPORT)
     618        {
     619            constructor.setManifestFile(this.site_home + File.separator + "collect" + File.separator + params.get(COL_PARAM) + File.separator + "manifests" + File.separator + "tempManifest.xml");
     620        }
     621
     622        GS2PerlListener listener = new GS2PerlListener();
    520623        constructor.addListener(listener);
    521624        constructor.start();
     
    651754        return null;
    652755    }
    653 
     756   
     757    protected void waitUntilReady(Element request)
     758    {
     759        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     760        HashMap params = GSXML.extractParams(param_list, false);
     761       
     762        String collection = (String)params.get(COL_PARAM);
     763
     764        if(checkCollectionIsNotBusy(collection))
     765        {
     766            return;
     767        }
     768
     769        while(collectionOperationMap.get(collection) != null)
     770        {
     771            try
     772            {
     773                Thread.currentThread().sleep(1000);
     774            }
     775            catch(Exception ex)
     776            {
     777                ex.printStackTrace();
     778            }
     779        }
     780    }
     781   
     782    protected void signalReady(Element request)
     783    {
     784        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     785        HashMap params = GSXML.extractParams(param_list, false);
     786       
     787        String collection = (String)params.get(COL_PARAM);
     788
     789        collectionOperationMap.remove(collection);
     790    }
     791   
     792    protected synchronized boolean checkCollectionIsNotBusy(String collection)
     793    {
     794        if(collectionOperationMap.get(collection) == null)
     795        {
     796            collectionOperationMap.put(collection, true);
     797            return true;
     798        }
     799        return false;
     800    }
    654801}
Note: See TracChangeset for help on using the changeset viewer.