/**
   * getPluginsForForeignSource
   *
   * @param pluginClass a {@link java.lang.Class} object.
   * @param foreignSourceName a {@link java.lang.String} object.
   * @param <T> a T object.
   * @return a {@link java.util.List} object.
   */
  public <T> List<T> getPluginsForForeignSource(
      final Class<T> pluginClass, final String foreignSourceName) {
    final ForeignSource foreignSource =
        m_foreignSourceRepository.getForeignSource(foreignSourceName);
    assertNotNull(foreignSource, "Expected a foreignSource with name %s", foreignSourceName);

    final List<PluginConfig> configs = foreignSource.getPolicies();
    if (configs == null) {
      return Collections.emptyList();
    }

    final List<T> plugins = new ArrayList<T>(configs.size());
    for (final PluginConfig config : configs) {
      final T plugin = m_pluginRegistry.getPluginInstance(pluginClass, config);
      if (plugin == null) {
        LogUtils.tracef(
            this,
            "Configured plugin is not appropropriate for policy class %s: %s",
            pluginClass,
            config);
      } else {
        plugins.add(plugin);
      }
    }

    return plugins;
  }
 public void log(String level, String format, Object... args) {
   if ("TRACE".equals(level)) LogUtils.tracef(this, format, args);
   if ("DEBUG".equals(level)) LogUtils.debugf(this, format, args);
   if ("INFO".equals(level)) LogUtils.infof(this, format, args);
   if ("WARN".equals(level)) LogUtils.warnf(this, format, args);
   if ("ERROR".equals(level)) LogUtils.errorf(this, format, args);
   if ("FATAL".equals(level)) LogUtils.errorf(this, format, args);
 }
Example #3
0
 private final void setState(final State oldState, final State newState) {
   if (!m_state.compareAndSet(oldState, newState)) {
     LogUtils.debugf(
         this,
         "Attempted to move to state %s with state not %s (actual value %s)",
         newState,
         oldState,
         m_state.get());
   } else {
     if (LogUtils.isTraceEnabled(this)) {
       LogUtils.tracef(this, "Set state to %s\n", newState);
     }
   }
 }
  private void handleOrphanedComponents(
      final NCSComponent parent,
      final ComponentIdentifier child,
      final ComponentEventQueue ceq,
      final boolean deleteOrphans) {
    final ComponentIdentifier parentId = getIdentifier(parent);
    final NCSComponent childComponent = getComponent(child);

    final Set<ComponentIdentifier> childChildren =
        getIdentifiers(childComponent.getSubcomponents());
    final Set<ComponentIdentifier> childParents =
        getIdentifiers(childComponent.getParentComponents());

    LogUtils.tracef(this, "handleOrphanedComponents: parent: %s", parentId);
    LogUtils.tracef(this, "handleOrphanedComponents: child: %s", child);
    LogUtils.tracef(this, "handleOrphanedComponents: child's children: %s", childChildren);
    LogUtils.tracef(this, "handleOrphanedComponents: child's parents: %s", childParents);

    if (childParents.size() == 1) {
      final ComponentIdentifier childParent = childParents.iterator().next();
      if (childParent.equals(parentId)) {
        LogUtils.tracef(
            this,
            "handleOrphanedComponents: child (%s) has only one parent (%s) and it's being deleted.",
            child,
            childParent);
        deleteComponent(child, ceq, deleteOrphans);
      } else {
        LogUtils.tracef(
            this,
            "handleOrphanedComponents: child (%s) has only one parent (%s) but it's not the one we expected. This is weird.",
            child,
            childParent);
        ceq.componentUpdated(childParent);
      }
    } else {
      LogUtils.tracef(
          this,
          "handleOrphanedComponents: child (%s) has more than one parent, sending updates for remaining parents.",
          child);
      for (final ComponentIdentifier childParent : childParents) {
        ceq.componentUpdated(childParent);
      }
    }
  }
  public SyslogMessage parse() throws SyslogParserException {
    final SyslogMessage syslogMessage = new SyslogMessage();

    String message = getText();

    int lbIdx = message.indexOf('<');
    int rbIdx = message.indexOf('>');

    if (lbIdx < 0 || rbIdx < 0 || lbIdx >= (rbIdx - 1)) {
      LogUtils.warnf(this, "Syslogd received an unparsable message!");
    }

    int priCode = 0;
    String priStr = message.substring(lbIdx + 1, rbIdx);

    try {
      priCode = Integer.parseInt(priStr);
    } catch (final NumberFormatException ex) {
      LogUtils.debugf(this, "ERROR Bad priority code '%s'", priStr);
    }

    LogUtils.tracef(this, "priority code = %d", priCode);

    syslogMessage.setFacility(SyslogFacility.getFacilityForCode(priCode));
    syslogMessage.setSeverity(SyslogSeverity.getSeverityForCode(priCode));

    message = message.substring(rbIdx + 1, message.length());

    final Matcher idMatcher = m_messageIdPattern.matcher(message);
    if (idMatcher.find()) {
      final String messageId = idMatcher.group(2);
      LogUtils.tracef(this, "found message ID '%s'", messageId);
      syslogMessage.setMessageID(messageId);
      message = message.substring(idMatcher.group(1).length() - 1);
    }

    LogUtils.tracef(this, "message = %s", message);

    Matcher oldDateMatcher = m_oldDatePattern.matcher(message);
    if (!oldDateMatcher.find()) {
      oldDateMatcher = null;
    }
    LogUtils.tracef(this, "stdMsg = %s", Boolean.toString(oldDateMatcher != null));

    if (!this.find()) {
      if (traceEnabled()) {
        LogUtils.tracef(
            this, "Lenient Syslog pattern '%s' did not match '%s'", getPattern(), getText());
      }
      return null;
    }

    String timestamp;

    if (oldDateMatcher == null) {
      final Matcher stampMatcher = m_datePattern.matcher(message);
      if (stampMatcher.find()) {
        timestamp = stampMatcher.group(2);
        LogUtils.tracef(this, "found timestamp '%s'", timestamp);
        //                message = message.substring(stampMatcher.group(1).length());
      } else {
        try {
          timestamp = SyslogTimeStamp.getInstance().format(new Date());
        } catch (final IllegalArgumentException ex) {
          LogUtils.debugf(this, "ERROR INTERNAL DATE ERROR!");
          timestamp = "";
        }
      }
    } else {
      timestamp = oldDateMatcher.group(1);
      message = oldDateMatcher.replaceFirst("");
    }

    LogUtils.tracef(this, "timestamp = %s", timestamp);
    syslogMessage.setDate(parseDate(timestamp));

    // These 2 debugs will aid in analyzing the regexes as syslog seems
    // to differ a lot depending on implementation or message structure.

    if (LogUtils.isTraceEnabled(this)) {
      LogUtils.tracef(this, "message = %s", message);
      LogUtils.tracef(this, "pattern = %s", m_forwardingPattern);
      LogUtils.tracef(this, "host group = %d", m_matchingGroupHost);
      LogUtils.tracef(this, "message group = %d", m_matchingGroupMessage);
    }

    // We will also here find out if, the host needs to
    // be replaced, the message matched to a UEI, and
    // last if we need to actually hide the message.
    // this being potentially helpful in avoiding showing
    // operator a password or other data that should be
    // confidential.

    final Pattern pattern = m_forwardingPattern;
    final Matcher m = pattern.matcher(message);

    /*
     * We matched on a regexp for host/message pair.
     * This can be a forwarded message as in BSD Style
     * or syslog-ng.
     */

    if (m.matches()) {

      final String matchedMessage = m.group(m_matchingGroupMessage);
      syslogMessage.setMatchedMessage(matchedMessage);

      if (LogUtils.isTraceEnabled(this)) {
        LogUtils.tracef(
            this, "Syslog message '%s' matched regexp '%s'", message, m_forwardingPattern);
        LogUtils.tracef(this, "Found host '%s'", m.group(m_matchingGroupHost));
        LogUtils.tracef(this, "Found message '%s'", matchedMessage);
      }

      syslogMessage.setHostName(m.group(m_matchingGroupHost));

      message = matchedMessage;
    } else {
      LogUtils.debugf(this, "Regexp not matched: %s", message);
      return null;
    }

    lbIdx = message.indexOf('[');
    rbIdx = message.indexOf(']');
    final int colonIdx = message.indexOf(':');
    final int spaceIdx = message.indexOf(' ');

    int processId = 0;
    String processName = "";
    String processIdStr = "";

    if (lbIdx < (rbIdx - 1) && colonIdx == (rbIdx + 1) && spaceIdx == (colonIdx + 1)) {
      processName = message.substring(0, lbIdx);
      processIdStr = message.substring(lbIdx + 1, rbIdx);
      message = message.substring(colonIdx + 2);

      try {
        processId = Integer.parseInt(processIdStr);
      } catch (final NumberFormatException ex) {
        LogUtils.debugf(this, "Bad process id '%s'", processIdStr);
        processId = 0;
      }
    } else if (lbIdx < 0 && rbIdx < 0 && colonIdx > 0 && spaceIdx == (colonIdx + 1)) {
      processName = message.substring(0, colonIdx);
      message = message.substring(colonIdx + 2);
    }

    syslogMessage.setProcessId(processId);
    syslogMessage.setProcessName(processName);
    syslogMessage.setMessage(message.trim());

    return syslogMessage;
  }