Skip to content

Commit

Permalink
web-fragment.xml ordering
Browse files Browse the repository at this point in the history
more Serializable
  • Loading branch information
pizzi80 committed Mar 1, 2022
1 parent 792a3d6 commit 408e4cf
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

public class SecurityContextImpl implements SecurityContext, Serializable {

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 4741091906552051471L;

private CallerDetailsResolver callerDetailsResolver;
private ResourceAccessResolver resourceAccessResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package org.glassfish.soteria;

import jakarta.security.enterprise.CallerPrincipal;

import java.io.Serializable;
import java.security.Principal;

import jakarta.security.enterprise.CallerPrincipal;
public class WrappingCallerPrincipal extends CallerPrincipal implements Serializable {

private static final long serialVersionUID = -3852163082966221424L;

public class WrappingCallerPrincipal extends CallerPrincipal {

private final Principal wrapped;

public WrappingCallerPrincipal(Principal wrapped) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package org.glassfish.soteria.authorization.spi;

import java.io.Serializable;
import java.security.Principal;
import java.util.Set;

public interface CallerDetailsResolver {
public interface CallerDetailsResolver extends Serializable {

Principal getCallerPrincipal();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import org.glassfish.soteria.authorization.JACC;
import org.glassfish.soteria.authorization.spi.ResourceAccessResolver;

public class JaccResourceAccessResolver implements ResourceAccessResolver {
import java.io.Serializable;

public class JaccResourceAccessResolver implements ResourceAccessResolver , Serializable {

private static final long serialVersionUID = -5710259122181927223L;

@Override
public boolean hasAccessToWebResource(String resource, String... methods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,29 @@

package org.glassfish.soteria.authorization.spi.impl;

import static java.lang.System.getProperty;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.list;
import jakarta.security.enterprise.CallerPrincipal;
import jakarta.security.jacc.PolicyContext;
import jakarta.security.jacc.PolicyContextException;
import jakarta.servlet.http.HttpServletRequest;
import org.glassfish.soteria.authorization.JACC;

import java.lang.reflect.InvocationHandler;
import javax.security.auth.Subject;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.security.Principal;
// import java.security.acl.Group;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.security.auth.Subject;

import org.glassfish.soteria.authorization.JACC;
import static java.lang.System.getProperty;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.list;

import jakarta.security.enterprise.CallerPrincipal;
import jakarta.security.jacc.PolicyContext;
import jakarta.security.jacc.PolicyContextException;
import jakarta.servlet.http.HttpServletRequest;
public class SubjectParser implements Serializable {

public class SubjectParser {
private static final long serialVersionUID = -7008577639483461696L;

private static Object geronimoPolicyConfigurationFactoryInstance;
private static ConcurrentMap<String, Map<Principal, Set<String>>> geronimoContextToRoleMapping;
Expand Down Expand Up @@ -116,15 +107,15 @@ public SubjectParser(String contextID, Collection<String> allDeclaredRoles) {
// Try to get a hold of the proprietary role mapper of each known
// AS. Sad that this is needed :(
if (tryGlassFish(contextID,allDeclaredRoles)) {

return;
} else if (tryJBoss()) {

return;
} else if (tryLiberty()) {

return;
} else if (tryWebLogic(contextID,allDeclaredRoles)) {

return;
} else if (tryGeronimo(contextID,allDeclaredRoles)) {

return;
} else {
oneToOneMapping = true;
}
Expand Down
23 changes: 7 additions & 16 deletions impl/src/main/java/org/glassfish/soteria/cdi/CdiDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,16 @@

package org.glassfish.soteria.cdi;

import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.function.BiFunction;

import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Annotated;
import jakarta.enterprise.inject.spi.AnnotatedField;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.Decorator;
import jakarta.enterprise.inject.spi.InjectionPoint;

// WIP - Builder for dynanic decorators. May not be needed and/or replaced by
// WIP - Builder for dynamic decorators. May not be needed and/or replaced by
// CDI 2.0 bean builder
// See http://weld.cdi-spec.org/news/2015/02/25/weld-300Alpha5/#_bean_builder_api
public class CdiDecorator<T> extends CdiProducer<T> implements Decorator<T> {
Expand Down Expand Up @@ -68,7 +60,7 @@ public Set<Type> getDecoratedTypes() {
}

public Set<Annotation> getDelegateQualifiers() {
return emptySet();
return Set.of();
}

public CdiDecorator<T> decorator(Class<T> decorator) {
Expand All @@ -79,7 +71,7 @@ public CdiDecorator<T> decorator(Class<T> decorator) {

public CdiDecorator<T> delegateAndDecoratedType(Type type) {
delegateType = type;
decoratedTypes = asSet(type);
decoratedTypes = Set.of(type);
return this;
}

Expand All @@ -90,7 +82,7 @@ public CdiProducer<T> create(BeanManager beanManager, BiFunction<CreationalConte
beanManager.createAnnotatedType(decorator).getFields().iterator().next(),
this);

injectionPoints = singleton(decoratorInjectionPoint);
injectionPoints = Set.of(decoratorInjectionPoint);

this.beanManager = beanManager;

Expand All @@ -100,7 +92,7 @@ public CdiProducer<T> create(BeanManager beanManager, BiFunction<CreationalConte

private static class DecoratorInjectionPoint implements InjectionPoint {

private final Set<Annotation> qualifiers = singleton(new DefaultAnnotationLiteral());
private final Set<Annotation> qualifiers = Set.of(new DefaultAnnotationLiteral());

private final Type type;
private final AnnotatedField<?> annotatedField;
Expand Down Expand Up @@ -136,7 +128,6 @@ public boolean isDelegate() {
return true;
}


public boolean isTransient() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.glassfish.soteria.cdi.CdiUtils.addAnnotatedTypes;
import static org.glassfish.soteria.cdi.CdiUtils.getAnnotation;

import java.beans.Beans;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -86,16 +87,14 @@ public void register(@Observes BeforeBeanDiscovery beforeBean, BeanManager beanM
);
}

public <T> void processBean(@Observes ProcessBean<T> eventIn, BeanManager beanManager) {

ProcessBean<T> event = eventIn; // JDK8 u60 workaround
public <T> void processBean(@Observes ProcessBean<T> event, BeanManager beanManager) {

Class<?> beanClass = event.getBean().getBeanClass();
Optional<EmbeddedIdentityStoreDefinition> optionalEmbeddedStore = getAnnotation(beanManager, event.getAnnotated(), EmbeddedIdentityStoreDefinition.class);
optionalEmbeddedStore.ifPresent(embeddedIdentityStoreDefinition -> {
logActivatedIdentityStore(EmbeddedIdentityStore.class, beanClass);

identityStoreBeans.add(new CdiProducer<IdentityStore>()
identityStoreBeans.add( new CdiProducer<IdentityStore>()
.scope(ApplicationScoped.class)
.beanClass(IdentityStore.class)
.types(Object.class, IdentityStore.class, EmbeddedIdentityStore.class)
Expand Down
29 changes: 11 additions & 18 deletions impl/src/main/java/org/glassfish/soteria/cdi/CdiProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@

package org.glassfish.soteria.cdi;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableSet;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.enterprise.inject.spi.PassivationCapable;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.enterprise.inject.spi.PassivationCapable;
import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;

// May be replaced by CDI 2.0 bean builder API when ready.
// See http://weld.cdi-spec.org/news/2015/02/25/weld-300Alpha5/#_bean_builder_api
Expand All @@ -40,8 +38,8 @@ public class CdiProducer<T> implements Bean<T>, PassivationCapable {
private String id = this.getClass().getName();
private String name;
private Class<?> beanClass = Object.class;
private Set<Type> types = singleton(Object.class);
private Set<Annotation> qualifiers = unmodifiableSet(asSet(new DefaultAnnotationLiteral(), new AnyAnnotationLiteral()));
private Set<Type> types = Set.of(Object.class);
private Set<Annotation> qualifiers = Set.of(new DefaultAnnotationLiteral(), new AnyAnnotationLiteral());
private Class<? extends Annotation> scope = Dependent.class;
private Function<CreationalContext<T>, T> create;

Expand Down Expand Up @@ -124,7 +122,7 @@ protected CdiProducer<T> beanClass(Class<?> beanClass) {
}

protected CdiProducer<T> types(Type... types) {
this.types = asSet(types);
this.types = Set.of(types);
return this;
}

Expand All @@ -135,7 +133,7 @@ protected CdiProducer<T> beanClassAndType(Class<?> beanClass) {
}

protected CdiProducer<T> qualifiers(Annotation... qualifiers) {
this.qualifiers = asSet(qualifiers);
this.qualifiers = Set.of(qualifiers);
return this;
}

Expand All @@ -150,9 +148,4 @@ protected CdiProducer<T> addToId(Object object) {
return this;
}

@SafeVarargs
protected static <T> Set<T> asSet(T... a) {
return new HashSet<>(asList(a));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,43 +52,15 @@ public class SamRegistrationInstaller implements ServletContainerInitializer, Se

private static final Logger logger = Logger.getLogger(SamRegistrationInstaller.class.getName());

// total 1 sec wait
private static final int RETRY = 4;
private static final int SLEEP_MILLIS = 250;

private static final void waitForCDI() {
for ( int i=1 ; i <= RETRY ; i++ ) {
try {
CDI.current();
} catch (IllegalStateException e) {

if( i == RETRY ) throw new IllegalStateException();

try {
Thread.sleep(SLEEP_MILLIS);
} catch (InterruptedException ex) {
//Thread.currentThread().interrupt();
// handle appropriately
}
}
}
}


// --- ServletContainerInitializer ----------------------------------------------------------------------------

@Override
public void onStartup(Set<Class<?>> classes, ServletContext context) {

if (classes!=null) classes.stream().map(String::valueOf).forEach(logger::info);

// Obtain a reference to the CdiExtension that was used to see if
// there's an enabled bean

try {
//CDI.current().getBeanManager(); //CdiUtils.getBeanManager();

waitForCDI();
CDI.current(); //CdiUtils.getBeanManager();

if (logger.isLoggable(INFO)) {
String version = getClass().getPackage().getImplementationVersion();
Expand Down Expand Up @@ -134,13 +106,13 @@ public void onStartup(Set<Class<?>> classes, ServletContext context) {
// --- ServletContextListener ----------------------------------------------------------------------------

@Override
public void contextInitialized(ServletContextEvent sce) {
public void contextInitialized(ServletContextEvent event) {
// noop
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
deregisterServerAuthModule(sce.getServletContext());
public void contextDestroyed(ServletContextEvent event) {
deregisterServerAuthModule(event.getServletContext());
}

}
11 changes: 9 additions & 2 deletions impl/src/main/resources/META-INF/web-fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-fragment_5_0.xsd"
version="5.0"
>

<!--
We absolutely need CDI
Servlet spec at the moment does not define ServletContainerInitializer ordering,
so we defined a web-fragment.xml to do that.
https://github.com/eclipse-ee4j/soteria/issues/315
https://eclipse.org/lists/servlet-dev/msg00170.html
-->
<name>soteria</name>

<distributable />

<!--
<ordering>
<after>
<others />
</after>
</ordering>
-->

</web-fragment>

0 comments on commit 408e4cf

Please sign in to comment.