Search in sources :

Example 1 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class ScopeDataResolverTest method testGenericAction.

@Test
public void testGenericAction() {
    ScopeDataResolver scopeDataResolver = new ScopeDataResolver();
    ScopeData[] scopeData = scopeDataResolver.resolveScopeData(GenAction.class);
    ScopeData.In[] in1 = scopeData[ScopeType.REQUEST.value()].in;
    ScopeData.Out[] out1 = scopeData[ScopeType.REQUEST.value()].out;
    ScopeData.In in = in1[0];
    ScopeData.Out out = out1[0];
    assertEquals("input", in.name);
    assertEquals(String.class, in.type);
    assertEquals("output", out.name);
    assertEquals(Integer.class, out.type);
}
Also used : ScopeDataResolver(jodd.madvoc.component.ScopeDataResolver) In(jodd.madvoc.meta.In) ScopeData(jodd.madvoc.ScopeData) Out(jodd.madvoc.meta.Out) Test(org.junit.Test)

Example 2 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class ScopeDataResolver method resolveScopeData.

/**
	 * Resolve scope data in given type for all scope types.
	 * Returns <code>null</code> if no scope data exist.
	 */
public ScopeData[] resolveScopeData(Class type) {
    final ScopeType[] allScopeTypes = ScopeType.values();
    ScopeData[] scopeData = new ScopeData[allScopeTypes.length];
    int count = 0;
    for (ScopeType scopeType : allScopeTypes) {
        ScopeData sd = inspectClassScopeData(type, scopeType);
        if (sd != null) {
            count++;
        }
        scopeData[scopeType.value()] = sd;
    }
    if (count == 0) {
        return null;
    }
    return scopeData;
}
Also used : ScopeType(jodd.madvoc.ScopeType) ScopeData(jodd.madvoc.ScopeData)

Example 3 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class ScopeDataResolver method inspectMethodParameterScopeData.

// ---------------------------------------------------------------- inspect method
/**
	 * Inspects all method parameters for scope type.
	 */
protected ScopeData inspectMethodParameterScopeData(String name, Class type, Annotation[] annotations, ScopeType scopeType) {
    ScopeData sd = new ScopeData();
    int count = 0;
    for (Annotation annotation : annotations) {
        if (annotation instanceof In) {
            ScopeData.In scopeDataIn = inspectIn((In) annotation, scopeType, name, type);
            if (scopeDataIn != null) {
                count++;
                sd.in = new ScopeData.In[] { scopeDataIn };
            }
        } else if (annotation instanceof Out) {
            ScopeData.Out scopeDataOut = inspectOut((Out) annotation, scopeType, name, type);
            if (scopeDataOut != null) {
                count++;
                sd.out = new ScopeData.Out[] { scopeDataOut };
            }
        } else if (annotation instanceof InOut) {
            ScopeData.In scopeDataIn = inspectIn((InOut) annotation, scopeType, name, type);
            if (scopeDataIn != null) {
                count++;
                sd.in = new ScopeData.In[] { scopeDataIn };
            }
            ScopeData.Out scopeDataOut = inspectOut((InOut) annotation, scopeType, name, type);
            if (scopeDataOut != null) {
                count++;
                sd.out = new ScopeData.Out[] { scopeDataOut };
            }
        }
    }
    if (count == 0) {
        return null;
    }
    return sd;
}
Also used : In(jodd.madvoc.meta.In) ScopeData(jodd.madvoc.ScopeData) Annotation(java.lang.annotation.Annotation) InOut(jodd.madvoc.meta.InOut) Out(jodd.madvoc.meta.Out) InOut(jodd.madvoc.meta.InOut)

Example 4 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class ScopeDataResolver method inspectClassScopeData.

/**
	 * Inspect action for all In/Out annotations.
	 * Returns <code>null</code> if there are no In and Out data.
	 */
