Example #1
0
  public void run() {
    // ----------------------------------------- 建立
    log.infof("start TcpServer [%s] @ %d", Thread.currentThread().getName(), port);
    try {
      listener = new ServerSocket(port);
    } catch (IOException e1) {
      throw Lang.wrapThrow(e1);
    }
    // ----------------------------------------- 循环
    log.infof("TcpServer listen @ %d", port);
    while (!stop) {
      log.info("before accept ...");
      Socket socket = null;
      try {
        socket = listener.accept();
      } catch (IOException e) {
        log.fatalf("Fail to accept %s @ %d , System.exit!", Thread.currentThread().getName(), port);
        System.exit(0);
      }

      log.info("do listen ...");
      listen(socket);
      log.infof(
          "done for listen [%s:%d], stop=%b",
          socket.getInetAddress().getHostName(), socket.getPort(), stop);
    }
    // ----------------------------------------- 关闭
    try {
      listener.close();
      log.infof("TcpServer shutdown @ %d", port);
    } catch (IOException e) {
      throw Lang.wrapThrow(e);
    }
  }
Example #2
0
 /**
  * 获取colName所在的行数
  *
  * @param meta 从连接中取出的ResultSetMetaData
  * @param colName 字段名
  * @return 所在的索引,如果不存在就抛出异常
  * @throws SQLException 指定的colName找不到
  */
 public static int getColumnIndex(ResultSetMetaData meta, String colName) throws SQLException {
   if (meta == null) return 0;
   int columnCount = meta.getColumnCount();
   for (int i = 1; i <= columnCount; i++)
     if (meta.getColumnName(i).equalsIgnoreCase(colName)) return i;
   // TODO 尝试一下meta.getColumnLabel?
   log.infof("Can not find @Column(%s) in table/view (%s)", colName, meta.getTableName(1));
   throw Lang.makeThrow(SQLException.class, "Can not find @Column(%s)", colName);
 }
Example #3
0
 /**
  * 关闭本DaoUp,将关闭DataSource并将dao和dataSource置为null!!!
  *
  * <p><b>只能在程序关闭时调用,严禁在每次Dao操作后调用!!</b>
  */
 public synchronized void close() {
   if (dao == null) return;
   log.infof("shutdown DaoUp(name=%s)", name);
   try {
     Mirror.me(dataSource).invoke(dataSource, "close");
   } catch (Throwable e) {
   }
   this.dataSource = null;
   this.dao = null;
 }
Example #4
0
  /**
   * 启动的主函数,接受一个参数,为 web 服务器的配置文件路径。如果没有这个参数,默认在 classpath 下寻找 "web.properties" 文件。
   *
   * <p>这个文件遵循 Nutz 多行属性文件规范,同时必须具备如下的键:
   *
   * <ul>
   *   <li>"app-root" - 应用的根路径,比如 "~/workspace/git/danoo/strato/domain/ROOT"
   *   <li>"app-port" - 应用监听的端口,比如 8080
   *   <li>"app-rs" - 应用静态资源的地址前缀,比如 "http://localhost/strato",或者 "/rs" 等
   *   <li>"app-classpath" - 应用的类路径,可多行
   *   <li>"admin-port" - 应用的管理端口,比如 8081
   * </ul>
   *
   * 这个文件的例子,请参看源码 conf 目录下的 web.properties
   *
   * @param args 接受一个参数作为 web 服务器的配置文件路径
   */
  public static void main(String[] args) {
    String path = Strings.sBlank(Lang.first(args), Webs.CONF_PATH);

    log.infof("launch by '%s'", path);

    final WebServer server = new WebServer(new WebConfig(path));

    server.run();

    log.info("Server is down!");
  }
Example #5
0
 public void init() {
   if (topicGlobalWatchers != null) {
     for (String username : Strings.splitIgnoreBlank(topicGlobalWatchers)) {
       User user = dao.fetch(User.class, username);
       if (user == null) {
         log.infof("no such user[name=%s] for topic watch", username);
         continue;
       }
       globalWatcherIds.add(user.getId());
     }
   }
 }
Example #6
0
  /** 这是最后的大招了,查一下形参的名字,作为@Param("形参名")进行处理 */
  protected ParamInjector paramNameInject(Method method, int index) {
    if (!Lang.isAndroid) {
      List<String> names = MethodParamNamesScaner.getParamNames(method);
      if (names != null)
        return new NameInjector(names.get(index), null, method.getParameterTypes()[index], null);
      else if (log.isInfoEnabled())
        log.infof(
            "Complie without debug info? can't deduce param name. fail back to PathArgInjector!! index=%d > %s",
            index, method);
    }

    return new PathArgInjector(method.getParameterTypes()[index]);
  }
