List of usage examples for java.util Stack empty
public boolean empty()
From source file:org.apache.catalina.core.StandardContext.java
/** * Get naming context full name./*from www. j a va 2 s .c o m*/ */ private String getNamingContextName() { if (namingContextName == null) { Container parent = getParent(); if (parent == null) { namingContextName = getName(); } else { Stack stk = new Stack(); StringBuffer buff = new StringBuffer(); while (parent != null) { stk.push(parent.getName()); parent = parent.getParent(); } while (!stk.empty()) { buff.append("/" + stk.pop()); } buff.append(getName()); namingContextName = buff.toString(); } } return namingContextName; }
From source file:pl.datamatica.traccar.api.GPXParser.java
public Result parse(InputStream inputStream, Device device) throws XMLStreamException, ParseException, IOException { Result result = new Result(); TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); DateFormat dateFormatWithMS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); dateFormat.setTimeZone(tz);/*from ww w. j ava2 s .com*/ dateFormatWithMS.setTimeZone(tz); XMLStreamReader xsr = XMLInputFactory.newFactory().createXMLStreamReader(inputStream); ObjectMapper jsonMapper = new ObjectMapper(); result.positions = new LinkedList<>(); Position position = null; Stack<String> extensionsElements = new Stack<>(); boolean extensionsStarted = false; Map<String, Object> other = null; while (xsr.hasNext()) { xsr.next(); if (xsr.getEventType() == XMLStreamReader.START_ELEMENT) { if (xsr.getLocalName().equalsIgnoreCase("trkpt")) { position = new Position(); position.setLongitude(Double.parseDouble(xsr.getAttributeValue(null, "lon"))); position.setLatitude(Double.parseDouble(xsr.getAttributeValue(null, "lat"))); position.setValid(Boolean.TRUE); position.setDevice(device); } else if (xsr.getLocalName().equalsIgnoreCase("time")) { if (position != null) { String strTime = xsr.getElementText(); if (strTime.length() == 20) { position.setTime(dateFormat.parse(strTime)); } else { position.setTime(dateFormatWithMS.parse(strTime)); } } } else if (xsr.getLocalName().equalsIgnoreCase("ele") && position != null) { position.setAltitude(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("address") && position != null) { position.setAddress(StringEscapeUtils.unescapeXml(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("protocol") && position != null) { position.setProtocol(xsr.getElementText()); } else if (xsr.getLocalName().equalsIgnoreCase("speed") && position != null) { position.setSpeed(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("power") && position != null) { position.setPower(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("course") && position != null) { position.setCourse(Double.parseDouble(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("other") && position != null) { position.setOther(StringEscapeUtils.unescapeXml(xsr.getElementText())); } else if (xsr.getLocalName().equalsIgnoreCase("extensions")) { other = new LinkedHashMap<>(); extensionsStarted = true; } else if (position != null && extensionsStarted && other != null) { extensionsElements.push(xsr.getLocalName()); } } else if (xsr.getEventType() == XMLStreamReader.END_ELEMENT) { if (xsr.getLocalName().equalsIgnoreCase("trkpt")) { if (other == null) { other = new HashMap<>(); } if (position.getOther() != null) { if (position.getOther().startsWith("<")) { XMLStreamReader otherReader = XMLInputFactory.newFactory() .createXMLStreamReader(new StringReader(position.getOther())); while (otherReader.hasNext()) { if (otherReader.next() == XMLStreamReader.START_ELEMENT && !otherReader.getLocalName().equals("info")) { other.put(otherReader.getLocalName(), otherReader.getElementText()); } } } else { Map<String, Object> parsedOther = jsonMapper.readValue(position.getOther(), LinkedHashMap.class); other.putAll(parsedOther); } } if (other.containsKey("protocol") && position.getProtocol() == null) { position.setProtocol(other.get("protocol").toString()); } else if (!other.containsKey("protocol") && position.getProtocol() == null) { position.setProtocol("gpx_import"); } other.put("import_type", (result.positions.isEmpty() ? "import_start" : "import")); position.setOther(jsonMapper.writeValueAsString(other)); result.positions.add(position); if (result.latestPosition == null || result.latestPosition.getTime().compareTo(position.getTime()) < 0) { result.latestPosition = position; } position = null; other = null; } else if (xsr.getLocalName().equalsIgnoreCase("extensions")) { extensionsStarted = false; } else if (extensionsStarted) { extensionsElements.pop(); } } else if (extensionsStarted && other != null && xsr.getEventType() == XMLStreamReader.CHARACTERS && !xsr.getText().trim().isEmpty() && !extensionsElements.empty()) { String name = ""; for (int i = 0; i < extensionsElements.size(); i++) { name += (name.length() > 0 ? "-" : "") + extensionsElements.get(i); } other.put(name, xsr.getText()); } } if (result.positions.size() > 1) { Position last = ((LinkedList<Position>) result.positions).getLast(); Map<String, Object> parsedOther = jsonMapper.readValue(last.getOther(), LinkedHashMap.class); parsedOther.put("import_type", "import_end"); last.setOther(jsonMapper.writeValueAsString(parsedOther)); } return result; }
From source file:com.pironet.tda.SunJDKParser.java
/** * parse the next thread dump from the stream passed with the constructor. * * @return null if no more thread dumps were found. *///from w w w .j a va 2 s. co m public MutableTreeNode parseNext() { if (nextDump != null) { MutableTreeNode tmpDump = nextDump; nextDump = null; return (tmpDump); } boolean retry = false; String line = null; do { try { Map<String, String> threads = new HashMap<>(); ThreadDumpInfo overallTDI = new ThreadDumpInfo("Dump No. " + counter++, 0); if (withCurrentTimeStamp) { overallTDI.setStartTime((new Date(System.currentTimeMillis())).toString()); } DefaultMutableTreeNode threadDump = new DefaultMutableTreeNode(overallTDI); DefaultMutableTreeNode catThreads = new DefaultMutableTreeNode( new TableCategory("Threads", IconFactory.THREADS)); threadDump.add(catThreads); DefaultMutableTreeNode catWaiting = new DefaultMutableTreeNode( new TableCategory("Threads waiting for Monitors", IconFactory.THREADS_WAITING)); DefaultMutableTreeNode catSleeping = new DefaultMutableTreeNode( new TableCategory("Threads sleeping on Monitors", IconFactory.THREADS_SLEEPING)); DefaultMutableTreeNode catLocking = new DefaultMutableTreeNode( new TableCategory("Threads locking Monitors", IconFactory.THREADS_LOCKING)); // create category for monitors with disabled filtering. // NOTE: These strings are "magic" in that the methods // TDA#displayCategory and TreeCategory#getCatComponent both // checks these literal strings and the behavior differs. DefaultMutableTreeNode catMonitors = new DefaultMutableTreeNode( new TreeCategory("Monitors", IconFactory.MONITORS, false)); DefaultMutableTreeNode catMonitorsLocks = new DefaultMutableTreeNode( new TreeCategory("Monitors without locking thread", IconFactory.MONITORS_NO_LOCKS, false)); DefaultMutableTreeNode catBlockingMonitors = new DefaultMutableTreeNode( new TreeCategory("Threads blocked by Monitors", IconFactory.THREADS_LOCKING, false)); String title = null; String dumpKey = null; StringBuffer content = null; boolean inLocking = false; boolean inSleeping = false; boolean inWaiting = false; int threadCount = 0; int waiting = 0; int locking = 0; int sleeping = 0; boolean locked = true; boolean finished = false; final MonitorMap mmap = new MonitorMap(); final Stack<String> monitorStack = new Stack<>(); long startTime = 0; int singleLineCounter = 0; boolean concurrentSyncsFlag = false; Matcher matched = getDm().getLastMatch(); while (getBis().ready() && !finished) { line = getNextLine(); lineCounter++; singleLineCounter++; if (locked) { if (line.contains("Full thread dump")) { locked = false; if (!withCurrentTimeStamp) { overallTDI.setLogLine(lineCounter); if (startTime != 0) { startTime = 0; } else if (matched != null && matched.matches()) { String parsedStartTime = matched.group(1); if (!getDm().isDefaultMatches() && isMillisTimeStamp()) { try { // the factor is a hack for a bug in oc4j timestamp printing (pattern timeStamp=2342342340) if (parsedStartTime.length() < 13) { startTime = Long.parseLong(parsedStartTime) * (long) Math.pow(10, 13 - parsedStartTime.length()); } else { startTime = Long.parseLong(parsedStartTime); } } catch (NumberFormatException nfe) { startTime = 0; } if (startTime > 0) { overallTDI.setStartTime((new Date(startTime)).toString()); } } else { overallTDI.setStartTime(parsedStartTime); } matched = null; getDm().resetLastMatch(); } } dumpKey = overallTDI.getName(); } else if (!getDm().isPatternError() && (getDm().getRegexPattern() != null)) { Matcher m = getDm().checkForDateMatch(line); if (m != null) { matched = m; } } } else { if (line.startsWith("\"")) { // We are starting a group of lines for a different thread // First, flush state for the previous thread (if any) concurrentSyncsFlag = false; String stringContent = content != null ? content.toString() : null; if (title != null) { threads.put(title, content.toString()); content.append("</pre></pre>"); addToCategory(catThreads, title, null, stringContent, singleLineCounter, true); threadCount++; } if (inWaiting) { addToCategory(catWaiting, title, null, stringContent, singleLineCounter, true); inWaiting = false; waiting++; } if (inSleeping) { addToCategory(catSleeping, title, null, stringContent, singleLineCounter, true); inSleeping = false; sleeping++; } if (inLocking) { addToCategory(catLocking, title, null, stringContent, singleLineCounter, true); inLocking = false; locking++; } singleLineCounter = 0; while (!monitorStack.empty()) { mmap.parseAndAddThread(monitorStack.pop(), title, content.toString()); } // Second, initialize state for this new thread title = line; content = new StringBuffer("<body bgcolor=\"ffffff\"><pre><font size=" + TDA.getFontSizeModifier(-1) + '>'); content.append(line); content.append('\n'); } else if (line.contains("at ")) { content.append(line); content.append('\n'); } else if (line.contains("java.lang.Thread.State")) { content.append(line); content.append('\n'); if (title.indexOf("t@") > 0) { // in this case the title line is missing state informations String state = line.substring(line.indexOf(':') + 1).trim(); if (state.indexOf(' ') > 0) { title += " state=" + state.substring(0, state.indexOf(' ')); } else { title += " state=" + state; } } } else if (line.contains("Locked ownable synchronizers:")) { concurrentSyncsFlag = true; content.append(line); content.append('\n'); } else if (line.contains("- waiting on")) { content.append(linkifyMonitor(line)); monitorStack.push(line); inSleeping = true; content.append('\n'); } else if (line.contains("- parking to wait")) { content.append(linkifyMonitor(line)); monitorStack.push(line); inSleeping = true; content.append('\n'); } else if (line.contains("- waiting to")) { content.append(linkifyMonitor(line)); monitorStack.push(line); inWaiting = true; content.append('\n'); } else if (line.contains("- locked")) { content.append(linkifyMonitor(line)); inLocking = true; monitorStack.push(line); content.append('\n'); } else if (line.contains("- ")) { if (concurrentSyncsFlag) { content.append(linkifyMonitor(line)); monitorStack.push(line); } else { content.append(line); } content.append('\n'); } // last thread reached? if ((line.contains("\"Suspend Checker Thread\"")) || (line.contains("\"VM Periodic Task Thread\"")) || (line.contains("<EndOfDump>"))) { finished = true; getBis().mark(getMarkSize()); if ((checkForDeadlocks(threadDump)) == 0) { // no deadlocks found, set back original position. getBis().reset(); } if (!checkThreadDumpStatData(overallTDI)) { // no statistical data found, set back original position. getBis().reset(); } getBis().mark(getMarkSize()); if (!(foundClassHistograms = checkForClassHistogram(threadDump))) { getBis().reset(); } } } } // last thread String stringContent = content != null ? content.toString() : null; if (title != null) { threads.put(title, content.toString()); content.append("</pre></pre>"); addToCategory(catThreads, title, null, stringContent, singleLineCounter, true); threadCount++; } if (inWaiting) { addToCategory(catWaiting, title, null, stringContent, singleLineCounter, true); waiting++; } if (inSleeping) { addToCategory(catSleeping, title, null, stringContent, singleLineCounter, true); sleeping++; } if (inLocking) { addToCategory(catLocking, title, null, stringContent, singleLineCounter, true); locking++; } while (!monitorStack.empty()) { mmap.parseAndAddThread(monitorStack.pop(), title, content.toString()); } int monitorCount = mmap.size(); int monitorsWithoutLocksCount = 0; int contendedMonitors = 0; int blockedThreads = 0; // dump monitors if (mmap.size() > 0) { int[] result = dumpMonitors(catMonitors, catMonitorsLocks, mmap); monitorsWithoutLocksCount = result[0]; overallTDI.setOverallThreadsWaitingWithoutLocksCount(result[1]); result = dumpBlockingMonitors(catBlockingMonitors, mmap); contendedMonitors = result[0]; blockedThreads = result[1]; } // display nodes with stuff to display if (waiting > 0) { overallTDI.setWaitingThreads((Category) catWaiting.getUserObject()); threadDump.add(catWaiting); } if (sleeping > 0) { overallTDI.setSleepingThreads((Category) catSleeping.getUserObject()); threadDump.add(catSleeping); } if (locking > 0) { overallTDI.setLockingThreads((Category) catLocking.getUserObject()); threadDump.add(catLocking); } if (monitorCount > 0) { overallTDI.setMonitors((Category) catMonitors.getUserObject()); threadDump.add(catMonitors); } if (contendedMonitors > 0) { overallTDI.setBlockingMonitors((Category) catBlockingMonitors.getUserObject()); threadDump.add(catBlockingMonitors); } if (monitorsWithoutLocksCount > 0) { overallTDI.setMonitorsWithoutLocks((Category) catMonitorsLocks.getUserObject()); threadDump.add(catMonitorsLocks); } overallTDI.setThreads((Category) catThreads.getUserObject()); ((Category) catThreads.getUserObject()) .setName(catThreads.getUserObject() + " (" + threadCount + " Threads overall)"); ((Category) catWaiting.getUserObject()) .setName(catWaiting.getUserObject() + " (" + waiting + " Threads waiting)"); ((Category) catSleeping.getUserObject()) .setName(catSleeping.getUserObject() + " (" + sleeping + " Threads sleeping)"); ((Category) catLocking.getUserObject()) .setName(catLocking.getUserObject() + " (" + locking + " Threads locking)"); ((Category) catMonitors.getUserObject()) .setName(catMonitors.getUserObject() + " (" + monitorCount + " Monitors)"); ((Category) catBlockingMonitors.getUserObject()).setName(catBlockingMonitors.getUserObject() + " (" + blockedThreads + " Threads blocked by " + contendedMonitors + " Monitors)"); ((Category) catMonitorsLocks.getUserObject()).setName( catMonitorsLocks.getUserObject() + " (" + monitorsWithoutLocksCount + " Monitors)"); // add thread dump to passed dump store. if ((threadCount > 0) && (dumpKey != null)) { threadStore.put(dumpKey.trim(), threads); } // check custom categories addCustomCategories(threadDump); return (threadCount > 0 ? threadDump : null); } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Error during parsing of a found thread dump, skipping to next one!\n" + "Check for possible broken dumps, sometimes, stream flushing mixes the logged data.\n" + "Error Message is \"" + e.getLocalizedMessage() + "\". \n" + (line != null ? "Last line read was \"" + line + "\". \n" : ""), "Error during Parsing Thread Dump", JOptionPane.ERROR_MESSAGE); retry = true; } catch (IOException e) { e.printStackTrace(); } } while (retry); return (null); }