protected ScopeData inspectClassScopeData(Class actionClass, ScopeType scopeType) {
    ClassDescriptor cd = ClassIntrospector.lookup(actionClass);
    PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
    List<ScopeData.In> listIn = new ArrayList<>(allProperties.length);
    List<ScopeData.Out> listOut = new ArrayList<>(allProperties.length);
    for (PropertyDescriptor pd : allProperties) {
        // collect annotations
        In in = null;
        if (pd.getFieldDescriptor() != null) {
            in = pd.getFieldDescriptor().getField().getAnnotation(In.class);
        }
        if (in == null && pd.getWriteMethodDescriptor() != null) {
            in = pd.getWriteMethodDescriptor().getMethod().getAnnotation(In.class);
        }
        if (in == null && pd.getReadMethodDescriptor() != null) {
            in = pd.getReadMethodDescriptor().getMethod().getAnnotation(In.class);
        }
        InOut inout = null;
        if (pd.getFieldDescriptor() != null) {
            inout = pd.getFieldDescriptor().getField().getAnnotation(InOut.class);
        }
        if (inout == null && pd.getWriteMethodDescriptor() != null) {
            inout = pd.getWriteMethodDescriptor().getMethod().getAnnotation(InOut.class);
        }
        if (inout == null && pd.getReadMethodDescriptor() != null) {
            inout = pd.getReadMethodDescriptor().getMethod().getAnnotation(InOut.class);
        }
        Out out = null;
        if (pd.getFieldDescriptor() != null) {
            out = pd.getFieldDescriptor().getField().getAnnotation(Out.class);
        }
        if (out == null && pd.getWriteMethodDescriptor() != null) {
            out = pd.getWriteMethodDescriptor().getMethod().getAnnotation(Out.class);
        }
        if (out == null && pd.getReadMethodDescriptor() != null) {
            out = pd.getReadMethodDescriptor().getMethod().getAnnotation(Out.class);
        }
        if (inout != null) {
            if (in != null || out != null) {
                throw new MadvocException("@InOut can not be used with @In or @Out: " + pd.getClassDescriptor().getClass() + '#' + pd.getName());
            }
        }
        // inspect all
        ScopeData.In ii = inspectIn(in, scopeType, pd.getName(), pd.getType());
        if (ii != null) {
            listIn.add(ii);
        }
        ii = inspectIn(inout, scopeType, pd.getName(), pd.getType());
        if (ii != null) {
            listIn.add(ii);
        }
        ScopeData.Out oi = inspectOut(out, scopeType, pd.getName(), pd.getType());
        if (oi != null) {
            listOut.add(oi);
        }
        oi = inspectOut(inout, scopeType, pd.getName(), pd.getType());
        if (oi != null) {
            listOut.add(oi);
        }
    }
    if ((listIn.isEmpty()) && (listOut.isEmpty())) {
        return null;
    }
    ScopeData scopeData = new ScopeData();
    if (!listIn.isEmpty()) {
        scopeData.in = listIn.toArray(new ScopeData.In[listIn.size()]);
    }
    if (!listOut.isEmpty()) {
        scopeData.out = listOut.toArray(new ScopeData.Out[listOut.size()]);
    }
    return scopeData;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) In(jodd.madvoc.meta.In) ArrayList(java.util.ArrayList) Out(jodd.madvoc.meta.Out) InOut(jodd.madvoc.meta.InOut) ScopeData(jodd.madvoc.ScopeData) InOut(jodd.madvoc.meta.InOut) MadvocException(jodd.madvoc.MadvocException)

Example 5 with ScopeData

use of jodd.madvoc.ScopeData in project jodd by oblac.

the class ApplicationScopeInjector method outject.

public void outject(ActionRequest actionRequest) {
    ScopeData[] outjectData = lookupScopeData(actionRequest);
    if (outjectData == null) {
        return;
    }
    Target[] targets = actionRequest.getTargets();
    ServletContext context = actionRequest.getHttpServletRequest().getSession().getServletContext();
    for (int i = 0; i < targets.length; i++) {
        Target target = targets[i];
        if (outjectData[i] == null) {
            continue;
        }
        ScopeData.Out[] scopes = outjectData[i].out;
        if (scopes == null) {
            continue;
        }
        for (ScopeData.Out out : scopes) {
            Object value = getTargetProperty(target, out);
            context.setAttribute(out.name, value);
        }
    }
}
Also used : ScopeData(jodd.madvoc.ScopeData) ServletContext(javax.servlet.ServletContext)

Aggregations

ScopeData (jodd.madvoc.ScopeData)21 Enumeration (java.util.Enumeration)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 ServletContext (javax.servlet.ServletContext)4 In (jodd.madvoc.meta.In)4 Out (jodd.madvoc.meta.Out)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 ScopeType (jodd.madvoc.ScopeType)2 ScopeDataResolver (jodd.madvoc.component.ScopeDataResolver)2 InOut (jodd.madvoc.meta.InOut)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Cookie (javax.servlet.http.Cookie)1 ClassDescriptor (jodd.introspector.ClassDescriptor)1 PropertyDescriptor (jodd.introspector.PropertyDescriptor)1 ActionConfig (jodd.madvoc.ActionConfig)1 ActionConfigSet (jodd.madvoc.ActionConfigSet)1