Example usage for java.lang Throwable getCause

List of usage examples for java.lang Throwable getCause

Introduction

In this page you can find the example usage for java.lang Throwable getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:io.s4.zeno.SiteTest.java

public static void main(String[] arg) throws JSONException, KeeperException, IOException, InterruptedException {

    PropertyConfigurator.configure("log4j.properties");

    String name = arg[0];//ww w. j  a  va2 s  . c  o  m
    String zkaddr = arg[1];
    String zkbase = arg[2];
    String specStr = arg[3];

    ConfigMap spec = new JSONConfigMap(specStr);

    ZooKeeper zk = new ZooKeeper(zkaddr, 3000, zkhandler);
    zookeeper = new ZooKeeperHelper(zk, 5, 5000);
    zkpath = new ZKPaths(zkbase);

    ZooKeeperInfo zkinfo = new ZooKeeperInfo();
    zkinfo.zookeeper = zookeeper;
    zkinfo.zkpath = zkpath;
    zkinfo.taskHolder = new NonblockingLockset(zookeeper, zkpath.taskBase);
    zkinfo.partsHolder = new NonblockingLockset(zookeeper, zkpath.partsBase);
    zkinfo.standbySequence = new DistributedSequence(zookeeper, zkpath.standbyBase);

    final Site site = new Site(name, spec);

    ZKCluster cluster = new ZKCluster(zookeeper, zkpath);
    ZKJobList jobList = new ZKJobList(site, zkinfo);
    ZKPartList partList = new ZKPartList(site, zkinfo);
    SiteInitializer init = new SiteInitializer();

    site.setCluster(cluster);
    site.setJobList(jobList);
    site.setPartList(partList);
    site.setInitializer(init);

    class ZenoThreadGroup extends ThreadGroup {
        public ZenoThreadGroup() {
            super("ZenoThreadGroup");
        }

        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("ZENOTHREADGROUP CAUGHT AND EXCEPTION FROM THREAD " + t
                    + ". SO EXITING. DETAILS:" + e + "\nCAUSED BY: " + e.getCause() + "\n");
            e.printStackTrace();

            System.exit(1);
        }
    }

    Thread t = new Thread(new ZenoThreadGroup(), new Runnable() {
        public void run() {
            site.start();
        }
    }, "zenorunthread");
    t.start();

    System.out.println();

    t.join();
}

From source file:com.jredrain.startup.Bootstrap.java

public static void main(String[] args) {

    if (daemon == null) {
        daemon = new Bootstrap();
    }//from ww  w.j  a  v a2  s.  c  o m

    try {
        if (isEmpty(args)) {
            logger.warn("Bootstrap: error,usage start|stop");
        } else {
            String command = args[0];
            if ("start".equals(command)) {
                daemon.init();
                daemon.start();
                /**
                 * await for shundown
                 */
                daemon.await();
                daemon.stopServer();
            } else if ("stop".equals(command)) {
                daemon.shutdown();
            } else {
                logger.warn("Bootstrap: command \"" + command + "\" does not exist.");
            }
        }
    } catch (Throwable t) {
        if (t instanceof InvocationTargetException && t.getCause() != null) {
            t = t.getCause();
        }
        handleThrowable(t);
        t.printStackTrace();
        System.exit(1);
    }
}

From source file:com.amazonaws.services.iot.demo.danbo.rpi.Danbo.java

