Search in sources :

Example 21 with RenderResult

use of cn.bran.japid.template.RenderResult in project japid42 by branaway.

the class RenderInvokerUtils method invokeNamedArgsRender.

public static <T extends JapidTemplateBaseWithoutPlay> RenderResult invokeNamedArgsRender(Class<T> c, NamedArgRuntime[] args) {
    int modifiers = c.getModifiers();
    if (Modifier.isAbstract(modifiers)) {
        throw new RuntimeException("Cannot init the template class since it's an abstract class: " + c.getName());
    }
    try {
        // String methodName = "render";
        Constructor<T> ctor = c.getConstructor(StringBuilder.class);
        StringBuilder sb = new StringBuilder(8000);
        JapidTemplateBaseWithoutPlay t = ctor.newInstance(sb);
        RenderResult rr = renderWithNamedArgs(t, args);
        JapidFlags.logTimeLogs(t);
        return rr;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Could not match the arguments with the template args.");
    } catch (InstantiationException e) {
        // e.printStackTrace();
        throw new RuntimeException("Could not instantiate the template object. Abstract?");
    } catch (InvocationTargetException e) {
        // e.printStackTrace();
        Throwable e1 = e.getTargetException();
        throw new RuntimeException("Could not invoke the template object:  ", e1);
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException("Could not invoke the template object: ", e);
    // throw new RuntimeException(e);
    }
}
Also used : JapidTemplateBaseWithoutPlay(cn.bran.japid.template.JapidTemplateBaseWithoutPlay) RenderResult(cn.bran.japid.template.RenderResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with RenderResult

use of cn.bran.japid.template.RenderResult in project japid42 by branaway.

the class RenderInvokerUtils method invokeRender.

/**
	 * @param <T>
	 * @param c
	 * @param args
	 * @return
	 * @throws NoSuchMethodException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
public static <T extends JapidTemplateBaseWithoutPlay> RenderResult invokeRender(Class<T> c, Object... args) {
    int modifiers = c.getModifiers();
    if (Modifier.isAbstract(modifiers)) {
        throw new RuntimeException("Cannot init the template class since it's an abstract class: " + c.getName());
    }
    try {
        // String methodName = "render";
        Constructor<T> ctor = c.getConstructor(StringBuilder.class);
        RenderResult rr = invokeRenderer(ctor, args);
        // methodName, args);
        return rr;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Could not match the arguments with the template args.");
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException("Could not invoke the template object: ", e);
    }
}
Also used : RenderResult(cn.bran.japid.template.RenderResult) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with RenderResult

use of cn.bran.japid.template.RenderResult in project japid42 by branaway.

the class RenderInvokerUtils method render.

public static <T extends JapidTemplateBaseWithoutPlay> RenderResult render(T t, Object... args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    if (args == null) {
        // treat it as a single null argument
        args = new Object[] { null };
    }
    Method m = t.renderMethodInstance;
    if (m == null) {
        throw new RuntimeException("The render method cache is not initialized for: " + t.getClass().getName());
    }
    Object invoke;
    try {
        invoke = m.invoke(t, args);
        return (RenderResult) invoke;
    } catch (IllegalArgumentException e) {
        String msg = e.getMessage();
        String paramTypes = flatArgs(m.getParameterTypes());
        msg = msg + ": the arguments do not match the parameters of the template " + t.getClass().getName() + paramTypes;
        String ss = flatArgs(args);
        throw new RuntimeException(msg + ": " + ss);
    // System.err.println(e + ": " + m + args);
    // throw e;
    }
// } catch (IllegalArgumentException e) {
// throw new RuntimeException("Template argument type mismatch: ", e);
// } catch (InvocationTargetException e) {
// Throwable te = e.getTargetException();
// Throwable cause = te.getCause();
// if (cause != null)
// throw new RuntimeException("error in running the renderer: ", cause);
// else
// throw new RuntimeException("error in running the renderer: ", te);
// // te.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// throw new RuntimeException(e);
// }
}
Also used : RenderResult(cn.bran.japid.template.RenderResult) Method(java.lang.reflect.Method)

Example 24 with RenderResult

use of cn.bran.japid.template.RenderResult in project Japid by branaway.

the class RenderResultCacheTest method test11sconds.

@Test
public void test11sconds() {
    RenderResultCache.setAltCache(new AltCacheSimpleImpl());
    RenderResult rr = new RenderResult(null, null, 0);
    RenderResultCache.set(KEY1, rr, "11s");
    RenderResult rrr;
    try {
        rrr = RenderResultCache.get(KEY1);
        assertNotNull(rrr);
    } catch (ShouldRefreshException e1) {
        throw new RuntimeException(e1);
    }
    System.out.println("let wait for 9 secs");
    waitfor(9000);
    try {
        rrr = RenderResultCache.get(KEY1);
        assertNotNull(rrr);
    } catch (ShouldRefreshException e) {
        fail("should be safe");
    }
    waitfor(1000);
    // the second time in refreshing zone should get the item
    try {
        rrr = RenderResultCache.get(KEY1);
        fail("should alert expiration");
    } catch (ShouldRefreshException e) {
        try {
            rrr = RenderResultCache.get(KEY1);
            assertNotNull(rrr);
        } catch (ShouldRefreshException e1) {
            fail("should alert expiration ONCE");
        }
    }
}
Also used : RenderResult(cn.bran.japid.template.RenderResult) Test(org.junit.Test)

Example 25 with RenderResult

use of cn.bran.japid.template.RenderResult in project Japid by branaway.

the class RenderResultCacheTest method testReadThruWithThread.

@Test
public void testReadThruWithThread() throws ShouldRefreshException {
    assertFalse(RenderResultCache.shouldIgnoreCache());
    RenderResultCache.setAltCache(new AltCacheSimpleImpl());
    RenderResult rr = new RenderResult(null, null, 0);
    RenderResultCache.set(KEY1, rr, "4s");
    RenderResultCache.setIgnoreCache(true);
    RenderResult rrr = RenderResultCache.get(KEY1);
    assertNull(rrr);
    final AtomicBoolean b = new AtomicBoolean(false);
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                RenderResult rrr = RenderResultCache.get(KEY1);
                b.set(rrr != null);
            } catch (ShouldRefreshException e) {
                fail();
            }
        }
    });
    t.start();
    waitfor(100);
    assertTrue(b.get());
    RenderResultCache.setIgnoreCache(false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RenderResult(cn.bran.japid.template.RenderResult) Test(org.junit.Test)

Aggregations

RenderResult (cn.bran.japid.template.RenderResult)28 Test (org.junit.Test)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 InternetAddress (javax.mail.internet.InternetAddress)3 Email (org.apache.commons.mail.Email)3 EmailAttachment (org.apache.commons.mail.EmailAttachment)3 EmailException (org.apache.commons.mail.EmailException)3 HtmlEmail (org.apache.commons.mail.HtmlEmail)3 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)3 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)2 JapidTemplateException (cn.bran.japid.exceptions.JapidTemplateException)2 JapidTemplateBaseWithoutPlay (cn.bran.japid.template.JapidTemplateBaseWithoutPlay)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 MailException (play.exceptions.MailException)2 UnexpectedException (play.exceptions.UnexpectedException)2