Skip to content

Instantly share code, notes, and snippets.

Avatar
🙃

Laird Nelson ljnelson

🙃
View GitHub Profile
@ljnelson
ljnelson / Main.java
Created Nov 15, 2016
Start and run a CDI 2.0 container in a vendor-independent fashion
View Main.java
/*
* Copyright 2016 Laird Nelson.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
View TerseCdiMain.java
/*
* Copyright 2016 Laird Nelson. Released under the terms of the MIT license: https://opensource.org/licenses/MIT
*/
package com.foobar;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
public class Main {
public static final void main(final String[] args) throws Exception {
View SimplePortableExtension.java
package com.serverco;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
public class SimplePortableExtension implements Extension {
View GrizzlyHttpServerFactoryExample.java
java.net.URI baseUri = javax.ws.rs.core.UriBuilder.fromUri("http://localhost/").port(9998).build();
org.glassfish.jersey.server.ResourceConfig config = new org.glassfish.jersey.server.ResourceConfig(developer.HelloWorldResource.class);
org.glassfish.grizzly.http.server.HttpServer server = org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(baseUri, config);
// and so on and so on
View HelloWorldApplication.java
/*
* Copyright 2016 Laird Nelson. Released under the terms of the MIT license: https://opensource.org/licenses/MIT
*/
package developer;
import java.util.Collections;
import java.util.Set;
import javax.ws.rs.core.Application;
View ProducersIterationOne.java
/*
* Copyright 2016 Laird Nelson. Released under the terms of the MIT license: https://opensource.org/licenses/MIT
*/
package serverco;
import java.net.URI;
import java.net.URISyntaxException;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
View Config.Key.java
public class Config {
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@interface Key {
String value();
}
}
View Property.java
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
private @interface Property {
@Nonbinding
String value() default "";
}
View PropertyProducerMethodRound1.java
@Produces
@Dependent // not really needed; I like to be explicit
@Property
private final Object produceProperty(final InjectionPoint ip, final BigCoConfigurationEngine config) {
// Get the right configuration value identified by the injection point
// using the BigCoConfigurationEngine
throw new UnsupportedOperationException("Not done yet");
}
View ProcessInjectionPointExample.java
private final void processInjectionPoint(@Observes final ProcessInjectionPoint<?, ?> event) {
if (event != null) {
final InjectionPoint ip = event.getInjectionPoint();
assert ip != null;
final Set<Annotation> existingQualifiers = ip.getQualifiers();
if (existingQualifiers != null && !existingQualifiers.isEmpty()) {
final Set<Annotation> newQualifiers = new HashSet<>();
existingQualifiers.stream().forEach((qualifier) -> {
assert qualifier != null;
if (qualifier instanceof Key) {