/** * Creates a common target over a value, with known scope data. */ public static Target ofValue(final Object value, final ScopeData scopeData) { return new Target(value, null, scopeData, null, VALUE_INSTANCE_CREATOR); } /**
public Targets(final Object actionOrTarget, final ScopeData scopeData) { targets = new Target[]{Target.ofValue(actionOrTarget, scopeData)}; }
/** * Reads value from the target. If something goes wrong, exception * is thrown. We assume that outjection is controlled by developer * and that each reading of a value must be a successful operation. */ public Object readValue(final InjectionPoint injectionPoint) { return readValue(injectionPoint.targetName()); } public Object readValue(final String name) {
/** * Writes value to this target. Depending on a flag, writing the value can be * completely silent, when no exception is thrown and with top performances. * Otherwise, an exception is thrown on a failure. */ public void writeValue(final InjectionPoint injectionPoint, final Object propertyValue, final boolean silent) { writeValue(injectionPoint.targetName(), propertyValue, silent); } public void writeValue(final String name, final Object propertyValue, final boolean silent) {
target[0] = actionTarget; final Object action = actionTarget.value(); paramTarget = Target.ofValue(createActionMethodArgument(paramType, action), newScopeData); paramTarget = Target.ofMethodParam(methodParam, createActionMethodArgument(paramType, action)); paramTarget = Target.ofMethodParam(methodParam, type -> createActionMethodArgument(type, action));
@Override public void inject(final Targets targets) { targets.forEachTarget(target -> { final Class targetType = target.resolveType(); final String baseName = baseNameOf(targetType); final ParamManager madvocPetiteParamManager = madpc.paramManager(); final String[] params = madvocPetiteParamManager.filterParametersForBeanName(baseName, true); for (final String param : params) { final Object value = madvocPetiteParamManager.get(param); final String propertyName = param.substring(baseName.length() + 1); target.writeValue(propertyName, value, false); } }); }
/** * Iterates all targets and for each target iterates all OUT injection points of given scope. */ public void forEachTargetAndOut(final MadvocScope scope, final BiConsumer<Target, InjectionPoint> biConsumer) { for (final Target target : targets) { final ScopeData scopeData = target.scopeData(); if (scopeData.out() == null) { continue; } for (final InjectionPoint out : scopeData.out()) { if (out.scope() != scope) { continue; } biConsumer.accept(target, out); } } }
/** * Collects all parameters from target into an array. */ public Object[] extractParametersValues() { final Object[] values = new Object[targets.length - 1]; for (int i = 1; i < targets.length; i++) { values[i - 1] = targets[i].value(); } return values; }
/** * Injects request attributes. */ protected void injectAttributes(final HttpServletRequest servletRequest, final Targets targets) { final Enumeration<String> attributeNames = servletRequest.getAttributeNames(); while (attributeNames.hasMoreElements()) { final String attrName = attributeNames.nextElement(); targets.forEachTargetAndIn(this, (target, in) -> { final String name = in.matchedName(attrName); if (name != null) { final Object attrValue = servletRequest.getAttribute(attrName); target.writeValue(name, attrValue, true); } }); } }
/** * Iterates all targets and for each target iterates all IN injection points of given scope. */ public void forEachTargetAndIn(final MadvocScope scope, final BiConsumer<Target, InjectionPoint> biConsumer) { for (final Target target : targets) { final ScopeData scopeData = target.scopeData(); if (scopeData.in() == null) { continue; } for (final InjectionPoint in : scopeData.in()) { if (in.scope() != scope) { continue; } biConsumer.accept(target, in); } } }
@Override public void inject(final ServletContext servletContext, final Targets targets) { instancesInjector.inject(servletContext, targets); final Enumeration<String> attributeNames = servletContext.getAttributeNames(); while (attributeNames.hasMoreElements()) { final String attrName = attributeNames.nextElement(); targets.forEachTargetAndIn(this, (target, in) -> { final String name = in.matchedName(attrName); if (name != null) { final Object attrValue = servletContext.getAttribute(attrName); target.writeValue(name, attrValue, true); } }); } }
@Override public void outject(final ActionRequest actionRequest, final Targets targets) { final HttpServletResponse servletResponse = actionRequest.getHttpServletResponse(); targets.forEachTargetAndOut(this, (target, out) -> { final Cookie cookie = (Cookie) target.readValue(out); if (cookie != null) { servletResponse.addCookie(cookie); } }); } }
/** * Creates a common target over a method param. */ public static Target ofMethodParam(final MethodParam methodParam, final Object object) { return new Target(object, methodParam.type(), methodParam.scopeData(), methodParam.mapperFunction(), VALUE_INSTANCE_CREATOR); } /**
public Targets(final ActionRequest actionRequest) { targets = makeTargets( Target.ofValue(actionRequest.getAction(), actionRequest.getActionRuntime().getScopeData()), actionRequest.getActionRuntime().getMethodParams()); }
public void inject(final ServletContext servletContext, final Targets targets) { targets.forEachTargetAndIn(madvocScope, (target, in) -> { final Class inType = in.type(); Object value = null; if (inType == ServletContext.class) { value = servletContext; } if (value != null) { target.writeValue(in, value, true); } }); }
@Override public void outject(final ActionRequest actionRequest, final Targets targets) { final ServletContext context = actionRequest.getHttpServletRequest().getServletContext(); targets.forEachTargetAndOut(this, (target, out) -> { final Object value = target.readValue(out); context.setAttribute(out.name(), value); }); }
/** * Creates a common target over a method param. */ public static Target ofMethodParam(final MethodParam methodParam, final Function<Class, Object> valueInstanceCreator) { return new Target(null, methodParam.type(), methodParam.scopeData(), methodParam.mapperFunction(), valueInstanceCreator); }
@Override public void inject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); targets.forEachTargetAndIn(this, (target, in) -> { final Enumeration<String> headerNames = servletRequest.getHeaders(in.name()); if (headerNames != null) { final List<String> allValues = new ArrayList<>(); while (headerNames.hasMoreElements()) { allValues.add(headerNames.nextElement()); } final Object value; if (allValues.size() == 1) { value = allValues.get(0); } else { value = allValues; } target.writeValue(in, value, true); } }); }
@Override public void outject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); final HttpSession session = servletRequest.getSession(); targets.forEachTargetAndOut(this, (target, out) -> { final Object value = target.readValue(out); session.setAttribute(out.name(), value); }); } }
@Override public void inject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); final HttpSession session = servletRequest.getSession(); final Enumeration<String> attributeNames = session.getAttributeNames(); while (attributeNames.hasMoreElements()) { final String attrName = attributeNames.nextElement(); targets.forEachTargetAndIn(this, (target, in) -> { final String name = in.matchedName(attrName); if (name != null) { final Object attrValue = session.getAttribute(attrName); target.writeValue(name, attrValue, true); } }); } }