com.bazaarvoice.jolt.chainr.spec.ChainrEntry Java Examples

The following examples show how to use com.bazaarvoice.jolt.chainr.spec.ChainrEntry. 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: TransformFactory.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected static List<JoltTransform> getChainrJoltTransformations(ClassLoader classLoader, Object specJson) throws Exception{
    if(!(specJson instanceof List)) {
        throw new SpecException("JOLT Chainr expects a JSON array of objects - Malformed spec.");
    } else {

        List operations = (List)specJson;

        if(operations.isEmpty()) {
            throw new SpecException("JOLT Chainr passed an empty JSON array.");
        } else {

            ArrayList<JoltTransform> entries = new ArrayList<>(operations.size());

            for(Object chainrEntryObj : operations) {

                if(!(chainrEntryObj instanceof Map)) {
                    throw new SpecException("JOLT ChainrEntry expects a JSON map - Malformed spec");
                } else {
                    Map chainrEntryMap = (Map)chainrEntryObj;
                    String opString = (String) chainrEntryMap.get("operation");
                    String operationClassName;

                    if(opString == null) {
                        throw new SpecException("JOLT Chainr \'operation\' must implement Transform or ContextualTransform");
                    } else {

                        operationClassName = ChainrEntry.STOCK_TRANSFORMS.getOrDefault(opString, opString);

                        entries.add(getCustomTransform(classLoader,operationClassName,chainrEntryMap.get("spec")));
                    }
                }
            }

            return entries;
        }
    }

}
 
Example #2
Source File: GuiceChainrInstantiator.java    From jolt with Apache License 2.0 5 votes vote down vote up
@Override
public JoltTransform hydrateTransform( ChainrEntry entry ) {

    final Class<? extends JoltTransform> transformClass = entry.getJoltTransformClass();
    final Object transformSpec = entry.getSpec();

    try {

        if ( entry.isSpecDriven() ) {

            // In order to inject an "Object" into the constructor of a SpecTransform, we create an Injector just for this class.
            Injector injector;
            injector = Guice.createInjector( new AbstractModule() {
                @Override
                protected void configure() {

                    // install the parent module so that Custom Java Transforms or Templates can have @Injected properties filled in
                    install( parentModule );

                    // Bind the "spec" for the transform
                    bind( Object.class ).toInstance( transformSpec );
                }
            } );

            return injector.getInstance( transformClass );

        } else {
            // else normal no-op constructor OR non-spec constructor with @Inject annotation
            return nonSpecInjector.getInstance( transformClass );
        }

    }
    catch ( Exception creationException ) {
        throw new SpecException( "Exception using Guice to initialize class:" + transformClass.getCanonicalName() + entry.getErrorMessageIndexSuffix(), creationException );
    }
}
 
Example #3
Source File: ChainrBuilder.java    From jolt with Apache License 2.0 5 votes vote down vote up
public Chainr build() {
    ChainrSpec chainrSpec = new ChainrSpec( chainrSpecObj, classLoader );
    List<JoltTransform> transforms = new ArrayList<>( chainrSpec.getChainrEntries().size() );
    for ( ChainrEntry entry : chainrSpec.getChainrEntries() ) {

        JoltTransform transform = chainrInstantiator.hydrateTransform( entry );
        transforms.add( transform );
    }

    return new Chainr( transforms );
}
 
Example #4
Source File: DefaultChainrInstantiator.java    From jolt with Apache License 2.0 5 votes vote down vote up
@Override
public JoltTransform hydrateTransform( ChainrEntry entry ) {

    Object spec = entry.getSpec();
    Class<? extends JoltTransform> transformClass = entry.getJoltTransformClass();

    try {
        // If the transform class is a SpecTransform, we try to construct it with the provided spec.
        if ( entry.isSpecDriven() ) {

            try {
                // Lookup a Constructor with a Single "Object" arg.
                Constructor constructor = transformClass.getConstructor( Object.class );

                return (JoltTransform) constructor.newInstance( spec );

            } catch ( NoSuchMethodException nsme ) {
                // This means the transform class "violated" the SpecTransform marker interface
                throw new SpecException( "JOLT Chainr encountered an exception constructing SpecTransform className:" + transformClass.getCanonicalName() +
                        ".  Specifically, no single arg constructor found" + entry.getErrorMessageIndexSuffix(), nsme );
            }
        }
        else {
            // The opClass is just a Transform, so just create a newInstance of it.
            return transformClass.newInstance();
        }
    } catch ( Exception e ) {
        // FYI 3 exceptions are known to be thrown here
        // IllegalAccessException, InvocationTargetException, InstantiationException
        throw new SpecException( "JOLT Chainr encountered an exception constructing Transform className:"
                + transformClass.getCanonicalName() + entry.getErrorMessageIndexSuffix(), e );
    }
}
 
