Search in sources :

Example 96 with SuppressFBWarnings

use of edu.umd.cs.findbugs.annotations.SuppressFBWarnings in project jaggery by wso2.

the class JavaScriptFileManagerImpl method getFile.

@SuppressFBWarnings("PATH_TRAVERSAL_IN")
@Override
public File getFile(String uri) throws ScriptException {
    File file;
    if (uri.startsWith("file://")) {
        try {
            file = FileUtils.toFile(new URL(uri));
        } catch (MalformedURLException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
    } else {
        String oldPath = uri;
        uri = FilenameUtils.normalizeNoEndSeparator(uri);
        if (uri == null) {
            String msg = "Invalid file URI : " + oldPath;
            log.error(msg);
            throw new ScriptException(msg);
        }
        file = new File(uri);
    }
    return file;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) MalformedURLException(java.net.MalformedURLException) File(java.io.File) URL(java.net.URL) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 97 with SuppressFBWarnings

use of edu.umd.cs.findbugs.annotations.SuppressFBWarnings in project jaggery by wso2.

the class WebAppFileManager method getDirectoryPath.

@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
@Override
public String getDirectoryPath(String path) throws ScriptException {
    if (path.startsWith(FILE_PATH)) {
        return new JavaScriptFileManagerImpl().getFile(path).getAbsolutePath();
    }
    String oldPath = path;
    path = FilenameUtils.normalizeNoEndSeparator(path);
    if (path == null) {
        String msg = "Invalid file path : " + oldPath;
        log.error(msg);
        throw new ScriptException(msg);
    }
    File file = new File(context.getRealPath("/"), path);
    return file.getPath();
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JavaScriptFileManagerImpl(org.jaggeryjs.hostobjects.file.JavaScriptFileManagerImpl) JavaScriptFile(org.jaggeryjs.hostobjects.file.JavaScriptFile) File(java.io.File) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 98 with SuppressFBWarnings

use of edu.umd.cs.findbugs.annotations.SuppressFBWarnings in project jaggery by wso2.

the class WebAppFile method open.

@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
@Override
public void open(String mode) throws ScriptException {
    if ("r".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "r");
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
    } else if ("r+".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.seek(0);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
        writable = true;
    } else if ("w".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.setLength(0);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        writable = true;
    } else if ("w+".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.setLength(0);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
        writable = true;
    } else if ("a".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.seek(file.length());
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        writable = true;
    } else if ("a+".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.seek(file.length());
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
        writable = true;
    } else {
        String msg = "Invalid or unsupported file mode, path : " + realPath + ", mode : " + mode;
        log.error(msg);
        throw new ScriptException(msg);
    }
    opened = true;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 99 with SuppressFBWarnings

use of edu.umd.cs.findbugs.annotations.SuppressFBWarnings in project JMRI by JMRI.

the class SensorTurnoutOperator method run.

/**
     * Do the autmation for a turnout with sensor feedback. Keep trying up to
     * maxTries until the sensor tells us the change has actually happened. Note
     * the call to operatorCheck each time we're about to actually do something
     * - if we're no longer the current operator this throws
     * TurnoutOperatorException which just terminates the thread.
     */
@Override
public void run() {
    //long startTime = System.currentTimeMillis();
    listener = new PropertyChangeListener() {

        @SuppressFBWarnings(value = "NN_NAKED_NOTIFY", justification = "notify not naked, outside sensor and turnout is shared state")
        @Override
        public void propertyChange(PropertyChangeEvent e) {
            if (e.getPropertyName().equals("KnownState")) {
                synchronized (this) {
                    this.notify();
                }
            }
        }
    };
    myTurnout.addPropertyChangeListener(listener);
    try {
        operatorCheck();
        myTurnout.forwardCommandChangeToLayout();
        while (++tries < maxTries) {
            long nextTry = System.currentTimeMillis() + interval;
            long remaining;
            while ((remaining = nextTry - System.currentTimeMillis()) > 0) {
                try {
                    synchronized (this) {
                        wait(remaining);
                    }
                } catch (InterruptedException e) {
                    // retain if needed later
                    Thread.currentThread().interrupt();
                }
            }
            if (myTurnout.isConsistentState()) {
                break;
            }
            operatorCheck();
            myTurnout.forwardCommandChangeToLayout();
            log.warn("retrying " + myTurnout.getSystemName() + ", try #" + (tries + 1));
        }
        if (!myTurnout.isConsistentState()) {
            log.warn("failed to throw " + myTurnout.getSystemName());
        }
    } catch (TurnoutOperatorException e) {
    }
    myTurnout.removePropertyChangeListener(listener);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 100 with SuppressFBWarnings

use of edu.umd.cs.findbugs.annotations.SuppressFBWarnings in project JMRI by JMRI.

the class LayoutBlock method updateRoutingInfo.

//This lot might need changing to only forward on the best route details.
@SuppressFBWarnings(value = "FE_FLOATING_POINT_EQUALITY", justification = "checking against a error value of -1; bad practice to use values for errors, but not an FFPE")
void updateRoutingInfo(LayoutBlock src, RoutingPacket update) {
    if (enableUpdateRouteLogging) {
        log.info("From " + this.getDisplayName() + " src: " + src.getDisplayName() + ", block: " + update.getBlock().getDisplayName() + ", hopCount: " + update.getHopCount() + ", metric: " + update.getMetric() + ", status: " + update.getBlockState() + ", packetID: " + update.getPacketId());
    }
    Block srcblk = src.getBlock();
    Adjacencies adj = getAdjacency(srcblk);
    if (adj == null) {
        if (enableUpdateRouteLogging) {
            log.info("From " + this.getDisplayName() + " packet is from a src that is not registered " + srcblk.getDisplayName());
        }
        //Then we will simply reject it.
        return;
    }
    if (updatePacketActedUpon(update.getPacketId())) {
        if (adj.updatePacketActedUpon(update.getPacketId())) {
            if (enableUpdateRouteLogging) {
                log.info("Reject packet update as we have already acted up on it from this neighbour");
            }
            return;
        }
    }
    if (enableUpdateRouteLogging) {
        log.info("From " + this.getDisplayName() + " an Update packet from neighbour " + src.getDisplayName());
    }
    Block updateBlock = update.getBlock();
    //for the block that they are referring too.
    if (updateBlock == this.getBlock()) {
        if (enableUpdateRouteLogging) {
            log.info("Reject packet update as it is a route advertised by our selves");
        }
        return;
    }
    Routes ro = null;
    boolean neighbour = false;
    if (updateBlock == srcblk) {
        //Very likely that this update is from a neighbour about its own status.
        ro = getValidRoute(this.getBlock(), updateBlock);
        neighbour = true;
    } else {
        ro = getValidRoute(srcblk, updateBlock);
    }
    if (ro == null) {
        if (enableUpdateRouteLogging) {
            log.info("From " + this.getDisplayName() + " update is from a source that we do not have listed as a route to the destination");
            log.info("From " + this.getDisplayName() + " update packet is for a block that we do not have route registered for " + updateBlock.getDisplayName());
        }
        //Then we will simply reject it.
        return;
    }
    /*This prevents us from entering into an update loop.
           We only add it to our list once it has passed through as being a valid
           packet, otherwise we may get the same packet id back, but from a valid source
           which would end up be rejected*/
    actedUponUpdates.add(update.getPacketId());
    adj.addPacketRecievedFromNeighbour(update.getPacketId());
    int hopCount = update.getHopCount();
    int packetmetric = update.getMetric();
    int blockstate = update.getBlockState();
    float length = update.getLength();
    //Need to add in a check for a block that is directly connected.
    if (hopCount != -1) {
        //int oldHop = ro.getHopCount();
        if (ro.getHopCount() != hopCount) {
            if (enableUpdateRouteLogging) {
                log.info(this.getDisplayName() + " Hop counts to " + ro.getDestBlock().getDisplayName() + " not the same so will change from " + ro.getHopCount() + " to " + hopCount);
            }
            ro.setHopCount(hopCount);
            hopCount++;
        } else {
            //No point in forwarding on the update if the hopcount hasn't changed
            hopCount = -1;
        }
    }
    //bad to use values as errors, but it's pre-existing code, and code wins
    if ((int) length != -1) {
        //Length is added at source
        float oldLength = ro.getLength();
        if (oldLength != length) {
            ro.setLength(length);
            boolean forwardUpdate = true;
            if (ro != getBestRouteByLength(update.getBlock())) {
                forwardUpdate = false;
            }
            if (enableUpdateRouteLogging) {
                log.info("From " + this.getDisplayName() + " updating length from " + oldLength + " to " + length);
            }
            if (neighbour) {
                length = srcblk.getLengthMm();
                adj.setLength(length);
                //Also if neighbour we need to update the cost of the routes via it to reflect the new metric 02/20/2011
                if (forwardUpdate) {
                    ArrayList<Routes> neighbourRoute = getNextRoutes(srcblk);
                    //that will need to have their metric updated to reflect the change.
                    for (int i = 0; i < neighbourRoute.size(); i++) {
                        Routes nRo = neighbourRoute.get(i);
                        //Need to remove old metric to the neigbour, then add the new one on
                        float updateLength = nRo.getLength();
                        updateLength = (updateLength - oldLength) + length;
                        if (enableUpdateRouteLogging) {
                            log.info("From " + this.getDisplayName() + " update metric for route " + nRo.getDestBlock().getDisplayName() + " from " + nRo.getLength() + " to " + updateLength);
                        }
                        nRo.setLength(updateLength);
                        ArrayList<Block> messageRecipients = getThroughPathDestinationBySource(srcblk);
                        RoutingPacket newUpdate = new RoutingPacket(UPDATE, nRo.getDestBlock(), -1, -1, updateLength + block.getLengthMm(), -1, getNextPacketID());
                        updateRoutesToNeighbours(messageRecipients, nRo, newUpdate);
                    }
                }
            } else if (forwardUpdate) {
                //This can cause a loop, if the layout is in a loop, so we send out the same packetID.
                ArrayList<Block> messageRecipients = getThroughPathSourceByDestination(srcblk);
                RoutingPacket newUpdate = new RoutingPacket(UPDATE, updateBlock, -1, -1, length + block.getLengthMm(), -1, update.getPacketId());
                updateRoutesToNeighbours(messageRecipients, ro, newUpdate);
            }
            length = length + metric;
        } else {
            length = -1;
        }
    }
    if (packetmetric != -1) {
        //Metric is added at source
        //Keep a reference of the old metric.
        int oldmetric = ro.getMetric();
        if (oldmetric != packetmetric) {
            ro.setMetric(packetmetric);
            if (enableUpdateRouteLogging) {
                log.info("From " + this.getDisplayName() + " updating metric from " + oldmetric + " to " + packetmetric);
            }
            boolean forwardUpdate = true;
            if (ro != getBestRouteByMetric(update.getBlock())) {
                forwardUpdate = false;
            }
            //rather than trust what is in the message at this stage.
            if (neighbour) {
                packetmetric = src.getBlockMetric();
                adj.setMetric(packetmetric);
                if (forwardUpdate) {
                    //ro.setMetric(packetmetric);
                    //Also if neighbour we need to update the cost of the routes via it to
                    //reflect the new metric 02/20/2011
                    ArrayList<Routes> neighbourRoute = getNextRoutes(srcblk);
                    //will need to have their metric updated to reflect the change.
                    for (int i = 0; i < neighbourRoute.size(); i++) {
                        Routes nRo = neighbourRoute.get(i);
                        //Need to remove old metric to the neigbour, then add the new one on
                        int updatemet = nRo.getMetric();
                        updatemet = (updatemet - oldmetric) + packetmetric;
                        if (enableUpdateRouteLogging) {
                            log.info("From " + this.getDisplayName() + " update metric for route " + nRo.getDestBlock().getDisplayName() + " from " + nRo.getMetric() + " to " + updatemet);
                        }
                        nRo.setMetric(updatemet);
                        ArrayList<Block> messageRecipients = getThroughPathDestinationBySource(srcblk);
                        RoutingPacket newUpdate = new RoutingPacket(UPDATE, nRo.getDestBlock(), hopCount, updatemet + metric, -1, -1, getNextPacketID());
                        updateRoutesToNeighbours(messageRecipients, nRo, newUpdate);
                    }
                }
            } else if (forwardUpdate) {
                //This can cause a loop, if the layout is in a loop, so we send out the same packetID.
                ArrayList<Block> messageRecipients = getThroughPathSourceByDestination(srcblk);
                RoutingPacket newUpdate = new RoutingPacket(UPDATE, updateBlock, hopCount, packetmetric + metric, -1, -1, update.getPacketId());
                updateRoutesToNeighbours(messageRecipients, ro, newUpdate);
            }
            packetmetric = packetmetric + metric;
        //Think we need a list of routes that originate from this source neighbour
        } else {
            //No point in forwarding on the update if the metric hasn't changed
            packetmetric = -1;
        //Potentially when we do this we need to update all the routes that go via this block, not just this route.
        }
    }
    if (blockstate != -1) {
        //We will update all the destination blocks with the new state, it
        //saves re-firing off new updates block status
        boolean stateUpdated = false;
        ArrayList<Routes> rtr = getDestRoutes(updateBlock);
        for (Routes rt : rtr) {
            if (rt.getState() != blockstate) {
                stateUpdated = true;
                rt.stateChange();
            }
        }
        if (stateUpdated) {
            RoutingPacket newUpdate = new RoutingPacket(UPDATE, updateBlock, -1, -1, -1, blockstate, getNextPacketID());
            firePropertyChange("routing", null, newUpdate);
        }
    }
    //We need to expand on this so that any update to routing metric is propergated correctly
    if ((packetmetric != -1) || (hopCount != -1) || (length != -1)) {
        //We only want to send the update on to neighbours that we have advertised the route to.
        ArrayList<Block> messageRecipients = getThroughPathSourceByDestination(srcblk);
        RoutingPacket newUpdate = new RoutingPacket(UPDATE, updateBlock, hopCount, packetmetric, length, blockstate, update.getPacketId());
        updateRoutesToNeighbours(messageRecipients, ro, newUpdate);
    }
//Was just pass on hop count
}
Also used : ArrayList(java.util.ArrayList) Block(jmri.Block) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)142 IOException (java.io.IOException)23 File (java.io.File)22 ArrayList (java.util.ArrayList)20 JPanel (javax.swing.JPanel)14 RollingStock (jmri.jmrit.operations.rollingstock.RollingStock)13 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)13 FlowLayout (java.awt.FlowLayout)8 BoxLayout (javax.swing.BoxLayout)7 Location (jmri.jmrit.operations.locations.Location)7 Dimension (java.awt.Dimension)5 FileOutputStream (java.io.FileOutputStream)5 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 Iterator (java.util.Iterator)5 JScrollPane (javax.swing.JScrollPane)5 RouteLocation (jmri.jmrit.operations.routes.RouteLocation)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 List (java.util.List)4 Entry (java.util.Map.Entry)4