public void setBasePackage(String basePackage) { Assert.notBlank(basePackage); packages.add(basePackage + ".controller"); packages.add(basePackage + ".service"); packages.add(basePackage + ".config"); packages.add(basePackage + ".context"); packages.add(basePackage + ".plugins"); packages.add(basePackage + ".init"); packages.add(basePackage + ".interceptor"); }
/** * set base package * * @param basePackage * @return */ public Blade basePackage(String basePackage) { Assert.notBlank(basePackage); bConfig.setBasePackage(basePackage); return this; }
/** * Setting Properties configuration file File path based on classpath * * @param location properties file name * @return return blade */ public Blade loadAppConf(String location) { Assert.notBlank(location); bConfig.load(location); return this; }
/** * Setting blade web root path * * @param webRoot web root path * @return return blade */ public Blade webRoot(final String webRoot) { Assert.notBlank(webRoot); bConfig.setWebRoot(webRoot); return this; }
@Override public Set<ClassInfo> getClassByAnnotation(String packageName, Class<?> parent, Class<? extends Annotation> annotation, boolean recursive) { Assert.notBlank(packageName); Set<ClassInfo> classes = CollectionKit.newHashSet(); // 获取包的名字 并进行替换 String packageDirName = packageName.replace('.', '/'); // 定义一个枚举的集合 并进行循环来处理这个目录下的URL Enumeration<URL> dirs; try { dirs = this.getClass().getClassLoader().getResources(packageDirName); // 循环迭代下去 while (dirs.hasMoreElements()) { // 获取下一个元素 URL url = dirs.nextElement(); Set<ClassInfo> subClasses = this.getClasses(url, packageDirName, packageName, parent, annotation, recursive, classes); if(subClasses.size() > 0){ classes.addAll(subClasses); } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } return classes; }
@Override public Set<ClassInfo> getClassByAnnotation(String packageName, Class<?> parent, Class<? extends Annotation> annotation, boolean recursive) { Assert.notBlank(packageName); Set<ClassInfo> classes = CollectionKit.newHashSet(); // 获取包的名字 并进行替换 String packageDirName = packageName.replace('.', '/'); // 定义一个枚举的集合 并进行循环来处理这个目录下的URL Enumeration<URL> dirs; try { dirs = this.getClass().getClassLoader().getResources(packageDirName); // 循环迭代下去 while (dirs.hasMoreElements()) { // 获取下一个元素 URL url = dirs.nextElement(); // 获取包的物理路径 String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); Set<ClassInfo> subClasses = findClassByPackage(packageName, filePath, parent, annotation, recursive, classes); if(subClasses.size() > 0){ classes.addAll(subClasses); } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } catch (ClassNotFoundException e) { LOGGER.error("Add user custom view class error Can't find such Class files."); throw new ClassReaderException(e); } return classes; }
public void addRoute(HttpMethod httpMethod, String path, Object controller, Class<?> controllerType, Method method) { Assert.notBlank(path); String key = path + "#" + httpMethod.toString(); // existent if (null != this.routes.get(key)) { LOGGER.warn("\tRoute {} -> {} has exist", path, httpMethod.toString()); } Route route = new Route(httpMethod, path, controller, controllerType, method); if (httpMethod == HttpMethod.BEFORE || httpMethod == HttpMethod.AFTER) { if (null != this.interceptors.get(key)) { LOGGER.warn("\tInterceptor {} -> {} has exist", path, httpMethod.toString()); } this.interceptors.put(key, route); LOGGER.info("Add Interceptor: {}", route); } else { this.routes.put(key, route); LOGGER.info("Add Route => {}", route); } }
@Override public Response render(ModelAndView modelAndView) { Assert.notBlank(modelAndView.getView(), "view not is null"); String viewPath = Path.cleanPath(modelAndView.getView()); modelAndView.setView(viewPath); try { templateEngine.render(modelAndView, response.getWriter()); } catch (Exception e) { LOGGER.error("", e); } return this; }
/** * Registration of a configuration file, e.g: "com.xxx.route","route.conf" * * @param basePackage controller package name * @param conf Configuration file path, the configuration file must be in * classpath * @return return blade */ public Blade routeConf(String basePackage, String conf) { try { Assert.notBlank(basePackage); Assert.notBlank(conf); InputStream ins = Blade.class.getResourceAsStream("/" + conf); ClassPathRouteLoader routesLoader = new ClassPathRouteLoader(ins); routesLoader.setBasePackage(basePackage); List<Route> routes = routesLoader.load(); routers.addRoutes(routes); } catch (RouteException | ParseException e) { throw new BladeException(e); } return this; }