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.core.eventlog; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fujitsu.dc.core.DcCoreConfig; import com.fujitsu.dc.core.DcCoreException; import com.fujitsu.dc.core.event.EventUtils; import com.fujitsu.dc.core.model.Cell; /** * Archive?????. */ public class ArchiveLogCollection { static Logger log = LoggerFactory.getLogger(ArchiveLogCollection.class); private long created; private long updated; private String url; private String directoryPath; private List<ArchiveLogFile> archivefileList = new ArrayList<ArchiveLogFile>();; /** * . * @param cell ??Cell * @param uriInfo ?URL */ public ArchiveLogCollection(Cell cell, UriInfo uriInfo) { // archive???????? // ????????????????????createFileInformation??? this.created = cell.getPublished(); this.updated = cell.getPublished(); // archive?URL? StringBuilder urlSb = new StringBuilder(); UriBuilder uriBuilder = uriInfo.getBaseUriBuilder(); uriBuilder.scheme(DcCoreConfig.getUnitScheme()); urlSb.append(uriBuilder.build().toASCIIString()); urlSb.append(uriInfo.getPath()); this.url = urlSb.toString(); StringBuilder archiveDirName = EventUtils.getEventLogDir(cell.getId(), cell.getOwner()).append("archive"); this.directoryPath = archiveDirName.toString(); } /** * ????. */ public void createFileInformation() { File archiveDir = new File(this.directoryPath); // ??????????????????? if (!archiveDir.exists()) { return; } File[] fileList = archiveDir.listFiles(); for (File file : fileList) { // ?? long fileUpdated = file.lastModified(); // ?????? if (this.updated < fileUpdated) { this.updated = fileUpdated; } BasicFileAttributes attr = null; ZipFile zipFile = null; long fileCreated = 0L; long size = 0L; try { // ??? attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class); fileCreated = attr.creationTime().toMillis(); // ????API??????????????????? zipFile = new ZipFile(file); Enumeration<? extends ZipEntry> emu = zipFile.entries(); while (emu.hasMoreElements()) { ZipEntry entry = (ZipEntry) emu.nextElement(); if (null == entry) { log.info("Zip file entry is null."); throw DcCoreException.Event.ARCHIVE_FILE_CANNOT_OPEN; } size += entry.getSize(); } } catch (ZipException e) { log.info("ZipException", e); throw DcCoreException.Event.ARCHIVE_FILE_CANNOT_OPEN; } catch (IOException e) { log.info("IOException", e); throw DcCoreException.Event.ARCHIVE_FILE_CANNOT_OPEN; } finally { IOUtils.closeQuietly(zipFile); } // ??????API???????????????(.zip)?????? String fileName = file.getName(); String fileNameWithoutZip = fileName.substring(0, fileName.length() - ".zip".length()); String fileUrl = this.url + "/" + fileNameWithoutZip; ArchiveLogFile archiveFile = new ArchiveLogFile(fileCreated, fileUpdated, size, fileUrl); this.archivefileList.add(archiveFile); log.info(String.format("filename:%s created:%d updated:%d size:%d", file.getName(), fileCreated, fileUpdated, size)); } } /** * ??. * @return ? */ public long getCreated() { return created; } /** * ?. * @return */ public long getUpdated() { return updated; } /** * URL?. * @return URL */ public String getUrl() { return url; } /** * ???. * @return URL */ public String getDirectoryPath() { return directoryPath; } /** * ???. * @return Archivefile? */ public List<ArchiveLogFile> getArchivefileList() { return archivefileList; } }