@Test(groups = "dev")
  public void autoWrapTest() throws EventDeliveryException {
    ctx.put(MongoSink.AUTO_WRAP, Boolean.toString(true));
    ctx.put(MongoSink.DB_NAME, "test_wrap");

    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    Transaction tx = channel.getTransaction();
    tx.begin();
    String msg =
        "2012/10/26 11:23:08 [error] 7289#0: *6430831 open() \"/usr/local/nginx/html/50x.html\" failed (2: No such file or directory), client: 10.160.105.161, server: sg15.redatoms.com, request: \"POST /mojo/ajax/embed HTTP/1.0\", upstream: \"fastcgi://unix:/tmp/php-fpm.sock:\", host: \"sg15.redatoms.com\", referrer: \"http://sg15.redatoms.com/mojo/mobile/package\"";

    Event e = EventBuilder.withBody(msg.getBytes());
    channel.put(e);
    tx.commit();
    tx.close();

    sink.process();
    sink.stop();

    DB db = mongo.getDB("test_wrap");
    DBCollection collection = db.getCollection("test_log");
    DBCursor cursor = collection.find(new BasicDBObject(MongoSink.DEFAULT_WRAP_FIELD, msg));
    assertTrue(cursor.hasNext());
    DBObject dbObject = cursor.next();
    assertNotNull(dbObject);
    assertEquals(dbObject.get(MongoSink.DEFAULT_WRAP_FIELD), msg);
    mongo.dropDatabase("test_wrap");
  }
Пример #2
0
 private void initContext() {
   context.put(JavacTaskImpl.class, this);
   if (context.get(TaskListener.class) != null)
     context.put(TaskListener.class, (TaskListener) null);
   if (taskListener != null) context.put(TaskListener.class, ccw.wrap(taskListener));
   // initialize compiler's default locale
   context.put(Locale.class, locale);
 }
Пример #3
0
  /**
   * Build the top level menu
   *
   * @param context
   */
  private void buildMenu(Context context) {
    // build the menu
    Menu bar = new MenuImpl();
    bar.add(new MenuEntry(rb.getString("archive.button.single"), "doView_single"));
    bar.add(new MenuEntry(rb.getString("archive.button.batch"), "doView_batch"));
    bar.add(new MenuEntry(rb.getString("archive.button.download"), "doView_download"));

    context.put(Menu.CONTEXT_MENU, bar);
    context.put(Menu.CONTEXT_ACTION, "ArchiveAction");
  }
  @Test(groups = "dev")
  public void timestampExistingFieldTest() throws EventDeliveryException, ParseException {
    ctx.put(MongoSink.MODEL, MongoSink.CollectionModel.dynamic.name());
    String tsField = "createdOn";
    ctx.put(MongoSink.TIMESTAMP_FIELD, tsField);
    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    JSONObject msg = new JSONObject();
    msg.put("age", 11);
    msg.put("birthday", new Date().getTime());
    String dateText = "2013-02-19T14:20:53+08:00";
    msg.put(tsField, dateText);

    Transaction tx;

    for (int i = 0; i < 10; i++) {
      tx = channel.getTransaction();
      tx.begin();
      msg.put("name", "test" + i);
      JSONObject header = new JSONObject();
      header.put(MongoSink.COLLECTION, "my_events");
      header.put(MongoSink.DB_NAME, "dynamic_db");

      Event e = EventBuilder.withBody(msg.toJSONString().getBytes(), header);
      channel.put(e);
      tx.commit();
      tx.close();
    }
    sink.process();
    sink.stop();

    msg.put(tsField, MongoSink.dateTimeFormatter.parseDateTime(dateText).toDate());
    for (int i = 0; i < 10; i++) {
      msg.put("name", "test" + i);

      System.out.println("i = " + i);

      DB db = mongo.getDB("dynamic_db");
      DBCollection collection = db.getCollection("my_events");
      DBCursor cursor = collection.find(new BasicDBObject(msg));
      assertTrue(cursor.hasNext());
      DBObject dbObject = cursor.next();
      assertNotNull(dbObject);
      assertEquals(dbObject.get("name"), msg.get("name"));
      assertEquals(dbObject.get("age"), msg.get("age"));
      assertEquals(dbObject.get("birthday"), msg.get("birthday"));
      assertTrue(dbObject.get(tsField) instanceof Date);
      System.out.println("ts = " + dbObject.get(tsField));
    }
  }
