Java tutorial
/** * personium.io * Copyright 2014 FUJITSU LIMITED * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.fujitsu.dc.common.ads; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.IOUtils; /** * ADS???????. <br /> * ? singleton ??????<br /> * ???(PCS?)??????????????<br /> * ???ADS??????????????????? */ public final class RollingAdsWriteFailureLog extends AbstractAdsWriteFailureLog { private File rotatedAdsWriteFailureLog; private LineNumberReader reader; /** * ??. */ private RollingAdsWriteFailureLog() { super(); } /** * . * @param rotatedAdsWriteFailureLog ??ADS??? * @param baseDir * @param pcsVersion PCS? * @param physicalDelete ADS?????????default true */ public RollingAdsWriteFailureLog(File rotatedAdsWriteFailureLog, String baseDir, String pcsVersion, boolean physicalDelete) { this(); this.rotatedAdsWriteFailureLog = rotatedAdsWriteFailureLog; this.baseDir = baseDir; this.pcsVersion = pcsVersion; this.isPhysicalDelete = physicalDelete; } /** * ??ADS??????????. * @param pcsVersion PCS?? * @param rotatedAdsWriteFailureLog ??ADS??? * @return ????? * @throws AdsWriteFailureLogException ??ADS?????????????????????? */ public static long getCreatedTimeFromFileName(String pcsVersion, File rotatedAdsWriteFailureLog) throws AdsWriteFailureLogException { final String messageFormat = "Illegal Rotated adsWriteFailureLog file name format. [%s]"; if (!rotatedAdsWriteFailureLog.isFile()) { // ????????????? String message = String.format("Rotated adsWriteFailureLog is not found. [%s]", rotatedAdsWriteFailureLog.getAbsolutePath()); throw new AdsWriteFailureLogException(message); } String fileName = rotatedAdsWriteFailureLog.getName(); String fileFormat = String.format(LOGNAME_FORMAT_ROTATE, pcsVersion, 0); String filePattern = fileFormat.substring(0, fileFormat.length() - 1); long createdTime = -1L; if (fileName.startsWith(filePattern)) { String createdTimeStr = fileName.replace(filePattern, ""); try { createdTime = Long.parseLong(createdTimeStr); } catch (NumberFormatException e) { String message = String.format(messageFormat, rotatedAdsWriteFailureLog.getAbsolutePath()); if (fileName.endsWith(LOGICAL_DELETED_LOGNAME_SUFFIX)) { message = String.format("Logical deleted adsWriteFailureLog file. [%s]", rotatedAdsWriteFailureLog.getAbsolutePath()); } throw new AdsWriteFailureLogException(message); } } else { String message = String.format(messageFormat, rotatedAdsWriteFailureLog.getAbsolutePath()); throw new AdsWriteFailureLogException(message); } return createdTime; } /** * ??ADS????. * @throws AdsWriteFailureLogException ADS???????? */ public void openRotatedFile() throws AdsWriteFailureLogException { try { // default encoding(UTF-8) reader = new LineNumberReader(new BufferedReader(new FileReader(this.rotatedAdsWriteFailureLog))); } catch (FileNotFoundException e) { String messsage = String.format("Failed to open rotated adsWriteFailureLog. [%s]", rotatedAdsWriteFailureLog.getAbsolutePath()); throw new AdsWriteFailureLogException(messsage, e); } } /** * ??ADS????. */ public synchronized void closeRotatedFile() { if (null == reader) { logger.info("Acitve adsWriteFailureLog is not opened."); return; } IOUtils.closeQuietly(reader); } /** * ?????ADS????. * @param recordNum ? * @return ??? * @throws AdsWriteFailureLogException ADS?????????? */ public List<String> readAdsFailureLog(int recordNum) throws AdsWriteFailureLogException { try { if (null == this.reader) { reader = new LineNumberReader(new BufferedReader(new FileReader(this.rotatedAdsWriteFailureLog))); } List<String> logRecords = new ArrayList<String>(); for (int i = 0; i < recordNum; i++) { String aRecord = reader.readLine(); if (null == aRecord) { break; } logRecords.add(aRecord); } return logRecords; } catch (IOException e) { String messsage = String.format("Failed to read rotated adsWriteFailureLog. [%s]", rotatedAdsWriteFailureLog.getAbsolutePath()); throw new AdsWriteFailureLogException(messsage, e); } } /** * ??ADS????. <br /> * @throws AdsWriteFailureLogException ????? */ public synchronized void deleteRotatedLog() throws AdsWriteFailureLogException { closeRotatedFile(); super.deleteRotatedLog(rotatedAdsWriteFailureLog); } }