Exemplo n.º 1
0
 /** 输出Sitemap */
 @At
 @Ok("raw:xml")
 public File sitemap() throws MalformedURLException, ParseException {
   String tmpdir = conf.get("website.tmp_dir", "/tmp");
   Files.createDirIfNoExists(tmpdir);
   final WebSitemapGenerator gen = new WebSitemapGenerator(urlbase, new File(tmpdir));
   gen.addUrl(urlbase + "/yvr/list");
   dao.each(
       Topic.class,
       Cnd.orderBy().desc("createTime"),
       dao.createPager(1, 1000),
       new Each<Topic>() {
         public void invoke(int index, Topic topic, int length) {
           try {
             Options options = new Options(urlbase + "/yvr/t/" + topic.getId());
             // TODO 从redis读取最后更新时间
             // options.lastMod(topic.getCreateAt());
             WebSitemapUrl url = new WebSitemapUrl(options);
             gen.addUrl(url);
           } catch (Exception e) {
             e.printStackTrace();
           }
         }
       });
   List<File> list = gen.write();
   if (list.size() > 0) return list.get(0);
   return null;
 }
Exemplo n.º 2
0
  /** 全文输出 */
  @At
  @Ok("raw:xml")
  public String rss() throws IOException, FeedException {
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    String urlbase = conf.get("website.urlbase", "https://nutz.cn");
    feed.setLink(urlbase);
    feed.setTitle(conf.get("website.title", "Nutz社区"));
    feed.setDescription(conf.get("website.description", "一个有爱的社区"));

    feed.setAuthor(conf.get("website.author", "wendal"));
    feed.setEncoding("UTF-8");
    feed.setLanguage("zh-cn");

    List<SyndEntry> entries = new ArrayList<SyndEntry>();
    SyndEntry entry;
    SyndContent description;
    List<Topic> list =
        dao.query(Topic.class, Cnd.orderBy().desc("createTime"), dao.createPager(1, 10));
    for (Topic topic : list) {
      dao.fetchLinks(topic, "author");
      entry = new SyndEntryImpl();
      entry.setTitle(topic.getTitle());
      entry.setLink(urlbase + "/yvr/t/" + topic.getId());
      entry.setPublishedDate(topic.getCreateTime());
      description = new SyndContentImpl();
      description.setType("text/html");
      description.setValue(Markdowns.toHtml(topic.getContent(), urlbase));
      entry.setDescription(description);
      entry.setAuthor(topic.getAuthor().getLoginname());
      entries.add(entry);
    }

    feed.setEntries(entries);
    if (list.size() > 0) {
      feed.setPublishedDate(list.get(0).getCreateTime());
    }

    SyndFeedOutput output = new SyndFeedOutput();
    return output.outputString(feed, true);
  }