Пример #5
0
  /** build the context for batch archive confirm */
  public String buildBatchModeArchiveConfirmContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);

    // go to template
    return "-batch-archive-confirm";
  }
Пример #6
0
  /** build the context for single mode import/export */
  public String buildSingleModeContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);
    buildMenu(context);

    return "";
  }
Пример #7
0
  /** build the context */
  public String buildMainPanelContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    String template = null;

    // if not logged in as the super user, we won't do anything
    if (!securityService.isSuperUser()) {
      context.put("tlang", rb);
      return (String) getContext(rundata).get("template") + "_noaccess";
    }

    // check mode and dispatch
    String mode = (String) state.getAttribute(STATE_MODE);

    if (StringUtils.equals(mode, SINGLE_MODE)) {
      template = buildSingleModeContext(portlet, context, rundata, state);
    } else if (StringUtils.equals(mode, BATCH_MODE)) {
      template = buildBatchModeContext(portlet, context, rundata, state);
    } else if (StringUtils.equals(mode, BATCH_ARCHIVE_CONFIRM_MODE)) {
      template = buildBatchModeArchiveConfirmContext(portlet, context, rundata, state);
    } else if (StringUtils.equals(mode, DOWNLOAD_MODE)) {
      template = buildDownloadContext(portlet, context, rundata, state);
    }

    return (String) getContext(rundata).get("template") + template;
  } // buildMainPanelContext
  @Test(groups = "dev")
  public void sinkSingleModelTest() throws EventDeliveryException {
    ctx.put(MongoSink.MODEL, MongoSink.CollectionModel.single.name());

    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    Transaction tx = channel.getTransaction();
    tx.begin();
    JSONObject msg = new JSONObject();
    msg.put("name", "test");
    msg.put("age", 11);
    msg.put("birthday", new Date().getTime());

    Event e = EventBuilder.withBody(msg.toJSONString().getBytes());
    channel.put(e);
    tx.commit();
    tx.close();

    sink.process();
    sink.stop();

    DB db = mongo.getDB("test_events");
    DBCollection collection = db.getCollection("test_log");
    DBCursor cursor = collection.find(new BasicDBObject(msg));
    assertTrue(cursor.hasNext());
    DBObject dbObject = cursor.next();
    assertNotNull(dbObject);
    assertEquals(dbObject.get("name"), msg.get("name"));
    assertEquals(dbObject.get("age"), msg.get("age"));
    assertEquals(dbObject.get("birthday"), msg.get("birthday"));
  }
Пример #9
0
  /** build the context for batch import/export */
  public String buildBatchModeContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);
    buildMenu(context);

    // check if we are already running. Template will render just the message if so
    String statusMessage = getCurrentBatchArchiveStatusMessage();
    if (StringUtils.isNotBlank(statusMessage)) {
      context.put("isRunning", true);
      context.put("statusMessage", statusMessage);
    }

    // get list of terms
    List<AcademicSession> terms = courseManagementService.getAcademicSessions();
    context.put("terms", terms);

    return "-batch";
  }
  @BeforeMethod(groups = {"dev"})
  public static void setup() throws UnknownHostException {
    mongo = new Mongo("localhost", 27017);

    Map<String, String> ctxMap = new HashMap<String, String>();
    ctxMap.put(MongoSink.HOST, "localhost");
    ctxMap.put(MongoSink.PORT, "27017");
    ctxMap.put(MongoSink.DB_NAME, "test_events");
    ctxMap.put(MongoSink.COLLECTION, "test_log");
    ctxMap.put(MongoSink.BATCH_SIZE, "100");

    ctx.putAll(ctxMap);

    Context channelCtx = new Context();
    channelCtx.put("capacity", "1000000");
    channelCtx.put("transactionCapacity", "1000000");
    channel = new MemoryChannel();
    Configurables.configure(channel, channelCtx);
  }
