move type based service finder to sse bundles (create new com.sap.sse.osgi bundle)

This commit is contained in:
Fredrik Teschke committed 2015-01-23 16:56:49 +01:00
1 parent 3170d94fd4
commit 3edbbb04bb
65 files changed
+226 -81

No files matched your search

@@ -0,0 +1,26 @@
package com.sap.sse.common;
import java.io.Serializable;
public class NoCorrespondingServiceRegisteredException extends RuntimeException implements Serializable {
public String type;
public String serviceInterface;
private static final long serialVersionUID = -358955216089477585L;
protected NoCorrespondingServiceRegisteredException() {}
public NoCorrespondingServiceRegisteredException(String message, String type, String serviceInterface) {
super(message);
this.type = type;
this.serviceInterface = serviceInterface;
}
public String getDeviceType() {
return type;
}
@Override
public String toString() {
return super.toString() + " (Service: " + serviceInterface + ", Type: " + type + ")";
}
}
@@ -0,0 +1,30 @@
package com.sap.sse.common;
/**
* @author Fredrik Teschke
*
* @param <ServiceType> Specifies desired the service interface
*/
public interface TypeBasedServiceFinder<ServiceType> {
public static final String TYPE = "type";
/**
* Find a service that implements the <ServiceType> interface for the specified {@code type}
* of object (e.g. type of a {@link GPSFix}).
* @param type A {@link String} that specifies for which type the service should be registered
* @throws NoCorrespondingServiceRegisteredException Thrown, if no service is registered for this type.
*/
ServiceType findService(String type) throws NoCorrespondingServiceRegisteredException;
/**
* Sets a fallback service (optional), that is used if no service is registered for a {@code type}.
* This should be a generic service, that can deal with any {@code type}, and provides a
* "better-than-nothing" implementation, which will be returned instead of throwing a
* {@link NoCorrespondingServiceRegisteredException}.
* <p>
* This method does not need to be implemented, but can remain a stub, if no fallback behaviour
* is desired.
*/
void setFallbackService(ServiceType fallback);
}
@@ -0,0 +1,18 @@
package com.sap.sse.common;
/**
* A factory for creating {@link TypeBasedServiceFinder}s, for a specific type of service.
* The desired service type is specified by the {@link Class} object passed to the
* {@link #createServiceFinder(Class)} method.
*
* The created {@link TypeBasedServiceFinder} can then be used to find the desired
* service for a specific type. E.g., there may be a persistence service interface for
* {@link GPSFix}es, and there are several different service registered, depending on the type
* of {@link GPSFix}. The service finder then can find the appropriate service for each fix.
* @author Fredrik Teschke
*
*/
public interface TypeBasedServiceFinderFactory {
<ServiceT> TypeBasedServiceFinder<ServiceT> createServiceFinder(Class<ServiceT> clazz);
}
@@ -0,0 +1,40 @@
package com.sap.sse.common.impl;
import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
import com.sap.sse.common.TypeBasedServiceFinder;
/**
* Only can provide the service for a single {@code type}, e.g. on smartphones with no OSGi context.
* Optionally, a {@link #setFallbackService(Object) fallback} can be set.
* @author Fredrik Teschke
*/
public class SingleTypeBasedServiceFinderImpl<ServiceType> implements
TypeBasedServiceFinder<ServiceType> {
private final ServiceType service;
private final String type;
private ServiceType fallback;
public SingleTypeBasedServiceFinderImpl(ServiceType service, ServiceType fallback, String type) {
this.service = service;
this.type = type;
this.fallback = fallback;
}
public SingleTypeBasedServiceFinderImpl(ServiceType service, String type) {
this(service, null, type);
}
@Override
public ServiceType findService(String type)
throws NoCorrespondingServiceRegisteredException {
if (type.equals(this.type)) return service;
if (fallback != null) return fallback;
throw new NoCorrespondingServiceRegisteredException("Only one service registered", type, service.getClass().getSimpleName());
}
@Override
public void setFallbackService(ServiceType fallback) {
this.fallback = fallback;
}
}