Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions microscoped-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@
<version>${openejb.javaee.api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-jaxrs</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings("unchecked")
class Scope<Key> {
private final Instance<?> NOTHING = new Instance<>(null, null, null);
private final Instance<?> NOTHING = new Instance<Object>(null, null, null);

private final Map<Contextual<?>, Instance> instances = new ConcurrentHashMap<>();
private final Map<Contextual<?>, Instance> instances = new ConcurrentHashMap<Contextual<?>, Instance>();

private final Key key;

Expand All @@ -43,7 +44,12 @@ public Key getKey() {
* @return existing or newly created bean instance, never null
*/
public <T> T get(final Contextual<T> contextual, final CreationalContext<T> creationalContext) {
return (T) instances.computeIfAbsent(contextual, c -> new Instance<>(contextual, creationalContext)).get();
Instance<T> instance = instances.get(contextual);
if(instance == null) {
instances.put(contextual, new Instance<T>(contextual, creationalContext));
instance = instances.get(contextual);
}
return instance.get();
}

/**
Expand All @@ -53,15 +59,22 @@ public <T> T get(final Contextual<T> contextual, final CreationalContext<T> crea
* @return existing the bean instance or null
*/
public <T> T get(final Contextual<T> contextual) {
return (T) instances.getOrDefault(contextual, NOTHING).get();
Instance<T> instance = instances.get(contextual);
if(instance == null){
return (T) NOTHING.get();
} else {
return instance.get();
}
}

/**
* Destroy all the instances in this scope
*/
public void destroy() {
// TODO We really should ensure no more instances can be added during or after this
instances.values().stream().forEach(Instance::destroy);
for(Instance instance : instances.values()) {
instance.destroy();
}
instances.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class ScopeContext<Key> implements Context {

private final Map<Key, Scope> scopes = new ConcurrentHashMap<>();
private final Map<Key, Scope> scopes = new ConcurrentHashMap<Key, Scope>();

/**
* The use of an AtomicReference inside the ThreadLocal is a clever way to be able to
Expand All @@ -38,7 +38,7 @@ public class ScopeContext<Key> implements Context {
*
* Similar to if Map.Entry had a 'setValue(Object)' method
*/
private final ThreadLocal<AtomicReference<Scope<Key>>> active = ThreadLocal.withInitial(this::inactive);
private final ThreadLocal<AtomicReference<Scope<Key>>> active = new ThreadLocal<AtomicReference<Scope<Key>>>();

private final Class<? extends Annotation> scopeAnnotation;

Expand All @@ -55,7 +55,11 @@ public ScopeContext(final Class<? extends Annotation> scopeAnnotation) {
* @return returns the previous "center of the world" so it can be reassociated when this scope exits
*/
public Key enter(Key key) {
final Scope<Key> scope = scopes.computeIfAbsent(key, k -> new Scope(key));
Scope<Key> scope = scopes.get(key);
if(scope == null) {
scope = new Scope(key);
scopes.put(key, scope);
}
return scope().getAndSet(scope).getKey();
}

Expand Down Expand Up @@ -85,14 +89,16 @@ public void exit(Key previous) {
* @param key the key of the scope
*/
public void destroy(Key key) {
scopes.computeIfPresent(key, (k, scope) -> {
Scope scope = scopes.get(key);
if(scope != null) {
scope.destroy();
return null;
});
}
}

public void destroyAll() {
scopes.values().stream().forEach(Scope::destroy);
for(Scope scope : scopes.values()) {
scope.destroy();
}
scopes.clear();
}

Expand All @@ -103,10 +109,15 @@ public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContex

@Override
public <T> T get(Contextual<T> contextual) {
return scope().get().get(contextual);
AtomicReference<Scope<Key>> scope = scope();
Scope<Key> keyScope = scope.get();
return keyScope.get(contextual);
}

private AtomicReference<Scope<Key>> scope() {
if(active.get() == null) {
active.set(inactive());
}
return active.get();
}

Expand Down Expand Up @@ -138,6 +149,6 @@ public void destroy() {
};

private AtomicReference<Scope<Key>> inactive() {
return new AtomicReference<>(inactiveScope);
return new AtomicReference<Scope<Key>>(inactiveScope);
}
}
18 changes: 6 additions & 12 deletions microscoped-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,18 @@
<version>${openejb.javaee.api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-jaxrs</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;


public class DomainScopedExtension implements Extension {

public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {
Expand All @@ -33,7 +32,7 @@ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {

public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) {

abd.addContext(new ScopeContext<>(DomainScoped.class));
abd.addContext(new ScopeContext<DomainScoped>(DomainScoped.class));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.supertribe.domain;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class AppPath extends Application {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,31 @@
*/
package org.supertribe.domain;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.tomitribe.microscoped.core.ScopeContext;
import org.tomitribe.microscoped.domain.DomainScopedExtension;

import javax.enterprise.inject.spi.Extension;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
*
* This test requires some DNS hacking.
* Edit your /etc/hosts to contain the following entries:
*
* <pre>
* $ cat /etc/hosts | egrep 'orange|red'
* 127.0.0.1 orange
* 127.0.0.1 red
* </pre>
*
* Then comment out the @Ignore
* This test relies on 127.0.0.1 being different from localhost
*/
@RunWith(Arquillian.class)
public class DomainScopedTest extends Assert {
Expand All @@ -59,37 +52,40 @@ public static WebArchive createDeployment() {
.addPackage(ScopeContext.class.getPackage())
.addPackage(DomainScopedExtension.class.getPackage())
.addPackage(SimpleService.class.getPackage())
.addPackages(true, "org.apache.http")
.addAsWebInfResource(new ClassLoaderAsset("META-INF/beans.xml"), "classes/META-INF/beans.xml")
.addAsWebInfResource(new StringAsset(DomainScopedExtension.class.getName()),
"classes/META-INF/services/" + Extension.class.getName()
);
.addAsServiceProviderAndClasses(Extension.class, DomainScopedExtension.class);
}

@ArquillianResource
private URL webappUrl;

@Test
@Ignore("Comment this out to run the test")
public void test() throws Exception {
assertDomain("orange", 1);
assertDomain("orange", 2);
assertDomain("red", 1);
assertDomain("red", 2);
assertDomain("red", 3);
assertDomain("orange", 3);
assertDomain("orange", 4);
assertDomain("red", 4);
String localhost = "localhost";
String realHost = "127.0.0.1";
assertDomain(localhost, 1);
assertDomain(localhost, 2);
assertDomain(realHost, 1);
assertDomain(realHost, 2);
assertDomain(realHost, 3);
assertDomain(localhost, 3);
assertDomain(localhost, 4);
assertDomain(realHost, 4);
}

private void assertDomain(String orange, int i) throws URISyntaxException {
final URI uri = setDomain(orange, webappUrl.toURI());
final WebClient webClient = WebClient.create(uri);
private void assertDomain(String domain, int i) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = httpclient.execute(new HttpGet(createUri(domain)));
HttpEntity entity = response.getEntity();
String s = EntityUtils.toString(entity);

final String expected = String.format("%s domain, %s invocations", domain, i);

final String result = webClient.path("domain").get(String.class);
assertEquals(orange + " domain, " + i + " invocations", result);
assertEquals(expected, s);
}

private static URI setDomain(String domain, URI uri) throws URISyntaxException {
return new URI(uri.getScheme(), uri.getUserInfo(), domain, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
private URI createUri(String domain) throws Exception{
return new URL(webappUrl.getProtocol(), domain, webappUrl.getPort(), webappUrl.getFile()+"domain").toURI();
}
}
12 changes: 0 additions & 12 deletions microscoped-header/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@
<version>${openejb.javaee.api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-jaxrs</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {

public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) {

abd.addContext(new ScopeContext<>(HeaderScoped.class));
abd.addContext(new ScopeContext<HeaderScoped>(HeaderScoped.class));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class HeaderScopedFilter implements javax.servlet.Filter {
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {

final String value = getKey((HttpServletRequest) servletRequest);

final ScopeContext<String> context = (ScopeContext<String>) beanManager.getContext(HeaderScoped.class);

// set the name and value in the thread for others to see
Expand All @@ -63,7 +62,6 @@ public void doFilter(final ServletRequest servletRequest, final ServletResponse

private String getKey(final HttpServletRequest request) {
final String header = request.getHeader(config.getHeaderName());

if (header != null) {
return header;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.supertribe.header;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class AppPath extends Application{
}
Loading