Java tutorial
/* * cBean Copyright 2016, Tom Everett <tom@khubla.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.khubla.cbean.kvservice.fskv; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.commons.pool.ObjectPool; import org.apache.commons.pool.impl.GenericObjectPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.khubla.cbean.CBeanKey; import com.khubla.cbean.kvservice.KVService; import com.khubla.cbean.kvservice.KVServiceException; import com.khubla.cbean.url.CBeanUrl; public class FSKVService implements KVService { /** * logger */ private static final Logger logger = LoggerFactory.getLogger(FSKVService.class); /** * max connections */ private static final int MAX_STORAGESERVICE_CONNECTIONS = 5; /** * pool of StorageService connections */ private final ObjectPool<File> fileClientPool; /** * ctor */ public FSKVService(CBeanUrl cBeanUrl) { final String rootFS = cBeanUrl.getHostName() + File.separator + cBeanUrl.getClusterName() + File.separator; File dir = new File(rootFS); dir.mkdirs(); logger.info("FSKV data is at '" + dir.getAbsolutePath() + "'"); fileClientPool = new GenericObjectPool<File>(new FSPoolableObjectFactoryImpl(rootFS), MAX_STORAGESERVICE_CONNECTIONS); } @Override public void delete(CBeanKey cBeanKey) throws KVServiceException { File fileSystem = null; File file = null; try { /* * get a pooled connection */ fileSystem = fileClientPool.borrowObject(); /* * file */ file = getFile(fileSystem, cBeanKey.getKey()); if (file.exists()) { file.delete(); } logger.info("Deleted asset '" + cBeanKey.getKey() + "'"); } catch (final Exception e) { logger.info( "Unable to delete asset '" + cBeanKey.getKey() + "' at path '" + file.getAbsolutePath() + "'"); throw new KVServiceException(e); } finally { try { if (null != fileSystem) { fileClientPool.returnObject(fileSystem); } } catch (final Exception e) { e.printStackTrace(); } } } /** * get a file for a url on a fs */ private File getFile(File fileSystem, String url) { final String filePath = fileSystem.getAbsolutePath() + File.separator + url; return new File(filePath); } @Override public String[] list(CBeanKey cBeanKey) throws KVServiceException { File fileSystem = null; try { /* * get a pooled connection */ fileSystem = fileClientPool.borrowObject(); /* * list */ String[] ret = null; if (true == fileSystem.isDirectory()) { final File[] lst = fileSystem.listFiles(); final List<String> l = new ArrayList<String>(); for (final File file : lst) { if (false == file.isHidden()) { if (file.isDirectory()) { l.add(file.getName() + "/"); } else { l.add(file.getName()); } } } ret = new String[lst.length]; l.toArray(ret); } return ret; } catch (final Exception e) { throw new KVServiceException(e); } finally { try { if (null != fileSystem) { fileClientPool.returnObject(fileSystem); } } catch (final Exception e) { e.printStackTrace(); } } } @Override public byte[] load(CBeanKey cBeanKey) throws KVServiceException { File fileSystem = null; try { /* * get a pooled connection */ fileSystem = fileClientPool.borrowObject(); /* * query */ final File file = getFile(fileSystem, cBeanKey.getKey()); if (file.exists() && (false == file.isDirectory())) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(new FileInputStream(file), baos); logger.info("Loaded asset '" + cBeanKey.getKey() + "'"); return baos.toByteArray(); } logger.info( "Unable to load asset '" + cBeanKey.getKey() + "' at path '" + file.getAbsolutePath() + "'"); return null; } catch (final Exception e) { throw new KVServiceException(e); } finally { try { if (null != fileSystem) { fileClientPool.returnObject(fileSystem); } } catch (final Exception e) { e.printStackTrace(); } } } @Override public void save(CBeanKey cBeanKey, byte[] asset) throws KVServiceException { File fileSystem = null; File file = null; try { /* * get a pooled connection */ fileSystem = fileClientPool.borrowObject(); /* * copy file */ file = getFile(fileSystem, cBeanKey.getKey()); if (false == file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } FileOutputStream fileOs = null; try { fileOs = new FileOutputStream(file); final ByteArrayInputStream bois = new ByteArrayInputStream(asset); IOUtils.copy(bois, fileOs); } catch (final Exception e) { throw new KVServiceException(e); } finally { fileOs.flush(); fileOs.close(); } /* * done */ logger.info("Saved asset '" + cBeanKey.getKey() + "'"); } catch (final Exception e) { throw new KVServiceException(e); } finally { try { if (null != fileSystem) { fileClientPool.returnObject(fileSystem); } } catch (final Exception e) { e.printStackTrace(); logger.info("Failed to save asset '" + cBeanKey.getKey() + "' at path '" + file.getAbsolutePath() + "'"); } } } }