org.apache.velocity.util.introspection.Uberspect Java Examples

The following examples show how to use org.apache.velocity.util.introspection.Uberspect. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConversionHandlerTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testCustomConversionHandlerInstance()
{
    RuntimeInstance ve = new RuntimeInstance();
    ve.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
    ve.setProperty(Velocity.RUNTIME_LOG_INSTANCE, log);
    ve.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file");
    ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TEST_COMPARE_DIR + "/conversion");
    ve.setProperty(RuntimeConstants.CONVERSION_HANDLER_INSTANCE, new MyCustomConverter());
    ve.init();
    Uberspect uberspect = ve.getUberspect();
    assertTrue(uberspect instanceof UberspectImpl);
    UberspectImpl ui = (UberspectImpl)uberspect;
    TypeConversionHandler ch = ui.getConversionHandler();
    assertTrue(ch != null);
    assertTrue(ch instanceof MyCustomConverter);
    VelocityContext context = new VelocityContext();
    context.put("obj", new Obj());
    Writer writer = new StringWriter();
    ve.evaluate(context, writer, "test", "$obj.objectString(1.0)");
    assertEquals("String ok: foo", writer.toString());
}
 
Example #2
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testDisambiguation()
        throws Exception
{
    VelPropertySet setter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // setUnambigous() - String
    setter = u.getPropertySet(uto, "unambiguous", "string", null);
    assertNotNull(setter);
    setter.invoke(uto, "string");

    // setUnambigous() - HashMap
    setter = u.getPropertySet(uto, "unambiguous", new HashMap(), null);
    assertNotNull(setter);
    setter.invoke(uto, new HashMap());
}
 
Example #3
Source File: UberspectDelegate.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void init() throws Exception {
	try {
		this.uberspector = (Uberspect) runtimeServices.getProperty( DELEGATE_TARGET_PROPERTY_NAME);
	} catch( ClassCastException e) {
		logger.error( "Cannot cast delegate target to " + Uberspect.class.getCanonicalName(), e);
		
		throw e;
	}
		
	if( this.uberspector == null) {
		logger.warn( "No delegate target defined");
		
		throw new RuntimeException( "No delegate target defined");
	} else {
		if( logger.isDebugEnabled())
			logger.debug( "Delegate target defined. Class is " + this.uberspector.getClass().getCanonicalName());
		
		initUberspectTarget( this.uberspector);
	}
}
 
Example #4
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testMultipleParameterTypes()
        throws Exception
{
    VelPropertySet setter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // setAmbigous() - String
    setter = u.getPropertySet(uto, "Ambigous", "", null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "setAmbigous", setter.getMethodName());

    // setAmbigous() - StringBuffer
    setter = u.getPropertySet(uto, "Ambigous", new StringBuffer(), null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "setAmbigous", setter.getMethodName());
}
 
Example #5
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testNullParameterType()
        throws Exception
{
    VelPropertySet setter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // setRegular()
    setter = u.getPropertySet(uto, "Regular", null, null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "setRegular", setter.getMethodName());

    // setAmbigous() - String and StringBuffer available
    setter = u.getPropertySet(uto, "Ambigous", null, null);
    assertNull(setter);

    // setAmbigous() - same with Object?
    setter = u.getPropertySet(uto, "Ambigous", new Object(), null);
    assertNull(setter);
}
 
Example #6
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testNullPropertySetter()
    throws Exception
{
    Uberspect u = ri.getUberspect();
    GetPutObject gpo = new GetPutObject();
    Map map = new HashMap();

    // Don't screw up on null properties. That should map to put() on the GPO.
    VelPropertySet setter = u.getPropertySet(gpo, null, "", null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "put", setter.getMethodName());

    // And should be null on a Map which does not have a put()
    setter = u.getPropertySet(map, null, "", null);
    assertNull(setter);
}
 
Example #7
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testNullPropertyGetter()
    throws Exception
{
    Uberspect u = ri.getUberspect();
    GetPutObject gpo = new GetPutObject();
    Map map = new HashMap();

    VelPropertyGet getter = u.getPropertyGet(gpo, null, null);

    // Don't screw up on null properties. That should map to get() on the GPO.
    assertNotNull(getter);
    assertEquals("Found wrong method", "get", getter.getMethodName());

    // And should be null on a Map which does not have a get()
    getter = u.getPropertyGet(map, null, null);
    assertNull(getter);

}
 
Example #8
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testEmptyPropertyGetter()
        throws Exception
{
    Uberspect u = ri.getUberspect();
    Map map = new HashMap();

    VelPropertyGet getter = u.getPropertyGet(map, "", null);

    // Don't screw up on empty properties. That should map to get("")
    assertNotNull(getter);
    assertEquals("Found wrong method", "get", getter.getMethodName());
}
 
Example #9
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testRegularSetters()
        throws Exception
{
    VelPropertySet setter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // setRegular()
    setter = u.getPropertySet(uto, "Regular", "", null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "setRegular", setter.getMethodName());

    // Lowercase regular
    setter = u.getPropertySet(uto, "regular", "", null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "setRegular", setter.getMethodName());

    // lowercase: setpremium()
    setter = u.getPropertySet(uto, "premium", "", null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "setpremium", setter.getMethodName());

    // test uppercase: getpremium()
    setter = u.getPropertySet(uto, "Premium", "", null);
    assertNotNull(setter);
    assertEquals("Found wrong method", "setpremium", setter.getMethodName());
}
 
