📄 proxydircontext.java
字号:
/**
* Searches in the named context or object for entries that satisfy the
* given search filter. Performs the search as specified by the search
* controls.
*
* @param name the name of the context or object to search
* @param filterExpr the filter expression to use for the search.
* The expression may contain variables of the form "{i}" where i is a
* nonnegative integer. May not be null.
* @param filterArgs the array of arguments to substitute for the
* variables in filterExpr. The value of filterArgs[i] will replace each
* occurrence of "{i}". If null, equivalent to an empty array.
* @param cons the search controls that control the search. If null, the
* default search controls are used (equivalent to (new SearchControls())).
* @return an enumeration of SearchResults of the objects that satisy the
* filter; never null
* @exception ArrayIndexOutOfBoundsException if filterExpr contains {i}
* expressions where i is outside the bounds of the array filterArgs
* @exception InvalidSearchControlsException if cons contains invalid
* settings
* @exception InvalidSearchFilterException if filterExpr with filterArgs
* represents an invalid search filter
* @exception NamingException if a naming exception is encountered
*/
public NamingEnumeration search(Name name, String filterExpr,
Object[] filterArgs, SearchControls cons)
throws NamingException {
return dirContext.search(parseName(name), filterExpr, filterArgs,
cons);
}
/**
* Searches in the named context or object for entries that satisfy the
* given search filter. Performs the search as specified by the search
* controls.
*
* @param name the name of the context or object to search
* @param filterExpr the filter expression to use for the search.
* The expression may contain variables of the form "{i}" where i is a
* nonnegative integer. May not be null.
* @param filterArgs the array of arguments to substitute for the
* variables in filterExpr. The value of filterArgs[i] will replace each
* occurrence of "{i}". If null, equivalent to an empty array.
* @param cons the search controls that control the search. If null, the
* default search controls are used (equivalent to (new SearchControls())).
* @return an enumeration of SearchResults of the objects that satisy the
* filter; never null
* @exception ArrayIndexOutOfBoundsException if filterExpr contains {i}
* expressions where i is outside the bounds of the array filterArgs
* @exception InvalidSearchControlsException if cons contains invalid
* settings
* @exception InvalidSearchFilterException if filterExpr with filterArgs
* represents an invalid search filter
* @exception NamingException if a naming exception is encountered
*/
public NamingEnumeration search(String name, String filterExpr,
Object[] filterArgs, SearchControls cons)
throws NamingException {
return dirContext.search(parseName(name), filterExpr, filterArgs,
cons);
}
// --------------------------------------------------------- Public Methods
/**
* Retrieves the named object as a cache entry, without any exception.
*
* @param name the name of the object to look up
* @return the cache entry bound to name
*/
public CacheEntry lookupCache(String name) {
CacheEntry entry = cacheLookup(name);
if (entry == null) {
entry = new CacheEntry();
entry.name = name;
try {
Object object = dirContext.lookup(parseName(name));
if (object instanceof InputStream) {
entry.resource = new Resource((InputStream) object);
} else if (object instanceof DirContext) {
entry.context = (DirContext) object;
} else if (object instanceof Resource) {
entry.resource = (Resource) object;
} else {
entry.resource = new Resource(new ByteArrayInputStream
(object.toString().getBytes()));
}
Attributes attributes = dirContext.getAttributes(parseName(name));
if (!(attributes instanceof ResourceAttributes)) {
attributes = new ResourceAttributes(attributes);
}
entry.attributes = (ResourceAttributes) attributes;
} catch (NamingException e) {
entry.exists = false;
}
}
return entry;
}
// ------------------------------------------------------ Protected Methods
/**
* Parses a name.
*
* @return the parsed name
*/
protected String parseName(String name)
throws NamingException {
return name;
}
/**
* Parses a name.
*
* @return the parsed name
*/
protected Name parseName(Name name)
throws NamingException {
return name;
}
/**
* Lookup in cache.
*/
protected CacheEntry cacheLookup(String name) {
if (cache == null)
return (null);
if (name == null)
name = "";
for (int i = 0; i < nonCacheable.length; i++) {
if (name.startsWith(nonCacheable[i])) {
return (null);
}
}
CacheEntry cacheEntry = cache.lookup(name);
if (cacheEntry == null) {
cacheEntry = new CacheEntry();
cacheEntry.name = name;
// Load entry
cacheLoad(cacheEntry);
} else {
if (!validate(cacheEntry)) {
if (!revalidate(cacheEntry)) {
cacheUnload(cacheEntry.name);
return (null);
} else {
cacheEntry.timestamp =
System.currentTimeMillis() + cacheTTL;
}
}
cacheEntry.accessCount++;
}
return (cacheEntry);
}
/**
* Validate entry.
*/
protected boolean validate(CacheEntry entry) {
if (((!entry.exists)
|| (entry.context != null)
|| ((entry.resource != null)
&& (entry.resource.getContent() != null)))
&& (System.currentTimeMillis() < entry.timestamp)) {
return true;
}
return false;
}
/**
* Revalidate entry.
*/
protected boolean revalidate(CacheEntry entry) {
// Get the attributes at the given path, and check the last
// modification date
if (!entry.exists)
return false;
if (entry.attributes == null)
return false;
long lastModified = entry.attributes.getLastModified();
long contentLength = entry.attributes.getContentLength();
if (lastModified <= 0)
return false;
try {
Attributes tempAttributes = dirContext.getAttributes(entry.name);
ResourceAttributes attributes = null;
if (!(tempAttributes instanceof ResourceAttributes)) {
attributes = new ResourceAttributes(tempAttributes);
} else {
attributes = (ResourceAttributes) tempAttributes;
}
long lastModified2 = attributes.getLastModified();
long contentLength2 = attributes.getContentLength();
return (lastModified == lastModified2)
&& (contentLength == contentLength2);
} catch (NamingException e) {
return false;
}
}
/**
* Load entry into cache.
*/
protected void cacheLoad(CacheEntry entry) {
String name = entry.name;
// Retrieve missing info
boolean exists = true;
// Retrieving attributes
if (entry.attributes == null) {
try {
Attributes attributes = dirContext.getAttributes(entry.name);
if (!(attributes instanceof ResourceAttributes)) {
entry.attributes =
new ResourceAttributes(attributes);
} else {
entry.attributes = (ResourceAttributes) attributes;
}
} catch (NamingException e) {
exists = false;
}
}
// Retriving object
if ((exists) && (entry.resource == null) && (entry.context == null)) {
try {
Object object = dirContext.lookup(name);
if (object instanceof InputStream) {
entry.resource = new Resource((InputStream) object);
} else if (object instanceof DirContext) {
entry.context = (DirContext) object;
} else if (object instanceof Resource) {
entry.resource = (Resource) object;
} else {
entry.resource = new Resource(new ByteArrayInputStream
(object.toString().getBytes()));
}
} catch (NamingException e) {
exists = false;
}
}
// Load object content
if ((exists) && (entry.resource != null)
&& (entry.resource.getContent() == null)
&& (entry.attributes.getContentLength() >= 0)
&& (entry.attributes.getContentLength() <
(cacheObjectMaxSize * 1024))) {
int length = (int) entry.attributes.getContentLength();
// The entry size is 1 + the resource size in KB, if it will be
// cached
entry.size += (entry.attributes.getContentLength() / 1024);
InputStream is = null;
try {
is = entry.resource.streamContent();
int pos = 0;
byte[] b = new byte[length];
while (pos < length) {
int n = is.read(b, pos, length - pos);
if (n < 0)
break;
pos = pos + n;
}
entry.resource.setContent(b);
} catch (IOException e) {
; // Ignore
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
; // Ignore
}
}
}
// Set existence flag
entry.exists = exists;
// Set timestamp
entry.timestamp = System.currentTimeMillis() + cacheTTL;
// Add new entry to cache
synchronized (cache) {
// Check cache size, and remove elements if too big
if ((cache.lookup(name) == null) && cache.allocate(entry.size)) {
cache.load(entry);
}
}
}
/**
* Remove entry from cache.
*/
protected boolean cacheUnload(String name) {
if (cache == null)
return false;
synchronized (cache) {
return cache.unload(name);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -