Commit 7f171406 authored by Matt Tucker's avatar Matt Tucker Committed by matt

No longer needed.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@43 b35dd754-fafc-0310-a699-88a17e54d16e
parent c7660169
/*
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 1999-2003 CoolServlets, Inc. All rights reserved.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package org.jivesoftware.util;
import org.jivesoftware.messenger.auth.UnauthorizedException;
/**
* Interface for querying and setting cache configuration information.
* Since there will be several active caches within a single server, caches can be
* given names (hopefully with some relationship to the the client using the
* cache) for display to administrators.<p>
*
* This interface is separated from the cache so that objects can update
* cache configuration separate from the cache itself. (Allowing it to delay
* changes to the cache to times best suited to its own performance.) Alternatively,
* objects can update the cache directly since it extends the CacheInfo interface.
*
* @author Iain Shigeoka
*/
public interface CacheInfo {
/**
* <p>Obtains the name of this cache.</p>
* <p>The name is completely arbitrary and used only for
* display to administrators. However, it should have some
* relationship to the primary user of the cache.</p>
*
* @return the name of this cache.
*/
public String getName();
/**
* <p>Obtain the maximum size of the cache.</p>
* <p>If the cache grows larger
* than the max size, the least frequently used items will be removed. If
* the max cache size is set to -1, there is no size limit.</p>
*
* @return The maximum size of the cache (-1 indicates unlimited max size)
* <p/>
*
*/
public int getMaxCacheSize();
/**
* Sets the maximum size of the cache. If the cache grows larger
* than the max size, the least frequently used items will be removed. If
* the max cache size is set to -1, there is no size limit.
*
* @param size The maximum size of this cache (-1 indicates unlimited max size)
* @throws UnauthorizedException If there is insufficient permissions to adjust this setting
*/
public void setMaxCacheSize(int size) throws UnauthorizedException;
/**
* Returns the maximum number of milleseconds that any object can live
* in cache. Once the specified number of milleseconds passes, the object
* will be automatically expried from cache. If the max lifetime is set
* to -1, then objects never expire.
*
* @return the maximum number of milleseconds before objects are expired.
*/
public long getMaxLifetime();
/**
* Sets the maximum number of milleseconds that any object can live
* in cache. Once the specified number of milleseconds passes, the object
* will be automatically expried from cache. If the max lifetime is set
* to -1, then objects never expire.
*
* @param maxLifetime the maximum number of milleseconds before objects are expired.
* @throws UnauthorizedException If there is insufficient permissions to adjust this setting
*/
public void setMaxLifetime(long maxLifetime) throws UnauthorizedException;
/**
* Returns the size of the cache contents in bytes. This value is only a
* rough approximation, so cache users should expect that actual VM
* memory used by the cache could be significantly higher than the value
* reported by this method.
*
* @return the size of the cache contents in bytes.
*/
public int getCacheSize();
/**
* Returns the number of cache hits. A cache hit occurs every
* time the get method is called and the cache contains the requested
* object.<p>
*
* Keeping track of cache hits and misses lets one measure how efficient
* the cache is; the higher the percentage of hits, the more efficient.
*
* @return the number of cache hits.
*/
public long getCacheHits();
/**
* Returns the number of cache misses. A cache miss occurs every
* time the get method is called and the cache does not contain the
* requested object.<p>
*
* Keeping track of cache hits and misses lets one measure how efficient
* the cache is; the higher the percentage of hits, the more efficient.
*
* @return the number of cache hits.
*/
public long getCacheMisses();
}
/*
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package org.jivesoftware.util;
import org.jivesoftware.util.LinkedListNode;
/**
* Wrapper for all objects put into cache. It's primary purpose is to maintain
* references to the linked lists that maintain the creation time of the object
* and the ordering of the most used objects.
* <p/>
* This class is optimized for speed rather than strictly correct encapsulation.
*
* @author Jive Software
*/
public final class CacheObject {
/**
* Underlying object wrapped by the CacheObject.
*/
public Object object;
/**
* The size of the Cacheable object. The size of the Cacheable
* object is only computed once when it is added to the cache. This makes
* the assumption that once objects are added to cache, they are mostly
* read-only and that their size does not change significantly over time.
*/
public int size;
/**
* A reference to the node in the cache order list. We keep the reference
* here to avoid linear scans of the list. Every time the object is
* accessed, the node is removed from its current spot in the list and
* moved to the front.
*/
public LinkedListNode lastAccessedListNode;
/**
* A reference to the node in the age order list. We keep the reference
* here to avoid linear scans of the list. The reference is used if the
* object has to be deleted from the list.
*/
public LinkedListNode ageListNode;
/**
* A count of the number of times the object has been read from cache.
*/
public int readCount = 0;
/**
* Creates a new cache object wrapper. The size of the Cacheable object
* must be passed in in order to prevent another possibly expensive
* lookup by querying the object itself for its size.<p>
*
* @param object the underlying Object to wrap.
* @param size the size of the Cachable object in bytes.
*/
public CacheObject(Object object, int size) {
this.object = object;
this.size = size;
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
/*
* @(#)URLUtils.java
*
* Copyright 2003-2004 by Jive Software,
* 135 W 29th St, Suite 802, New York, NY 10001, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Jive Software.
*/
package org.jivesoftware.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* <code>URLUtils</code> class handles most cases when using URL's.
*
* @author Derek DeMoro
* @version 1.0, 04/21/2004
*/
public class URLUtils {
private URLUtils() {
}
/**
* Copy a give inputStream to given outputstream.
*/
private static void copy(InputStream in, OutputStream out) throws IOException {
final byte[] buffer = new byte[4096];
while (true) {
final int bytesRead = in.read(buffer);
if (bytesRead < 0) {
break;
}
out.write(buffer, 0, bytesRead);
}
}
/**
* Returns a suffix(if any) of a url.
*
* @param url the url to retrieve the suffix from.
* @return suffix of the given url, null if no suffix is found.
*/
public static String getSuffix(URL url) {
final String path = url.getPath();
int lastDot = path.lastIndexOf('.');
return (lastDot >= 0) ? path.substring(lastDot) : "";
}
/**
* Copies the contents at <CODE>source</CODE> to <CODE>destination</CODE>.
*/
public static void copyURL(URL source, File destination) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = source.openStream();
out = new FileOutputStream(destination);
destination.mkdirs();
copy(in, out);
}
finally {
try {
if (in != null) in.close();
if (out != null) out.close();
}
catch (IOException e) {
}
}
}
/**
* Returns the canonical form of the <code>URL</code>
*
* @param url the url to retrieve the canoncial form from.
* @return the canoncical form of a url.
*/
public URL canonicalize(URL url) {
return url;
}
/**
* Checks to see if the URL can be read.
*
* @param url the url to read from.
* @return true if the URL can be read, false otherwise.
*/
public boolean canRead(URL url) {
try {
final URLConnection urlConnection = url.openConnection();
return urlConnection.getDoInput();
}
catch (Exception e) {
return false;
}
}
/**
* Checks to see if the URL can be written to.
*
* @param url the url to write to.
* @return true if the url can be written to.
*/
public boolean canWrite(URL url) {
try {
final URLConnection urlConnection = url.openConnection();
return urlConnection.getDoOutput();
}
catch (Exception e) {
return false;
}
}
/**
* Checks to see if the resource at the given url can be created.
*
* @param url the url to check if creation is possible.
* @return true if the resource can be created.
*/
public boolean canCreate(URL url) {
return true;
}
/**
* Tests to see if the URL is valid.
*
* @param url the url to test.
* @return true if the url is valid.
*/
public boolean isValid(URL url) {
if (exists(url)) {
return true;
}
return canCreate(url);
}
/**
* Tests to see if the resource at the given <code>URL</code> is valid.
*
* @param url the url to check.
* @return true if the resource at the given <code>URL</code> exists.
*/
public static boolean exists(URL url) {
return toFile(url).exists();
}
/**
* Creates directory(s) at the given <code>URL</code>
*
* @param url the url where the directory(s) should be made.
* @return true if the directory(s) were created.
*/
public static boolean mkdirs(URL url) {
final File file = toFile(url);
if (!file.exists()) {
return file.mkdirs();
}
return true;
}
/**
* Returns the name of the resource at a given <code>URL</code>
*
* @param url the url.
* @return the filename of the url.
*/
public static String getFileName(URL url) {
if (url == null) {
return "";
}
final String path = url.getPath();
if (path.equals("/")) {
return "/";
}
final int lastSep = path.lastIndexOf('/');
if (lastSep == path.length() - 1) {
final int lastSep2 = path.lastIndexOf('/', lastSep - 1);
return path.substring(lastSep2 + 1, lastSep);
}
else {
return path.substring(lastSep + 1);
}
}
/**
* Returns the numbers of bytes in the resource identified by
* the given <code>URL</code>
*
* @param url the url of the resource.
* @return the length in bytes of the resource.
*/
public long getLength(URL url) {
try {
final URLConnection urlConnection = url.openConnection();
return urlConnection.getContentLength();
}
catch (Exception e) {
return -1;
}
}
/**
* This creates a valid path by converting file sepeartor to forward slases.
*/
public static String createValidPath(String path) {
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
if (!path.startsWith("/")) {
path = "/" + path;
}
return path;
}
public static final File toFile(URL url) {
final String path = url.getPath();
final File file = new File(path);
return file;
}
public static URL getParent(URL url) {
final File file = toFile(url);
final File parentFile = file.getParentFile();
if (parentFile != null && !file.equals(parentFile)) {
try {
return parentFile.toURL();
}
catch (Exception ex) {
return null;
}
}
return null;
}
}
package org.jivesoftware.util;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class XMLUtils {
/**
* Handle URL
*/
final public static Object getObject(URL url) throws Exception {
InputStream stream = url.openStream();
XMLDecoder d = new XMLDecoder(stream);
Object result = d.readObject();
d.close();
stream.close();
return result;
}
final public static void writeObject(Object document, URL url) throws Exception {
final OutputStream out = openOutputStream(url);
XMLEncoder e = new XMLEncoder(out);
e.writeObject(document);
e.close();
}
/**
* Handle Output and InputStreams
*/
final public static Object getObject(InputStream stream) throws Exception {
XMLDecoder d = new XMLDecoder(stream);
Object result = d.readObject();
d.close();
stream.close();
return result;
}
final public static Object getObject(String objectStr) throws Exception {
final ByteArrayInputStream stream = new ByteArrayInputStream(objectStr.getBytes("UTF-8"));
XMLDecoder d = new XMLDecoder(stream);
Object result = d.readObject();
d.close();
stream.close();
return result;
}
final public static void writeObject(Object obj, OutputStream stream) throws Exception {
XMLEncoder e = new XMLEncoder(stream);
e.writeObject(obj);
e.close();
}
/**
* Handle File handling.
*/
final public static Object getObject(File file) throws Exception {
XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));
Object result = d.readObject();
d.close();
return result;
}
final public static void writeObject(Object obj, File file) throws Exception {
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)));
e.writeObject(obj);
e.close();
}
final public static String toDocument(Object obj) throws Exception {
ByteArrayOutputStream s = new ByteArrayOutputStream();
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
XMLEncoder e = new XMLEncoder(s);
e.writeObject(obj);
e.close();
String returnStr = s.toString();
s.flush();
s.close();
printWriter.flush();
printWriter.close();
return returnStr;
}
public static String toString(Document xmlDocument) {
try {
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(printWriter);
transformer.transform(source, result);
stringWriter.close();
return stringWriter.toString();
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static OutputStream openOutputStream(URL url)
throws IOException {
final String path = url.getPath();
try {
return new FileOutputStream(path);
}
catch (FileNotFoundException e) {
final File file = url2File(url);
final File dir = file.getParentFile();
dir.mkdirs();
return new FileOutputStream(path);
}
}
private static final File url2File(URL url) {
final String path = url.getPath();
final File file = new File(path);
return file;
}
public final static String getEncodedObject(Object o) {
try {
String value = toDocument(o);
value = URLEncoder.encode(value, "UTF-8");
return value;
}
catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public final static Object getDecodedObject(String object) {
try {
object = URLDecoder.decode(object, "UTF-8");
return getObject(object);
}
catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment