Exemplo n.º 1
0
public class Context {

  private String targetIO = "";

  private Object extraContext = "";

  private int port = 0;

  private final String TMPFI = "/tmp/fi/";
  private final String HADOOP_USERNAME = "******" + System.getenv("USER");
  private final String HADOOP_STORAGE_DIR = TMPFI + HADOOP_USERNAME + "/";

  // ********************************************
  // rest
  // ********************************************

  public Context() {}

  public Context(String s) {
    targetIO = new String(s);
  }

  public Context(int port) {
    this.port = port;
  }

  public int getPort() {
    return port;
  }

  public String getTargetIO() {
    return targetIO;
  }

  public void setExtraContext(Object extra) {
    this.extraContext = extra;
  }

  public Object getExtraContext() {
    return extraContext;
  }

  public String toString() {
    String tmp = targetIO;
    if (targetIO.contains("/tmp/" + HADOOP_USERNAME))
      tmp = tmp.replaceFirst("/tmp/" + HADOOP_USERNAME, "/thh/");
    if (targetIO.contains(HADOOP_STORAGE_DIR)) tmp = tmp.replaceFirst(HADOOP_STORAGE_DIR, "/rhh/");
    return tmp;
  }

  public void setTargetIO(String f) {
    targetIO = new String(f);
  }
}
Exemplo n.º 2
0
/** 感谢对开源热心的少年们提供的各种代码,这个DD是站在巨人的肩膀上的 感谢 @波总 的JFinal,@闲.大赋 的beetl,向你们致敬! :) */
public class Myconfig extends JFinalConfig {
  private String json = java.lang.System.getenv("VCAP_SERVICES");
  private boolean isLocal = StringKit.isBlank(json);
  /** 配置常量 */
  public void configConstant(Constants me) {
    loadPropertyFile("classes/config.txt");
    if (isLocal) {
      me.setDevMode(true);
    }
    me.setError404View("/common/404.html");
    me.setError500View("/common/500.html");
    me.setMainRenderFactory(new BeetlRenderFactory());
    GroupTemplate gt = BeetlRenderFactory.groupTemplate;
    gt.setStatementStart("@");
    gt.setStatementEnd(null);
  }

  /** 配置路由 */
  public void configRoute(Routes me) {
    me.add("/", IndexController.class).add("/topic", TopicController.class);
    me.add("/post", PostController.class).add("/reply", ReplyController.class);
    me.add("/admin/welcome", WelcomeController.class).add("/admin/module", ModuleController.class);
  }

  /** 配置插件 */
  public void configPlugin(Plugins me) {
    // [ copy from @ mike 的适配器 :) ]
    String dbname, username, password, host, port, driver;
    driver = getProperty("driverClass");
    if (isLocal) {
      dbname = getProperty("dbname");
      username = getProperty("username");
      port = getProperty("port");
      host = getProperty("host");
      password = getProperty("password");
    } else {
      JSONObject credentials =
          JSONObject.parseObject(json)
              .getJSONArray("mysql-5.1")
              .getJSONObject(0)
              .getJSONObject("credentials");
      host = credentials.getString("host");
      port = credentials.getString("port");
      dbname = credentials.getString("name");
      username = credentials.getString("username");
      password = credentials.getString("password");
    }
    DruidPlugin druidPlugin =
        new DruidPlugin(
            "jdbc:mysql://" + host + ":" + port + "/" + dbname, username, password, driver);
    druidPlugin.setInitialSize(3).setMaxActive(10);
    me.add(druidPlugin);
    // 配置ActiveRecord插件
    ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
    if (isLocal) {
      arp.setShowSql(true);
    }
    arp.addMapping("module", Module.class)
        .addMapping("topic", Topic.class)
        .addMapping("post", Post.class);
    arp.addMapping("reply", Reply.class).addMapping("sub_module", SubModule.class);
    me.add(arp);
    // 缓存插件
    me.add(new EhCachePlugin());
  }

  /** 配置全局拦截器 */
  public void configInterceptor(Interceptors me) {
    // 支持在jfinal中用sesion
    me.add(new SessionInViewInterceptor());
    me.add(new GlobalInterceptor());
  }

  /** 配置处理器 */
  public void configHandler(Handlers me) {}

  /** 初始化常量 */
  public void afterJFinalStart() {
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    MyConstants.PAGE_SIZE = getPropertyToInt("page_size", 10);
    MyConstants.PAGE_SIZE_OF_REPLY = getPropertyToInt("page_size_of_reply", 3);
    MyConstants.MODULE_LIST = Module.dao.getModuleList();
    MyConstants.TOPIC_CONTENT_PREVIEW_SIZE = getPropertyToInt("topic_preview_size", 280);
    MyConstants.ADMIN_NAME = getProperty("admin_name");
    MyConstants.ADMIN_PASSWORD = getProperty("admin_password");
    MyConstants.SIDEBAR_TOPIC_SIZE = getPropertyToInt("sidebar_topic_size", 6);
    MyConstants.PAGE_SIZE_FOR_ADMIN = getPropertyToInt("page_size_for_admin", 30);
  }

