Java tutorial
/* * Copyright 2012 Alibaba.com All right reserved. This software is the * confidential and proprietary information of Alibaba.com ("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 Alibaba.com. */ package com.taobao.tanggong; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.io.IOUtils; import com.alibaba.fastjson.JSON; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; /** * DataFilePersisterImpl.java??TODO ?? * * @author jingjing.zhijj 2012-12-16 ?5:25:30 */ public class DataFilePersisterImpl implements LifeCycle { private ServerConfiguration serverConfiguration; private File dataDirectory; private Multimap<String, String> uri2HashCodes = HashMultimap.create(); public void init() throws TangException { this.dataDirectory = new File(this.serverConfiguration.getBaseDirectory(), "data"); if (!dataDirectory.exists()) { this.dataDirectory.mkdirs(); } this.loadUris(); } public Collection<String> getHashs(String uri) { return this.uri2HashCodes.get(uri); } public void destory() throws TangException { // TODO Auto-generated method stub } private void loadUris() { if (null != this.dataDirectory) { File url2HashMapFile = new File(this.dataDirectory, "index.txt"); if (url2HashMapFile.exists()) { List<String> dburis = new ArrayList<String>(); InputStream inputStream = null; try { inputStream = new FileInputStream(url2HashMapFile); List<String> uris = IOUtils.readLines(inputStream); if (null != uris) { for (String uri : uris) { dburis.add(uri); } } for (String uri : dburis) { String[] kv = uri.split("="); if (kv.length == 2) { String[] vs = kv[1].split(","); for (String v : vs) { this.uri2HashCodes.put(kv[0], v); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { IOUtils.closeQuietly(inputStream); } } } } private void saveHash2UrlMap() { if (null != dataDirectory && !this.uri2HashCodes.isEmpty()) { File url2HashMapFile = new File(this.dataDirectory, "index.txt"); File url2HashMapBackupFile = new File(this.dataDirectory, "index.txt.bak"); if (url2HashMapFile.exists()) { url2HashMapFile.renameTo(url2HashMapBackupFile); } OutputStream output = null; try { output = new FileOutputStream(url2HashMapFile); StringBuilder sb = new StringBuilder(); for (String k : this.uri2HashCodes.keySet()) { sb.append(k).append("="); Collection<String> vs = this.uri2HashCodes.get(k); if (!vs.isEmpty()) { for (String v : vs) { sb.append(v).append(","); } sb.delete(sb.length() - 1, sb.length()); sb.append("\n"); } } IOUtils.write(sb.toString(), output); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { IOUtils.closeQuietly(output); } } } public void save(TangRequest r, List<TangRequest> data) { if (null != dataDirectory) { File dataFile = new File(this.dataDirectory, r.md5hash()); OutputStream output = null; try { output = new FileOutputStream(dataFile); IOUtils.write(JSON.toJSONString(data), output); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { IOUtils.closeQuietly(output); } this.uri2HashCodes.put(r.getUri(), r.md5hash()); this.saveHash2UrlMap(); } } public String get(String filename) { if (null != dataDirectory) { File dataFile = new File(this.dataDirectory, filename); if (!dataFile.exists()) { return null; } InputStream inputStream = null; try { inputStream = new FileInputStream(dataFile); return IOUtils.toString(inputStream); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { IOUtils.closeQuietly(inputStream); } } return null; } public void setServerConfiguration(ServerConfiguration serverConfiguration) { this.serverConfiguration = serverConfiguration; } }