public static void main(String[] args) throws Exception {
    log.debug("starting");

    // uses pin 6 for the red Led
    final Led redLed = new Led(6);
    // uses pin 26 for the green Led
    final Led greenLed = new Led(26);

    // turns the red led on initially
    redLed.on();//from w  w  w. j a v a 2s .  co  m

    // turns the green led off initially
    greenLed.off();

    // loads properties from danbo.properties file - make sure this file is
    // available on the pi's home directory
    InputStream input = new FileInputStream("/home/pi/danbo/danbo.properties");
    Properties properties = new Properties();
    properties.load(input);
    endpoint = properties.getProperty("awsiot.endpoint");
    rootCA = properties.getProperty("awsiot.rootCA");
    privateKey = properties.getProperty("awsiot.privateKey");
    certificate = properties.getProperty("awsiot.certificate");
    url = protocol + endpoint + ":" + port;

    log.debug("properties loaded");

    // turns off both eyes
    RGBLed rgbLed = new RGBLed("RGBLed1", Danbo.pinLayout1, Danbo.pinLayout2, new Color(0, 0, 0),
            new Color(0, 0, 0), 0, 100);
    new Thread(rgbLed).start();

    // resets servo to initial positon
    Servo servo = new Servo("Servo", 1);
    new Thread(servo).start();

    // gets the Pi serial number and uses it as part of the thing
    // registration name
    clientId = clientId + getSerialNumber();

    // AWS IoT things shadow topics
    updateTopic = "$aws/things/" + clientId + "/shadow/update";
    deltaTopic = "$aws/things/" + clientId + "/shadow/update/delta";
    rejectedTopic = "$aws/things/" + clientId + "/shadow/update/rejected";

    // AWS IoT controller things shadow topic (used to register new things)
    controllerUpdateTopic = "$aws/things/Controller/shadow/update";

    // defines an empty danbo shadow POJO
    final DanboShadow danboShadow = new DanboShadow();
    DanboShadow.State state = danboShadow.new State();
    final DanboShadow.State.Reported reported = state.new Reported();
    reported.setEyes("readyToBlink");
    reported.setHead("readyToMove");
    reported.setMouth("readyToSing");
    reported.setName(clientId);
    state.setReported(reported);
    danboShadow.setState(state);

    // defines an empty controller shadow POJO
    final ControllerShadow controllerShadow = new ControllerShadow();
    ControllerShadow.State controllerState = controllerShadow.new State();
    final ControllerShadow.State.Reported controllerReported = controllerState.new Reported();
    controllerReported.setThingName(clientId);
    controllerState.setReported(controllerReported);
    controllerShadow.setState(controllerState);

    try {
        log.debug("registering");

        // registers the thing (creates a new thing) by updating the
        // controller
        String message = gson.toJson(controllerShadow);
        MQTTPublisher controllerUpdatePublisher = new MQTTPublisher(controllerUpdateTopic, qos, message, url,
                clientId + "-controllerupdate" + rand.nextInt(100000), cleanSession, rootCA, privateKey,
                certificate);
        new Thread(controllerUpdatePublisher).start();

        log.debug("registered");

        // clears the thing status (in case the thing already existed)
        Danbo.deleteStatus("initialDelete");

        // creates an MQTT subscriber to the things shadow delta topic
        // (command execution notification)
        MQTTSubscriber deltaSubscriber = new MQTTSubscriber(new DanboShadowDeltaCallback(), deltaTopic, qos,
                url, clientId + "-delta" + rand.nextInt(100000), cleanSession, rootCA, privateKey, certificate);
        new Thread(deltaSubscriber).start();

        // creates an MQTT subscriber to the things shadow error topic
        MQTTSubscriber errorSubscriber = new MQTTSubscriber(new DanboShadowRejectedCallback(), rejectedTopic,
                qos, url, clientId + "-rejected" + rand.nextInt(100000), cleanSession, rootCA, privateKey,
                certificate);
        new Thread(errorSubscriber).start();

        // turns the red LED off
        redLed.off();

        ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
        exec.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                // turns the green LED on
                greenLed.on();

                log.debug("running publish state thread");

                int temp = -300;
                int humid = -300;

                reported.setTemperature(new Integer(temp).toString());
                reported.setHumidity(new Integer(humid).toString());

                try {
                    // reads the temperature and humidity data
                    Set<Sensor> sensors = Sensors.getSensors();
                    log.debug(sensors.size());

                    for (Sensor sensor : sensors) {
                        log.debug(sensor.getPhysicalQuantity());
                        log.debug(sensor.getValue());
                        if (sensor.getPhysicalQuantity().toString().equals("Temperature")) {
                            temp = sensor.getValue().intValue();
                        }
                        if (sensor.getPhysicalQuantity().toString().equals("Humidity")) {
                            humid = sensor.getValue().intValue();
                        }
                    }

                    log.debug("temperature: " + temp);
                    log.debug("humidity: " + humid);
                    reported.setTemperature(new Integer(temp).toString());
                    reported.setHumidity(new Integer(humid).toString());
                } catch (Exception e) {
                    log.error("an error has ocurred: " + e.getMessage());
                    e.printStackTrace();
                }

                try {
                    // reports current state - last temperature and humidity
                    // read
                    String message = gson.toJson(danboShadow);
                    MQTTPublisher updatePublisher = new MQTTPublisher(updateTopic, qos, message, url,
                            clientId + "-update" + rand.nextInt(100000), cleanSession, rootCA, privateKey,
                            certificate);
                    new Thread(updatePublisher).start();
                } catch (Exception e) {
                    log.error("an error has ocurred: " + e.getMessage());
                    e.printStackTrace();
                }

                // turns the green LED off
                greenLed.off();
            }
        }, 0, 5, TimeUnit.SECONDS); // runs this thread every 5 seconds,
        // with an initial delay of 5 seconds
    } catch (MqttException me) {
        // Display full details of any exception that occurs
        log.error("reason " + me.getReasonCode());
        log.error("msg " + me.getMessage());
        log.error("loc " + me.getLocalizedMessage());
        log.error("cause " + me.getCause());
        log.error("excep " + me);
        me.printStackTrace();
    } catch (Throwable th) {
        log.error("msg " + th.getMessage());
        log.error("loc " + th.getLocalizedMessage());
        log.error("cause " + th.getCause());
        log.error("excep " + th);
        th.printStackTrace();
    }
}

