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);
}
}
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);
}
}
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);
// }
}
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");
}
}
}
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);
}
Aggregations