Skip to content

Instantly share code, notes, and snippets.

@chetanmeh
Created October 19, 2012 14:42
Show Gist options
  • Save chetanmeh/3918574 to your computer and use it in GitHub Desktop.
Save chetanmeh/3918574 to your computer and use it in GitHub Desktop.
DS sample to manage multiple references (SLING-2575)
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import javax.jcr.*;
import org.apache.felix.scr.annotations.*;
import org.apache.jackrabbit.api.security.user.*;
import org.apache.jackrabbit.core.security.user.action.AbstractAuthorizableAction;
import org.apache.jackrabbit.core.security.user.action.AuthorizableAction;
import org.apache.sling.commons.osgi.ServiceUtil;
import org.slf4j.*;
@Component
@Service(value = AuthorizableAction.class)
@Reference(name = "AuthorizableAction",
policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
referenceInterface = AuthorizableAction.class
)
public class MultiplexingAuthorizableAction extends AbstractAuthorizableAction{
private Logger log = LoggerFactory.getLogger(getClass());
/**
* The map entries are sorted on Service ranking in descending order. So the action instance with higher
* ranking would be invoked first
*/
private Map<Comparable<Object>,AuthorizableAction> actionMap =
new ConcurrentSkipListMap<Comparable<Object>, AuthorizableAction>(Collections.reverseOrder());
public void onCreate(User user, String password, Session session) throws RepositoryException {
log.info("Created user {}", user.getID());
for(AuthorizableAction a : getActions()){
a.onCreate(user,password,session);
}
}
@Deactivate
private void deactivate(){
actionMap.clear();
}
private Collection<AuthorizableAction> getActions() {
return actionMap.values();
}
private void bindAuthorizableAction(AuthorizableAction action,Map<String,Object> config){
actionMap.put(ServiceUtil.getComparableForServiceRanking(config), action);
}
private void unbindAuthorizableAction(AuthorizableAction action,Map<String,Object> config){
actionMap.remove(ServiceUtil.getComparableForServiceRanking(config));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment