All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.dreampie.common.config.AutoBindRoutes Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package cn.dreampie.common.config;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.jfinal.ext.kit.ClassSearcher;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;

import java.util.List;

/**
 * Created by wangrenhui on 14-1-2.
 */
public class AutoBindRoutes extends Routes {

  private boolean autoScan = true;

  private List> excludeClasses = Lists.newArrayList();

  private boolean includeAllJarsInLib;

  private List includeJars = Lists.newArrayList();

  protected final Logger logger = Logger.getLogger(getClass());

  private String suffix = "Controller";

  public AutoBindRoutes autoScan(boolean autoScan) {
    this.autoScan = autoScan;
    return this;
  }

  public AutoBindRoutes addExcludeClasses(Class... clazzes) {
    if (clazzes != null) {
      for (Class 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
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void config() {
    List> controllerClasses = ClassSearcher.of(Controller.class)
        .includeAllJarsInLib(includeAllJarsInLib).injars(includeJars).search();
    ControllerBind controllerBind = null;
    for (Class 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 clazz) {
    Preconditions.checkArgument(clazz.getSimpleName().endsWith(suffix),
        " does not has a @ControllerBind annotation and it's name is not end with " + suffix);
    String simpleName = clazz.getSimpleName();
    String controllerKey = "/";
    if (!simpleName.equalsIgnoreCase(suffix)) {
      controllerKey += StrKit.firstCharToLowerCase(simpleName.replace(suffix, ""));
    }
    return controllerKey;
  }

  public AutoBindRoutes includeAllJarsInLib(boolean includeAllJarsInLib) {
    this.includeAllJarsInLib = includeAllJarsInLib;
    return this;
  }

  public AutoBindRoutes suffix(String suffix) {
    this.suffix = suffix;
    return this;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy