/** * Looks up an OSGi service. If the service is not yet available, this method will wait for 60 seconds for the service to become available. If the service * does not appear in this period, a ServiceException is thrown. * * @param serviceClass * The service interface of the service to look up * @param timeoutInMillis * The amount of time in milliseconds to wait for the service to become available * @return an implementation of the given service interface * @throws a * ServiceException, if the service couldn't be found in the OSGi service registry */ public static <T> T getService(Class<T> serviceClass, long timeoutInMillis) { BundleContext ctx = FrameworkUtil.getBundle(ServiceUtil.class).getBundleContext(); ServiceTracker<T, T> tracker = new ServiceTracker<>(ctx, serviceClass, null); tracker.open(); T service = null; try { service = tracker.waitForService(timeoutInMillis); } catch (InterruptedException e) { throw new ServiceException("Interrupted while waiting for the service " + serviceClass.getName(), e); } tracker.close(); if (service != null) { return service; } else { throw new ServiceException("Service " + serviceClass.getName() + " not available"); } }
protected void handleInvalidRequest(SerializationStrategy serializationStrategy, ClassLoader loader, Method method, Object target, DataByteArrayOutputStream responseStream, Runnable onComplete) { //client made an invalid request int pos = responseStream.position(); try { Object value = null; Throwable error = (Throwable)target; serializationStrategy.encodeResponse(loader, null, value, error, responseStream); } catch(Exception e) { LOGGER.warn("Initial Encoding response for method "+method+" failed. Retrying",e); // we failed to encode the response.. reposition and write that error. try { responseStream.position(pos); serializationStrategy.encodeResponse(loader, null, null, new ServiceException(e.toString()), responseStream); } catch (Exception unexpected) { LOGGER.error("Error while servicing "+method,unexpected); } } finally { onComplete.run(); } }
public void send(Throwable error, Object value) { if( responded.compareAndSet(false, true) ) { Class resultType = getResultType(method); try { serializationStrategy.encodeResponse(loader, resultType, value, error, responseStream); } catch (Exception e) { // we failed to encode the response.. reposition and write that error. try { responseStream.position(pos); serializationStrategy.encodeResponse(loader, resultType, value, new ServiceException(e.toString()), responseStream); } catch (Exception unexpected) { LOGGER.error("Error while servicing "+method,unexpected); } } finally { onComplete.run(); } } }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if(method.getDeclaringClass()==Object.class) { //shortcut for equals, hashcode,... return method.invoke(this, args); } return request(this, address, service, classLoader, method, args); } catch (Throwable e) { if (e instanceof ExecutionException) { ExecutionException executionException = (ExecutionException)e; e = executionException.getCause(); } if (e instanceof RuntimeException) { RuntimeException runtimeException = (RuntimeException)e; throw runtimeException; } Class< ? >[] exceptionTypes = method.getExceptionTypes(); for (Class< ? > exceptionType : exceptionTypes) { if(exceptionType.isAssignableFrom(e.getClass())) throw e; } throw new ServiceException(e.getMessage(), e); } }
@Activate protected void activate(final Map<String, Object> props) throws ServiceException { logger.debug("activate(): props = {}", props); this.filterRoots = PropertiesUtil.toStringArray(props.get(PROP_FILTER_ROOTS), null); if (this.filterRoots == null) { throw new ServiceException(PROP_FILTER_ROOTS + " is mandatory!"); } final String localDirValue = StringUtils.trim(PropertiesUtil.toString(props.get(PROP_LOCAL_PATH), null)); if (localDirValue == null) { throw new ServiceException(PROP_LOCAL_PATH + " is mandatory!"); } this.localDir = new File(localDirValue); this.overwriteConfigFiles = PropertiesUtil.toBoolean(props.get(PROP_OVERWRITE_CONFIG_FILES), DEFAULT_OVERWRITE_CONFIG_FILES); this.syncOnceType = PropertiesUtil.toString(props.get(PROP_SYNC_ONCE_TYPE), SYNC_ONCE_DISABLED); generateFiles(); Long expectedSyncOnceTime = null; if (this.willSyncOnce) { expectedSyncOnceTime = PropertiesUtil.toLong(props.get(PROP_SYNC_ONCE_EXPECTED_TIME), DEFAULT_SYNC_ONCE_EXPECTED_TIME); } this.serviceSettings.addSyncRoot(this.localDir, expectedSyncOnceTime); }
@Test public void testActivateMissingProperties() { expectedEx.expect(ServiceException.class); expectedEx.expectMessage(" is mandatory!"); this.initialRegistration.activate(new LinkedHashMap<String, Object>()); }
@Override public final void service(SerializationStrategy serializationStrategy, ClassLoader loader, Method method, Object target, DataByteArrayInputStream requestStream, DataByteArrayOutputStream responseStream, Runnable onComplete) { if(method==null && target instanceof ServiceException) { handleInvalidRequest(serializationStrategy, loader, method, target, responseStream, onComplete); return; } doService(serializationStrategy, loader, method, target, requestStream, responseStream, onComplete); }
private SendTask(DataByteArrayInputStream bais, long correlation, Transport transport, String errorMessage) { this(new ServiceException(errorMessage), bais, null, correlation, new MethodData(new BlockingInvocationStrategy(), ObjectSerializationStrategy.INSTANCE, null),transport); }