Exemplo n.º 3
0
 public void init() throws Exception {
   String prefix = "cron.";
   for (String key : conf.getKeys()) {
     if (key.length() < prefix.length() + 1 || !key.startsWith(prefix)) continue;
     String name = key.substring(prefix.length());
     if ("pkgs".equals(name)) {
       log.debug("found cron job packages = " + conf.get(key));
       for (String pkg : Strings.splitIgnoreBlank(conf.get(key), ",")) {
         addPackage(pkg);
       }
       continue;
     }
     String cron = conf.get(key);
     log.debugf("job define name=%s cron=%s", name, cron);
     Class<?> klass = null;
     if (name.contains(".")) {
       klass = Lang.loadClass(name);
     } else {
       klass = Lang.loadClass(getClass().getPackage().getName() + ".job." + name);
     }
     cron(cron, klass);
   }
 }
  public void init() {
    try {
      super.init();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    // 添加全局变量
    Map<String, Object> share = groupTemplate.getSharedVars();
    if (share == null) {
      share = new NutMap();
      groupTemplate.setSharedVars(share);
    }
    Ioc ioc = Mvcs.getIoc();
    share.put("ioc", ioc);
    PropertiesProxy conf = ioc.get(PropertiesProxy.class, "conf");
    share.put("conf", conf.toMap());

    if (!conf.getBoolean("cdn.enable", false) || Strings.isBlank(conf.get("cdn.urlbase"))) {
      share.put("cdnbase", "");
    } else {
      share.put("cdnbase", conf.get("cdn.urlbase"));
      MarkdownFunction.cdnbase = conf.get("cdn.urlbase");
    }
  }
Exemplo n.º 5
0
  @SuppressWarnings("serial")
  public void init(NutConfig nc) {
    NutShiro.DefaultLoginURL = "/admin/logout";
    // 检查环境
    if (!Charset.defaultCharset().name().equalsIgnoreCase(Encoding.UTF8)) {
      log.warn("This project must run in UTF-8, pls add -Dfile.encoding=UTF-8 to JAVA_OPTS");
    }

    // 获取Ioc容器及Dao对象
    Ioc ioc = nc.getIoc();
    // 加载freemarker自定义标签 自定义宏路径
    ioc.get(Configuration.class)
        .setAutoImports(
            new HashMap<String, String>(2) {
              {
                put("p", "/ftl/pony/index.ftl");
                put("s", "/ftl/spring.ftl");
              }
            });
    ioc.get(FreeMarkerConfigurer.class, "mapTags");
    Dao dao = ioc.get(Dao.class);

    // 为全部标注了@Table的bean建表
    Daos.createTablesInPackage(dao, getClass().getPackage().getName() + ".bean", false);

    // 获取配置对象
    PropertiesProxy conf = ioc.get(PropertiesProxy.class, "conf");

    // 初始化SysLog,触发全局系统日志初始化
    ioc.get(SysLogService.class);

    // 初始化默认根用户
    User admin = dao.fetch(User.class, "admin");
    if (admin == null) {
      UserService us = ioc.get(UserService.class);
      admin = us.add("admin", "123456");
    }
    // 初始化游客用户
    User guest = dao.fetch(User.class, "guest");
    if (guest == null) {
      UserService us = ioc.get(UserService.class);
      guest = us.add("guest", "123456");
      UserProfile profile = dao.fetch(UserProfile.class, guest.getId());
      profile.setNickname("游客");
      dao.update(profile, "nickname");
    }

    // 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动
    ioc.get(NutQuartzCronJobFactory.class);

    // 权限系统初始化
    AuthorityService as = ioc.get(AuthorityService.class);
    as.initFormPackage("net.wendal.nutzbook");
    as.checkBasicRoles(admin);

    // 检查一下Ehcache CacheManager 是否正常.
    CacheManager cacheManager = ioc.get(CacheManager.class);
    log.debug("Ehcache CacheManager = " + cacheManager);
    // CachedNutDaoExecutor.DEBUG = true;

    // 启用FastClass执行入口方法
    Mvcs.disableFastClassInvoker = false;

    // 设置Markdown缓存
    if (cacheManager.getCache("markdown") == null) cacheManager.addCache("markdown");
    Markdowns.cache = cacheManager.getCache("markdown");
    if (conf.getBoolean("cdn.enable", false) && !Strings.isBlank(conf.get("cdn.urlbase"))) {
      MarkdownFunction.cdnbase = conf.get("cdn.urlbase");
    }
  }
Exemplo n.º 6
0
  @POST
  @At("/ht/savepamdata")
  @Ok("json")
  public JSONObject saveParameterDataFromAccess(
      String mdbPath,
      String stationId,
      String mdbTableName,
      String dateField,
      ServletContext context) {
    JSONObject json = new JSONObject();
    json.put(Constant.SUCCESS, false);
    // log.info("tableName:"+ mdbTableName + " timefield:" + dateField + " station:" +stationId) ;

    try {
      long start = System.currentTimeMillis();
      AccessUtil au = new AccessUtil(this.getAppRealPath(context) + mdbPath);
      Ioc ioc = Mvcs.getIoc();
      if (null != ioc) {
        PropertiesProxy prop = ioc.get(PropertiesProxy.class, "config");
        if (StringUtil.checkNotNull(prop.get("accessdriver"))) {
          au.setDburl(prop.get("accessdriver"));
        }
      }
      log.info(au.getDburl());
      Connection con = au.getConnection();

      Statement stat = con.createStatement();
      total = au.getTotalRowNumber(stat, mdbTableName);

      long pageTotal = au.getTotalPageNumber(total);

      long insertdb = 0;
      String tmpl =
          "select top PAGESPEED * from TABLENAME where ID >(select top 1 max(ID) from (select top BEFOREROW ID from TABLENAME order by ID asc)) order by ID asc ";
      tmpl =
          tmpl.replaceAll("ID", dateField)
              .replaceAll("PAGESPEED", String.valueOf(au.pageSpeed))
              .replaceAll("TABLENAME", mdbTableName);
      for (int i = 1; i <= pageTotal; i++) {
        int ii = (i - 1) * au.pageSpeed;
        String sql = tmpl.replaceAll("BEFOREROW", String.valueOf(ii));

        if (i == 1) {
          sql = sql.substring(0, sql.indexOf("where"));
          sql += "order by ID asc".replace("ID", dateField);
        }
        ResultSet rset = au.execSQL(stat, sql);
        if (null != rset) {
          // 把当前结果集中存在的fieldsName找出来
          List<String> available = new ArrayList<String>();
          List<String> list = au.getAllColumnName(rset.getMetaData());
          for (String str : au.fieldsName) {
            if (au.isExistString(list, str)) {
              available.add(str);
            }
          }

          List<Parameter> data = new ArrayList();
          while (rset.next()) {
            Parameter p = new Parameter();
            String time = rset.getString(dateField);

            p.setSrcTimestr(time);
            p.setCreateDate(DateUtil.convertStringToDate(time, DateUtil.pattern5));
            p.setStationID(stationId);
            // StringBuilder ss = new StringBuilder(rset.getString("mytime")).append("\t");
            // 遍历全部字段并存到java object 中
            for (String fn : available) {
              // ss.append(rset.getString(fn)).append("\t") ;
              BeanUtils.setProperty(p, fn, String.valueOf(rset.getString(fn)));
            }

            data.add(p);
            dls.insertNDY(tableName, p.getStationID(), null, p.getCreateDate());
          }
          // log.info("得到: " + data.size()) ;
          if (null != data && data.size() > 0) {
            // 把已经存在的对象删除掉
            batchDeleteParameter(stationId, data);
            insertdb += data.size();
            for (Parameter pa : data) {
              baseService.dao.insert(stationId, cov(pa, true));
            }
            // baseService.dao.insert(data) ;
            dls.insert("01", tableName, getHTLoginUserName());
            courr = insertdb;
          }
        }
      }
      // log.info("共得到: " + insertdb) ;
      long end = System.currentTimeMillis();

      json.put(Constant.SUCCESS, true);
      json.put("usetime", (end - start) / 1000 + " 秒");
      json.put("insertRow", insertdb + " 条");
      // log.debug(json.get("usetime")) ;
    } catch (Exception e) {
      // e.printStackTrace();
      json.put(Constant.INFO, e.getLocalizedMessage());
    } finally {
      return json;
    }
  }