Example #5
Source File: ChainrTest.java    From jolt with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> newActivity( String operation, Object spec ) {
    Map<String, Object> activity = new HashMap<>();
    activity.put( ChainrEntry.OPERATION_KEY, operation );
    if ( spec != null ) {
        activity.put( ChainrEntry.SPEC_KEY, spec );
    }
    return activity;
}
 
Example #6
Source File: ChainrTest.java    From jolt with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> newCustomJavaActivity( Class cls, Object spec ) {
    Map<String, Object> activity = new HashMap<>();
    activity.put( ChainrEntry.OPERATION_KEY, cls.getName() );
    if ( spec != null ) {
        activity.put( ChainrEntry.SPEC_KEY, spec );
    }

    return activity;
}
 
Example #7
Source File: ChainrSpecLoadingTest.java    From jolt with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "badFormatSpecs", expectedExceptions = SpecException.class )
public void testBadSpecs( Object chainrSpecObj ) {
    ChainrSpec chainrSpec = new ChainrSpec( chainrSpecObj );
    ChainrEntry chainrEntry = chainrSpec.getChainrEntries().get( 0 );
    DefaultChainrInstantiator instantiator = new DefaultChainrInstantiator();

    // This should fail
    instantiator.hydrateTransform( chainrEntry );
}
 
Example #8
Source File: JoltProcessor.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
void registerReflectiveClasses(BuildProducer<ReflectiveClassBuildItem> producer) {
    ChainrEntry.STOCK_TRANSFORMS.values().stream().forEach(c -> {
        producer.produce(new ReflectiveClassBuildItem(false, false, c));
    });
}
 
Example #9
Source File: TransformFactory.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
protected static List<JoltTransform> getChainrJoltTransformations(ClassLoader classLoader, Object specJson) throws Exception{
    if(!(specJson instanceof List)) {
        throw new SpecException("JOLT Chainr expects a JSON array of objects - Malformed spec.");
    } else {

        List operations = (List)specJson;

        if(operations.isEmpty()) {
            throw new SpecException("JOLT Chainr passed an empty JSON array.");
        } else {

            ArrayList<JoltTransform> entries = new ArrayList<JoltTransform>(operations.size());

            for(Object chainrEntryObj : operations) {

                if(!(chainrEntryObj instanceof Map)) {
                    throw new SpecException("JOLT ChainrEntry expects a JSON map - Malformed spec");
                } else {
                    Map chainrEntryMap = (Map)chainrEntryObj;
                    String opString = (String) chainrEntryMap.get("operation");
                    String operationClassName;

                    if(opString == null) {
                        throw new SpecException("JOLT Chainr \'operation\' must implement Transform or ContextualTransform");
                    } else {

                        if(ChainrEntry.STOCK_TRANSFORMS.containsKey(opString)) {
                            operationClassName = ChainrEntry.STOCK_TRANSFORMS.get(opString);
                        } else {
                            operationClassName = opString;
                        }

                        entries.add(getCustomTransform(classLoader,operationClassName,chainrEntryMap.get("spec")));
                    }
                }
            }

            return entries;
        }
    }

}
 
Example #10
Source File: TransformFactory.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected static List<JoltTransform> getChainrJoltTransformations(ClassLoader classLoader, Object specJson) throws Exception{
    if(!(specJson instanceof List)) {
        throw new SpecException("JOLT Chainr expects a JSON array of objects - Malformed spec.");
    } else {

        List operations = (List)specJson;

        if(operations.isEmpty()) {
            throw new SpecException("JOLT Chainr passed an empty JSON array.");
        } else {

            ArrayList<JoltTransform> entries = new ArrayList<JoltTransform>(operations.size());

            for(Object chainrEntryObj : operations) {

                if(!(chainrEntryObj instanceof Map)) {
                    throw new SpecException("JOLT ChainrEntry expects a JSON map - Malformed spec");
                } else {
                    Map chainrEntryMap = (Map)chainrEntryObj;
                    String opString = (String) chainrEntryMap.get("operation");
                    String operationClassName;

                    if(opString == null) {
                        throw new SpecException("JOLT Chainr \'operation\' must implement Transform or ContextualTransform");
                    } else {

                        if(ChainrEntry.STOCK_TRANSFORMS.containsKey(opString)) {
                            operationClassName = ChainrEntry.STOCK_TRANSFORMS.get(opString);
                        } else {
                            operationClassName = opString;
                        }

                        entries.add(getCustomTransform(classLoader,operationClassName,chainrEntryMap.get("spec")));
                    }
                }
            }

            return entries;
        }
    }

}
 
Example #11
Source File: ChainrTest.java    From jolt with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> newActivity( String opname ) {
    Map<String, Object> activity = new HashMap<>();
    activity.put( ChainrEntry.OPERATION_KEY, opname );
    return activity;
}
 
Example #12
Source File: ChainrInstantiator.java    From jolt with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiate the Transform class specified by the ChainrEntry.
 */
public JoltTransform hydrateTransform( ChainrEntry entry );