/** Creates an aspect for interception if the method should be intercepted. */ @Override public AspectGenerator<X> create(AnnotatedMethod<? super X> method, boolean isEnhanced) { RunAs runAs = method.getAnnotation(RunAs.class); if (runAs == null) runAs = _classRunAs; String runAsName = null; if (runAs != null) runAsName = runAs.value(); RolesAllowed rolesAllowed = method.getAnnotation(RolesAllowed.class); if (rolesAllowed == null) rolesAllowed = _classRolesAllowed; String[] roleNames = null; if (rolesAllowed != null) roleNames = rolesAllowed.value(); PermitAll permitAll = method.getAnnotation(PermitAll.class); if (permitAll != null || _classPermitAll != null) roleNames = null; DenyAll denyAll = method.getAnnotation(DenyAll.class); if (denyAll != null || _classDenyAll != null) roleNames = new String[0]; if (roleNames != null || runAs != null) { AspectGenerator<X> next = super.create(method, true); return new SecurityGenerator<X>(this, method, next, roleNames, runAsName); } else return super.create(method, isEnhanced); }
protected <Z extends Annotation> Z getAnnotation( Class<Z> annotationType, AnnotatedMethod<?> apiMethod, AnnotatedType<?> apiClass, AnnotatedMethod<?> implementationMethod, AnnotatedType<?> implementationClass) { Z annotation = null; if (apiMethod != null) { annotation = apiMethod.getAnnotation(annotationType); } if (annotation == null && apiClass != null) { annotation = apiClass.getAnnotation(annotationType); } if ((annotation == null) && (implementationMethod != null)) { annotation = implementationMethod.getAnnotation(annotationType); } if ((annotation == null) && (implementationClass != null)) { annotation = implementationClass.getAnnotation(annotationType); } return annotation; }
protected <Z extends Annotation> Z getAnnotation( Class<Z> annotationType, AnnotatedMethod<?> apiMethod, AnnotatedMethod<?> implMethod) { Z annotation; annotation = apiMethod.getAnnotation(annotationType); if ((annotation == null) && (implMethod != null)) { annotation = implMethod.getAnnotation(annotationType); } return annotation; }
@Override public ConfigProgram introspectMethod(AnnotatedMethod<?> method) { PersistenceContext pContext = method.getAnnotation(PersistenceContext.class); Method javaMethod = method.getJavaMember(); String location = getLocation(javaMethod); Class<?> param = javaMethod.getParameterTypes()[0]; if (!param.isAssignableFrom(EntityManager.class)) { throw new ConfigException( L.l( "{0}: @PersistenceContext method must be assignable from EntityManager.", getLocation(javaMethod))); } BeanValueGenerator gen; /* if (PersistenceContextType.EXTENDED.equals(type)) return generateExtendedContext(field, pContext); else */ gen = generateTransactionContext(location, pContext); return new MethodGeneratorProgram(javaMethod, gen); }
private RequestMappingInfo findMethodRequestMapping(FacesContext context, Bean<?> bean) { RequestMappingInfo result = null; Class clazz = bean.getBeanClass(); AnnotatedType annotatedType = beanManager.createAnnotatedType(clazz); Set<AnnotatedMethod> annotatedMethodSet = annotatedType.getMethods(); for (AnnotatedMethod method : annotatedMethodSet) { if (method.isAnnotationPresent(RequestMapping.class)) { RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); String[] mappings = requestMapping.value(); String mapping = null; for (String current : mappings) { String pathInfo = context.getExternalContext().getRequestPathInfo(); if (pathInfo.equals(current)) { result = new RequestMappingInfo(); result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(mapping); result.setMappingType(RequestMappingInfo.MappingType.EXACT); break; } else if (current.endsWith("*")) { current = current.substring(0, current.length() - 1); if (pathInfo.startsWith(current)) { if (result == null) { result = new RequestMappingInfo(); result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(current); result.setMappingType(RequestMappingInfo.MappingType.PREFIX); } else if (current.length() > result.getLength()) { result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(current); result.setMappingType(RequestMappingInfo.MappingType.PREFIX); } } } else if (current.startsWith("*")) { current = current.substring(1); if (pathInfo.endsWith(current)) { result = new RequestMappingInfo(); result.setBean(bean); result.setMethod(method.getJavaMember()); result.setRequestMapping(current); result.setMappingType(RequestMappingInfo.MappingType.EXTENSION); break; } } } } if (result != null && (result.getMappingType().equals(RequestMappingInfo.MappingType.EXACT) || (result.getMappingType().equals(RequestMappingInfo.MappingType.EXTENSION)))) { break; } } return result; }
@Override public void run() { if (System.currentTimeMillis() > expiryTime) { scheduledExecutorService.shutdown(); throw new RuntimeException( "failed to discover beans: " + managedTypes.getServiceEndpoints()); } if (registered.isEmpty()) { scheduledExecutorService.shutdown(); log.info("all services registered successfully"); return; } for (final AnnotatedType<?> type : managedTypes.getServiceEndpoints()) { if (!registered.contains(type) || beanManager.getBeans(type.getJavaClass()).size() == 0) { continue; } final MessageCallback callback = (MessageCallback) CDIServerUtil.lookupBean(beanManager, type.getJavaClass()); registered.remove(type); // Discriminate on @Command final Map<String, Method> commandPoints = new HashMap<String, Method>(); for (final AnnotatedMethod method : type.getMethods()) { if (method.isAnnotationPresent(Command.class)) { final Command command = method.getAnnotation(Command.class); for (String cmdName : command.value()) { if (cmdName.equals("")) cmdName = method.getJavaMember().getName(); commandPoints.put(cmdName, method.getJavaMember()); } } } final String subjectName = CDIServerUtil.resolveServiceName(type.getJavaClass()); if (commandPoints.isEmpty()) { bus.subscribe(subjectName, callback); } else { bus.subscribeLocal( subjectName, new CommandBindingsCallback(commandPoints, callback, bus)); } } }
@Override public ConfigProgram introspectMethod(AnnotatedMethod<?> method) { Resource resource = method.getAnnotation(Resource.class); Method javaMethod = method.getJavaMember(); String loc = getLocation(method.getJavaMember()); String jndiName = (javaMethod.getDeclaringClass().getName() + "/" + javaMethod.getName()); Class<?> bindType = javaMethod.getParameterTypes()[0]; ValueGenerator gen = generateContext(loc, bindType, jndiName, resource); bindJndi(javaMethod, gen); return new MethodGeneratorProgram(method.getJavaMember(), gen); }
private void subscribeServices(final BeanManager beanManager, final MessageBus bus) { for (final Map.Entry<AnnotatedType, List<AnnotatedMethod>> entry : managedTypes.getServiceMethods().entrySet()) { final Class<?> type = entry.getKey().getJavaClass(); for (final AnnotatedMethod method : entry.getValue()) { final Service svc = method.getAnnotation(Service.class); final String svcName = svc.value().equals("") ? method.getJavaMember().getName() : svc.value(); final Method callMethod = method.getJavaMember(); bus.subscribe( svcName, new MessageCallback() { @Override public void callback(final Message message) { final Object targetBean = CDIServerUtil.lookupBean(beanManager, type); try { callMethod.invoke(targetBean, message); } catch (Exception e) { ErrorHelper.sendClientError(bus, message, "Error dispatching service", e); } } }); } } /** * Due to the lack of contract in CDI guaranteeing when beans will be available, we use an * executor to search for the beans every 100ms until it finds them. Or, after a 25 seconds, * blow up if they don't become available. */ final ScheduledExecutorService startupScheduler = Executors.newScheduledThreadPool(1); startupScheduler.scheduleAtFixedRate( new StartupCallback(beanManager, bus, startupScheduler, 25), 0, 100, TimeUnit.MILLISECONDS); for (final Class<?> remoteInterfaceType : managedTypes.getRemoteInterfaces()) { createRPCScaffolding(remoteInterfaceType, bus, beanManager); } }