Package org.nutz.lang.util

Examples of org.nutz.lang.util.Context


  public static String render(Segment tableName) {
    Object obj = get();
    if (null == obj || !tableName.hasKey())
      return tableName.toString();

    Context context = Lang.context();
    if (isPrimitive(obj)) {
      for (String key : tableName.keys())
        context.set(key, obj);
    } else if (obj instanceof Context) {
      for (String key : tableName.keys())
        context.set(key, ((Context) obj).get(key));
    } else if (obj instanceof Map<?, ?>) {
      for (String key : tableName.keys())
        context.set(key, ((Map<?, ?>) obj).get(key));
    } else {
      Mirror<?> mirror = Mirror.me(obj);
      for (String key : tableName.keys())
        context.set(key, mirror.getValue(obj, key));
    }
    return tableName.render(context).toString();
  }
View Full Code Here


  }
  public String getVal() {
    return val;
  }
  public Object fetchVal(){
    Context context = ec.getContext();
    if(context != null && context.has(val)){
      return context.get(val);
    }
    return null;
  }
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

  private ElFieldMacro() {}

  public void onAfter(Connection conn, ResultSet rs) throws SQLException {
    if (rs.next()) {
      Context context = entityField.getEntity().wrapAsContext(getOperatingObject());
      Object value = bin.eval(context);
      entityField.setValue(getOperatingObject(), value);
    }
  }
View Full Code Here

    }
  }

  public static <T> T evalObj(NutConfig config, Class<T> type, String[] args) {
    // 用上下文替换参数
    Context context = config.getLoadingContext();
    for (int i = 0; i < args.length; i++) {
      args[i] = Segments.replace(args[i], context);
    }
    // 判断是否是 Ioc 注入
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

  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 = req.getSession()
                  .getServletContext()
                  .getAttribute(Loading.CONTEXT_NAME);
    if (globalContext != null) {
      context.putAll((Context) globalContext);
    }

    // 请求对象的属性列表
    Map<String,Object> a = new HashMap<String, Object>();
    for (Enumeration<String> en = req.getAttributeNames(); en.hasMoreElements();) {
      String tem = en.nextElement();
      a.put(tem, req.getAttribute(tem));
    }
    context.set("a", a);//TODO 是否应该用a呢? 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(req.getSession().getServletContext());
    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 ElFieldMacro() {}

    public void onAfter(Connection conn, ResultSet rs) throws SQLException {
        Context context = entityField.getEntity().wrapAsContext(getOperatingObject());
        Object value = bin.eval(context);
        entityField.setValue(getOperatingObject(), value);
    }
View Full Code Here

     * 保存对象到attribute
     */
    public static void putRequestAttribute(HttpServletRequest req, Object re){
        if (null != re){
            if(re instanceof Context){
                Context context = (Context) re;
                for(String key : context.keys()){
                    req.setAttribute(key, context.get(key));
                }
            } else {
                req.setAttribute(ViewProcessor.DEFAULT_ATTRIBUTE, re);
            }
        }
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.