com.janeluo.jfinalplus.route.AutoBindRoutes Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2013, kidzhou 周磊 ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.janeluo.jfinalplus.route;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.janeluo.jfinalplus.kit.ClassSearcher;
import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import java.util.List;
/**
* 自动创建路由
*
*/
public class AutoBindRoutes extends Routes {
private boolean autoScan = true;
private List> excludeClasses = Lists.newArrayList();
private boolean includeAllJarsInLib;
private List includeJars = Lists.newArrayList();
protected final Log logger = Log.getLog(getClass());
private String suffix = "Controller";
public AutoBindRoutes autoScan(boolean autoScan) {
this.autoScan = autoScan;
return this;
}
@SuppressWarnings("unchecked")
public AutoBindRoutes addExcludeClasses(Class extends Controller>... clazzes) {
if (clazzes != null) {
for (Class extends Controller> clazz : clazzes) {
excludeClasses.add(clazz);
}
}
return this;
}
public AutoBindRoutes addExcludeClasses(List> clazzes) {
excludeClasses.addAll(clazzes);
return this;
}
public AutoBindRoutes addJars(String... jars) {
if (jars != null) {
for (String jar : jars) {
includeJars.add(jar);
}
}
return this;
}
@Override
public void config() {
List> controllerClasses = ClassSearcher.of(Controller.class)
.includeAllJarsInLib(includeAllJarsInLib).injars(includeJars).search();
for (Class extends Controller> controller : controllerClasses) {
if (excludeClasses.contains(controller)) {
continue;
}
ControllerBind controllerBind = controller.getAnnotation(ControllerBind.class);
if (controllerBind == null) {
if (!autoScan) {
continue;
}
this.add(controllerKey(controller), controller);
logger.debug("routes.add(" + controllerKey(controller) + ", " + controller.getName() + ")");
} else if (StrKit.isBlank(controllerBind.viewPath())) {
this.add(controllerBind.controllerKey(), controller);
logger.debug("routes.add(" + controllerBind.controllerKey() + ", " + controller.getName() + ")");
} else {
this.add(controllerBind.controllerKey(), controller, controllerBind.viewPath());
logger.debug("routes.add(" + controllerBind.controllerKey() + ", " + controller + ","
+ controllerBind.viewPath() + ")");
}
}
}
private String controllerKey(Class extends Controller> clazz) {
Preconditions.checkArgument(clazz.getSimpleName().endsWith(suffix),
clazz.getName() + " is not annotated with @ControllerBind and not end with " + suffix);
String controllerKey = "/" + StrKit.firstCharToLowerCase(clazz.getSimpleName());
controllerKey = controllerKey.substring(0, controllerKey.indexOf(suffix));
return controllerKey;
}
public AutoBindRoutes includeAllJarsInLib(boolean includeAllJarsInLib) {
this.includeAllJarsInLib = includeAllJarsInLib;
return this;
}
public AutoBindRoutes suffix(String suffix) {
this.suffix = suffix;
return this;
}
}