From source file:Main.java

public static Throwable getRootCause(Throwable paramThrowable) {
    while (paramThrowable.getCause() != null)
        paramThrowable = paramThrowable.getCause();
    return paramThrowable;
}

From source file:Main.java

public static Throwable getDeepestThrowable(Throwable t) {
    Throwable parent = t;// w w w  .  jav a  2 s  .com
    Throwable child = t.getCause();
    while (null != child) {
        parent = child;
        child = parent.getCause();
    }

    return parent;

}

From source file:Main.java

/**
 * Method that can be used to find the "root cause", innermost
 * of chained (wrapped) exceptions./*  w  w w  . j  a v a 2s  .  c  o  m*/
 */
public static Throwable getRootCause(Throwable t) {
    while (t.getCause() != null) {
        t = t.getCause();
    }
    return t;
}

From source file:Main.java

public static String getExceptionCauseString(final Throwable ex) {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final PrintStream ps = new PrintStream(bos);

    try {/*from  www.j a v a  2 s.com*/
        // print directly
        Throwable t = ex;
        while (t.getCause() != null) {
            t = t.getCause();
        }
        t.printStackTrace(ps);
        return toVisualString(bos.toString());
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:br.com.tcc.rest.config.TccExceptionHandler.java

public static Throwable cleanException(Exception t) {
    Throwable ex = t;
    while (ex != null && ex.getCause() != null
            && ((ex instanceof InvocationTargetException) || (ex instanceof UndeclaredThrowableException)
                    || (ex instanceof ExecutionException) || (ex instanceof JsonMappingException))) {
        ex = ex.getCause();//w  ww .j  a  v a  2s. c om
    }
    return ex;
}

From source file:Main.java

/**
 * Apply the toString() method recursively to this throwable and all its causes.
 * The idea is to get cause information as in printStackTrace() without the stack trace.
 *
 * @param t the throwable to print.//from   w w  w.  j a va2s . c  o  m
 * @return
 */
public static String getThrowableAndCausesAsString(Throwable t) {
    StringBuffer buf = new StringBuffer();
    buf.append(t.toString());
    if (t.getCause() != null) {
        buf.append("\nCaused by: ");
        buf.append(getThrowableAndCausesAsString(t.getCause()));
    }
    return buf.toString();
}

From source file:amqp.spring.camel.component.SpringAMQPComponent.java

public static Throwable findRootCause(Throwable t) {
    if (t.getCause() == null)
        return t;
    return findRootCause(t.getCause());
}