org.newhart.jexample.JExample0.java Source code

Java tutorial

Introduction

Here is the source code for org.newhart.jexample.JExample0.java

Source

/**
 * Copyright 2012-2013 Clint Combs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.newhart.jexample;

import static java.lang.System.out;
import org.newhart.*;
import org.newhart.mongodb.AuditStore;
import com.mongodb.Mongo;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import java.net.UnknownHostException;
import java.util.Date;
import static org.newhart.japi.NewhartJava.*;
import org.newhart.japi.*;

/**
 * JExample0
 */
public class JExample0 {

    public static void main(final String args[]) throws Exception {
        out.println(JExample0.class.getSimpleName());
        run();
    }

    private static Mongo mongoInstance = null;

    private static synchronized Mongo mongo() throws UnknownHostException {
        if (mongoInstance == null) {
            mongoInstance = new Mongo("localhost");
        }

        return mongoInstance;
    }

    private static void run() throws UnknownHostException {
        Converters.add(NameNumber.class, new NameNumberConverter());

        final String auditName = JExample0.class.getSimpleName();

        final AuditStore store = AuditStore.instance(mongo());
        store.createAudit(auditName, 10000000);

        final Delivery delivery = store;

        final Date now = Entry.now();

        final DBObject d1 = new BasicDBObject();
        d1.put("a", 1);

        final DBObject d2 = new BasicDBObject();
        d2.put("test", "value");
        final DBObject d3 = new BasicDBObject();
        d3.put("date", new java.util.Date());
        d2.put("d2", d3);

        Exception errorEx = null;
        try {
            exceptionTest("testing error");
        } catch (final Exception e) {
            errorEx = e;
        }

        Exception warningEx = null;
        try {
            exceptionTest("testing warning");
        } catch (final Exception e) {
            warningEx = e;
        }

        final String hostname = "dummy-hostname";

        final JEntryBuilder b = entry().origin("org.newhart.jexample.JExample0").originKey("123")
                .originKeyType("org.newhart.jexample.JExample0").originHostname(hostname)
                .criticality(JCriticality.minor).auditTS(now).createTS(now).updateTS(now).msg("message")
                .data("d1", d1).data("d2", d2).data("d3", "a simple value").error("error message 1", null, null)
                .error("error message 2").error("error message 3", "errCode3")
                .error("error message 4", "errCode4", "this is supposed to be a stack trace for error 4")
                .warning("warning message 1", "warnCode1").label("label1").label("label2");
        if (errorEx != null)
            b.error(errorEx);
        if (warningEx != null)
            b.warning(warningEx);
        delivery.send(auditName, b.build());

        final Class<JExample0> origin = org.newhart.jexample.JExample0.class;
        final JEntryBuilder b1 = entryFromOriginNow(origin, JCriticality.major).originKey("123-JExample0")
                .originKeyType(JExample0.class)
                // skip originHostname(String)
                .msg("message").data("d1", d1).data("d2", d2)
                .data("nameNumber", Converters.get(NameNumber.class).toDB(new NameNumber("name1", 1)))
                .error("error message 1", null, null).warning("warning message 1", "warnCode1").label("label1");
        if (errorEx != null)
            b1.error(errorEx);
        if (warningEx != null)
            b1.warning(warningEx);
        delivery.send(auditName, b1.build());
    }

    private static void exceptionTest(final String msg) throws Exception {
        throw new Exception("exceptionTest: " + msg);
    }
}

class NameNumber {
    public NameNumber(final String name, final int number) {
        this.name = name;
        this.number = number;
    }

    public final String name;
    public final Integer number;
}

class NameNumberConverter implements DBObjectConverter<NameNumber> {
    public DBObject toDB(final NameNumber nn) {
        final BasicDBObject dbObj = new BasicDBObject();

        dbObj.put("name", nn.name);
        dbObj.put("number", nn.number);

        return dbObj;
    }

    public NameNumber fromDB(final DBObject dbObj) {
        return null;
    }
}