org.osgi.service.event.Event.getPropertyNames()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(149)

本文整理了Java中org.osgi.service.event.Event.getPropertyNames()方法的一些代码示例,展示了Event.getPropertyNames()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Event.getPropertyNames()方法的具体详情如下:
包路径:org.osgi.service.event.Event
类名称:Event
方法名:getPropertyNames

Event.getPropertyNames介绍

[英]Returns a list of this event's property names. The list will include the event topic property name "event.topics".
[中]返回此事件属性名称的列表。该列表将包括事件主题属性名称“event.topics”。

代码示例

代码示例来源:origin: eclipse/smarthome

public void handleEvent(org.osgi.service.event.Event osgiEvent) {
  logger.trace("Handle OSGi event (event: {})", osgiEvent);
  Object typeObj = osgiEvent.getProperty("type");
  Object payloadObj = osgiEvent.getProperty("payload");
  Object topicObj = osgiEvent.getProperty("topic");
  Object sourceObj = osgiEvent.getProperty("source");
  if (typeObj instanceof String && payloadObj instanceof String && topicObj instanceof String) {
    String typeStr = (String) typeObj;
    String payloadStr = (String) payloadObj;
    String topicStr = (String) topicObj;
    String sourceStr = (sourceObj instanceof String) ? (String) sourceObj : null;
    if (!typeStr.isEmpty() && !payloadStr.isEmpty() && !topicStr.isEmpty()) {
      handleEvent(typeStr, payloadStr, topicStr, sourceStr);
    }
  } else {
    logger.error(
        "The handled OSGi event is invalid. Expect properties as string named 'type', 'payload' and 'topic'. "
            + "Received event properties are: {}",
        Arrays.toString(osgiEvent.getPropertyNames()));
  }
}

代码示例来源:origin: apache/karaf-cellar

/**
 * Read a local {@code Event} and check if a property exists.
 *
 * @param event the local event to read.
 * @param name  the property name to check.
 * @return true if the property exists in the event, false else.
 */
