@Override
 public void handleTimeout(InetAddress address, EchoPacket request) {
   String oldPrefix = ThreadCategory.getPrefix();
   try {
     ThreadCategory.setPrefix(m_prefix);
     m_cb.handleTimeout(address, request);
   } finally {
     ThreadCategory.setPrefix(oldPrefix);
   }
 }
 @Override
 public void handleResponse(InetAddress address, EchoPacket response) {
   String oldPrefix = ThreadCategory.getPrefix();
   try {
     ThreadCategory.setPrefix(m_prefix);
     m_cb.handleResponse(address, response);
   } finally {
     ThreadCategory.setPrefix(oldPrefix);
   }
 }
  /** {@inheritDoc} */
  @Override
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    BufferedWriter bw =
        new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
    ThreadCategory.setPrefix(MapsConstants.LOG4J_CATEGORY);
    log = ThreadCategory.getInstance(this.getClass());

    try {
      String user = request.getRemoteUser();

      if (log.isDebugEnabled()) log.debug("MapStartUp for user:"******"Error in map's startup", e);
      bw.write(ResponseAssembler.getMapErrorResponse(MapsConstants.MAPS_STARTUP_ACTION));
    } finally {
      bw.close();
    }

    return null;
  }
예제 #4
0
  /** {@inheritDoc} */
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    ThreadCategory.setPrefix(MapsConstants.LOG4J_CATEGORY);
    log = ThreadCategory.getInstance(this.getClass());
    String action = request.getParameter("action");
    String elems = request.getParameter("elems");
    log.debug("Adding Nodes action:" + action + ", elems=" + elems);

    BufferedWriter bw =
        new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
    try {
      Integer[] nodeids = null;

      boolean actionfound = false;

      if (action.equals(MapsConstants.ADDNODES_ACTION)) {
        log.debug("Adding nodes by id: " + elems);
        actionfound = true;
        String[] snodeids = elems.split(",");
        nodeids = new Integer[snodeids.length];
        for (int i = 0; i < snodeids.length; i++) {
          nodeids[i] = new Integer(snodeids[i]);
        }
      }

      if (action.equals(MapsConstants.ADDNODES_BY_CATEGORY_ACTION)) {
        log.debug("Adding nodes by category: " + elems);
        actionfound = true;
        String categoryName = elems;
        CategoryFactory.init();
        CatFactory cf = CategoryFactory.getInstance();
        cf.getReadLock().lock();
        try {
          final String rule = cf.getEffectiveRule(categoryName);
          final List<InetAddress> nodeIPs = FilterDaoFactory.getInstance().getIPAddressList(rule);
          LogUtils.debugf(this, "ips found: %s", nodeIPs.toString());
          nodeids = new Integer[nodeIPs.size()];
          for (int i = 0; i < nodeIPs.size(); i++) {
            final InetAddress nodeIp = nodeIPs.get(i);
            final List<Integer> ids =
                NetworkElementFactory.getInstance(getServletContext())
                    .getNodeIdsWithIpLike(InetAddressUtils.str(nodeIp));
            LogUtils.debugf(this, "Ids by ipaddress %s: %s", nodeIp, ids.toString());
            nodeids[i] = ids.get(0);
          }
        } finally {
          cf.getReadLock().unlock();
        }
      }

      if (action.equals(MapsConstants.ADDNODES_BY_LABEL_ACTION)) {
        log.debug("Adding nodes by label: " + elems);
        actionfound = true;
        List<OnmsNode> nodes = NetworkElementFactory.getInstance(getServletContext()).getAllNodes();
        nodeids = new Integer[nodes.size()];
        for (int i = 0; i < nodes.size(); i++) {
          nodeids[i] = nodes.get(i).getId();
        }
      }

      if (action.equals(MapsConstants.ADDRANGE_ACTION)) {
        log.debug("Adding nodes by range: " + elems);
        actionfound = true;
        nodeids =
            (Integer[])
                NetworkElementFactory.getInstance(getServletContext())
                    .getNodeIdsWithIpLike(elems)
                    .toArray(new Integer[0]);
      }

      if (action.equals(MapsConstants.ADDNODES_NEIG_ACTION)) {
        log.debug("Adding nodes neighbor of:" + elems);
        actionfound = true;
        nodeids =
            (Integer[])
                NetworkElementFactory.getInstance(getServletContext())
                    .getLinkedNodeIdOnNode(WebSecurityUtils.safeParseInt(elems))
                    .toArray(new Integer[0]);
      }

      if (action.equals(MapsConstants.ADDNODES_WITH_NEIG_ACTION)) {
        log.debug("Adding nodes with neighbor of:" + elems);
        actionfound = true;
        Set<Integer> linkednodeids =
            NetworkElementFactory.getInstance(getServletContext())
                .getLinkedNodeIdOnNode(WebSecurityUtils.safeParseInt(elems));
        linkednodeids.add(new Integer(elems));
        nodeids = linkednodeids.toArray(new Integer[linkednodeids.size()]);
      }

      VMap map = manager.openMap();
      if (log.isDebugEnabled()) log.debug("Got map from manager " + map);

      List<VElement> velems = new ArrayList<VElement>();
      // response for addElement
      if (actionfound) {
        log.debug("Before Checking map contains elems");

        for (int i = 0; i < nodeids.length; i++) {
          int elemId = nodeids[i].intValue();
          if (map.containsElement(elemId, MapsConstants.NODE_TYPE)) {
            log.debug(
                "Action: "
                    + action
                    + " . Map Contains Element: "
                    + elemId
                    + MapsConstants.NODE_TYPE);
            continue;
          }

          velems.add(manager.newElement(map.getId(), elemId, MapsConstants.NODE_TYPE));
        } // end for

        // get links and add elements to map
        map = manager.addElements(map, velems);
        log.debug("After getting/adding links");

        bw.write(ResponseAssembler.getAddElementResponse(null, velems, map.getLinks()));
      }
    } catch (Throwable e) {
      log.error("Error while adding nodes for action: " + action, e);
      bw.write(ResponseAssembler.getMapErrorResponse(action));
    } finally {
      bw.close();
    }

    return null;
  }
 /** Constructor for DefaultReportWrapperService. */
 public DefaultReportWrapperService() {
   String oldPrefix = ThreadCategory.getPrefix();
   ThreadCategory.setPrefix(LOG4J_CATEGORY);
   log = ThreadCategory.getInstance(DefaultReportWrapperService.class);
   ThreadCategory.setPrefix(oldPrefix);
 }
  /** The execution context. */
  @Override
  public void run() {
    // get the context
    m_context = Thread.currentThread();

    // Get a log instance
    ThreadCategory.setPrefix(m_logPrefix);
    ThreadCategory log = ThreadCategory.getInstance(getClass());

    if (m_stop) {
      log.debug("Stop flag set before thread started, exiting");
      return;
    } else log.debug("Thread context started");

    // allocate a buffer
    final int length = 0xffff;
    final byte[] buffer = new byte[length];

    // set an SO timeout to make sure we don't block forever
    // if a socket is closed.
    try {
      log.debug("Setting socket timeout to " + SOCKET_TIMEOUT + "ms");
      m_dgSock.setSoTimeout(SOCKET_TIMEOUT);
    } catch (SocketException e) {
      log.warn("An I/O error occured while trying to set the socket timeout", e);
    }

    // Increase the receive buffer for the socket
    try {
      log.debug("Setting receive buffer size to " + length);
      m_dgSock.setReceiveBufferSize(length);
    } catch (SocketException e) {
      log.info("Failed to set the receive buffer to " + length, e);
    }
    // set to avoid numerous tracing message
    boolean ioInterrupted = false;
    // now start processing incoming requests
    while (!m_stop) {
      if (m_context.isInterrupted()) {
        log.debug("Thread context interrupted");
        break;
      }

      try {
        if (!ioInterrupted) {
          log.debug("Waiting on a datagram to arrive");
        }

        DatagramPacket pkt = new DatagramPacket(buffer, length);
        m_dgSock.receive(pkt);

        // SyslogConnection *Must* copy packet data and InetAddress as DatagramPacket is a mutable
        // type
        WaterfallExecutor.waterfall(
            m_executors,
            new SyslogConnection(
                pkt,
                m_matchPattern,
                m_hostGroup,
                m_messageGroup,
                m_UeiList,
                m_HideMessages,
                m_discardUei));
        ioInterrupted = false; // reset the flag
      } catch (SocketTimeoutException e) {
        ioInterrupted = true;
        continue;
      } catch (InterruptedIOException e) {
        ioInterrupted = true;
        continue;
      } catch (ExecutionException e) {
        log.error("Task execution failed in " + this.getClass().getSimpleName(), e);
        break;
      } catch (InterruptedException e) {
        log.error("Task interrupted in " + this.getClass().getSimpleName(), e);
        break;
      } catch (IOException e) {
        log.error("An I/O exception occured on the datagram receipt port, exiting", e);
        break;
      }
    } // end while status OK

    log.debug("Thread context exiting");
  }
  /** Constructor for XMPPNotificationManager. */
  protected XMPPNotificationManager() {

    // get the category logger
    String oldPrefix = ThreadCategory.getPrefix();
    ThreadCategory.setPrefix(LOG4J_CATEGORY);

    try {
      // Load up some properties

      File config = null;
      try {
        config = ConfigFileConstants.getFile(ConfigFileConstants.XMPP_CONFIG_FILE_NAME);
      } catch (IOException e) {
        log().warn(ConfigFileConstants.XMPP_CONFIG_FILE_NAME + " not readable", e);
      }
      if (Boolean.getBoolean("useSystemXMPPConfig") || !config.canRead()) {
        this.props.putAll(System.getProperties());
      } else {
        FileInputStream fis = null;
        try {
          fis = new FileInputStream(config);
          this.props.load(fis);
        } catch (FileNotFoundException e) {
          log().warn("unable to load " + config, e);
        } catch (IOException e) {
          log().warn("unable to load " + config, e);
        } finally {
          IOUtils.closeQuietly(fis);
        }
      }

      xmppServer = this.props.getProperty("xmpp.server");
      xmppServiceName = this.props.getProperty("xmpp.servicename", xmppServer);
      xmppUser = this.props.getProperty("xmpp.user");
      xmppPassword = this.props.getProperty("xmpp.pass");
      xmppPort = Integer.valueOf(this.props.getProperty("xmpp.port", XMPP_PORT));

      xmppConfig = new ConnectionConfiguration(xmppServer, xmppPort, xmppServiceName);

      boolean debuggerEnabled = Boolean.parseBoolean(props.getProperty("xmpp.debuggerEnabled"));
      xmppConfig.setDebuggerEnabled(debuggerEnabled);
      if (debuggerEnabled) {
        log().setLevel(ThreadCategory.Level.DEBUG);
      }

      xmppConfig.setSASLAuthenticationEnabled(
          Boolean.parseBoolean(props.getProperty("xmpp.SASLEnabled", "true")));
      xmppConfig.setSelfSignedCertificateEnabled(
          Boolean.parseBoolean(props.getProperty("xmpp.selfSignedCertificateEnabled")));

      if (Boolean.parseBoolean(props.getProperty("xmpp.TLSEnabled"))) {
        xmppConfig.setSecurityMode(SecurityMode.enabled);
      } else {
        xmppConfig.setSecurityMode(SecurityMode.disabled);
      }
      if (this.props.containsKey("xmpp.truststorePassword")) {
        xmppConfig.setTruststorePassword(this.props.getProperty("xmpp.truststorePassword"));
      } else {
        xmppConfig.setTruststorePassword(TRUST_STORE_PASSWORD);
      }

      if (log().isDebugEnabled()) {
        log().debug("XMPP Manager connection config: " + xmppConfig.toString());
      }

      xmpp = new XMPPConnection(xmppConfig);

      // Connect to xmpp server
      connectToServer();
    } finally {
      ThreadCategory.setPrefix(oldPrefix);
    }
  }