Skip to content

Instantly share code, notes, and snippets.

View ljnelson's full-sized avatar
🙃

Laird Nelson ljnelson

🙃
View GitHub Profile
@ljnelson
ljnelson / Main.java
Created November 15, 2016 22:37
Start and run a CDI 2.0 container in a vendor-independent fashion
/*
* 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:
/*
* 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 {
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 {
/*
* 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;
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
/*
* Copyright © 2016–2017 Laird Nelson. Released under the terms of the MIT license: https://opensource.org/licenses/MIT
*/
package com.serverco;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
/*
* 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;
public class Config {
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@interface Key {
String value();
}
}
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
private @interface Property {
@Nonbinding
String value() default "";
}
@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");
}