Package org.nutz.lang.util

Examples of org.nutz.lang.util.Context


   
    // ==================================================================

    // 重置当前线程所持有的对象
    public static Context resetALL() {
        Context context = ctx.reqThreadLocal.get();
        NAME.set(null);
        ctx.reqThreadLocal.set(Lang.context());
        return context;
    }
View Full Code Here


    protected String evalPath(HttpServletRequest req, Object obj) {
        if (null == dest)
            return null;

        Context context = Lang.context();

        // 解析每个表达式
        Context expContext = createContext(req, obj);
        for (Entry<String, El> en : exps.entrySet())
            context.set(en.getKey(), en.getValue().eval(expContext));

        // 生成解析后的路径
        return Strings.trim(this.dest.render(context).toString());
View Full Code Here

     *            入口函数的返回值
     * @return 上下文对象
     */
    @SuppressWarnings("unchecked")
    public static Context createContext(HttpServletRequest req, Object obj) {
        Context context = Lang.context();
        // 复制全局的上下文对象
        Object globalContext = Mvcs.getServletContext()
                                    .getAttribute(Loading.CONTEXT_NAME);
        if (globalContext != null) {
            context.putAll((Context) globalContext);
        }

        // 请求对象的属性列表
        Map<String,Object> req_attr = new HashMap<String, Object>();
        for (Enumeration<String> en = req.getAttributeNames(); en.hasMoreElements();) {
            String tem = en.nextElement();
            req_attr.put(tem, req.getAttribute(tem));
        }
        context.set("a", req_attr);//兼容最初的写法
        context.set("req_attr", req_attr);
       
        HttpSession session = Mvcs.getHttpSession(false);
        if (session != null) {
          Map<String,Object> session_attr = new HashMap<String, Object>();
            for (Enumeration<String> en = session.getAttributeNames(); en.hasMoreElements();) {
                String tem = en.nextElement();
                session_attr.put(tem, session.getAttribute(tem));
            }
            context.set("session_attr", session_attr);
        }
        // 请求的参数表,需要兼容之前的p.参数, Fix issue 418
        Map<String,String> p = new HashMap<String, String>();
        for (Object o : req.getParameterMap().keySet()) {
            String key = (String) o;
            String value = req.getParameter(key);
            p.put(key, value);
            context.set(key, value);//以支持直接获取请求参数
        }
        context.set("p", p);
       
        Map<String, String> u = new HashMap<String, String>();
        AtMap at = Mvcs.getAtMap();
        if (at != null) {
            for(Object o : at.keys()){
                String key = (String) o;
                u.put(key, at.get(key));
            }
            context.set("u", u);
        }
       
        // 加入返回对象
        if (null != obj)
            context.set(ViewProcessor.DEFAULT_ATTRIBUTE, obj);
        return context;
    }
View Full Code Here

    }

    private static void createContext(NutConfig config) {
        // 构建一个上下文对象,方便子类获取更多的环境信息
        // 同时,所有 Filter 和 Adaptor 都可以用 ${app.root} 来填充自己
        Context context = Lang.context();
        context.set("app.root", config.getAppRoot());

        if (log.isDebugEnabled()) {
            log.debugf(">> app.root = %s", config.getAppRoot());
        }

        // 载入环境变量
        for (Entry<String, String> entry : System.getenv().entrySet())
            context.set("env." + entry.getKey(), entry.getValue());
        // 载入系统变量
        for (Entry<Object, Object> entry : System.getProperties().entrySet())
            context.set("sys." + entry.getKey(), entry.getValue());

        if (log.isTraceEnabled()) {
            log.tracef(">>\nCONTEXT %s", Json.toJson(context, JsonFormat.nice()));
        }
        config.getServletContext().setAttribute(Loading.CONTEXT_NAME, context);
View Full Code Here

public class SegmentsTest {

    @Test
    public void test_simple_replace() {
        String ptn = "1${A}2${B}3${C}4";
        Context context = Lang.context();
        context.set("B", "haha");
        String str = Segments.replace(ptn, context);

        assertEquals("1${A}2haha3${C}4", str);
    }
View Full Code Here

      if (proxyFilter != null) {
        proxyFilter.doFilter(req, resp, chain);
        return;
      }
        String preName = Mvcs.getName();
        Context preContext = Mvcs.resetALL();
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        try {
            if (sp != null)
                req = sp.filter(request,
View Full Code Here

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String preName = Mvcs.getName();
        Context preContext = Mvcs.resetALL();
        try {
            if (sp != null)
                req = sp.filter(req, resp, getServletContext());
            Mvcs.set(selfName, req, resp);
            if (!handler.handle(req, resp))
View Full Code Here

            }

            if (log.isInfoEnabled())
                log.infof("Local socket is up at :%d with %d action ready", port, actions.size());

            final Context context = Lang.context();
            context.set("stop", false);
            /*
             * 启动一个守护线程,判断是否该关闭 socket 服务
             */
            (new Thread() {
                @Override
                public void run() {
                    setName("Nutz.Sockets monitor thread");
                    while (true) {
                        try {
                            Thread.sleep(1000);
                            if (context.getBoolean("stop")) {
                                try {
                                    server.close();
                                }
                                catch (Throwable e) {}
                                return;
                            }
                        }
                        catch (Throwable e) {}
                    }
                }
            }).start();
            /*
             * 准备 SocketAtom 的生成器
             */
            Borning borning = Mirror.me(klass).getBorningByArgTypes(Context.class,
                                                                    Socket.class,
                                                                    SocketActionTable.class);
            if (borning == null) {
                log.error("boring == null !!!!");
                return;
            }
            /*
             * 进入监听循环
             */
            while (!context.getBoolean("stop")) {
                try {
                    if (log.isDebugEnabled())
                        log.debug("Waiting for new socket");
                    Socket socket = server.accept();
                    if (context.getBoolean("stop")) {
                        Sockets.safeClose(socket);
                        break;// 监护线程也许还是睡觉,还没来得及关掉哦,所以自己检查一下
                    }
                    if (log.isDebugEnabled())
                        log.debug("accept a new socket, create new SocketAtom to handle it ...");
View Full Code Here

   * @param errorMsg
   * @param errors
   * @return
   */
  public static boolean el(String fieldName, Object obj, String el, String errorMsg, Errors errors) {
    Context context = Lang.context();
    context.set("value", obj);
    Object val = El.eval(context, el);
    if (! (val instanceof Boolean)) {
      errors.add(fieldName, errorMsg);
      return false;
    }
View Full Code Here

            }

            if (log.isInfoEnabled())
                log.infof("Local socket is up at :%d with %d action ready", port, actions.size());

            final Context context = Lang.context();
            context.set("stop", false);
            /*
             * 启动一个守护线程,判断是否该关闭 socket 服务
             */
            (new Thread() {
                @Override
                public void run() {
                    setName("Nutz.Sockets monitor thread");
                    while (true) {
                        try {
                            Thread.sleep(1000);
                            if (context.getBoolean("stop")) {
                                try {
                                    server.close();
                                }
                                catch (Throwable e) {}
                                return;
                            }
                        }
                        catch (Throwable e) {}
                    }
                }
            }).start();
            /*
             * 准备 SocketAtom 的生成器
             */
            Borning borning = Mirror.me(klass).getBorningByArgTypes(Context.class,
                                                                    Socket.class,
                                                                    SocketActionTable.class);
            if (borning == null) {
                log.error("boring == null !!!!");
                return;
            }
            /*
             * 进入监听循环
             */
            while (!context.getBoolean("stop")) {
                try {
                    if (log.isDebugEnabled())
                        log.debug("Waiting for new socket");
                    Socket socket = server.accept();
                    if (context.getBoolean("stop")) {
                        Sockets.safeClose(socket);
                        break;// 监护线程也许还是睡觉,还没来得及关掉哦,所以自己检查一下
                    }
                    if (log.isDebugEnabled())
                        log.debug("accept a new socket, create new SocketAtom to handle it ...");
View Full Code Here

TOP

Related Classes of org.nutz.lang.util.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.