- Timestamp:
- 2012-05-08T09:54:33+12:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Construct.java
r25067 r25540 21 21 import org.greenstone.gsdl3.util.*; 22 22 import org.greenstone.gsdl3.build.*; 23 import org.greenstone.util.GlobalProperties; 23 24 24 25 import org.w3c.dom.Document; … … 33 34 import java.util.List; 34 35 import java.util.ArrayList; 36 import java.io.BufferedWriter; 35 37 import java.io.File; 38 import java.io.FileWriter; 39 import java.lang.Thread.State; 36 40 import java.util.Locale; 41 42 import java.util.Timer; 43 import java.util.TimerTask; 37 44 38 45 import org.apache.log4j.*; … … 56 63 private static final String BUILD_SERVICE = "BuildCollection"; 57 64 private static final String ACTIVATE_SERVICE = "ActivateCollection"; 65 private static final String BUILD_AND_ACTIVATE_SERVICE = "BuildAndActivateCollection"; 58 66 private static final String DELETE_SERVICE = "DeleteCollection"; 59 67 private static final String RELOAD_SERVICE = "ReloadCollection"; … … 75 83 // set of listeners for any construction commands 76 84 protected Map listeners = null; 85 protected HashMap<String, Boolean> collectionOperationMap = new HashMap<String, Boolean>(); 77 86 78 87 public GS2Construct() 79 88 { 80 89 this.listeners = Collections.synchronizedMap(new HashMap()); 81 82 90 } 83 91 … … 155 163 } 156 164 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 157 198 protected Element processImportCollection(Element request) 158 199 { 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 159 253 return runCommand(request, GS2PerlConstructor.IMPORT); 160 254 } … … 182 276 return response; 183 277 } 184 278 185 279 UserContext userContext = new UserContext(request); 186 280 systemRequest("delete", coll_name, null, userContext); … … 422 516 e = this.doc.createElement(GSXML.SERVICE_ELEM); 423 517 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); 424 523 e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE); 425 524 this.short_service_info.appendChild(e); … … 498 597 coll_name = (String) params.get(COL_PARAM); 499 598 } 500 logger.info("Coll name = " + coll_name); 599 501 600 // makes a paramList of the relevant params 502 601 Element other_params = extractOtherParams(params, type); … … 512 611 } 513 612 514 GS2PerlListener listener = new GS2PerlListener();515 613 constructor.setSiteHome(this.site_home); 516 614 constructor.setCollectionName(coll_name); 517 615 constructor.setActionType(type); 518 616 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(); 520 623 constructor.addListener(listener); 521 624 constructor.start(); … … 651 754 return null; 652 755 } 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 } 654 801 }
Note:
See TracChangeset
for help on using the changeset viewer.