private static Log log = LogFactory.getLog(BundleControllerHandler.class);
public void registerBundle(Bundle bundle,
ApplicationContext applicationContext) {
PluginControllerInfo pluginControllerInfo = new PluginControllerInfo(
bundle);
// Bundle的名称
String bundleName = bundle.getSymbolicName();
// 从ApplicationContext得到MVC控制器列表
final Map<String, Object> handlerMap = applicationContext
.getBeansWithAnnotation(Controller.class);
Collection<Object> handlers = handlerMap.values();
if (handlers != null) {
for (Object handler : handlers) {
Class<?> controllerClazz = handler.getClass();
Method[] methods = controllerClazz.getMethods();
for (Method method : methods) {
// 查找RequestMapping注解
RequestMapping requestMapping = method
.getAnnotation(RequestMapping.class);
if (requestMapping == null)
continue;
for (String methodName : requestMapping.value()) {
// 内部URL,仅用于 Spring MVC内部匹配到控制器方法
// 要获取外部URL,请调用WebContext.getBundleMethodUrl方法
String innerMappingUrl = methodName;
if (!innerMappingUrl.startsWith("/")) {
innerMappingUrl = "/" + innerMappingUrl;
}
innerMappingUrl = "/" + bundleName + innerMappingUrl;
RequestMethod[] requestMethods = requestMapping
.method();
// 如果方法为空,则映射所有的HTTP方法
if (requestMethods == null
|| requestMethods.length == 0) {
requestMethods = RequestMethod.values();
}
StringBuilder sb = new StringBuilder();
for (RequestMethod requestMethod : requestMethods) {
sb.append(requestMethod.name());
sb.append(",");
// 添加到映射MAP中
String tmpMappingUrl = requestMethod.name()
.toUpperCase() + "_" + innerMappingUrl;
pluginControllerInfo.getMappingUrlHandlerMap().put(
tmpMappingUrl, handler);
}
if (sb.length() == 0)
// 正常情况下,这儿不可能被执行到。
sb.append("所有");
else
sb.setLength(sb.length() - 1);
log.debug(String.format(
"Spring MVC:映射内部URL路径[%s]的[%s]HTTP请求到处理器'%s'",
innerMappingUrl, sb.toString(), handler
.getClass().getName()));
// 将处理器与对应的适配器放入映射中
if (!pluginControllerInfo.getHandlerAdapterMap()
.containsKey(handler)) {
AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
adapter.setPathMatcher(new PluginPathMatcher(bundle
.getSymbolicName()));
adapter.setUrlPathHelper(new PluginUrlPathHelper());
pluginControllerInfo.getHandlerAdapterMap().put(
handler, adapter);
}
}
}
}