Search in sources :

Example 1 with ActionConfigBean

use of org.eweb4j.mvc.config.bean.ActionConfigBean in project eweb4j-framework by laiweiwei.

the class InterExecution method findActionUriMapping.

private boolean findActionUriMapping() {
    boolean result = false;
    Map<String, List<?>> map = null;
    if (ActionConfigBeanCache.containsKey(this.context.getUri()) || (map = ActionConfigBeanCache.getByMatches(this.context.getUri(), context.getHttpMethod())) != null) {
        // 找到了action 与当前访问的uri映射
        if (map.containsKey("mvcBean")) {
            ActionConfigBean acb = (ActionConfigBean) map.get("mvcBean").get(0);
            this.context.setActionConfigBean(acb);
            result = true;
        }
    }
    return result;
}
Also used : List(java.util.List) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean)

Example 2 with ActionConfigBean

use of org.eweb4j.mvc.config.bean.ActionConfigBean in project eweb4j-framework by laiweiwei.

the class CheckConfigBean method checkMVCAction.

/**
	 * Check the MVC independent components configuration files
	 * 
	 * @param mvc
	 * @return
	 */
public static String checkMVCAction(ActionConfigBean mvc, String xmlFile) {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if ("true".equalsIgnoreCase(cb.getMvc().getOpen()) || "1".equals(cb.getMvc().getOpen())) {
        StringBuilder sb = new StringBuilder();
        if (!"".equals(mvc.getClazz())) {
            try {
                Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(mvc.getClazz());
                if (clazz == null) {
                    sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
                } else {
                    if (mvc.getMethod() != null && !"".equals(mvc.getMethod())) {
                        Method m = new ReflectUtil(clazz.newInstance()).getMethod(mvc.getMethod());
                        if (m == null) {
                            sb.append("当前您填写的( method=").append(mvc.getMethod()).append(" )是错误的!它必须是一个有效的方法 ;\n");
                        }
                    }
                }
            } catch (ClassNotFoundException e) {
                sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
            } catch (InstantiationException e) {
                sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个提供无参构造方法的类 ;\n");
            } catch (IllegalAccessException e) {
                sb.append("当前您填写的( class=").append(mvc.getClazz()).append(" )是错误的!它必须是一个有效的类 ;\n");
            }
        }
        if (!"".equals(sb.toString())) {
            error = "\n<br /><b>" + xmlFile + ":[bean name=" + mvc.getUriMapping() + "]</b>\n" + sb.toString();
        }
    }
    return error;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean) Method(java.lang.reflect.Method)

Example 3 with ActionConfigBean

use of org.eweb4j.mvc.config.bean.ActionConfigBean in project eweb4j-framework by laiweiwei.

the class ActionAnnotationConfig method parseUriMappingSuffix.

/**
	 * 解析 URI Mapping 后部分
	 * 
	 * @param moduleName
	 * @param m
	 * @return
	 */
