| 1 17 18 package org.pentaho.plugin.shark; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.enhydra.shark.Shark; 30 import org.enhydra.shark.api.client.wfbase.BaseException; 31 import org.enhydra.shark.api.client.wfservice.PackageAdministration; 32 import org.enhydra.shark.api.client.wfservice.PackageHasActiveProcesses; 33 import org.enhydra.shark.api.client.wfservice.PackageInUse; 34 import org.enhydra.shark.api.client.wfservice.RepositoryMgr; 35 import org.pentaho.core.publisher.BasePublisher; 36 import org.pentaho.core.session.IPentahoSession; 37 import org.pentaho.core.system.PentahoSystem; 38 import org.pentaho.messages.Messages; 39 40 public class SharkPublisher extends BasePublisher { 41 42 45 private static final long serialVersionUID = 3866069850517891673L; 46 47 private static final Log logger = LogFactory.getLog(SharkPublisher.class); 48 49 private HashMap modifiedXPDLs; 50 51 private HashMap unmodifiedXPDLs; 52 53 private HashMap newXPDLs; 54 55 private SharkManager shark = null; 56 57 public Log getLogger() { 58 return logger; 59 } 60 61 public String getName() { 62 return Messages.getString("SharkPublisher.USER_TITLE"); } 64 65 public String getDescription() { 66 return Messages.getString("SharkPublisher.USER_PUBLISH_DESCRIPTION"); } 68 69 public String publish(IPentahoSession session) { 70 71 try { 72 unmodifiedXPDLs = new HashMap (); 73 modifiedXPDLs = new HashMap (); 74 newXPDLs = new HashMap (); 75 76 File solutionRoot = new File (PentahoSystem.getApplicationContext().getSolutionPath("")); 78 shark = SharkManager.getInstance(session); 79 PackageAdministration packageAdmin = Shark.getInstance().getAdminInterface().getPackageAdministration(); 80 RepositoryMgr repository = Shark.getInstance().getRepositoryManager(); 81 82 85 if (debug) 87 debug(Messages.getString("SharkPublisher.DEBUG_COPYING_FILES")); processDir(solutionRoot, repository, packageAdmin); 89 90 if (debug) 92 debug(Messages.getString("SharkPublisher.DEBUG_CLEANSING_REPOSITORY")); cleanupExternalRepository(); 94 95 if (debug) 97 debug(Messages.getString("SharkPublisher.DEBUG_ADDING_NEW_FILES")); addXpdlFiles(newXPDLs, repository, packageAdmin); 99 100 if (debug) 103 debug(Messages.getString("SharkPublisher.DEBUG_ADDING_UNMODIFIED_FILES")); addXpdlFiles(unmodifiedXPDLs, repository, packageAdmin); 105 106 if (debug) 108 debug(Messages.getString("SharkPublisher.DEBUG_UPDATING_FILES")); updateXpdlFiles(modifiedXPDLs, repository, packageAdmin); 110 111 SharkMaintenance maintenance = new SharkMaintenance(); 114 maintenance.doMaintenance(session); 115 } catch (Throwable t) { 116 error(Messages.getErrorString("SharkPublisher.ERROR_0009_PUBLISH_FAILED"), t); return Messages.getString("SharkPublisher.USER_ERROR_PUBLISH_FAILED") + t.getLocalizedMessage(); } 119 return Messages.getString("SharkPublisher.USER_WORKFLOW_UPDATED"); 121 } 122 123 private void processDir(File root, RepositoryMgr repository, PackageAdministration packageAdmin) { 124 125 File files[] = root.listFiles(); 127 if (files != null) { 128 for (int i = 0; i < files.length; i++) { 129 File file = files[i]; 130 if (file.getAbsolutePath().endsWith("shark\\repository\\external")) { file = null; 133 } else if (file.getAbsolutePath().endsWith("shark/repository/external")) { file = null; 136 } else if (file.isDirectory()) { 137 processDir(file, repository, packageAdmin); 139 } else { 140 if (file.getName().toLowerCase().endsWith(".xpdl")) { copyXpdlFile(file); 143 } 144 } 145 } 146 } 147 } 148 149 private void copyXpdlFile(File xpdlFile) { 150 151 try { 152 long size = xpdlFile.length(); 154 byte bytes[] = new byte[(int) size]; 155 FileInputStream stream = new FileInputStream (xpdlFile); 157 stream.read(bytes); 158 159 File destination = new File (shark.getExternalRepositoryPath() + File.separator + xpdlFile.getName()); 161 boolean modified = false; 162 if (destination.exists()) { 163 long destinationSize = destination.length(); 164 if (size != destinationSize) { 165 modified = true; 166 } else { 167 byte destinationBytes[] = new byte[(int) size]; 168 stream = new FileInputStream (destination); 169 stream.read(destinationBytes); 170 for (int i = 0; i < size; i++) { 172 if (bytes[i] != destinationBytes[i]) { 173 modified = true; 174 break; 175 } 176 } 177 } 178 if (modified) { 179 modifiedXPDLs.put(destination.getName(), ""); } else { 181 unmodifiedXPDLs.put(destination.getName(), ""); } 183 } else { 184 newXPDLs.put(destination.getName(), ""); modified = true; 186 } 187 if (modified) { 188 modifiedXPDLs.put(destination.getName(), ""); FileOutputStream outputStream = new FileOutputStream (destination); 190 outputStream.write(bytes); 191 outputStream.flush(); 192 outputStream.close(); 193 } 194 } catch (FileNotFoundException e) { 195 error(Messages.getErrorString("SharkPublisher.ERROR_0001_COULD_NOT_COPY_MISSING_FILE", xpdlFile.getName())); e.printStackTrace(); 198 } catch (IOException e) { 199 error(Messages.getErrorString("SharkPublisher.ERROR_0002_COULD_NOT_COPY_FILE", xpdlFile.getName())); e.printStackTrace(); 201 } 202 203 } 204 205 private void cleanupExternalRepository() { 206 207 File externalRepositoryDirectory = new File (shark.getExternalRepositoryPath()); 208 if (!externalRepositoryDirectory.exists()) { 209 error(Messages.getErrorString("SharkPublisher.ERROR_0003_EXTERNAL_REPOSITORY_DOES_NOT_EXIST")); return; 211 } 212 213 File files[] = externalRepositoryDirectory.listFiles(); 214 HashMap packagesInUse = new HashMap (); 215 if (files != null) { 216 for (int i = 0; i < files.length; i++) { 217 File file = files[i]; 218 String fileName = file.getName(); 219 if (!fileName.toLowerCase().endsWith(".xpdl")) { deleteFile(file); 222 } else { 223 225 if (!unmodifiedXPDLs.containsKey(fileName) && !modifiedXPDLs.containsKey(fileName) && !newXPDLs.containsKey(fileName)) { 226 227 try { 228 shark.unloadPackage(fileName); 229 if (!shark.isPackageLoaded(fileName)) 230 deleteFile(file); 231 } catch (PackageHasActiveProcesses pe) { 232 warn(Messages.getErrorString("SharkPublisher.ERROR_0004_UNLOAD_FAIL_PROCESSES_RUNNING", fileName)); } catch (PackageInUse pue) { 234 warn(Messages.getErrorString("SharkPublisher.ERROR_0005_UNLOAD_FAILED_DUE_TO_REFERENCES", fileName)); packagesInUse.put(fileName, file); 236 } catch (BaseException e) { 237 error(Messages.getErrorString("SharkPublisher.ERROR_0006_UNLOAD_FAILED", fileName), e); } 239 } 240 } 241 } 242 } 243 244 for (Iterator it = packagesInUse.keySet().iterator(); it.hasNext();) { 248 String fileName = (String ) it.next(); 249 try { 250 shark.unloadPackage(fileName); 251 if (!shark.isPackageLoaded(fileName)) 252 deleteFile((File ) packagesInUse.get(fileName)); 253 } catch (Exception e) { 254 error(Messages.getErrorString("SharkPublisher.ERROR_0006_UNLOAD_FAILED", fileName), e); } 256 } 257 258 } 259 260 private void deleteFile(File file) { 261 if (file.isDirectory()) { 262 File files[] = file.listFiles(); 263 for (int i = 0; i < files.length; i++) { 264 deleteFile(files[i]); 265 } 266 } 267 268 if (file.delete()) { 269 info(Messages.getString("SharkPublisher.INFO_DELETING_FILE", file.getAbsolutePath())); } else { 271 warn(Messages.getString("SharkPublisher.WARN_COULD_NOT_DELETE_FILE", file.getAbsolutePath())); } 273 } 274 275 private void addXpdlFiles(HashMap xpdlFileList, RepositoryMgr repository, PackageAdministration packageAdmin) { 276 277 Iterator fileListIterator = xpdlFileList.keySet().iterator(); 278 while (fileListIterator.hasNext()) { 279 String xpdlFileName = (String ) fileListIterator.next(); 280 try { 281 String pkgId = repository.getPackageId(xpdlFileName); 282 if (!packageAdmin.isPackageOpened(pkgId)) { 283 packageAdmin.openPackage(xpdlFileName); 284 } 285 } catch (Exception e) { 286 error(Messages.getErrorString("SharkPublisher.ERROR_0007_COULD_NOT_ADD_FILE", xpdlFileName)); e.printStackTrace(); 288 } 289 } 290 291 } 292 293 private void updateXpdlFiles(HashMap xpdlFileList, RepositoryMgr repository, PackageAdministration packageAdmin) { 294 295 Iterator fileListIterator = xpdlFileList.keySet().iterator(); 296 while (fileListIterator.hasNext()) { 297 String xpdlFileName = (String ) fileListIterator.next(); 298 try { 299 String pkgId = repository.getPackageId(xpdlFileName); 300 if (pkgId == null) { 301 if (!packageAdmin.isPackageOpened(pkgId)) { 302 packageAdmin.openPackage(xpdlFileName); 303 } 304 305 } else { 306 packageAdmin.updatePackage(pkgId, xpdlFileName); 307 } 308 } catch (Exception e) { 309 error(Messages.getErrorString("SharkPublisher.ERROR_0008_COULD_NOT_UPDATE_FILE", xpdlFileName)); e.printStackTrace(); 311 } 312 } 313 314 } 315 } 316 | Popular Tags |