Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package org.isisaddons.module.command.dom.direct;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import com.google.common.collect.Maps;
import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.NatureOfService;
import org.apache.isis.applib.services.bookmark.Bookmark;
import org.apache.isis.applib.services.bookmark.BookmarkHolder;
import org.apache.isis.applib.services.bookmark.BookmarkService2;
import org.apache.isis.applib.services.registry.ServiceRegistry2;
/**
* Patches {@link BookmarkService2} to support domain services also.
*/
@DomainService(nature = NatureOfService.DOMAIN, menuOrder = "100")
public class BookmarkServiceSupportingDomainServices implements BookmarkService2 {
@Override
public Object lookup(final Bookmark bookmark) {
return lookup(bookmark, FieldResetPolicy.RESET);
}
@Override
public Object lookup(
final Bookmark bookmark,
final FieldResetPolicy fieldResetPolicy) {
if(bookmark == null) {
return null;
}
final String objectType = bookmark.getObjectType();
final Object service = lookupService(objectType);
if(service != null) {
return service;
}
return getUnderlying().lookup(bookmark, fieldResetPolicy);
}
@Override
public Object lookup(final BookmarkHolder bookmarkHolder) {
return lookup(bookmarkHolder.bookmark());
}
@Override
public Object lookup(
final BookmarkHolder bookmarkHolder,
final FieldResetPolicy fieldResetPolicy) {
return lookup(bookmarkHolder.bookmark(), fieldResetPolicy);
}
@Override
public <T> T lookup(final Bookmark bookmark, final Class<T> aClass) {
return (T)lookup(bookmark);
}
@Override
public <T> T lookup(
final Bookmark bookmark, final FieldResetPolicy fieldResetPolicy, final Class<T> aClass) {
return (T)lookup(bookmark, fieldResetPolicy);
}
private Map<String,Object> servicesByClassName;
private Object lookupService(final String className) {
cacheServicesByClassNameIfNecessary();
return servicesByClassName.get(className);
}
private void cacheServicesByClassNameIfNecessary() {
if (servicesByClassName == null) {
final Map<String,Object> servicesByClassName = Maps.newHashMap();
final List<Object> registeredServices = serviceRegistry2.getRegisteredServices();
for (Object registeredService : registeredServices) {
final String serviceClassName = registeredService.getClass().getName();
servicesByClassName.put(serviceClassName, registeredService);
}
this.servicesByClassName = servicesByClassName;
}
}
@Override public Bookmark bookmarkFor(final Object o) {
return getUnderlying().bookmarkFor(o);
}
@Override
public Bookmark bookmarkFor(final Class<?> aClass, final String s) {
return getUnderlying().bookmarkFor(aClass, s);
}
private BookmarkService2 underlying;
protected BookmarkService2 getUnderlying() {
return underlying != null ? underlying : (underlying = findUnderlying());
}
protected BookmarkService2 findUnderlying() {
for (BookmarkService2 bookmarkService : bookmarkServices) {
if(bookmarkService == this) {
continue;
}
return bookmarkService;
}
throw new IllegalStateException("Could not locate underlying BookmarkService2 impl.");
}
@Inject
List<BookmarkService2> bookmarkServices;
@Inject
ServiceRegistry2 serviceRegistry2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.