private ActionConfigBean parseUriMappingSuffix(String moduleName, Method m) {
    ActionConfigBean acb = new ActionConfigBean();
    String methodName = m.getName();
    String fullName = m.toString();
    boolean hasPath = JAXWSUtil.hasPath(m);
    //log.debug("has path of method "+m+" ->" + hasPath);
    String uriMapping = null;
    if (methodName.startsWith(ActionMethod.PREFIX)) {
        uriMapping = methodName.substring(ActionMethod.PREFIX.length());
        // doUriBindParam1AndParam2JoinUriAtPostOrGet
        String at = null;
        int indexOfAt = methodName.indexOf(ActionMethod.AT);
        if (indexOfAt != -1) {
            at = methodName.substring(indexOfAt + ActionMethod.AT.length());
            if (methodName.startsWith(ActionMethod.PREFIX))
                uriMapping = uriMapping.substring(0, uriMapping.indexOf(ActionMethod.AT));
            String[] httpMethods = at.split(ActionMethod.OR);
            StringBuilder sb = new StringBuilder();
            for (String httpMethod : httpMethods) {
                if (sb.length() > 0)
                    sb.append("|");
                sb.append(httpMethod.toUpperCase());
            }
            if (sb.length() > 0) {
                acb.setHttpMethod(sb.toString());
            }
        }
        String join = "";
        String bind;
        int indexOfBind = methodName.indexOf(ActionMethod.BIND);
        if (indexOfBind != -1) {
            if (indexOfAt != -1 && indexOfAt > indexOfBind) {
                bind = methodName.substring(indexOfBind + ActionMethod.BIND.length(), indexOfAt);
            } else {
                bind = methodName.substring(indexOfBind + ActionMethod.BIND.length());
            }
            uriMapping = uriMapping.substring(0, uriMapping.indexOf(ActionMethod.BIND));
            int indexOfJoin = bind.indexOf(ActionMethod.JOIN);
            if (indexOfJoin != -1) {
                String[] joins = bind.split(ActionMethod.JOIN);
                if (joins.length > 1) {
                    bind = joins[0];
                    join = joins[1];
                }
            }
            String[] pathParams = bind.split(ActionMethod.AND);
            StringBuilder pathParamSB = new StringBuilder();
            for (int i = 0; i < pathParams.length; i++) {
                pathParams[i] = CommonUtil.toLowCaseFirst(pathParams[i]);
                pathParamSB.append("/{").append(pathParams[i]).append("}");
            }
            if (pathParamSB.length() > 0)
                uriMapping = uriMapping + pathParamSB.toString();
            acb.setPathParams(pathParams);
        }
        uriMapping = CommonUtil.toLowCaseFirst(uriMapping);
        uriMapping = CommonUtil.hump2ohter(uriMapping, "-");
        if (join.length() > 0) {
            join = CommonUtil.toLowCaseFirst(join);
            join = CommonUtil.hump2ohter(join, "-");
            uriMapping = uriMapping + "/" + join;
        }
    } else if (!hasPath) {
        //log.debug("parse action by default rule......");
        /* 8 个默认方法 */
        ActionConfigBean defaultAcb = parseDefaultActionConfig(methodName, moduleName);
        //log.debug("parse action by default rule......--->"+defaultAcb);
        if (defaultAcb != null) {
            acb.setHttpMethod(defaultAcb.getHttpMethod());
            acb.getResult().addAll(defaultAcb.getResult());
            uriMapping = defaultAcb.getUriMapping();
        } else {
            String info = fullName + " does not starts with '" + ActionMethod.PREFIX + "' so that can not be a valid action uri mapping";
            //log.debug(info);
            return null;
        }
    }
    if (hasPath) {
        uriMapping = CommonUtil.parsePropValue(org.eweb4j.mvc.config.PathUtil.getPathValue(m));
    }
    //log.debug("parse uri mapping first -> " + uriMapping + ", hasPath->" + hasPath);
    acb.setUriMapping(uriMapping);
    return acb;
}
Also used : ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean)

Example 4 with ActionConfigBean

use of org.eweb4j.mvc.config.bean.ActionConfigBean in project eweb4j-framework by laiweiwei.

the class ActionConfig method check.

/** 涉及到文件IO,需要同步保证正确性 */
public static synchronized String check() {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if (cb == null)
        return null;
    // eweb4j-start-config.xml中<mvc>节点里的actionXmlFile
    List<String> xmlFilePaths = cb.getMvc().getActionXmlFiles().getPath();
    for (String filePath : xmlFilePaths) {
        if (filePath == null || filePath.length() == 0)
            continue;
        // 解决中文文件夹名
        File configFile = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
        try {
            XMLReader reader = BeanXMLUtil.getBeanXMLReader(configFile);
            reader.setBeanName("action");
            reader.setClass("action", ActionConfigBean.class);
            List<ActionConfigBean> mvcList = reader.read();
            if (mvcList == null || mvcList.isEmpty()) {
                error = rebuildXmlFile(configFile, ConfigErrCons.CANNOT_READ_CONFIG_INFO);
            } else {
                for (Iterator<ActionConfigBean> it = mvcList.iterator(); it.hasNext(); ) {
                    ActionConfigBean mvc = it.next();
                    // 检查MVC.Action配置是否有错误
                    String error1 = CheckConfigBean.checkMVCAction(mvc, filePath);
                    if (error1 != null)
                        if (error != null)
                            error += error1;
                        else
                            error = error1;
                    // 检查MVC.Action中的Result部分配置是否有错误
                    String error2 = CheckConfigBean.checkMVCResultPart(mvc.getResult(), mvc.getUriMapping(), filePath);
                    if (error2 != null)
                        if (error != null)
                            error += error2;
                        else
                            error = error2;
                    // 检查MVC.Action.Validator中的部分配置是否有错误
                    String error4 = CheckConfigBean.checkMVCValidator(mvc.getValidator(), mvc.getUriMapping(), filePath);
                    if (error4 != null)
                        if (error != null)
                            error += error4;
                        else
                            error = error4;
                }
                // 如果没有任何错误,将Action的配置信息放入缓存,供框架运行期使用
                if (error == null)
                    for (Iterator<ActionConfigBean> it = mvcList.iterator(); it.hasNext(); ) {
                        ActionConfigBean mvc = it.next();
                        if (!"".equals(mvc.getClazz()))
                            if (!"".equals(mvc.getUriMapping()))
                                ActionConfigBeanCache.add(mvc.getUriMapping(), mvc);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
            error = rebuildXmlFile(configFile, CommonUtil.getExceptionString(e));
        }
    }
    // 如果有错误,清空缓存
    if (error != null)
        ActionConfigBeanCache.clear();
    return error;
}
Also used : Iterator(java.util.Iterator) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) CheckConfigBean(org.eweb4j.config.CheckConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) File(java.io.File) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) XMLReader(org.eweb4j.util.xml.XMLReader)

