Search in sources :

Example 6 with Message

use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.

the class Util method messagesToJson.

/**
     * Print set of messages into json array
     *
     * @param set set of messages
     * @return json array containing the set of messages
     */
@SuppressWarnings("unchecked")
public static JSONArray messagesToJson(SortedSet<Message> set) {
    JSONArray array = new JSONArray();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    for (Message m : set) {
        JSONObject message = new JSONObject();
        message.put("class", Util.encode(m.getClassName()));
        message.put("expiration", Util.encode(df.format(m.getExpiration())));
        message.put("created", Util.encode(df.format(m.getCreated())));
        message.put("text", Util.encode(m.getText()));
        JSONArray tags = new JSONArray();
        for (String t : m.getTags()) {
            tags.add(Util.encode(t));
        }
        message.put("tags", tags);
        array.add(message);
    }
    return array;
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with Message

use of org.opensolaris.opengrok.configuration.messages.Message in project OpenGrok by OpenGrok.

the class RuntimeEnvironmentTest method testCanAcceptMessage.

@Test
public void testCanAcceptMessage() throws Exception {
    RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
    instance.removeAllMessages();
    Message m1 = new NormalMessage();
    m1.addTag("main");
    m1.setExpiration(new Date(System.currentTimeMillis() - 3000));
    Assert.assertFalse(instance.canAcceptMessage(m1));
    m1.setExpiration(new Date(System.currentTimeMillis() - 2000));
    Assert.assertFalse(instance.canAcceptMessage(m1));
    m1.setExpiration(new Date(System.currentTimeMillis() - 1000));
    Assert.assertFalse(instance.canAcceptMessage(m1));
    m1.setExpiration(new Date(System.currentTimeMillis() - 1));
    Assert.assertFalse(instance.canAcceptMessage(m1));
    m1.setExpiration(new Date(System.currentTimeMillis() - 0));
    Assert.assertTrue(instance.canAcceptMessage(m1));
    m1.setExpiration(new Date(System.currentTimeMillis() + 1));
    Assert.assertTrue(instance.canAcceptMessage(m1));
    m1.setExpiration(new Date(System.currentTimeMillis() + 5000));
    Assert.assertEquals(0, instance.getMessagesInTheSystem());
    for (int i = 0; i < instance.getMessageLimit(); i++) {
        Message m2 = new NormalMessage();
        m2.addTag("main");
        m2.setText("text");
        m2.setExpiration(new Date(System.currentTimeMillis() + 5000));
        m2.setCreated(new Date(System.currentTimeMillis() + i));
        Assert.assertTrue(instance.canAcceptMessage(m2));
        m2.apply(instance);
        Assert.assertEquals(i + 1, instance.getMessagesInTheSystem());
    }
    Assert.assertEquals(instance.getMessageLimit(), instance.getMessagesInTheSystem());
    for (int i = 0; i < instance.getMessageLimit() * 2; i++) {
        Message m2 = new NormalMessage();
        m2.addTag("main");
        m2.setText("text");
        m2.setExpiration(new Date(System.currentTimeMillis() + 5000));
        m2.setCreated(new Date(System.currentTimeMillis() + i + instance.getMessageLimit()));
        Assert.assertFalse(instance.canAcceptMessage(m2));
        m2.apply(instance);
        Assert.assertEquals(instance.getMessageLimit(), instance.getMessagesInTheSystem());
    }
    instance.removeAllMessages();
}
Also used : NormalMessage(org.opensolaris.opengrok.configuration.messages.NormalMessage) NormalMessage(org.opensolaris.opengrok.configuration.messages.NormalMessage) Message(org.opensolaris.opengrok.configuration.messages.Message) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Message (org.opensolaris.opengrok.configuration.messages.Message)7 IOException (java.io.IOException)3 JSONObject (org.json.simple.JSONObject)3 UnknownHostException (java.net.UnknownHostException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 XMLDecoder (java.beans.XMLDecoder)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)1 Date (java.util.Date)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 JSONArray (org.json.simple.JSONArray)1 ParseException (org.json.simple.parser.ParseException)1