/*
* Copyright (c) 1998-2010 Jitterbit, Inc.
*
*/
package org.jitterbit.integration.client.wsdl.cache;
import java.io.File;
import org.apache.commons.codec.binary.Hex;
import org.jitterbit.application.cache.AbstractTextFileCache;
import org.jitterbit.application.cache.DataCache;
import org.jitterbit.application.cache.DataCacheException;
import org.jitterbit.application.cache.TextFileCache;
/**
* Class that implements a cache of WSDL files that have been downloaded to the client from the
* server.
* <p>
* <strong>Note:</strong> This class is thread-safe.
*
* @author Torgil Zethson
*
*/
public class WsdlFileCache implements DataCache {
public static final String CACHE_ID = "WsdlFileCache";
private final TextFileCache cacheImpl;
/**
* Creates a new <code>WsdlFileCache</code> instance in the specified directory.
* <p>
* This constructor is package private - instances of <code>WsdlFileCache</code> must be created
* through the <code>WsdlFileCacheCollection</code>.
*
* @param parentDir
* the directory in which the cached information will be stored.
*/
public WsdlFileCache(File parentDir) {
this.cacheImpl = new AbstractTextFileCache(CACHE_ID, parentDir, true) {
@Override
protected String getFileName(Object id) {
return id + ".wsdl";
}
};
}
@Override
public String getID() {
return cacheImpl.getID();
}
/**
* Stores the specified WSDL file in the cache.
*
* @param wsdlLocator
* the WSDL file locator. This is either a file name or a URL.
* @param contents
* a byte array holding the contents of the WSDL file.
* @throws DataCacheException
* if the WSDL file could not be cached successfully.
*/
public synchronized void store(String wsdlLocator, byte[] contents) throws DataCacheException {
cacheImpl.store(translateLocator(wsdlLocator), contents);
}
/**
* Checks if this cache contains the specified WSDL file.
*
* @param wsdlLocator
* the WSDL file locator. This is either a file name or a URL.
* @return <code>true</code> iff this cache contains the specified WSDL file.
*/
public synchronized boolean contains(String wsdlLocator) {
return cacheImpl.contains(translateLocator(wsdlLocator));
}
/**
* Retrieves the specifid WSDL file from this cache.
*
* @param wsdlLocator
* the WSDL file locator. This is either a file name or a URL.
* @return a byte array holding the contents of the WSDL file, or <code>null</code> if this
* cache does not contain the specified WSDL file.
* @throws DataCacheException
* if this cache contains the requested WSDL file, but the file's contents could not be
* retrieved.
*/
public synchronized byte[] retrieve(String wsdlLocator) throws DataCacheException {
return cacheImpl.retrieveAsBytes(translateLocator(wsdlLocator));
}
private static String translateLocator(String locator) {
return new String(Hex.encodeHex(locator.getBytes()));
}
}