@SuppressWarnings({"rawtypes", "unchecked", "unused"}) private void logServiceProperties(final Class<?> klaz, final Object instance) throws Exception { final BundleContext context = bundleContext(); final ServiceReference[] referenceArray = context.getAllServiceReferences(klaz.getName(), null); for (final ServiceReference reference : referenceArray) { final Object service = context.getService(reference); if (service == instance) { log.info("instance=" + instance); final String[] propKeyArray = reference.getPropertyKeys(); for (final String propKey : propKeyArray) { final Object propValue = reference.getProperty(propKey); log.info(propKey + "=" + propValue); } // final String[] nameArray = (String[]) reference // .getProperty("objectClass"); // for (final String name : nameArray) { // log.info("name=" + name); // } } } }
private static ServiceReferenceDTO getServiceReferenceDTO(ServiceReference<?> ref) { if (ref == null) { return null; } Bundle b = ref.getBundle(); if (b == null) { // service has been unregistered return null; } ServiceReferenceDTO dto = new ServiceReferenceDTO(); dto.bundle = b.getBundleId(); String[] keys = ref.getPropertyKeys(); Map<String, Object> properties = newMap(keys.length); for (String k : keys) { Object v = ref.getProperty(k); if (Constants.SERVICE_ID.equals(k)) { dto.id = ((Long) v).longValue(); } properties.put(k, mapValue(v)); } dto.properties = properties; Bundle[] using = ref.getUsingBundles(); final int length = (using == null) ? 0 : using.length; long[] usingBundles = new long[length]; for (int i = 0; i < length; i++) { usingBundles[i] = using[i].getBundleId(); } dto.usingBundles = usingBundles; return dto; }
/** * Creates a wrapper for the given service reference providing read only access to the reference * properties. */ public ReadOnlyDictionary(final ServiceReference serviceReference) { Hashtable properties = new Hashtable(); final String[] keys = serviceReference.getPropertyKeys(); if (keys != null) { for (int j = 0; j < keys.length; j++) { final String key = keys[j]; properties.put(key, serviceReference.getProperty(key)); } } m_delegatee = properties; }
private void warnPorletProperties( String portletName, ServiceReference<Portlet> serviceReference) { if (!_log.isWarnEnabled()) { return; } List<String> invalidKeys = _portletPropertyValidator.validate(serviceReference.getPropertyKeys()); for (String invalidKey : invalidKeys) { _log.warn("Invalid property " + invalidKey + " for portlet " + portletName); } }
private String getServiceProperties(ServiceReference<HelloService> ref) { StringBuilder builder = new StringBuilder(); for (String key : ref.getPropertyKeys()) { Object value = ref.getProperty(key); String strValue; if (value instanceof Object[]) { strValue = Arrays.toString((Object[]) value); } else { strValue = value.toString(); } if (builder.length() > 0) { builder.append("\n"); } builder.append("\t|").append(key).append(" : ").append(strValue); } return builder.toString(); }
protected T doAction(ServiceReference<S> serviceReference, T service, int action) { Map<String, String> initParameters = new HashMap<String, String>(); if (action != ACTION_REMOVED) { for (String key : serviceReference.getPropertyKeys()) { if (key.startsWith("init.")) { String value = GetterUtil.getString(serviceReference.getProperty(key)); initParameters.put(key.substring(5), value); } } int serviceRanking = GetterUtil.getInteger(serviceReference.getProperty("service.ranking")); initParameters.put("service.ranking", String.valueOf(serviceRanking)); } Bundle bundle = serviceReference.getBundle(); try { BundleServletContext bundleServletContext = httpSupport.getBundleServletContext(bundle); if (action != ACTION_ADDING) { unregisterService(bundleServletContext, serviceReference); } if (action != ACTION_REMOVED) { String contextId = GetterUtil.getString(serviceReference.getProperty("contextId")); HttpContext httpContext = httpSupport.getHttpContext(contextId); if (httpContext == null) { httpContext = bundleServletContext.getHttpContext(); } registerService( bundleServletContext, serviceReference, service, initParameters, httpContext); } } catch (Exception e) { _log.error(e, e); } return service; }
protected void collectInitParams( ServiceReference<Portlet> serviceReference, com.liferay.portal.model.Portlet portletModel) { Map<String, String> initParams = new HashMap<>(); for (String initParamKey : serviceReference.getPropertyKeys()) { if (!initParamKey.startsWith("javax.portlet.init-param.")) { continue; } initParams.put( initParamKey.substring("javax.portlet.init-param.".length()), GetterUtil.getString(serviceReference.getProperty(initParamKey))); } initParams.put(InvokerPortlet.INIT_INVOKER_PORTLET_NAME, "portlet-servlet"); portletModel.setInitParams(initParams); }
@Override protected List<ServiceEntry> load() { List<ServiceReference> listServiceReferences = serviceUtils.listServiceReferences(Domain.class); List<ServiceEntry> result = new ArrayList<ServiceListPanel.ServiceEntry>(); for (ServiceReference ref : listServiceReferences) { ServiceEntry entry = new ServiceEntry(); for (String key : ref.getPropertyKeys()) { entry.properties.put(key, ref.getProperty(key)); } Domain service = serviceUtils.getService(Domain.class, ref); try { entry.aliveState = service.getAliveState(); } catch (Exception e) { LOGGER.error("Couldn't load service entry " + ref, e); entry.aliveState = AliveState.OFFLINE; } result.add(entry); } return result; }
public void propertyChange(PropertyChangeEvent event) { String property = event.getPropertyName(); Object value = event.getNewValue(); String valueString = value.toString(); Iterator iterator = eventListeners.iterator(); final Properties eventProperties = new Properties(); eventProperties.put(property, value); // eventProperties.put(property,valueString); while (iterator.hasNext()) { final ServiceReference serviceReference = (ServiceReference) iterator.next(); String[] properties = serviceReference.getPropertyKeys(); new Thread(null, null, "EventNotifier") { public void run() { try { UPnPEventListener listener = (UPnPEventListener) context.getService(serviceReference); listener.notifyUPnPEvent(deviceId, serviceId, eventProperties); context.ungetService(serviceReference); } catch (Exception ex) { } } }.start(); } }
@Override protected void activate(ComponentContext componentContext) { // TODO Auto-generated method stub super.activate(componentContext); logger.debug( "IN: org.eclipse.smarthome.protocols.enocean.sample.client.Activator.start(bc: " + bundleContext + ")"); /* Track device creation */ try { deviceTracker = new ServiceTracker<EnOceanDevice, Object>( bundleContext, bundleContext.createFilter("(&(objectclass=" + EnOceanDevice.class.getName() + "))"), this); deviceTracker.open(); } catch (InvalidSyntaxException e) { logger.error( "Cannot create ServiceTreacker for filter: " + "(&(objectclass=" + EnOceanDevice.class.getName() + "))", e); } // try { // Thread.sleep(3000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // Display the EnOceanDevice services. ServiceReference<?>[] srs; try { srs = bundleContext.getAllServiceReferences(EnOceanDevice.class.getName(), null); logger.debug("srs: " + srs); if (srs == null) { logger.debug( "There is NO service registered with the following class name: " + EnOceanDevice.class.getName()); } else { logger.debug("srs.length: " + srs.length); int i = 0; while (i < srs.length) { ServiceReference<?> sr = srs[i]; logger.debug("sr: " + sr); String[] pks = sr.getPropertyKeys(); int j = 0; while (j < pks.length) { logger.debug( "pks[" + j + "]: " + pks[j] + ", event.getProperty(" + pks[j] + "): " + sr.getProperty(pks[j])); j = j + 1; } EnOceanDevice eod = (EnOceanDevice) bundleContext.getService(sr); logger.debug("eod: " + eod); logger.debug("eod.getChipId(): " + eod.getChipId()); logger.debug("eod.getFunc(): " + eod.getFunc()); logger.debug("eod.getManufacturer(): " + eod.getManufacturer()); logger.debug("eod.getRollingCode(): " + eod.getRollingCode()); logger.debug("eod.getRorg(): " + eod.getRorg()); logger.debug("eod.getSecurityLevelFormat(): " + eod.getSecurityLevelFormat()); logger.debug("eod.getType(): " + eod.getType()); logger.debug("eod.getClass(): " + eod.getClass()); logger.debug("eod.getEncryptionKey(): " + eod.getEncryptionKey()); logger.debug("eod.getLearnedDevices(): " + eod.getLearnedDevices()); logger.debug("eod.getRPCs(): " + eod.getRPCs()); // // The following RPC is a copy of: // // org.osgi.test.cases.enocean.rpc.QueryFunction // EnOceanRPC rpc = new EnOceanRPC() { // // // sender='0x0180abb8' // // // propertyNames[0]: enocean.device.profile.func, // // event.getProperty(propertyNames[0]): -1 // // // propertyNames[1]: enocean.device.profile.rorg, // // event.getProperty(propertyNames[1]): 165 // // // propertyNames[2]: enocean.device.chip_id, // // event.getProperty(propertyNames[2]): 25209784 // // // propertyNames[3]: enocean.device.profile.type, // // event.getProperty(propertyNames[3]): -1 // // // propertyNames[4]: enocean.message, // // event.getProperty(propertyNames[4]): a5000074080180abb800 // // // private int senderId = -1; // private int senderId = 0x0180abb8; // // public void setSenderId(int chipId) { // this.senderId = chipId; // } // // // public void setPayload(byte[] data) { // // // does nothing; // // logger.debug("rpc.setPayLoad(data: " + data + ")"); // // } // // public int getSenderId() { // return senderId; // } // // public byte[] getPayload() { // return null; // } // // public int getManufacturerId() { // return 0x07ff; // } // // public int getFunctionId() { // // return 0x0007; // return -1; // } // // public String getName() { // // TODO Auto-generated method stub // return null; // } // }; // // EnOceanHandler handler = new EnOceanHandler() { // public void notifyResponse(EnOceanRPC enOceanRPC, // byte[] payload) { // logger.debug("enOceanRPC: " + enOceanRPC // + ", payload: " + payload); // } // }; // // logger.debug("BEFORE invoking..."); // eod.invoke(rpc, handler); // logger.debug("AFTER invoking..."); i = i + 1; // // Let's create an enoceanrpc in order to turn on the plug. // EnOceanRPC turnOnRpc = new EnOceanRPC() { // private int senderId = 0x0180abb8; // // public void setSenderId(int chipId) { // this.senderId = chipId; // } // // public int getSenderId() { // return senderId; // } // // public byte[] getPayload() { // return null; // } // // public int getManufacturerId() { // return -1; // } // // public int getFunctionId() { // return -1; // } // // public String getName() { // return "HARDCODED_TURN_ON"; // } // }; // // EnOceanHandler handlerTurnOnRpc = new EnOceanHandler() { // public void notifyResponse(EnOceanRPC enOceanRPC, // byte[] payload) { // logger.debug("enOceanRPC: " + enOceanRPC // + ", payload: " + payload); // } // }; // // logger.debug("BEFORE invoking..."); // eod.invoke(turnOnRpc, handlerTurnOnRpc); // logger.debug("AFTER invoking..."); } } } catch (InvalidSyntaxException e) { logger.error("Could not get devives from service reference", e); } // eventAdmin ServiceReference<EventAdmin> eventAdminRef = bundleContext.getServiceReference(EventAdmin.class); if (eventAdminRef != null) { eventAdmin = bundleContext.getService(eventAdminRef); } else { logger.debug("No event admin service."); } logger.debug( "OUT: org.eclipse.smarthome.protocols.enocean.sample.client.Activator.start(bc: " + bundleContext + ")"); }
private void printProperties(ServiceReference<?> serviceRef) { for (String key : serviceRef.getPropertyKeys()) { System.out.println(key + " = " + ShellUtil.getValueString(serviceRef.getProperty(key))); } }
protected void verifyServiceReference() { ServiceReference<S> serviceReference = Mockito.verify(_serviceReference); serviceReference.getBundle(); serviceReference.getPropertyKeys(); }
public Enumeration<?> getInitParameterNames() { List<?> keys = Arrays.asList(reference.getPropertyKeys()); return Collections.enumeration(keys); }