001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * 012 */ 013package org.nuxeo.ecm.core.convert.cache; 014 015import java.io.File; 016import java.io.IOException; 017import java.util.ArrayList; 018import java.util.List; 019 020import org.nuxeo.common.utils.Path; 021import org.nuxeo.ecm.core.api.Blob; 022import org.nuxeo.ecm.core.api.Blobs; 023import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 024import org.nuxeo.ecm.core.api.impl.blob.FileBlob; 025 026/** 027 * Cachable implementation of the {@link SimpleBlobHolder}. 028 * 029 * @author tiry 030 */ 031public class SimpleCachableBlobHolder extends SimpleBlobHolder implements CachableBlobHolder { 032 033 public SimpleCachableBlobHolder() { 034 } 035 036 public SimpleCachableBlobHolder(Blob blob) { 037 super(blob); 038 } 039 040 public SimpleCachableBlobHolder(List<Blob> blobs) { 041 super(blobs); 042 } 043 044 public SimpleCachableBlobHolder(String path) { 045 super(new FileBlob(new File(path))); 046 } 047 048 @Override 049 public void load(String path) throws IOException { 050 blobs = new ArrayList<Blob>(); 051 File base = new File(path); 052 if (base.isDirectory()) { 053 addDirectoryToList(base, ""); 054 } else { 055 File file = new File(path); 056 Blob mainBlob = Blobs.createBlob(file); 057 mainBlob.setFilename(file.getName()); 058 blobs.add(mainBlob); 059 } 060 061 orderIndexPageFirst(blobs); 062 } 063 064 public void addDirectoryToList(File directory, String prefix) throws IOException { 065 File[] directoryContent = directory.listFiles(); 066 for (File file : directoryContent) { 067 if (file.isDirectory()) { 068 int beginIndex = prefix.length(); 069 prefix = prefix + file.getName() + File.separatorChar; 070 addDirectoryToList(file, prefix); 071 prefix = prefix.substring(0, beginIndex); 072 } else { 073 Blob blob = Blobs.createBlob(file); 074 blob.setFilename(prefix + file.getName()); 075 if (file.getName().equalsIgnoreCase("index.html")) { 076 blobs.add(0, blob); 077 } else { 078 blobs.add(blob); 079 } 080 } 081 } 082 } 083 084 @Override 085 public String persist(String basePath) throws IOException { 086 if (blobs == null || blobs.isEmpty()) { 087 return null; 088 } 089 Path path = new Path(basePath); 090 path = path.append(getHash()); 091 if (blobs.size() == 1) { 092 File file = new File(path.toString()); 093 getBlob().transferTo(file); 094 return file.getAbsolutePath(); 095 } else { 096 File dir = new File(path.toString()); 097 dir.mkdir(); 098 for (Blob blob : blobs) { 099 File file = new File(path.append(blob.getFilename()).toString()); 100 blob.transferTo(file); 101 } 102 return dir.getAbsolutePath(); 103 } 104 } 105 106 /** 107 * Rearrange blobs to have smallest index.html page as the first blob. 108 * 109 * @since 5.7.1 110 */ 111 protected void orderIndexPageFirst(List<Blob> blobs) { 112 Blob indexPage = null; 113 for (Blob blob : blobs) { 114 if (blob.getFilename().contains("index.html") 115 && (indexPage == null || blob.getFilename().compareTo(indexPage.getFilename()) < 0)) { 116 indexPage = blob; 117 } 118 } 119 120 if (indexPage != null) { 121 blobs.remove(indexPage); 122 blobs.add(0, indexPage); 123 } 124 } 125 126}