Skip to content

Commit

Permalink
CAUSEWAY-3641: simplified handling of the MetaModelContext singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Nov 6, 2023
1 parent 9ea799a commit ba64a7f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@
public final class MetaModelContext_forTesting
implements MetaModelContext {

public static MetaModelContext_forTestingBuilder builder() {
return new MetaModelContext_forTestingBuilder() {
@Override
public MetaModelContext_forTesting build() {
var mmc = super.build();
MetaModelContext.INSTANCE_HOLDER.set(mmc);
return mmc;
}
};
}

public static MetaModelContext buildDefault() {
return MetaModelContext_forTesting.builder()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
package org.apache.causeway.core.metamodel.context;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import org.springframework.lang.Nullable;

import org.apache.causeway.commons.internal.exceptions._Exceptions;
import org.apache.causeway.core.metamodel.object.ManagedObject;

/**
Expand All @@ -30,9 +36,25 @@ default MetaModelContext getMetaModelContext() {
return this;
}

// -- INSTANCE (SINGLETON)

static final AtomicReference<MetaModelContext> INSTANCE_HOLDER = new AtomicReference<>();
@Nullable
static MetaModelContext instanceNullable() {
return INSTANCE_HOLDER.get();
}
static Optional<MetaModelContext> instance() {
return Optional.ofNullable(instanceNullable());
}
static MetaModelContext instanceElseFail() {
return instance()
.orElseThrow(()->_Exceptions.noSuchElement("MetaModelContext not yet or no longer available."));
}

// -- EXTRACTORS

public static MetaModelContext from(final ManagedObject adapter) {
@Deprecated
static MetaModelContext from(final ManagedObject adapter) {
return adapter.getSpecification().getMetaModelContext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public MetaModelContext_usingSpring(final _IocContainer iocContainer) {
this.iocContainer = iocContainer;
}

/**
* Called by Spring as instructed by bean declaration
* {@link MetaModelContexts#metaModelContext(CausewaySystemEnvironment)}.
*/
void onDestroy() {
MetaModelContext.INSTANCE_HOLDER.set(null);
}

@Getter(lazy=true)
private final CausewaySystemEnvironment systemEnvironment =
getSingletonElseFail(CausewaySystemEnvironment.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.causeway.core.metamodel.context;

import javax.inject.Named;
import javax.inject.Singleton;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -37,9 +36,12 @@
@Named(CausewayModuleCoreMetamodel.NAMESPACE + ".MetaModelContexts")
public class MetaModelContexts {

@Bean @Singleton @Primary
@Primary
@Bean(destroyMethod = "onDestroy")
public MetaModelContext metaModelContext(final CausewaySystemEnvironment systemEnvironment) {
return new MetaModelContext_usingSpring(systemEnvironment.getIocContainer());
var mmc = new MetaModelContext_usingSpring(systemEnvironment.getIocContainer());
MetaModelContext.INSTANCE_HOLDER.set(mmc);
return mmc;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public HintStore getHintStore() {
private <X> X computeIfAbsent(final Class<X> type, final X existingIfAny) {
return existingIfAny!=null
? existingIfAny
: WktContext.getMetaModelContext().lookupServiceElseFail(type);
: MetaModelContext.instanceElseFail().lookupServiceElseFail(type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@
*/
package org.apache.causeway.viewer.wicket.model.util;

import java.util.Optional;

import org.apache.wicket.Application;
import org.apache.wicket.ThreadContext;
import org.apache.wicket.core.request.handler.ListenerRequestHandler;
import org.apache.wicket.request.cycle.RequestCycle;
import org.springframework.lang.Nullable;

import org.apache.causeway.commons.internal.base._Casts;
import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.core.metamodel.context.HasMetaModelContext;
import org.apache.causeway.core.metamodel.context.MetaModelContext;

import lombok.NonNull;
import lombok.experimental.UtilityClass;

/**
Expand All @@ -40,45 +32,11 @@
@UtilityClass
public class WktContext {

@Nullable
public String getApplicationKey() {
final Application application = ThreadContext.getApplication(); // nullable
return Optional.ofNullable(application)
.map(Application::getName)
.orElse(null);
}

@Nullable
public MetaModelContext getMetaModelContext() {
final Application application = ThreadContext.getApplication(); // nullable
return _Casts.castTo(HasMetaModelContext.class, application)
.map(HasMetaModelContext::getMetaModelContext)
.orElse(null);
}

@Nullable
public MetaModelContext getMetaModelContext(final @NonNull String applicationKey) {
final Application application = Application.get(applicationKey); // nullable
return _Casts.castTo(HasMetaModelContext.class, application)
.map(HasMetaModelContext::getMetaModelContext)
.orElse(null);
}

@Nullable
public MetaModelContext computeIfAbsent(final MetaModelContext mmc) {
return mmc!=null
? mmc
: getMetaModelContext();
}

@Nullable
public MetaModelContext computeIfAbsent(
final @Nullable MetaModelContext mmc, final @Nullable String applicationKey) {
return mmc!=null
? mmc
: _Strings.isEmpty(applicationKey)
? getMetaModelContext()
: getMetaModelContext(applicationKey);
: MetaModelContext.instanceNullable();
}

public void pageReload() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.causeway.viewer.wicket.ui.util;

import java.time.Duration;
import java.util.Optional;

import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;
Expand All @@ -30,7 +29,6 @@
import org.apache.causeway.core.metamodel.context.MetaModelContext;
import org.apache.causeway.viewer.commons.model.decorators.TooltipDecorator.TooltipDecorationModel;
import org.apache.causeway.viewer.commons.model.layout.UiPlacementDirection;
import org.apache.causeway.viewer.wicket.model.util.WktContext;
import org.apache.causeway.viewer.wicket.ui.components.widgets.linkandlabel.ActionLink;
import org.apache.causeway.viewer.wicket.ui.util.ExtendedPopoverConfig.PopoverBoundary;

Expand Down Expand Up @@ -205,7 +203,7 @@ private PopoverConfig createPopoverConfigDefault() {
* Lookup in config, else returns default.
*/
private static TextMode getTooltipTextMode() {
val textMode = Optional.ofNullable(WktContext.getMetaModelContext())
val textMode = MetaModelContext.instance()
.map(MetaModelContext::getConfiguration)
.map(cfg->cfg.getViewer().getWicket().getTooltipTextMode())
.orElseGet(TextMode::defaults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static AuthenticatedWebSessionForCauseway get() {
@Nullable
@Override
public MetaModelContext getMetaModelContext() {
return mmc = WktContext.computeIfAbsent(mmc, applicationKey);
return mmc = WktContext.computeIfAbsent(mmc);
}

/**
Expand Down Expand Up @@ -162,7 +162,6 @@ public void setPrimedInteractionContext(final @NonNull InteractionContext authen
@Getter
private UUID sessionGuid;
private String cachedSessionId;
private @Nullable String applicationKey; // nullable ... JUnit support

/**
* Optionally the current HttpSession's Id,
Expand All @@ -181,7 +180,6 @@ public Optional<String> getCachedSessionId() {
public AuthenticatedWebSessionForCauseway(final Request request) {
super(request);
sessionGuid = UUID.randomUUID();
applicationKey = WktContext.getApplicationKey();
}

@Override
Expand Down

0 comments on commit ba64a7f

Please sign in to comment.