public boolean hasEventProperty(Event event, String name) {
  String[] propertyNames = event.getPropertyNames();
  for (String propertyName : propertyNames) {
    if (propertyName.equals(name)) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: org.apache.karaf.decanter.marshaller/org.apache.karaf.decanter.marshaller.csv

private String marshal(Event event) {
  StringBuilder builder = new StringBuilder();
  for (String propertyName : event.getPropertyNames()) {
    Object propertyValue = event.getProperty(propertyName);
    builder.append(propertyName).append("=").append(propertyValue.toString()).append(separator);
  }
  String result = builder.toString();
  result = result.substring(0, result.length() - 1);
  return result;
}

代码示例来源:origin: apache/ace

private Map<String, String> getPayload(Event event) {
    Map<String, String> payload = new HashMap<>();
    for (String propertyName : event.getPropertyNames()) {
      payload.put(propertyName, event.getProperty(propertyName).toString());
    }
    return payload;
  }
}

代码示例来源:origin: apache/karaf

@Override
public Iterable<String> keys() {
  String[] keys = event.getPropertyNames();
  Arrays.sort(keys);
  return () -> new Iterator<String>() {

代码示例来源:origin: org.apache.karaf.decanter.alerting/org.apache.karaf.decanter.alerting.checker

private Event populateAlertEvent(String level, Event collectEvent, String attribute, String pattern, boolean recovery) {
  Map<String, Object> data = new HashMap<>();
  data.put("alertLevel", level);
  data.put("alertAttribute", attribute);
  data.put("alertPattern", pattern);
  data.put("alertBackToNormal", recovery);
  for (String name : collectEvent.getPropertyNames()) {
    data.put(name, collectEvent.getProperty(name));
  }
  Event alertEvent = new Event("decanter/alert/" + level, data);
  return alertEvent;
}

代码示例来源:origin: apache/karaf-cellar

/**
 * Read a local {@code Event} and create a map object out of it.
 *
 * @param event the local event to read.
 * @return the map
 */
public Map<String, Serializable> getEventProperties(Event event) {
  String[] propertyNames = event.getPropertyNames();
  Map<String, Serializable> properties = new HashMap<String, Serializable>();
  for (String propertyName : propertyNames) {
    // event property (org.osgi.framework.ServiceEvent for instance) contains non serializable objects (like source or service reference)
    if (!propertyName.equals("event")) {
      Object property = event.getProperty(propertyName);
      if (property instanceof Serializable)
        properties.put(propertyName, (Serializable) property);
    }
  }
  return properties;
}

代码示例来源:origin: apache/karaf

@Override
public void accept(Event event) {
  out.println(getTimeStamp(event) + " - " + event.getTopic());
  if (verbose) {
    for (String key : event.getPropertyNames()) {
      if (!key.equals("event.topics") && !key.equals("timestamp")) {
        out.println(key + ": " + getPrintValue(event, key));
      }
    }
    out.println();
    out.flush();
  }
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

/**
 * Serializes an OSGi {@link org.osgi.service.event.Event} into a JSON
 * object string
 *
 * @param event the event to be serialized as
 * @return a serialized JSON object
 */
protected static String constructMessage(Event event) {
  Map<String, Object> eventProperties = new LinkedHashMap<>();
  for (String prop : event.getPropertyNames()) {
    Object val = event.getProperty(prop);
    Object converted = convertValue(val);
    eventProperties.put(prop, converted == null ? val : converted);
  }
  eventProperties.put(PROP_TIMESTAMP, ISO8601.format(Calendar.getInstance()));
  Gson gson = new Gson();
  return gson.toJson(eventProperties);
}

代码示例来源:origin: org.eclipse.gemini.blueprint/gemini-blueprint-extender

private void sendEvent(Event osgiEvent) {
    boolean trace = log.isTraceEnabled();
    ServiceReference ref = bundleContext.getServiceReference(EVENT_ADMIN);
    if (ref != null) {
      EventAdmin eventAdmin = (EventAdmin) bundleContext.getService(ref);
      if (eventAdmin != null) {
        if (trace) {
          StringBuilder sb = new StringBuilder();
          String[] names = osgiEvent.getPropertyNames();
          sb.append("{");
          for (int i = 0; i < names.length; i++) {
            String name = names[i];
            sb.append(name);
            sb.append("=");
            Object value = osgiEvent.getProperty(name);
            sb.append(ObjectUtils.getDisplayString(value));
            if (i < names.length - 1)
              sb.append(",");
          }
          sb.append("}");

          log.trace("Broadcasting OSGi event " + osgiEvent + " w/ props " + sb.toString());
        }
        publisher.publish(eventAdmin, osgiEvent);
      }
    } else {
      log.trace("No event admin found for broadcasting event " + osgiEvent);
    }
  }
}

代码示例来源:origin: org.apache.sling/org.apache.sling.event.dea

final String[] propNames = event.getPropertyNames();
if ( propNames != null && propNames.length > 0 ) {
  for(final String propName : propNames) {

代码示例来源:origin: org.apache.sling/org.apache.sling.distribution.core

public void handleEvent(Event event) {
    DistributionRequestType action = SlingConstants.TOPIC_RESOURCE_REMOVED.equals(event.getTopic()) ?
        DistributionRequestType.DELETE : DistributionRequestType.ADD;
    log.info("triggering distribution from event {}", event);
    for (String pn : event.getPropertyNames()) {
      log.info("property {} : {}", pn, event.getProperty(pn));
    }
    Object pathProperty = event.getProperty("path");
    if (pathProperty != null) {
      String distributingPath = String.valueOf(pathProperty);
      requestHandler.handle(null, new SimpleDistributionRequest(action, distributingPath));
    }
  }
}

代码示例来源:origin: apache/felix

String[] properties = event.getPropertyNames();
for (int j = 0; j < properties.length && !dataKeyPresent; j++) {
  if (dataKey.equals(properties[j])) {

代码示例来源:origin: openhab/openhab-core

public void handleEvent(org.osgi.service.event.Event osgiEvent) {
  logger.trace("Handle OSGi event (event: {})", osgiEvent);
  Object typeObj = osgiEvent.getProperty("type");
  Object payloadObj = osgiEvent.getProperty("payload");
  Object topicObj = osgiEvent.getProperty("topic");
  Object sourceObj = osgiEvent.getProperty("source");
  if (typeObj instanceof String && payloadObj instanceof String && topicObj instanceof String) {
    String typeStr = (String) typeObj;
    String payloadStr = (String) payloadObj;
    String topicStr = (String) topicObj;
    String sourceStr = (sourceObj instanceof String) ? (String) sourceObj : null;
    if (!typeStr.isEmpty() && !payloadStr.isEmpty() && !topicStr.isEmpty()) {
      handleEvent(typeStr, payloadStr, topicStr, sourceStr);
    }
  } else {
    logger.error(
        "The handled OSGi event is invalid. Expect properties as string named 'type', 'payload' and 'topic'. "
            + "Received event properties are: {}",
        Arrays.toString(osgiEvent.getPropertyNames()));
  }
}

代码示例来源:origin: org.apache.felix/org.apache.felix.ipojo.handler.eventadmin

String[] properties = event.getPropertyNames();
for (int j = 0; j < properties.length && !dataKeyPresent; j++) {
  if (dataKey.equals(properties[j])) {

代码示例来源:origin: apache/karaf

private Event prepareEvent(Event event) {
  boolean needTimeStamp = addTimestamp && !event.containsProperty(EventConstants.TIMESTAMP);
  boolean needSubject = addSubject && !event.containsProperty(SUBJECT);
  Subject subject = null;
  if (needSubject) {
    subject = Subject.getSubject(AccessController.getContext());
    needSubject = (subject != null);
  }
  if (needTimeStamp || needSubject) {
    String[] names = event.getPropertyNames();
    HashMap<String, Object> map = new HashMap<>(names.length + 1);
    for (String name : names) {
      if (!EventConstants.EVENT_TOPIC.equals(name)) {
        map.put(name, event.getProperty(name));
      }
    }
    if (needTimeStamp) {
      map.put(EventConstants.TIMESTAMP, System.currentTimeMillis());
    }
    if (needSubject) {
      map.put(SUBJECT, subject);
    }
    event = new Event(event.getTopic(), map);
  }
  return event;
}

代码示例来源:origin: org.apache.karaf.services/org.apache.karaf.services.eventadmin

private Event prepareEvent(Event event) {
  boolean needTimeStamp = addTimestamp && !event.containsProperty(EventConstants.TIMESTAMP);
  boolean needSubject = addSubject && !event.containsProperty(SUBJECT);
  Subject subject = null;
  if (needSubject) {
    subject = Subject.getSubject(AccessController.getContext());
    needSubject = (subject != null);
  }
  if (needTimeStamp || needSubject) {
    String[] names = event.getPropertyNames();
    HashMap<String, Object> map = new HashMap<>(names.length + 1);
    for (String name : names) {
      if (!EventConstants.EVENT_TOPIC.equals(name)) {
        map.put(name, event.getProperty(name));
      }
    }
    if (needTimeStamp) {
      map.put(EventConstants.TIMESTAMP, System.currentTimeMillis());
    }
    if (needSubject) {
      map.put(SUBJECT, subject);
    }
    event = new Event(event.getTopic(), map);
  }
  return event;
}

代码示例来源:origin: org.fusesource.insight/insight-log-storage

for (String name : event.getPropertyNames()) {
  if (first) {
    first = false;

代码示例来源:origin: io.fabric8.insight/insight-log-storage

for (String name : event.getPropertyNames()) {
  if (first) {
    first = false;

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

/**
 * Serializes an OSGi {@link org.osgi.service.event.Event} into a JSON object string
 *
 * @param event the event to be serialized as
 * @return a serialized JSON object
 * @throws org.apache.sling.commons.json.JSONException
 */
protected static String constructMessage(Event event) throws JSONException {
  JSONObject obj = new JSONObject();
  for (String prop : event.getPropertyNames()) {
    Object val = event.getProperty(prop);
    Object converted = convertValue(val);
    obj.put(prop, converted == null ? val : converted);
  }
  obj.put(PROP_TIMESTAMP, ISO8601.format(Calendar.getInstance()));
  return obj.toString();
}

相关文章