2021SC@SDUSC山东大学软件学院软件工程应用与实践 COCOON 第七篇
作者:互联网
2021SC@SDUSC
目录
DefaultIncludeCacheManager.java
Transformation.helpers:
本次将对transformation.helper包中的各类进行分析。
DefaultIncludeCacheManager.java
1.总结:
公共最终类DefaultIncludeCacheManager
扩展org.apache.cocoon.util.AbstractLogEnabled
实施包括ChemManager、线程安全、可维护、一次性、可参数化
IncludeCacheManager的默认实现。如果使用抢占式加载,则此实现需要配置:<parameter name=“preemptive loader url”value=“some url”/>这是cocoon内部的url,包含抢占式加载程序url;它必须指定为绝对值(带http://...)如果无法启动此加载程序,则只会在日志中记录一个错误,因此实际上缓存的内容永远不会更新。
2.方法
void dispose()
IncludeCacheManagerSession getSession(Parameters pars)
// 为该请求创建一个session.这应该首先调用,并且每个请求只能调用一个。需要使
//用IncludeCacheManager.terminateSession(IncludeCacheManagerSession)终止会话。
String load(String uri, IncludeCacheManagerSession session)
//这会通知管理器应该加载URI.
void parameterize(Parameters parameters)
void service(ServiceManager manager)
void stream(String uri, IncludeCacheManagerSession session, org.apache.cocoon.xml.XMLConsumer handler)
//流式传输绝对URI的内容,根据缓存的配置和状态,内容可以从缓存中获取、获取等。。
void terminateSession(IncludeCacheManagerSession session)
//终止会话,该方法必须在请求结束时执行。。
3.重点代码分析:
public String load(String uri, IncludeCacheManagerSession session) throws IOException {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Load " + uri + " for session " + session);
}
if ( uri.indexOf("://") == -1) {
final Source source = session.resolveURI(uri, this.resolver);
uri = source.getURI();
}
if ( session.isParallel() && !session.isPreemptive()) {
IncludeCacheStorageProxy storage = session.getCacheStorageProxy();
CachedResponse response = (CachedResponse)storage.get(uri);
if ( null != response) {
SourceValidity[] validities = response.getValidityObjects();
if ( !session.isPurging()
&& validities[0].isValid() == SourceValidity.VALID) {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Using cached response for parallel processing.");
}
session.add(uri, response.getResponse());
return uri;
} else {
storage.remove(uri);
}
}
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Starting parallel thread for loading " + uri);
}
try {
XMLByteStreamCompiler serializer = new XMLByteStreamCompiler();
Source source = session.resolveURI(uri, this.resolver);
LoaderThread loader = new LoaderThread(source, serializer);
final RunnableManager runnableManager = (RunnableManager)this.manager.lookup( RunnableManager.ROLE );
session.add(uri, loader);
runnableManager.execute( new CocoonRunnable(loader) );
this.manager.release( runnableManager );
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Thread started for " + uri);
}
} catch (ServiceException ce) {
throw new SourceException("Unable to lookup thread pool, RunnableManager, or xml serializer.", ce);
} catch (Exception e) {
throw new SourceException("Unable to get pooled thread.", e);
}
}
return uri;
}
首先,将URI设置为绝对值,如果我们不是并行处理(或预先处理),我们不必在这种方法中做任何事情,一切在流方法中完成。如果我们并行处理并且不是预先处理,首先查找是否有一个有效的存储响应。如果有效并且没有清除,添加uri和回复到session中返回,否则删除存储中对应的url。
之后启动一个并行线程,该线程获取所有必需的avalon组件,不必查找。
public void stream(String uri,
IncludeCacheManagerSession session,
XMLConsumer handler)
throws IOException, SAXException {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Stream " + uri + " for session " + session);
}
if (session.isParallel() && !session.isPreemptive()) {
Object object = session.get(uri);
if (object == null) {
throw new SAXException("No pooled thread found for " + uri);
}
byte[] result;
if (object instanceof LoaderThread) {
LoaderThread loader = (LoaderThread)object;
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Waiting for pooled thread to finish loading.");
}
loader.join();
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Pooled thread finished loading.");
}
if ( null != loader.exception) {
if ( loader.exception instanceof SAXException ) {
throw (SAXException)loader.exception;
} else if (loader.exception instanceof SourceException ) {
throw (SourceException)loader.exception;
} else if (loader.exception instanceof IOException) {
throw (IOException)loader.exception;
} else {
throw new SAXException("Exception.", loader.exception);
}
}
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Streaming from pooled thread.");
}
result = loader.content;
if (session.getExpires() > 0) {
SourceValidity[] validities = new SourceValidity[1];
validities[0] = session.getExpiresValidity();
CachedResponse response = new CachedResponse(validities, result);
session.getCacheStorageProxy().put(uri, response);
}
} else {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Streaming from cached response.");
}
result = (byte[])object;
}
XMLByteStreamInterpreter deserializer = new XMLByteStreamInterpreter();
deserializer.setConsumer(handler);
deserializer.deserialize(result);
return;
} else {
IncludeCacheStorageProxy storage = session.getCacheStorageProxy();
CachedResponse response = (CachedResponse)storage.get(uri);
if ( null != response) {
SourceValidity[] validities = response.getValidityObjects();
if ( !session.isPurging()
&& (session.isPreemptive() || validities[0].isValid() == SourceValidity.VALID)) {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Streaming from cached response.");
}
XMLByteStreamInterpreter deserializer = new XMLByteStreamInterpreter();
deserializer.setConsumer(handler);
deserializer.deserialize(response.getResponse());
if ( session.getExpires() > 0
&& session.isPreemptive()
&& validities[0].isValid() != SourceValidity.VALID) {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Add uri to preemptive loader list " + uri);
}
if (!PreemptiveLoader.getInstance().alive) {
this.getLogger().error("Preemptive loader has not started yet.");
}
PreemptiveLoader.getInstance().add(session.getCacheStorageProxy(), uri, session.getExpires());
}
return;
} else {
storage.remove(uri);
}
}
}
XMLByteStreamCompiler serializer;
try {
final Source source = session.resolveURI(uri, this.resolver);
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Streaming directly from source.");
}
if (session.getExpires() > 0) {
serializer = new XMLByteStreamCompiler();
XMLTeePipe tee = new XMLTeePipe(handler, serializer);
SourceUtil.toSAX(source, tee);
SourceValidity[] validities = new SourceValidity[1];
validities[0] = session.getExpiresValidity();
CachedResponse response = new CachedResponse(validities,
(byte[])serializer.getSAXFragment());
session.getCacheStorageProxy().put(uri, response);
} else {
SourceUtil.toSAX(source, handler);
}
} catch (ProcessingException pe) {
throw new SAXException("ProcessingException", pe);
}
}
该方法代码较长,但是总体结构还是比较清晰。总体围绕是否是多线程和没有预处理分为两种情况。当session.isParallel() && !session.isPreemptive()为真时,获取缓存内容或池线程。
如果是池线程,新建后等待加入,同时捕获并释放异常。缓存响应(记住抢占已关闭),使用来自缓存的响应。最后流式传输内容。
弱我们没有预先处理,首先测试缓存的影响,如果清除已关闭,并且缓存的响应有效或正在加载抢占式响应,则使用缓存的响应,流式传输内容,如果相应无效,则加载预先处理内容。
若我们不是多线程并且没有有效的缓存相应,则直接进行流传输。
FormValidatorHelper.java
1.总结
公共类FormValidatorHelper,继承了Object类.ValidatorActionResult对象帮助器。由XSP使用。
2.方法
static Object getAttribute(Map objectModel, String name)
//获取指定的属性
protected static Configuration getConfiguration(String descriptor, org.apache.cocoon.environment.SourceResolver resolver, boolean reloadable, Log logger)
//设置补充配置文件。
protected static Configuration getConfigurationByName(Configuration[] conf, String name, Log logger)
//迭代一组配置并返回名称与给定配置匹配的配置
String getParameterAttribute(String attribute)
//获取在descriptor.xml中指定的上下文当前参数的属性。
String getParameterAttribute(String parameter, String attribute)
//获取在descriptor.xml中指定的上下文当前参数的属性。
static String getParameterAttributes(String descriptor, org.apache.cocoon.environment.SourceResolver resolver, boolean reloadable, String constraintset, String parameter, String attribute, Log logger)
//获取在descriptor.xml中指定的参数的属性。
org.apache.cocoon.acting.ValidatorActionResult getParamResult(Map objectModel)
//从上下文的当前请求参数的请求属性中提取验证结果
static org.apache.cocoon.acting.ValidatorActionResult getParamResult(Map objectModel, String name)
Extracts the validation results from the request attribute for a specific request parameter
static Map getResults(Map objectModel)
//从特定请求参数的请求属性中提取验证结果
boolean isError(Map objectModel)
//测试验证是否为上下文的当前参数返回错误。
static boolean isError(Map objectModel, String name)
//测试验证是否为此参数返回错误。
boolean isNoMatch(Map objectModel)
测试上下文的当前参数是否与请求的正则表达式不匹配。
static boolean isNoMatch(Map objectModel, String name)
测试验证的参数是否与请求的正则表达式不匹配。
boolean isNotPresent(Map objectModel)
测试上下文的当前参数是否未验证
static boolean isNotPresent(Map objectModel, String name)
//测试是否未验证已验证的参数
boolean isNull(Map objectModel)
//测试验证的上下文的当前参数是否为null,但不允许为空。
static boolean isNull(Map objectModel, String name)
//测试验证的参数是否为null,但不允许为空。
boolean isOK(Map objectModel)
//测试验证是否未返回上下文当前参数的错误。
static boolean isOK(Map objectModel, String name)
// 测试验证是否未返回此参数的错误。
boolean isTooLarge(Map objectModel)
// 测试上下文的当前参数是否太大。
static boolean isTooLarge(Map objectModel, String name)
// 测试验证的参数是否太大。
boolean isTooSmall(Map objectModel)
// 测试上下文的当前参数是否太小。
static boolean isTooSmall(Map objectModel, String name)
// 测试验证的参数是否太大小
void setConstraintSet(String constraintset)
// 跟踪当前约束集上下文
void setParameter(String parameter)
//跟踪当前参数上下文
标签:Map,2021SC,getLogger,objectModel,uri,第七篇,session,COCOON,String 来源: https://blog.csdn.net/qq_54049011/article/details/121156224