use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class VelocityEngineFactory method getVelocityEngine.
/**
* <p>
* Returns the VelocityEngine associated with this factory. If this is the first time we are using the engine,
* create it and initialise it.</p>
*
* <p>
* Note that velocity engines are hugely resource intensive, so we don't want too many of them. For the time being
* we have a single instance stored as a static variable. This would only be a problem if the VelocityLayout class
* ever wanted to use different engine configurations (unlikely).</p>
*
* @return the VelocityEngine associated with this factory.
*/
public static synchronized VelocityEngine getVelocityEngine() {
if (engine == null) {
String fileTemplates = ConfigurationProperties.getVelocityFileTemplates();
boolean cacheTemplates = ConfigurationProperties.getVelocityCacheTemplates();
VelocityEngine newEngine = new VelocityEngine();
Properties props = new Properties();
// "source mode" or not
if (fileTemplates != null && !"".equals(fileTemplates)) {
// Source mode
LOG.info("Velocity engine running in source mode from " + fileTemplates);
props.setProperty("resource.loader", "file,class");
props.setProperty("file.resource.loader.path", fileTemplates);
props.setProperty("file.resource.loader.cache", "false");
props.setProperty("file.resource.loader.modificationCheckInterval", "2");
props.setProperty("class.resource.loader.cache", "false");
props.setProperty("class.resource.loader.modificationCheckInterval", "2");
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
} else {
String cache = String.valueOf(cacheTemplates);
props.setProperty("class.resource.loader.cache", cache);
props.setProperty("resource.loader", "class");
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
// Setup commons logging for velocity
props.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "com.github.bordertech.wcomponents.velocity.VelocityLogger");
// Set up access to the common velocity macros.
props.setProperty(RuntimeConstants.VM_LIBRARY, ConfigurationProperties.getVelocityMacroLibrary());
try {
if (LOG.isInfoEnabled()) {
// Dump properties
StringWriter writer = new StringWriter();
props.list(new PrintWriter(writer));
LOG.info("Configuring velocity with the following properties...\n" + writer);
}
newEngine.init(props);
} catch (Exception ex) {
throw new SystemException("Failed to configure VelocityEngine", ex);
}
engine = newEngine;
}
return engine;
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class WPartialDateFieldRenderer_Test method testXssEscaping.
@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
WPartialDateField dateField = new WPartialDateField();
dateField.setData(getMaliciousContent());
try {
assertSafeContent(dateField);
Assert.fail("Invalid date should not have been parsed.");
} catch (SystemException e) {
Assert.assertNotNull("Exception has no message", e.getMessage());
}
dateField.setData(null);
dateField.setToolTip(getMaliciousAttribute("ui:datefield"));
assertSafeContent(dateField);
dateField.setAccessibleText(getMaliciousAttribute("ui:datefield"));
assertSafeContent(dateField);
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class AbstractWComponentTestCase method invokeSetMethod.
/**
* @param component the component to invoke the setter method on
* @param methodName the name of the method
* @param value the value to pass into the setter method
* @param args if required the variable args
*/
private void invokeSetMethod(final WComponent component, final String methodName, final Object value, final Object[] args) {
try {
if (args == null) {
PropertyUtils.setProperty(component, methodName, value);
} else {
// Invoke specifying the variable arg type as propertyUtils cannot handle this
String setter = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Class[] argTypes = new Class[] { value.getClass(), args.getClass() };
Method method = component.getClass().getMethod(setter, argTypes);
method.invoke(component, value, args);
}
} catch (Exception e) {
throw new SystemException("Failed to set value on component for method " + methodName + " on " + component.getClass(), e);
}
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class Serialization_Test method pipe.
/**
* Take a copy of an input object via serialization.
*
* @param obj the object to copy
*
* @return the copy of the object
*/
private static Object pipe(final Object obj) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
byte[] bytes = bos.toByteArray();
FileOutputStream fos = new FileOutputStream("SerializeText.txt");
fos.write(bytes);
fos.flush();
fos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
Object out = ois.readObject();
return out;
} catch (Exception ex) {
throw new SystemException("Failed to pipe " + obj, ex);
}
}
use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.
the class AbstractContainerHelper_Test method testHandleErrorDefault.
@Test
public void testHandleErrorDefault() throws IOException {
MyContainerHelper helper = new MyContainerHelper();
SystemException error = new SystemException("test");
helper.handleError(error);
String output = helper.stringWriter.toString();
Assert.assertTrue("Should contain default error message", output.contains("currently unavailable"));
}
Aggregations