Example #10
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testBooleanGetters()
        throws Exception
{
    VelPropertyGet getter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // getRegular()
    getter = u.getPropertyGet(uto, "RegularBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "isRegularBool", getter.getMethodName());

    // Lowercase regular
    getter = u.getPropertyGet(uto, "regularBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "isRegularBool", getter.getMethodName());

    // lowercase: getpremiumBool()
    getter = u.getPropertyGet(uto, "premiumBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "ispremiumBool", getter.getMethodName());

    // test uppercase: ()
    getter = u.getPropertyGet(uto, "PremiumBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "ispremiumBool", getter.getMethodName());
}
 
Example #11
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testRegularGetters()
        throws Exception
{
    VelPropertyGet getter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // getRegular()
    getter = u.getPropertyGet(uto, "Regular", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getRegular", getter.getMethodName());

    // Lowercase regular
    getter = u.getPropertyGet(uto, "regular", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getRegular", getter.getMethodName());

    // lowercase: getpremium()
    getter = u.getPropertyGet(uto, "premium", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getpremium", getter.getMethodName());

    // test uppercase: getpremium()
    getter = u.getPropertyGet(uto, "Premium", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getpremium", getter.getMethodName());
}
 
Example #12
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testEmptyPropertySetter()
        throws Exception
{
    Uberspect u = ri.getUberspect();
    Map map = new HashMap();

    VelPropertySet setter = u.getPropertySet(map, "", Object.class, null);

    // Don't screw up on empty properties. That should map to put("", Object)
    assertNotNull(setter);
    assertEquals("Found wrong method", "put", setter.getMethodName());
}
 
Example #13
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testNullObjects()
        throws Exception
{
    // How about some null objects... Gee, I'm mean. ;-)
    Uberspect u = ri.getUberspect();

    VelPropertyGet getter = u.getPropertyGet(null, "foo", null);
    assertNull(getter);

    VelPropertySet setter = u.getPropertySet(null, "foo", Object.class, null);
    assertNull(setter);
}
 
Example #14
Source File: UberspectDelegate.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Initialize the delegate target.
 * 
 * @param uberspectorTarget target to initialize
 * 
 * @throws Exception on error initializing the target
 */
private void initUberspectTarget( Uberspect uberspectorTarget) throws Exception {
	// Call methods of Aware-interfaces
	if( uberspectorTarget instanceof RuntimeServicesAware) {
		((RuntimeServicesAware) uberspectorTarget).setRuntimeServices( runtimeServices);
	}
	if( uberspectorTarget instanceof ContextAware) {
		((ContextAware) uberspectorTarget).setContext( context);
	}

	uberspectorTarget.init();
}
 
Example #15
Source File: AbstractVelocityWrapper.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new Velocity engine. If an Uberspector class is defined, this class will
 * be used for tracking the company context.  
 * 
 * @param companyId ID of the company for that the scripts are executed
 * @param factory factory to create the Uberspect delegate target
 * 
 * @return Velocity engine
 * 
 * @throws Exception on errors initializing Velocity
 */
private VelocityEngine createEngine( int companyId, UberspectDelegateTargetFactory factory) throws Exception {
	VelocityEngine ve = new VelocityEngine();
	
	ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
	ve.setProperty(RuntimeConstants.RUNTIME_LOG, velocityLogDir + "/velocity.log");
	ve.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
	ve.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
	ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, "org.agnitas.emm.core.velocity.IncludeParsePreventionHandler");
	
	Uberspect uberspectDelegateTarget = factory.newDelegateTarget( companyId);
	
	if( uberspectDelegateTarget != null) {
		if( logger.isInfoEnabled())
			logger.info( "Setting uberspect delegate target: " + uberspectDelegateTarget.getClass().getCanonicalName());
		
		ve.setProperty( RuntimeConstants.UBERSPECT_CLASSNAME, UberspectDelegate.class.getCanonicalName());
		ve.setProperty( UberspectDelegate.DELEGATE_TARGET_PROPERTY_NAME, uberspectDelegateTarget);
	} else
		logger.warn( "SECURITY LEAK: No uberspector defined");
	
	try {
		ve.init();

		return ve;
	} catch( Exception e) {
		logger.error( "Error initializing Velocity engine", e);
		
		throw e;
	}
	
}
 
Example #16
Source File: UberspectDelegateTargetFactoryImpl.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Uberspect newDelegateTarget(int companyId) {
	return new AgnVelocityUberspector( companyId, this.companyContextChecker);
}
 
Example #17
Source File: ComUberspectDelegateTargetFactoryImpl.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Uberspect newDelegateTarget(int companyId) {
	return new ComAgnVelocityUberspector( companyId, this.companyContextChecker, this.configService);
}
 
Example #18
Source File: RuntimeInstance.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the Uberspect object for this Instance.
 *
 * @return The Uberspect object for this Instance.
 */
public Uberspect getUberspect()
{
    return uberSpect;
}
 
Example #19
Source File: RuntimeServices.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the configured class introspection/reflection
 * implementation.
 * @return The current Uberspect object.
 */
Uberspect getUberspect();
 
Example #20
Source File: RuntimeSingleton.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the Uberspect object for this Instance.
 *
 * @return The Uberspect object for this Instance.
 * @see org.apache.velocity.runtime.RuntimeServices#getUberspect()
 * @see RuntimeInstance#getUberspect()
 */
public static Uberspect getUberspect()
{
    return ri.getUberspect();
}
 
Example #21
Source File: UberspectDelegateTargetFactory.java    From openemm with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Create a new delegate for given company ID.
 * 
 * @param companyId company ID that executes the Velocity script
 * 
 * @return new Uberspect delegate target
 */
public Uberspect newDelegateTarget( int companyId);