*/
public class ServiceProcessor implements MetaDataProcessor {
private Logger log = LoggerFactory.getLogger(ServiceProcessor.class);
public void process(final BootstrapContext context, MetaDataScanner reflections) {
final ErraiServiceConfiguratorImpl config = (ErraiServiceConfiguratorImpl) context.getConfig();
final Set<Class<?>> services = reflections.getTypesAnnotatedWithExcluding(Service.class, MetaDataScanner.CLIENT_PKG_REGEX);
for (Class<?> loadClass : services) {
Object svc = null;
Service svcAnnotation = loadClass.getAnnotation(Service.class);
if (null == svcAnnotation) {
// Diagnose Errai-111
StringBuffer sb = new StringBuffer();
sb.append("Service annotation cannot be loaded. (See https://jira.jboss.org/browse/ERRAI-111)\n");
sb.append(loadClass.getSimpleName()).append(" loader: ").append(loadClass.getClassLoader()).append("\n");
sb.append("@Service loader:").append(Service.class.getClassLoader()).append("\n");
log.warn(sb.toString());
continue;
}
boolean local = loadClass.isAnnotationPresent(Local.class);
String svcName = svcAnnotation.value();
// If no name is specified, just use the class name as the service by default.
if ("".equals(svcName)) {
svcName = loadClass.getSimpleName();
}
Map<String, Method> commandPoints = new HashMap<String, Method>();
for (final Method method : loadClass.getDeclaredMethods()) {
if (method.isAnnotationPresent(Command.class)) {
Command command = method.getAnnotation(Command.class);
for (String cmdName : command.value()) {
if (cmdName.equals("")) cmdName = method.getName();
commandPoints.put(cmdName, method);
}
}
}
Class remoteImpl = getRemoteImplementation(loadClass);
if (remoteImpl != null) {
svc = createRPCScaffolding(remoteImpl, loadClass, context);
}
else if (MessageCallback.class.isAssignableFrom(loadClass)) {
final Class<? extends MessageCallback> clazz = loadClass.asSubclass(MessageCallback.class);
log.info("discovered service: " + clazz.getName());
try {
svc = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(MessageCallback.class).to(clazz);
bind(MessageBus.class).toInstance(context.getBus());
bind(RequestDispatcher.class).toInstance(context.getService().getDispatcher());
bind(TaskManager.class).toInstance(TaskManagerFactory.get());
// Add any extension bindings.
for (Map.Entry<Class<?>, ResourceProvider> entry : config.getExtensionBindings().entrySet()) {
bind(entry.getKey()).toProvider(new GuiceProviderProxy(entry.getValue()));
}
}
}).getInstance(MessageCallback.class);
}
catch (Throwable t) {
t.printStackTrace();
}
if (commandPoints.isEmpty()) {
// Subscribe the service to the bus.
context.getBus().subscribe(svcName, (MessageCallback) svc);
}
RolesRequiredRule rule = null;
if (clazz.isAnnotationPresent(RequireRoles.class)) {
rule = new RolesRequiredRule(clazz.getAnnotation(RequireRoles.class).value(), context.getBus());
}
else if (clazz.isAnnotationPresent(RequireAuthentication.class)) {
rule = new RolesRequiredRule(new HashSet<Object>(), context.getBus());
}
if (rule != null) {
context.getBus().addRule(svcName, rule);
}
}
if (svc == null) {
svc = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(MessageBus.class).toInstance(context.getBus());
bind(RequestDispatcher.class).toInstance(context.getService().getDispatcher());
bind(TaskManager.class).toInstance(TaskManagerFactory.get());
// Add any extension bindings.
for (Map.Entry<Class<?>, ResourceProvider> entry : config.getExtensionBindings().entrySet()) {
bind(entry.getKey()).toProvider(new GuiceProviderProxy(entry.getValue()));
}
}
}).getInstance(loadClass);
}