Java tutorial
/* * Copyright 2009 by pactera.edg.am Corporation. Address:HePingLi East Street No.11 * 5-5, BeiJing, * * All rights reserved. * * This software is the confidential and proprietary information of pactera.edg.am * Corporation ("Confidential Information"). You shall not disclose such * Confidential Information and shall use it only in accordance with the terms * of the license agreement you entered into with pactera.edg.am. */ package com.pactera.edg.am.metamanager.extractor.control.impl; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.pactera.edg.am.metamanager.core.util.DateUtil; import com.pactera.edg.am.metamanager.extractor.adapter.mapping.IMetadataMappingService; import com.pactera.edg.am.metamanager.extractor.bo.ExtractorLogLevel; import com.pactera.edg.am.metamanager.extractor.bo.mappingload.Operation; import com.pactera.edg.am.metamanager.extractor.bo.mm.AppendMetadata; import com.pactera.edg.am.metamanager.extractor.control.IExtractorService; import com.pactera.edg.am.metamanager.extractor.increment.IIncrementAnalysisService; import com.pactera.edg.am.metamanager.extractor.load.IMetadataLoaderService; import com.pactera.edg.am.metamanager.extractor.util.AdapterExtractorContext; import com.pactera.edg.am.metamanager.extractor.util.AntZip; import com.pactera.edg.am.metamanager.extractor.util.ExtractorContextLoader; /** * ???????? * * @author user * @version 1.0 Date: Sep 27, 2009 * */ public class ExtractorServiceImpl implements IExtractorService { private static Log log = LogFactory.getLog(ExtractorServiceImpl.class); /** * ? */ private IMetadataMappingService mapper; /* * (non-Javadoc) * * @see com.pactera.edg.am.metamanager.extractor.control.IExtractorService#extract() */ public void extract() throws Throwable { try { AdapterExtractorContext.getInstance().setIClassifier(ExtractorContextLoader.getMMRmiServer()); boolean extractSuccess = false; if (AdapterExtractorContext.getInstance().isNeedMultiExtract()) { // ?? extractSuccess = multiExtract(); } else { extractSuccess = singleExtract(null); } if (!extractSuccess) { // ???, throw new Exception("?!"); } // ??,?? batchDelete(); } finally { // ((IMetadataLoaderService) // ExtractorContextLoader.getBean(IMetadataLoaderService.SPRING_NAME)) // .afterOperation(); AdapterExtractorContext.getInstance().multiExtractClear(); } } public static void batchDelete() throws Throwable { IIncrementAnalysisService incrementAnalyzer = (IIncrementAnalysisService) ExtractorContextLoader .getBean(IIncrementAnalysisService.FULL_INCREMENT_SPRING_NAME); Map<Operation, AppendMetadata> aMetadatas = incrementAnalyzer .incrementAnalysis(AdapterExtractorContext.getInstance().getAMetadata()); IMetadataLoaderService loader = (IMetadataLoaderService) ExtractorContextLoader .getBean(IMetadataLoaderService.SPRING_NAME); if (AdapterExtractorContext.getInstance().isNeedAudit()) { // ?,?? loader.batchDeleteAudit(aMetadatas); loader.batchUpdateOldAuditDatas(); } else { // ??,???? loader.batchDeleteMd(aMetadatas); } } /** * ? * * @param incrementAnalyzer * @throws Throwable */ private boolean multiExtract() throws Throwable { File dsDirectorys = new File(AdapterExtractorContext.getInstance().getDsDirectory()); if (!dsDirectorys.exists()) { // ?, AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, "???:" + AdapterExtractorContext.getInstance().getDsDirectory()); throw new FileNotFoundException( "???:" + AdapterExtractorContext.getInstance().getDsDirectory()); } return multiExtract(dsDirectorys); } /** * * * @param dsDirectorys * @param incrementAnalyzer * @throws Throwable */ private boolean multiExtract(File dsDirectorys) throws Throwable { String s1 = new StringBuilder("?").append(dsDirectorys.getAbsolutePath()).toString(); log.info(s1); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, s1); File[] dsFiles = dsDirectorys.listFiles(); // ()??? Arrays.sort(dsFiles, new Comparator<File>() { public int compare(File f1, File f2) { return f1.getName().compareTo(f2.getName()); } }); // ??,???; boolean multiExtractSuccess = false, singleExtractSuccess = false; int totalCount = dsFiles.length; for (int i = 0; i < totalCount; i++) { if (dsFiles[i].isDirectory()) { multiExtract(dsFiles[i]);// } else if (dsFiles[i].getName().toUpperCase().endsWith(".ZIP")) {// ? // ? singleExtractSuccess = this.zipExtract(dsFiles[i]); if (!multiExtractSuccess) { // ???,? multiExtractSuccess = singleExtractSuccess; } } else { // ? AdapterExtractorContext.getInstance().setDsAbsolutePath(dsFiles[i].getAbsolutePath()); String s = new StringBuilder("?").append(i + 1).append("") .append(totalCount).append(":").append(dsFiles[i].getAbsolutePath()) .toString(); log.info(s); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, s); // ? singleExtractSuccess = singleExtract(dsFiles[i].getAbsolutePath()); if (!multiExtractSuccess) { // ???,? multiExtractSuccess = singleExtractSuccess; } } } return multiExtractSuccess; } /** * * * @param zipFile * @param incrementAnalyzer * @return * @throws Throwable */ private boolean zipExtract(File zipFile) throws Throwable { // ,?/currenttime String extPlace = new StringBuilder(zipFile.getParent()).append(File.separator) .append(System.currentTimeMillis()).toString(); File file = new File(extPlace); String s2 = new StringBuilder("").append(zipFile.getAbsolutePath()) .append("").append(file.getAbsolutePath()).toString(); log.info(s2); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, s2); // file.mkdir(); // extZipFileList(zipFile, extPlace); // boolean zipExtractSuccess = multiExtract(file); // try { FileUtils.deleteDirectory(file); } catch (IOException e) { log.error("!", e); } return zipExtractSuccess; } public void extZipFileList(File zipFile, String extPlace) { AntZip.unZip(zipFile.getAbsolutePath(), extPlace); // try { // // ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile)); // // ZipEntry entry = null; // // while ((entry = in.getNextEntry()) != null) { // // String entryName = entry.getName(); // // if (entry.isDirectory()) { // File file = new File(new // StringBuilder(extPlace).append(File.separator).append(entryName) // .toString()); // file.mkdirs(); // log.info(":" + entryName); // } // else { // // FileOutputStream os = new FileOutputStream(new // StringBuilder(extPlace).append(File.separator) // .append(entryName).toString()); // // // Transfer bytes from the ZIP file to the output file // byte[] buf = new byte[1024]; // // int len; // while ((len = in.read(buf)) > 0) { // os.write(buf, 0, len); // } // os.close(); // in.closeEntry(); // // } // } // // } // catch (IOException e) { // e.printStackTrace(); // } } /** * ?? * * @param incrementAnalyzer * @throws Throwable */ private boolean singleExtract(String path) { Map<Operation, AppendMetadata> aMetadatas = null; try { IIncrementAnalysisService incrementAnalyzer = (IIncrementAnalysisService) ExtractorContextLoader .getBean(IIncrementAnalysisService.INCREMENT_SPRING_NAME); String logMsg = "???...\n:" + DateUtil.getFormatTime(System.currentTimeMillis(), DateUtil.DATA_FORMAT); log.info(logMsg); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, logMsg); //? AppendMetadata aMetadata = AdapterExtractorContext.getInstance().getAMetadata().cloneAppendMetadata(); mapper.metadataMapping(aMetadata); //??? execute(incrementAnalyzer, aMetadata); return true; } catch (Throwable e) { if (path != null) AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, new StringBuilder(":").append(path).append("!").toString()); log.error("", e); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, "" + e.getMessage()); // NULL return false; } finally { // ((IMetadataLoaderService) // ExtractorContextLoader.getBean(IMetadataLoaderService.SPRING_NAME)) // .afterSingleOperation(aMetadatas); cleanScene(aMetadatas); } } public static void execute(IIncrementAnalysisService incrementAnalyzer, AppendMetadata aMetadata) throws Throwable { Map<Operation, AppendMetadata> aMetadatas = null; String logMsg = ""; logMsg = "???!\n?:" + DateUtil.getFormatTime(System.currentTimeMillis(), DateUtil.DATA_FORMAT); log.info(logMsg); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, logMsg); log.info("?..."); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, "?..."); aMetadatas = incrementAnalyzer.incrementAnalysis(aMetadata); logMsg = "???,?:" + DateUtil.getFormatTime(System.currentTimeMillis(), DateUtil.DATA_FORMAT); log.info(logMsg); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, logMsg); log.info("?..."); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, "?..."); // ? AdapterExtractorContext.getInstance().genGlobalTime(); batchCreateOrUpdate(aMetadatas); // ?? deleteAmData(aMetadata.getMetadata().getId(), aMetadata.getDeleteMapDatas()); logMsg = "??:" + DateUtil.getFormatTime(System.currentTimeMillis(), DateUtil.DATA_FORMAT) + "??..."; log.info(logMsg); AdapterExtractorContext.getInstance().getIsCover().clear(); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, logMsg); } private static void deleteAmData(String rootId, Map<String, List<Map<String, String>>> deleteMapDatas) { if (deleteMapDatas.size() > 0) { IMetadataLoaderService loader = (IMetadataLoaderService) ExtractorContextLoader .getBean(IMetadataLoaderService.SPRING_NAME); if (!AdapterExtractorContext.getInstance().isNeedAudit()) { // ? loader.deleteData(rootId, deleteMapDatas); } } } /** * ?? * * @param metadatas */ private void cleanScene(Map<Operation, AppendMetadata> metadatas) { AdapterExtractorContext.getInstance().singleExtractClear(); } /** * ? * * @param aMetadatas * ? */ private static void batchCreateOrUpdate(Map<Operation, AppendMetadata> aMetadatas) throws Throwable { IMetadataLoaderService loader = (IMetadataLoaderService) ExtractorContextLoader .getBean(IMetadataLoaderService.SPRING_NAME); if (AdapterExtractorContext.getInstance().isNeedAudit()) { // ?, loader.batchCreateOrUpdateAudit(aMetadatas); if (!AdapterExtractorContext.getInstance().isFullIncrementCompare()) { // ?? loader.batchUpdateOldAuditDatas(); } } else { // ?,? loader.batchCreateOrUpdateMd(aMetadatas); } } public void setMapper(IMetadataMappingService mapper) { this.mapper = mapper; } }