Example 5 with ActionConfigBean

use of org.eweb4j.mvc.config.bean.ActionConfigBean in project eweb4j-framework by laiweiwei.

the class ActionConfigBeanCache method getByMatches.

public static Map<String, List<?>> getByMatches(final String aUri, final String reqMethod) {
    String uri = new String(aUri);
    Map<String, List<?>> result = null;
    for (Iterator<Entry<Object, ActionConfigBean>> it = ACTION_CFG_BEAN.entrySet().iterator(); it.hasNext(); ) {
        Entry<Object, ActionConfigBean> entry = it.next();
        Object beanID = entry.getKey();
        ActionConfigBean mvcBean = entry.getValue();
        if (!String.class.isAssignableFrom(beanID.getClass()))
            continue;
        // 如果是String
        String bid = String.valueOf(beanID).replace(ActionMethod.CON + mvcBean.getHttpMethod(), "");
        //			System.out.println("bid------>"+bid );
        if (bid.contains("{") || bid.contains("}"))
            continue;
        if (aUri.endsWith("/"))
            uri = aUri.substring(0, aUri.length() - 1);
        if (bid.endsWith("/"))
            bid = bid.substring(0, bid.length() - 1);
        String[] methods = mvcBean.getHttpMethod().split("\\|");
        boolean checkMethod = false;
        for (String m : methods) {
            if (m.trim().equalsIgnoreCase(reqMethod)) {
                checkMethod = true;
                break;
            }
        }
        if (bid != null && checkMethod && uri.matches(bid)) {
            result = new HashMap<String, List<?>>();
            // 先将类似 {xxx} 的东西找出来,然后取里面的xxx作为参数名,匹配到的位置作为参数值,不考虑具体的数据类型
            // 1.hello/{name}/test/{id}
            // 2.{"hello/","{name}","/test/{id}"}
            String urlMapping = mvcBean.getUriMapping();
            // 如果urlMapping的开头是“/”要去掉
            if (urlMapping.startsWith("/"))
                urlMapping = urlMapping.substring(1);
            String pattern = RegexList.path_var_regexp;
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(urlMapping);
            List<String> urlParamNames = new ArrayList<String>();
            List<String> urlParamValues = new ArrayList<String>();
            while (m.find()) {
                String g = m.group();
                //					System.out.println("g->" + g + ", p->"+pattern + ", um->"+urlMapping);
                String[] regexSplit = urlMapping.split(pattern);
                //					System.out.println("list--->"+Arrays.asList(regexSplit));
                String paramVal = UrlParamHandler.matchersUrlParam(uri, regexSplit);
                //					System.out.println("pV->" + paramVal);
                if (paramVal != null) {
                    urlParamValues.add(paramVal);
                    urlParamNames.add(g);
                    //要记得解析完一个就好把urlMapping的{xxx}参数的{和}给干掉,因为那个UrlParamHandler只能一次解析一个{xxx}参数值
                    urlMapping = urlMapping.replace(g, paramVal);
                } else {
                    //要记得解析完一个就好把urlMapping的{xxx}参数的{和}给干掉,因为那个UrlParamHandler只能一次解析一个{xxx}参数值
                    urlMapping = urlMapping.replace(g, g.replace("{", "").replace("}", ""));
                }
            }
            if (urlParamNames.size() > 0 && urlParamValues.size() > 0) {
                result.put("urlParamNames", urlParamNames);
                result.put("urlParamValues", urlParamValues);
            }
            List<ActionConfigBean> mvcBeanList = new ArrayList<ActionConfigBean>();
            mvcBeanList.add(mvcBean);
            result.put("mvcBean", mvcBeanList);
            break;
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) Entry(java.util.Map.Entry) RegexList(org.eweb4j.util.RegexList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)9 ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)3 ValidatorConfigBean (org.eweb4j.mvc.config.bean.ValidatorConfigBean)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ConfigBean (org.eweb4j.config.bean.ConfigBean)2 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)2 ParamConfigBean (org.eweb4j.mvc.config.bean.ParamConfigBean)2 File (java.io.File)1 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CheckConfigBean (org.eweb4j.config.CheckConfigBean)1 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)1 IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)1 Result (org.eweb4j.mvc.action.annotation.Result)1 InterConfigBean (org.eweb4j.mvc.config.bean.InterConfigBean)1 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)1