Пример #11
0
  private Context generateCtx() {

    Context ctx = new Context();
    ctx.put(Context.FLD_CATEGORY, getCategory());
    ctx.put(Context.FLD_SUBJECT, getSubject());
    ctx.put(Context.FLD_PREDICATE, getPredicate());
    ctx.put(Context.FLD_OBJECT, getObject());
    ctx.put(Context.FLD_START_FROM, getStartFrom());
    ctx.put(Context.FLD_END_AT, getEndAt());
    ctx.put(Context.FLD_SITE, getSite());
    ctx.put(Context.FLD_OWNER, getOwner());

    // FLD_ID, FLD_TIMESTAMP, FLD_CONSISTENCY are unnecessary

    ctx.put(Context.FLD_TIMESTAMP, TimeFormat.convert(timestamp)); // Set FLD_TIMESTAMP

    return ctx;
  }
  @Test(groups = "dev")
  public void sinkDynamicDbTest() throws EventDeliveryException {
    ctx.put(MongoSink.MODEL, MongoSink.CollectionModel.dynamic.name());
    MongoSink sink = new MongoSink();
    Configurables.configure(sink, ctx);

    sink.setChannel(channel);
    sink.start();

    JSONObject msg = new JSONObject();
    msg.put("age", 11);
    msg.put("birthday", new Date().getTime());

    Transaction tx;

    for (int i = 0; i < 10; i++) {
      tx = channel.getTransaction();
      tx.begin();
      msg.put("name", "test" + i);
      JSONObject header = new JSONObject();
      header.put(MongoSink.COLLECTION, "my_events");
      header.put(MongoSink.DB_NAME, "dynamic_db");

      Event e = EventBuilder.withBody(msg.toJSONString().getBytes(), header);
      channel.put(e);
      tx.commit();
      tx.close();
    }
    sink.process();
    sink.stop();

    for (int i = 0; i < 10; i++) {
      msg.put("name", "test" + i);

      System.out.println("i = " + i);

      DB db = mongo.getDB("dynamic_db");
      DBCollection collection = db.getCollection("my_events");
      DBCursor cursor = collection.find(new BasicDBObject(msg));
      assertTrue(cursor.hasNext());
      DBObject dbObject = cursor.next();
      assertNotNull(dbObject);
      assertEquals(dbObject.get("name"), msg.get("name"));
      assertEquals(dbObject.get("age"), msg.get("age"));
      assertEquals(dbObject.get("birthday"), msg.get("birthday"));
    }
  }
Пример #13
0
  protected Enter(Context context) {
    context.put(enterKey, this);

    log = Log.instance(context);
    reader = ClassReader.instance(context);
    make = TreeMaker.instance(context);
    syms = Symtab.instance(context);
    chk = Check.instance(context);
    memberEnter = MemberEnter.instance(context);
    types = Types.instance(context);
    annotate = Annotate.instance(context);
    lint = Lint.instance(context);

    predefClassDef =
        make.ClassDef(make.Modifiers(PUBLIC), syms.predefClass.name, null, null, null, null);
    predefClassDef.sym = syms.predefClass;
    todo = Todo.instance(context);
    fileManager = context.get(JavaFileManager.class);
  }