Example #7
0
 public AnnotationIocLoader(String... packages) {
   for (String packageZ : packages)
     for (Class<?> classZ : Scans.me().scanPackage(packageZ)) addClass(classZ);
   if (map.size() > 0) {
     if (log.isInfoEnabled())
       log.infof(
           "Scan complete ! Found %s classes in %s base-packages!\nbeans = %s",
           map.size(), packages.length, Castors.me().castToString(map.keySet()));
   } else {
     log.warn(
         "NONE Annotation-Class found!! Check your configure or report a bug!! packages="
             + Arrays.toString(packages));
   }
 }
  /** 添加URL时初始化权限 */
  @Override
  public void add(ActionChainMaker maker, ActionInfo ai, NutConfig config) {
    super.add(maker, ai, config);
    Method method = ai.getMethod();
    Authority a = method.getAnnotation(Authority.class);
    if (null == a) {
      return;
    }
    String id = a.value();
    String module = a.module();
    String desc = a.desc();
    boolean isDefault = a.isDefault();

    // 在这里可以把权限写入数据库以及其它操作,此处为运行demo简单只写日志.
    log.infof("AuthID=%s Module=%s Default=%s Desc=%s", id, module, isDefault, desc);
  }
Example #9
0
  public Object[] adapt(
      ServletContext sc, HttpServletRequest req, HttpServletResponse resp, String[] pathArgs) {

    Object[] args = new Object[argTypes.length];

    if (args.length != injs.length)
      throw new IllegalArgumentException(
          "args.length != injs.length , You get a bug, pls report it!!");

    AdaptorErrorContext errCtx = null;
    // 也许用户有自己的AdaptorErrorContext实现哦
    if (argTypes.length > 0) {
      if (AdaptorErrorContext.class.isAssignableFrom(argTypes[argTypes.length - 1]))
        errCtx =
            (AdaptorErrorContext) Mirror.me(argTypes[argTypes.length - 1]).born(argTypes.length);
    }

    Object obj;
    try {
      obj = getReferObject(sc, req, resp, pathArgs);
    } catch (Throwable e) {
      if (errCtx != null) {
        if (log.isInfoEnabled())
          log.info(
              "Adapter Error catched , but I found AdaptorErrorContext param, so, set it to args, and continue",
              e);
        errCtx.setAdaptorError(e, this);
        args[args.length - 1] = errCtx;
        return args;
      }
      throw Lang.wrapThrow(e);
    }

    int len = Math.min(args.length, null == pathArgs ? 0 : pathArgs.length);
    for (int i = 0; i < args.length; i++) {
      Object value = null;
      if (i < len) { // 路径参数
        value = null == pathArgs ? null : pathArgs[i];
      } else { // 普通参数
        value = obj;
      }
      try {
        args[i] = injs[i].get(sc, req, resp, value);
      } catch (Throwable e) {
        if (errCtx != null) {
          log.infof("Adapter Param Error(%s) index=%d", method, i, e);
          errCtx.setError(i, e, method, value, injs[i]); // 先错误保存起来,全部转好了,再判断是否需要抛出
        } else throw Lang.wrapThrow(e);
      }
      if (args[i] == null && argTypes[i].isPrimitive()) {
        args[i] = Lang.getPrimitiveDefaultValue(argTypes[i]);
      }
    }

    // 看看是否有任何错误
    if (errCtx == null) return args;
    for (Throwable err : errCtx.getErrors()) {
      if (err == null) continue;
      int lastParam = argTypes.length - 1;
      if (AdaptorErrorContext.class.isAssignableFrom(argTypes[lastParam])) {
        if (log.isInfoEnabled())
          log.info(
              "Adapter Param Error catched , but I found AdaptorErrorContext param, so, set it to args, and continue");
        args[lastParam] = errCtx;
        return args;
      }
      // 没有AdaptorErrorContext参数? 那好吧,按之前的方式,抛出异常
      throw Lang.wrapThrow(err);
    }

    return args;
  }
Example #10
0
 /**
  * 主动设置Dao实例
  *
  * @param dao Dao实例
  */
 public void setDao(Dao dao) {
   if (this.dao != null) log.infof("override old Dao=%s by new Dao=%s", this.dao, dao);
   this.dao = dao;
 }