Search in sources :

Example 1 with Targets

use of jodd.madvoc.config.Targets in project jodd by oblac.

the class ContextInjectorComponent method injectContext.

/**
 * Inject context into target.
 */
public void injectContext(final Object targetObject) {
    final Class targetType = targetObject.getClass();
    final ScopeData scopeData = scopeDataInspector.inspectClassScopesWithCache(targetType);
    final Targets targets = new Targets(targetObject, scopeData);
    // inject no context
    scopeResolver.forEachScope(madvocScope -> madvocScope.inject(targets));
    // inject special case
    scopeResolver.forScope(ParamsScope.class, scope -> scope.inject(targets));
    // inject servlet context
    final ServletContext servletContext = madvocController.getApplicationContext();
    if (servletContext != null) {
        scopeResolver.forEachScope(madvocScope -> madvocScope.inject(servletContext, targets));
    }
}
Also used : ScopeData(jodd.madvoc.config.ScopeData) ServletContext(javax.servlet.ServletContext) Targets(jodd.madvoc.config.Targets)

Example 2 with Targets

use of jodd.madvoc.config.Targets in project jodd by oblac.

the class ServletConfigInterceptor method inject.

/**
 * Performs injection.
 */
protected void inject(final ActionRequest actionRequest) {
    final Targets targets = actionRequest.getTargets();
    final ServletContext servletContext = actionRequest.getHttpServletRequest().getServletContext();
    scopeResolver.forEachScope(madvocScope -> madvocScope.inject(servletContext, targets));
    scopeResolver.forEachScope(madvocScope -> madvocScope.inject(actionRequest, targets));
    // insert all default values
    targets.forEachTargetAndIn((target, injectionPoint) -> {
        final String defaultValue = injectionPoint.defaultValue();
        if (!defaultValue.isEmpty()) {
            final Object value = target.readValue(injectionPoint);
            if (value == null) {
                target.writeValue(injectionPoint, defaultValue, true);
            }
        }
    });
}
Also used : ServletContext(javax.servlet.ServletContext) Targets(jodd.madvoc.config.Targets)

Example 3 with Targets

use of jodd.madvoc.config.Targets in project jodd by oblac.

the class ServletConfigInterceptor method outject.

/**
 * Performs outjection.
 */
protected void outject(final ActionRequest actionRequest) {
    final Targets targets = actionRequest.getTargets();
    scopeResolver.forEachScope(madvocScope -> madvocScope.outject(actionRequest, targets));
}
Also used : Targets(jodd.madvoc.config.Targets)

Example 4 with Targets

use of jodd.madvoc.config.Targets in project jodd by oblac.

the class MadvocParamsInjectorTest method testInjection.

@Test
void testInjection() {
    WebApp webapp = new WebApp();
    webapp.start();
    PetiteContainer madpc = webapp.madvocContainer().getPetiteContainer();
    String baseName = StringUtil.uncapitalize(FooBean.class.getSimpleName());
    madpc.defineParameter("foo", "1");
    madpc.defineParameter(baseName + ".integer", "173");
    madpc.defineParameter(baseName + ".string", "jodd");
    madpc.defineParameter(baseName, "huh");
    ParamsScope paramsScope = new ParamsScope();
    BeanUtil.declared.setProperty(paramsScope, "madpc", madpc);
    FooBean fooBean = new FooBean();
    paramsScope.inject(new Targets(fooBean, null));
    assertEquals(173, fooBean.getInteger().intValue());
    assertEquals("jodd", fooBean.getString());
}
Also used : ParamsScope(jodd.madvoc.scope.ParamsScope) Targets(jodd.madvoc.config.Targets) PetiteContainer(jodd.petite.PetiteContainer) WebApp(jodd.madvoc.WebApp) Test(org.junit.jupiter.api.Test)

Aggregations

Targets (jodd.madvoc.config.Targets)4 ServletContext (javax.servlet.ServletContext)2 WebApp (jodd.madvoc.WebApp)1 ScopeData (jodd.madvoc.config.ScopeData)1 ParamsScope (jodd.madvoc.scope.ParamsScope)1 PetiteContainer (jodd.petite.PetiteContainer)1 Test (org.junit.jupiter.api.Test)1