  /** 建议使用 JFinal 手册推荐的方式启动项目 运行此 main 方法可以启动项目,此main方法可以放置在任意的Class类定义中,不一定要放于此 */
  public static void main(String[] args) throws Exception {
    JFinal.start("Web", 80, "/", 5);
  }
}
Exemplo n.º 3
0
public class JobsTest {

  private String public_key = System.getenv("GENGO_PUBKEY");
  private String private_key = System.getenv("GENGO_PRIVKEY");

  @Test
  public void testPostJobsText() throws GengoException, JSONException, InterruptedException {
    // POST a text job
    GengoClient Gengo = new GengoClient(this.public_key, this.private_key, true);
    List<TranslationJob> jobList = new ArrayList<TranslationJob>();
    TranslationJob job =
        new TranslationJob("java client test", "This is a short story.", "en", "ja", Tier.STANDARD);
    TranslationJob job2 =
        new TranslationJob(
            "java client test 2", "This is another short story.", "en", "ja", Tier.STANDARD);
    job.setForceNewTranslation(true);
    job2.setForceNewTranslation(true);
    jobList.add(job);
    jobList.add(job2);
    JSONObject postResp = Gengo.postTranslationJobs(jobList, true);
    // Make assertions on POST response
    Assert.assertEquals(postResp.getString("opstat"), "ok");
    Assert.assertTrue(postResp.has("response"));
    JSONObject postResponse = postResp.getJSONObject("response");
    Assert.assertEquals(postResponse.get("job_count"), 2);
    Assert.assertTrue(postResponse.has("credits_used"));
    Assert.assertTrue(postResponse.has("order_id"));
    String orderId = postResponse.getString("order_id");

    // GET order
    // We have to sleep to give jobs processor some time to process the jobs,
    // then the IDs should be in jobs_available
    Thread.sleep(10000);
    JSONObject getOrderResp = Gengo.getOrderJobs(Integer.parseInt(orderId));
    // Make assertions on GET response
    Assert.assertEquals(getOrderResp.getString("opstat"), "ok");
    Assert.assertTrue(getOrderResp.has("response"));
    JSONObject getOrderResponse = getOrderResp.getJSONObject("response");
    String jobId =
        getOrderResponse.getJSONObject("order").getJSONArray("jobs_available").getString(1);

    // GET job
    JSONObject getJobResp = Gengo.getTranslationJob(Integer.parseInt(jobId));
    Assert.assertEquals(getJobResp.getString("opstat"), "ok");
    Assert.assertTrue(getJobResp.has("response"));
    JSONObject getJobResponse = getJobResp.getJSONObject("response");
    String bodySrc = getJobResponse.getJSONObject("job").getString("body_src");
    Assert.assertEquals(bodySrc, "This is a short story.");
  }

  @Test
  public void testPostJobsFiles() throws GengoException, JSONException, InterruptedException {
    GengoClient Gengo = new GengoClient(this.public_key, this.private_key, true);
    Map<String, String> filePaths = new HashMap<String, String>();
    FileJob job1 = new FileJob("someslug", "file_job_1", "en", "ja", Tier.STANDARD);
    FileJob job2 = new FileJob("someslug2", "file_job_2", "en", "ja", Tier.STANDARD);
    filePaths.put("file_job_1", "examples/testfiles/test_file1.txt");
    filePaths.put("file_job_2", "examples/testfiles/test_file2.txt");
    List<FileJob> jobList = new ArrayList<FileJob>();
    jobList.add(job1);
    jobList.add(job2);
    JSONObject rsp = Gengo.determineTranslationCostFiles(jobList, filePaths);
    Assert.assertEquals(rsp.getString("opstat"), "ok");
    Assert.assertTrue(rsp.has("response"));
  }

  @Test(expected = GengoException.class)
  public void testPostTranslationJobs() throws GengoException, JSONException, InterruptedException {
    GengoClient Gengo = new GengoClient(this.public_key, this.private_key, true);
    // there is no Afrikaans source language, so we expect this to raise an exception
    TranslationJob job =
        new TranslationJob("label_test", "'n Bietjie afrikaanse teks", "af", "es", Tier.STANDARD);
    List<TranslationJob> jobList = new ArrayList<TranslationJob>();
    jobList.add(job);
    JSONObject response = Gengo.postTranslationJobs(jobList, false);
  }
}