Пример #14
0
  /** build the context for batch archive confirm */
  public String buildDownloadContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);
    buildMenu(context);

    // get list of existing archives
    Collection<File> files = Collections.<File>emptySet();
    File archiveBaseDir =
        new File(serverConfigurationService.getString("archive.storage.path", "sakai/archive"));

    if (archiveBaseDir.exists() && archiveBaseDir.isDirectory()) {
      files = FileUtils.listFiles(archiveBaseDir, new SuffixFileFilter(".zip"), null);
    }

    List<SparseFile> zips = new ArrayList<SparseFile>();

    SimpleDateFormat dateFormatIn = new SimpleDateFormat("yyyyMMddHHmmss");
    SimpleDateFormat dateFormatOut = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Calendar calendar = Calendar.getInstance();

    // porcess the list. also get the hash for the file if it exists
    for (File f : files) {

      String absolutePath = f.getAbsolutePath();

      SparseFile sf = new SparseFile();
      sf.setFilename(f.getName());
      sf.setAbsolutePath(absolutePath);
      sf.setSize(FileUtils.byteCountToDisplaySize(f.length()));

      // get the datetime string, its the last part of the file name, convert back to a date that we
      // can display
      String dateTimeStr =
          StringUtils.substringAfterLast(StringUtils.removeEnd(f.getName(), ".zip"), "-");

      try {
        Date date = dateFormatIn.parse(dateTimeStr);
        sf.setDateCreated(dateFormatOut.format(date));
      } catch (ParseException pe) {
        // ignore, just don't set the date
      }

      // get siteId, first part of name
      String siteId = StringUtils.substringBeforeLast(f.getName(), "-");
      sf.setSiteId(siteId);

      // try to get site title if the site still exists
      try {
        Site site = siteService.getSite(siteId);
        sf.setSiteTitle(site.getTitle());
      } catch (IdUnusedException e) {
        // ignore, no site available
      }

      // get the hash. need to read it from the file. Same filename but diff extension
      String hashFilePath = StringUtils.removeEnd(absolutePath, ".zip");
      hashFilePath = hashFilePath + ".sha1";

      File hashFile = new File(hashFilePath);
      try {
        String hash = FileUtils.readFileToString(hashFile);
        sf.setHash(hash);
      } catch (IOException e) {
        // ignore, dont use the hash
      }

      zips.add(sf);
    }

    context.put("archives", zips);

    return "-download";
  }
Пример #15
0
 private static boolean parse(
     final String[] args, final Context context, final List<CompileParameter> parameters) {
   if (args.length == 1 && ("/?".equals(args[0]) || "-?".equals(args[0]) || "?".equals(args[0]))) {
     showHelpAndExit(context, true, parameters);
     return false;
   }
   final List<ParameterParser> customParsers = new ArrayList<ParameterParser>();
   for (final CompileParameter cp : parameters) {
     if (cp instanceof ParameterParser) {
       customParsers.add((ParameterParser) cp);
     }
   }
   final List<String> errors = new ArrayList<String>();
   for (String a : args) {
     if (a.startsWith("-") || a.startsWith("/")) a = a.substring(1);
     final int eq = a.indexOf('=');
     final String name = a.substring(0, eq != -1 ? eq : a.length());
     final String value = eq == -1 ? null : a.substring(eq + 1);
     final CompileParameter cp = from(name, parameters);
     if (cp == null) {
       boolean matched = false;
       for (final ParameterParser parser : customParsers) {
         final Either<Boolean> tryParse = parser.tryParse(name, value, context);
         if (!tryParse.isSuccess()) {
           errors.add(tryParse.explainError());
           matched = true;
           break;
         } else if (tryParse.get()) {
           matched = true;
           break;
         }
       }
       if (!matched) {
         errors.add("Unknown parameter: " + name);
       }
     } else {
       if (eq == -1 && cp.getUsage() != null) {
         if (cp instanceof ParameterParser) {
           Either<Boolean> tryParse = ((ParameterParser) cp).tryParse(name, null, context);
           if (tryParse.isSuccess() && tryParse.get()) {
             context.put(cp, null);
           } else {
             errors.add("Expecting " + cp.getUsage() + " after = for " + a);
           }
         } else {
           errors.add("Expecting " + cp.getUsage() + " after = for " + a);
         }
       } else {
         context.put(cp, value);
       }
     }
   }
   if (args.length == 0 || errors.size() > 0) {
     for (final String err : errors) {
       context.error(err);
     }
     showHelpAndExit(context, args.length == errors.size(), parameters);
     return false;
   }
   return true;
 }
Пример #16
0
 protected Options(Context context) {
   // DEBUGGING -- Use LinkedHashMap for reproducability
   values = new LinkedHashMap<String, String>();
   context.put(optionsKey, this);
 }