private final void stopWatching(@Observes @BeforeDestroyed(ApplicationScoped.class) @Priority(LIBRARY_BEFORE) final Object event) throws Exception { final Closeable watch = this.watch; if (watch != null) { KubernetesClientException closeException = this.closeException; try { watch.close(); } catch (final Exception everything) { if (closeException != null) { closeException.addSuppressed(everything); throw closeException; } else { throw everything; } } if (closeException != null) { throw closeException; } } }
/** * Activates the alternatives declared with {@code @Beans} globally for the * application. * <p/> * For every types and every methods of every types declared with * {@link Beans#alternatives()}, the {@code Priority} annotation is added * so that the corresponding alternatives are selected globally for the * entire application. * * @see Beans */ private <T> void alternatives(@Observes @WithAnnotations(Alternative.class) ProcessAnnotatedType<T> pat) { AnnotatedType<T> type = pat.getAnnotatedType(); if (!Arrays.asList(beans.alternatives()).contains(type.getJavaClass())) { // Only select globally the alternatives that are declared with @Beans return; } Set<AnnotatedMethod<? super T>> methods = new HashSet<>(); for (AnnotatedMethod<? super T> method : type.getMethods()) { if (method.isAnnotationPresent(Alternative.class) && !method.isAnnotationPresent(Priority.class)) { methods.add(new AnnotatedMethodDecorator<>(method, PriorityLiteral.of(APPLICATION))); } } if (type.isAnnotationPresent(Alternative.class) && !type.isAnnotationPresent(Priority.class)) { pat.setAnnotatedType(new AnnotatedTypeDecorator<>(type, PriorityLiteral.of(APPLICATION), methods)); } else if (!methods.isEmpty()) { pat.setAnnotatedType(new AnnotatedTypeDecorator<>(type, methods)); } }
public static int compareServices(Object o1, Object o2) { int prio1 = 0; int prio2 = 0; Priority prio1Annot = o1.getClass().getAnnotation(Priority.class); if (prio1Annot != null) { prio1 = prio1Annot.value(); } Priority prio2Annot = o2.getClass().getAnnotation(Priority.class); if (prio2Annot != null) { prio2 = prio2Annot.value(); } if (prio1 < prio2) { return 1; } if (prio2 < prio1) { return -1; } return o2.getClass().getSimpleName().compareTo(o1.getClass().getSimpleName()); }
public <X> void beans( final @Observes ProcessAnnotatedType<X> processBean ) { if (!processBean.getAnnotatedType().isAnnotationPresent(Interceptor.class)) { return; } final FilteringAnnotatedTypeWrapper<X> filtered = new FilteringAnnotatedTypeWrapper<>( processBean.getAnnotatedType(), it -> it != Priority.class ); processBean.setAnnotatedType(filtered); }
/** * The priority of this mapper. By default, it will use the {@link Priority} annotation's value as the priority. * If no annotation is present, it uses a default priority of {@link Priorities#USER}. * @return the priority of this mapper */ default int getPriority() { Priority priority = getClass().getAnnotation(Priority.class); if(priority == null) { return DEFAULT_PRIORITY; } return priority.value(); }
@Test public void testAnnotations() { // Check that the class is correctly annotated assertNotNull("@PreMatching annotation is required to modify headers", MediaTypeFilter.class.getAnnotation(PreMatching.class)); Priority priority = MediaTypeFilter.class.getAnnotation(Priority.class); assertNotNull("@Priority annotation is required to modify headers", priority); assertTrue("priority should be higher than HEADER_DECORATOR", priority.value() <= Priorities.HEADER_DECORATOR); }
/** * Finds view engine for a viewable. * * @param viewable the viewable to be used. * @return selected view engine or {@code null} if none found. */ public ViewEngine find(Viewable viewable) { Optional<ViewEngine> engine; final String view = viewable.getView(); // If engine specified in viewable, use it final Class<? extends ViewEngine> engineClass = viewable.getViewEngine(); if (engineClass != null) { engine = Optional.of(cdiUtils.newBean(engineClass)); } else { // Check cache first engine = Optional.ofNullable(cache.get(view)); if (!engine.isPresent()) { List<ViewEngine> engines = CdiUtils.getApplicationBeans(ViewEngine.class); // Gather set of candidates final Set<ViewEngine> candidates = engines.stream() .filter(e -> e.supports(view)).collect(toSet()); // Find candidate with highest priority engine = candidates.stream().max( (e1, e2) -> { final Priority p1 = getAnnotation(e1.getClass(), Priority.class); final int v1 = p1 != null ? p1.value() : Priorities.DEFAULT; final Priority p2 = getAnnotation(e2.getClass(), Priority.class); final int v2 = p2 != null ? p2.value() : Priorities.DEFAULT; return v1 - v2; }); // Update cache if (engine.isPresent()) { cache.put(view, engine.get()); } } } return engine.isPresent() ? engine.get() : null; }
public Locale resolve(ContainerRequestContext requestContext) { // prepare context instance LocaleResolverContext context = new LocaleResolverContextImpl(configuration, requestContext); List<LocaleResolver> resolvers = CdiUtils.getApplicationBeans(LocaleResolver.class); // candidates as sorted list List<LocaleResolver> candidates = StreamSupport.stream(resolvers.spliterator(), false) .sorted((resolver1, resolver2) -> { final Priority prio1 = getAnnotation(resolver1.getClass(), Priority.class); final Priority prio2 = getAnnotation(resolver2.getClass(), Priority.class); final int value1 = prio1 != null ? prio1.value() : 1000; final int value2 = prio2 != null ? prio2.value() : 1000; return value2 - value1; }) .collect(Collectors.toList()); // do the resolving for (LocaleResolver candidate : candidates) { Locale locale = candidate.resolveLocale(context); if (locale != null) { return locale; } } throw new IllegalStateException("Could not resolve locale with any of the " + candidates.size() + " resolver implementations"); }
private int getPriority(Converter<?> converter) { int priority = 100; Priority priorityAnnotation = converter.getClass().getAnnotation(Priority.class); if (priorityAnnotation != null) { priority = priorityAnnotation.value(); } return priority; }
private Object returnPrimaryOrHighestPriorityBean(List<Bean> beans) { long highestPriority = Integer.MIN_VALUE; Object highestPrioBean = null; for (Bean bean : beans) { if (bean.isPrimary()) { return bean.getInstance(); } // TODO figure out to retrieve order from BeanDefinition / // BeanDeclaration Object instance = bean.getInstance(); Order order = instance.getClass().getAnnotation(Order.class); if (order != null) { if (order.value() > highestPriority) { highestPriority = order.value(); highestPrioBean = instance; } } Priority priority = instance.getClass().getAnnotation(Priority.class); if (priority != null) { if (priority.value() > highestPriority) { highestPriority = priority.value(); highestPrioBean = instance; } } } return highestPrioBean; }
@Override public int compareTo(Object o) { if (!(o instanceof Filter)) return 1; Priority p1 = this.getClass().getAnnotation(Priority.class); Priority p2 = o.getClass().getAnnotation(Priority.class); return (p2 == null ? 0 : p2.value()) - (p1 == null ? 0 : p1.value()); }
/** * Checks the given type optionally annotated with a @Priority. If present the annotation's value is evaluated. * If no such annotation is present, a default priority {@code 1} is returned. * * @param type the type, not {@code null}. * @return a priority, by default 1. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static int getPriority(Class type) { int prio = 1; Priority priority = (Priority)type.getAnnotation(Priority.class); if (priority != null) { prio = priority.value(); } return prio; }
/** * Checks the given instance for a @Priority annotation. If present the annotation's value is evaluated. If no such * annotation is present, a default priority of {@code 1} is returned. * @param o the instance, not {@code null}. * @return a priority, by default 1. */ public static int getPriority(Object o){ int prio = 1; //X TODO discuss default priority Priority priority = o.getClass().getAnnotation(Priority.class); if (priority != null) { prio = priority.value(); } return prio; }
/** * Compare 2 filters for ordering the filter chain. * * @param filter1 the first filter * @param filter2 the second filter * @return the comparison result */ private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) { Priority prio1 = filter1.getClass().getAnnotation(Priority.class); Priority prio2 = filter2.getClass().getAnnotation(Priority.class); int ord1 = prio1 != null ? prio1.value() : 0; int ord2 = prio2 != null ? prio2.value() : 0; if (ord1 < ord2) { return -1; } else if (ord1 > ord2) { return 1; } else { return filter1.getClass().getName().compareTo(filter2.getClass().getName()); } }
/** * Checks the given type optionally annotated with a @Priority. If present the annotation's value is evaluated. * If no such annotation is present, a default priority {@code 1} is returned. * * @param type the type, not {@code null}. * @return a priority, by default 1. */ public static int getPriority(Class<? extends Object> type) { int prio = 1; Priority priority = type.getAnnotation(Priority.class); if (priority != null) { prio = priority.value(); } return prio; }
public void receive(@ObservesAsync @Priority(1) AuditEvent auditEvent) { System.out.println("Priority: no (ObservesAsync) " + auditEvent.getPriority() + " " + auditEvent.getMessage()); }
public void receive(@Observes @Priority(5000) AuditEvent auditEvent) { System.out.println("Priority: 5000 " + auditEvent.getPriority() + " " + auditEvent.getMessage()); }
public void receive(@Observes @Priority(APPLICATION + 500) AuditEvent auditEvent) { System.out.println("Priority: " + (APPLICATION + 500) + " " + auditEvent.getPriority() + " " + auditEvent.getMessage()); }
public void receive(@Observes @Priority(100) AuditEvent auditEvent) { System.out.println("Priority: 100 " + auditEvent.getPriority() + " " + auditEvent.getMessage()); }
public void receive(@Observes @Priority(1000) AuditEvent auditEvent) { System.out.println("Priority: 1000 " + auditEvent.getPriority() + " " + auditEvent.getMessage()); }
private final void watch(@Observes @Initialized(ApplicationScoped.class) @Priority(LIBRARY_AFTER) final Object event, final KubernetesClient client, final BeanManager beanManager) throws InterruptedException { if (client != null && beanManager != null && this.startWatcher) { final javax.enterprise.event.Event<Event> eventBroadcaster = beanManager.getEvent().select(Event.class); assert eventBroadcaster != null; final javax.enterprise.event.Event<Event> addedBroadcaster = eventBroadcaster.select(Added.Literal.INSTANCE); assert addedBroadcaster != null; final javax.enterprise.event.Event<Event> modifiedBroadcaster = eventBroadcaster.select(Modified.Literal.INSTANCE); assert modifiedBroadcaster != null; final javax.enterprise.event.Event<Event> deletedBroadcaster = eventBroadcaster.select(Deleted.Literal.INSTANCE); assert deletedBroadcaster != null; final javax.enterprise.event.Event<Event> errorBroadcaster = eventBroadcaster.select(Error.Literal.INSTANCE); assert errorBroadcaster != null; // TODO: restrict namespace, filter events using strategy found // here: // http://stackoverflow.com/questions/32894599/how-do-i-get-events-associated-with-a-pod-via-the-api#comment53651235_32898853 this.watch = client.events().inAnyNamespace().watch(new Watcher<Event>() { @Override public final void eventReceived(final Action action, final Event resource) { javax.enterprise.event.Event<Event> specificBroadcaster = null; if (action != null) { switch (action) { case ADDED: specificBroadcaster = addedBroadcaster; break; case MODIFIED: specificBroadcaster = modifiedBroadcaster; break; case DELETED: specificBroadcaster = deletedBroadcaster; break; case ERROR: specificBroadcaster = errorBroadcaster; break; default: throw new IllegalStateException(); } } assert specificBroadcaster != null; specificBroadcaster.fire(resource); } @Override public final void onClose(final KubernetesClientException kubernetesClientException) { if (kubernetesClientException != null) { closeException = kubernetesClientException; unblock(); } } }); } }
public void onMessage(@Observes @Priority(value = 1) Message message) { LOG.log(Level.INFO, "observes event with @Priority(value = 1):{0}", message); }
public void onAnotherMessage(@Observes @Priority(value = 2) Message message) { LOG.log(Level.INFO, "observes event with @Priority(value = 2):{0}", message); }
private int getConverterPriority(Class<? extends Converter<?>> converter) { Priority priority = converter.getAnnotation(Priority.class); return priority == null ? 0 : priority.value(); }
/** * Returns the {@link Priority#value()} if available or 5000 if not. * * @param config * @return */ private static int getPriority(SecurityCustomiser config) { return getAnnotationValue(config, Priority.class, 5000); }