@Override
public void handle(AtmosphereFramework framework, Class<Object> annotatedClass) {
try {
Class<?> aClass = annotatedClass;
ManagedService a = aClass.getAnnotation(ManagedService.class);
framework.setBroadcasterCacheClassName(a.broadcasterCache().getName());
List<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
atmosphereConfig(a.atmosphereConfig(), framework);
filters(a.broadcastFilters(), framework);
AtmosphereInterceptor aa = listeners(a.listeners(), framework);
if (aa != null) {
l.add(aa);
}
Object c = framework.newClassInstance(Object.class, aClass);
AtmosphereHandler handler = framework.newClassInstance(ManagedAtmosphereHandler.class,
ManagedAtmosphereHandler.class).configure(framework.getAtmosphereConfig(), c);
// MUST BE ADDED FIRST, ALWAYS!
l.add(framework.newClassInstance(AtmosphereInterceptor.class, ManagedServiceInterceptor.class));
framework.filterManipulator(new BroadcasterConfig.FilterManipulator() {
@Override
public Object unwrap(Object o) {
if (o != null && ManagedAtmosphereHandler.Managed.class.isAssignableFrom(o.getClass())) {
o = ManagedAtmosphereHandler.Managed.class.cast(o).object();
}
return o;
}
@Override
public BroadcastFilter.BroadcastAction wrap(BroadcastFilter.BroadcastAction a, boolean wasWrapped) {
if (wasWrapped) {
return new BroadcastFilter.BroadcastAction(a.action(), new ManagedAtmosphereHandler.Managed(a.message()));
} else {
return a;
}
}
});
AnnotationUtil.defaultInterceptors(framework, l);
AnnotationUtil.interceptors(framework, a.interceptors(), l);
framework.addAtmosphereHandler(a.path(), handler, broadcaster(framework, a.broadcaster(), a.path()), l);
} catch (Throwable e) {
logger.warn("", e);
}
}