create streams-plugin-pojo
Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/1c78ecc7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/1c78ecc7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/1c78ecc7 Branch: refs/feature/STREAMS-389,398 Commit: 1c78ecc7fe7bf72dad6cd204cffc4fae1ac4b41b Parents: f0a3518 Author: Steve Blackmon @steveblackmon <sblack...@apache.org> Authored: Mon Mar 28 08:32:00 2016 -0500 Committer: Steve Blackmon @steveblackmon <sblack...@apache.org> Committed: Mon Mar 28 08:32:00 2016 -0500 ---------------------------------------------------------------------- pom.xml | 5 + streams-plugins/pom.xml | 2 +- streams-plugins/streams-plugin-hive/pom.xml | 5 +- .../plugins/StreamsHiveResourceGenerator.java | 219 +++++++++++++++ .../StreamsHiveResourceGeneratorMojo.java | 68 +++++ .../apache/streams/plugins/StreamsPojoHive.java | 242 ----------------- .../streams/plugins/StreamsPojoHiveMojo.java | 71 ----- .../test/StreamsHiveResourceGeneratorTest.java | 55 ++++ .../plugins/test/StreamsPojoHiveTest.java | 55 ---- streams-plugins/streams-plugin-pojo/pom.xml | 239 ++++++++++++++++ .../plugins/StreamsPojoGenerationConfig.java | 198 ++++++++++++++ .../plugins/StreamsPojoSourceGeneratorMojo.java | 272 +++++++++++++++++++ .../test/StreamsPojoSourceGeneratorTest.java | 54 ++++ .../test/resources/streams-plugin-pojo/pom.xml | 42 +++ 14 files changed, 1156 insertions(+), 371 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 74e5cf4..b284ae2 100644 --- a/pom.xml +++ b/pom.xml @@ -175,6 +175,7 @@ <module>streams-osgi-components</module> <module>streams-pojo</module> <module>streams-pojo-extensions</module> + <module>streams-plugins</module> <module>streams-runtimes</module> <module>streams-testing</module> <module>streams-util</module> @@ -183,6 +184,10 @@ <packaging>pom</packaging> + <properties> + <jsonschema2pojo.version>0.4.22</jsonschema2pojo.version> + </properties> + <build> <plugins> <plugin> http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/pom.xml ---------------------------------------------------------------------- diff --git a/streams-plugins/pom.xml b/streams-plugins/pom.xml index 2306aab..390c8a0 100644 --- a/streams-plugins/pom.xml +++ b/streams-plugins/pom.xml @@ -36,7 +36,7 @@ </properties> <modules> - <module>streams-plugin-scala</module> + <module>streams-plugin-pojo</module> </modules> <dependencyManagement> http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-hive/pom.xml ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-hive/pom.xml b/streams-plugins/streams-plugin-hive/pom.xml index cbc055b..22d75ce 100644 --- a/streams-plugins/streams-plugin-hive/pom.xml +++ b/streams-plugins/streams-plugin-hive/pom.xml @@ -213,19 +213,20 @@ <version>2.4</version> <executions> <execution> - <id>streams-pojo-resource-dependencies</id> + <id>resource-dependencies</id> <phase>process-test-resources</phase> <goals> <goal>unpack-dependencies</goal> </goals> <configuration> <includeArtifactIds>streams-pojo</includeArtifactIds> - <includes>org/apache/streams/pojo/json/**</includes> + <includes>**/*.json</includes> <outputDirectory>${project.build.directory}/test-classes</outputDirectory> </configuration> </execution> </executions> </plugin> + </plugins> </build> http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGenerator.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGenerator.java b/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGenerator.java new file mode 100644 index 0000000..4edacf0 --- /dev/null +++ b/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGenerator.java @@ -0,0 +1,219 @@ +package org.apache.streams.plugins; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.reflections.ReflectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.Serializable; +import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Created by sblackmon on 11/18/15. + */ +public class StreamsHiveResourceGenerator implements Runnable { + + private final static Logger LOGGER = LoggerFactory.getLogger(StreamsHiveResourceGenerator.class); + + private final static String LS = System.getProperty("line.separator"); + + private StreamsPojoHiveMojo mojo; + + String inDir = "./target/test-classes/activities"; + String outDir = "./target/generated-sources/hive"; + String packages[] = {"org.apache.streams.pojo.json"}; + + public void main(String[] args) { + StreamsHiveResourceGenerator streamsHiveResourceGenerator = new StreamsHiveResourceGenerator(); + Thread thread = new Thread(streamsPojoScala); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + LOGGER.error("InterruptedException", e); + } catch (Exception e) { + LOGGER.error("Exception", e); + } + return; + } + + public StreamsHiveResourceGenerator(StreamsPojoHiveMojo mojo) { + this.mojo = mojo; + if ( mojo != null && + mojo.getTarget() != null && + !Strings.isNullOrEmpty(mojo.getTarget().getAbsolutePath()) + ) + outDir = mojo.getTarget().getAbsolutePath(); + + if ( mojo != null && + mojo.getPackages() != null && + mojo.getPackages().length > 0 + ) + packages = mojo.getPackages(); + } + + public StreamsHiveResourceGenerator() { + } + + public void run() { + + List<Class<?>> serializableClasses = detectSerializableClasses(); + + LOGGER.info("Detected {} serialiables:", serializableClasses.size()); + for( Class clazz : serializableClasses ) + LOGGER.debug(clazz.toString()); + + List<Class<?>> pojoClasses = detectPojoClasses(serializableClasses); + + LOGGER.info("Detected {} pojos:", pojoClasses.size()); + for( Class clazz : pojoClasses ) { + LOGGER.debug(clazz.toString()); + + } + + + for( Class clazz : pojoClasses ) { + String pojoPath = clazz.getPackage().getName().replace(".pojo.json", ".hive").replace(".","/")+"/"; + String pojoName = clazz.getSimpleName()+".hql"; + String pojoHive = renderPojo(clazz); + writeFile(outDir+"/"+pojoPath+pojoName, pojoHive); + } + + } + + private void writeFile(String pojoFile, String pojoHive) { + try { + File path = new File(pojoFile); + File dir = path.getParentFile(); + if( !dir.exists() ) + dir.mkdirs(); + Files.write(Paths.get(pojoFile), pojoHive.getBytes(), StandardOpenOption.CREATE_NEW); + } catch (Exception e) { + LOGGER.error("Write Exception: {}", e); + } + } + + public List<Class<?>> detectSerializableClasses() { + + Set<Class<? extends Serializable>> classes = + reflections.getSubTypesOf(java.io.Serializable.class); + + List<Class<?>> result = Lists.newArrayList(); + + for( Class clazz : classes ) { + result.add(clazz); + } + + return result; + } + + public List<Class<?>> detectPojoClasses(List<Class<?>> classes) { + + List<Class<?>> result = Lists.newArrayList(); + + for( Class clazz : classes ) { + try { + clazz.newInstance().toString(); + } catch( Exception e) {} + // super-halfass way to know if this is a jsonschema2pojo + if( clazz.getAnnotations().length >= 1 ) + result.add(clazz); + } + + return result; + } + + public String renderPojo(Class<?> pojoClass) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("CREATE TABLE "); + stringBuffer.append(pojoClass.getPackage().getName().replace(".pojo.json", ".hive")); + stringBuffer.append(LS); + stringBuffer.append("("); + stringBuffer.append(LS); + + Set<Field> fields = ReflectionUtils.getAllFields(pojoClass); + appendFields(stringBuffer, fields, "", ","); + + stringBuffer.append(")"); + + return stringBuffer.toString(); + } + + private void appendFields(StringBuffer stringBuffer, Set<Field> fields, String varDef, String fieldDelimiter) { + if( fields.size() > 0 ) { + stringBuffer.append(LS); + Map<String,Field> fieldsToAppend = uniqueFields(fields); + for( Iterator<Field> iter = fieldsToAppend.values().iterator(); iter.hasNext(); ) { + Field field = iter.next(); + stringBuffer.append(name(field)); + stringBuffer.append(": "); + stringBuffer.append(type(field)); + if( iter.hasNext()) stringBuffer.append(fieldDelimiter); + stringBuffer.append(LS); + } + } else { + stringBuffer.append(LS); + } + } + + private String value(Field field) { + if( field.getName().equals("verb")) { + return "\"post\""; + } else if( field.getName().equals("objectType")) { + return "\"application\""; + } else return null; + } + + private String type(Field field) { + if( field.getType().equals(java.lang.String.class)) { + return "STRING"; + } else if( field.getType().equals(java.lang.Integer.class)) { + return "INT"; + } else if( field.getType().equals(org.joda.time.DateTime.class)) { + return "DATE"; + }else if( field.getType().equals(java.util.Map.class)) { + return "MAP"; + } else if( field.getType().equals(java.util.List.class)) { + return "ARRAY"; + } + return field.getType().getCanonicalName().replace(".pojo.json", ".scala"); + } + + private Map<String,Field> uniqueFields(Set<Field> fieldset) { + Map<String,Field> fields = Maps.newTreeMap(); + Field item = null; + for( Iterator<Field> it = fieldset.iterator(); it.hasNext(); item = it.next() ) { + if( item != null && item.getName() != null ) { + Field added = fields.put(item.getName(), item); + } + // ensure right class will get used + } + return fields; + } + + private String name(Field field) { + if( field.getName().equals("object")) + return "obj"; + else return field.getName(); + } + + private boolean override(Field field) { + try { + if( field.getDeclaringClass().getSuperclass().getField(field.getName()) != null ) + return true; + else return false; + } catch( Exception e ) { + return false; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGeneratorMojo.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGeneratorMojo.java b/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGeneratorMojo.java new file mode 100644 index 0000000..1f8c782 --- /dev/null +++ b/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsHiveResourceGeneratorMojo.java @@ -0,0 +1,68 @@ +package org.apache.streams.plugins; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.Execute; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; + +@Mojo( name = "hive", + defaultPhase = LifecyclePhase.GENERATE_SOURCES +) +@Execute( goal = "hive", + phase = LifecyclePhase.GENERATE_SOURCES +) +public class StreamsHiveResourceGeneratorMojo extends AbstractMojo { + + private final static Logger LOGGER = LoggerFactory.getLogger(StreamsHiveResourceGeneratorMojo.class); + + @Component + private MavenProject project; + +// @Component +// private Settings settings; +// +// @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) +// protected ArtifactRepository localRepository; +// +// @Parameter( defaultValue = "${plugin}", readonly = true ) // Maven 3 only +// private PluginDescriptor plugin; +// + @Parameter( defaultValue = "${project.basedir}", readonly = true ) + private File basedir; + + @Parameter(defaultValue = "${project.build.directory}", readonly = true) + private File target; + + @Parameter(defaultValue = "org.apache.streams.pojo.json", readonly = true) + private String[] packages; + + public void execute() throws MojoExecutionException { + StreamsHiveResourceGenerator streamsPojoScala = new StreamsHiveResourceGenerator(this); + Thread thread = new Thread(streamsPojoScala); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + LOGGER.error("InterruptedException", e); + } catch (Exception e) { + LOGGER.error("Exception", e); + } + return; + } + + public File getTarget() { + return target; + } + + public String[] getPackages() { + return packages; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHive.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHive.java b/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHive.java deleted file mode 100644 index f8a00f7..0000000 --- a/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHive.java +++ /dev/null @@ -1,242 +0,0 @@ -package org.apache.streams.plugins; - -import com.google.common.base.Strings; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.apache.streams.data.DocumentClassifier; -import org.reflections.ReflectionUtils; -import org.reflections.Reflections; -import org.reflections.scanners.SubTypesScanner; -import org.reflections.scanners.TypeAnnotationsScanner; -import org.reflections.util.ClasspathHelper; -import org.reflections.util.ConfigurationBuilder; -import org.reflections.ReflectionUtils.*; -import org.reflections.util.FilterBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.annotation.Generated; -import java.io.File; -import java.io.IOException; -import java.io.Serializable; -import java.lang.reflect.Field; -import java.nio.file.Files; -import java.nio.file.OpenOption; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Created by sblackmon on 11/18/15. - */ -public class StreamsPojoHive implements Runnable { - - private final static Logger LOGGER = LoggerFactory.getLogger(StreamsPojoHive.class); - - private final static String LS = System.getProperty("line.separator"); - - private StreamsPojoHiveMojo mojo; - - String outDir = "./target/generated-sources/hive"; - String packages[] = {"org.apache.streams.pojo.json"}; - - private final Reflections reflections = new Reflections( - new ConfigurationBuilder() - .forPackages(packages) - .filterInputsBy(new FilterBuilder().includePackage(packages)) - .setScanners( - new SubTypesScanner(), - new TypeAnnotationsScanner())); - - public void main(String[] args) { - StreamsPojoHive streamsPojoScala = new StreamsPojoHive(); - Thread thread = new Thread(streamsPojoScala); - thread.start(); - try { - thread.join(); - } catch (InterruptedException e) { - LOGGER.error("InterruptedException", e); - } catch (Exception e) { - LOGGER.error("Exception", e); - } - return; - } - - public StreamsPojoHive(StreamsPojoHiveMojo mojo) { - this.mojo = mojo; - if ( mojo != null && - mojo.getTarget() != null && - !Strings.isNullOrEmpty(mojo.getTarget().getAbsolutePath()) - ) - outDir = mojo.getTarget().getAbsolutePath(); - - if ( mojo != null && - mojo.getPackages() != null && - mojo.getPackages().length > 0 - ) - packages = mojo.getPackages(); - } - - public StreamsPojoHive() { - } - - public void run() { - - List<Class<?>> serializableClasses = detectSerializableClasses(); - - LOGGER.info("Detected {} serialiables:", serializableClasses.size()); - for( Class clazz : serializableClasses ) - LOGGER.debug(clazz.toString()); - - List<Class<?>> pojoClasses = detectPojoClasses(serializableClasses); - - LOGGER.info("Detected {} pojos:", pojoClasses.size()); - for( Class clazz : pojoClasses ) { - LOGGER.debug(clazz.toString()); - - } - - - for( Class clazz : pojoClasses ) { - String pojoPath = clazz.getPackage().getName().replace(".pojo.json", ".hive").replace(".","/")+"/"; - String pojoName = clazz.getSimpleName()+".hql"; - String pojoHive = renderPojo(clazz); - writeFile(outDir+"/"+pojoPath+pojoName, pojoHive); - } - - } - - private void writeFile(String pojoFile, String pojoHive) { - try { - File path = new File(pojoFile); - File dir = path.getParentFile(); - if( !dir.exists() ) - dir.mkdirs(); - Files.write(Paths.get(pojoFile), pojoHive.getBytes(), StandardOpenOption.CREATE_NEW); - } catch (Exception e) { - LOGGER.error("Write Exception: {}", e); - } - } - - public List<Class<?>> detectSerializableClasses() { - - Set<Class<? extends Serializable>> classes = - reflections.getSubTypesOf(java.io.Serializable.class); - - List<Class<?>> result = Lists.newArrayList(); - - for( Class clazz : classes ) { - result.add(clazz); - } - - return result; - } - - public List<Class<?>> detectPojoClasses(List<Class<?>> classes) { - - List<Class<?>> result = Lists.newArrayList(); - - for( Class clazz : classes ) { - try { - clazz.newInstance().toString(); - } catch( Exception e) {} - // super-halfass way to know if this is a jsonschema2pojo - if( clazz.getAnnotations().length >= 1 ) - result.add(clazz); - } - - return result; - } - - public String renderPojo(Class<?> pojoClass) { - StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append("CREATE TABLE "); - stringBuffer.append(pojoClass.getPackage().getName().replace(".pojo.json", ".hive")); - stringBuffer.append(LS); - stringBuffer.append("("); - stringBuffer.append(LS); - - Set<Field> fields = ReflectionUtils.getAllFields(pojoClass); - appendFields(stringBuffer, fields, "", ","); - - stringBuffer.append(")"); - - return stringBuffer.toString(); - } - - private void appendFields(StringBuffer stringBuffer, Set<Field> fields, String varDef, String fieldDelimiter) { - if( fields.size() > 0 ) { - stringBuffer.append(LS); - Map<String,Field> fieldsToAppend = uniqueFields(fields); - for( Iterator<Field> iter = fieldsToAppend.values().iterator(); iter.hasNext(); ) { - Field field = iter.next(); - stringBuffer.append(name(field)); - stringBuffer.append(": "); - stringBuffer.append(type(field)); - if( iter.hasNext()) stringBuffer.append(fieldDelimiter); - stringBuffer.append(LS); - } - } else { - stringBuffer.append(LS); - } - } - - private String value(Field field) { - if( field.getName().equals("verb")) { - return "\"post\""; - } else if( field.getName().equals("objectType")) { - return "\"application\""; - } else return null; - } - - private String type(Field field) { - if( field.getType().equals(java.lang.String.class)) { - return "STRING"; - } else if( field.getType().equals(java.lang.Integer.class)) { - return "INT"; - } else if( field.getType().equals(org.joda.time.DateTime.class)) { - return "DATE"; - }else if( field.getType().equals(java.util.Map.class)) { - return "MAP"; - } else if( field.getType().equals(java.util.List.class)) { - return "ARRAY"; - } - return field.getType().getCanonicalName().replace(".pojo.json", ".scala"); - } - - private Map<String,Field> uniqueFields(Set<Field> fieldset) { - Map<String,Field> fields = Maps.newTreeMap(); - Field item = null; - for( Iterator<Field> it = fieldset.iterator(); it.hasNext(); item = it.next() ) { - if( item != null && item.getName() != null ) { - Field added = fields.put(item.getName(), item); - } - // ensure right class will get used - } - return fields; - } - - private String name(Field field) { - if( field.getName().equals("object")) - return "obj"; - else return field.getName(); - } - - private boolean override(Field field) { - try { - if( field.getDeclaringClass().getSuperclass().getField(field.getName()) != null ) - return true; - else return false; - } catch( Exception e ) { - return false; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHiveMojo.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHiveMojo.java b/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHiveMojo.java deleted file mode 100644 index a0af5ac..0000000 --- a/streams-plugins/streams-plugin-hive/src/main/java/org/apache/streams/plugins/StreamsPojoHiveMojo.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.apache.streams.plugins; - -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.Execute; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.apache.maven.settings.Settings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; - -@Mojo( name = "hive", - defaultPhase = LifecyclePhase.GENERATE_SOURCES -) -@Execute( goal = "hive", - phase = LifecyclePhase.GENERATE_SOURCES -) -public class StreamsPojoHiveMojo extends AbstractMojo { - - private final static Logger LOGGER = LoggerFactory.getLogger(StreamsPojoHiveMojo.class); - - @Component - private MavenProject project; - -// @Component -// private Settings settings; -// -// @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) -// protected ArtifactRepository localRepository; -// -// @Parameter( defaultValue = "${plugin}", readonly = true ) // Maven 3 only -// private PluginDescriptor plugin; -// - @Parameter( defaultValue = "${project.basedir}", readonly = true ) - private File basedir; - - @Parameter(defaultValue = "${project.build.directory}", readonly = true) - private File target; - - @Parameter(defaultValue = "org.apache.streams.pojo.json", readonly = true) - private String[] packages; - - public void execute() throws MojoExecutionException { - StreamsPojoHive streamsPojoScala = new StreamsPojoHive(this); - Thread thread = new Thread(streamsPojoScala); - thread.start(); - try { - thread.join(); - } catch (InterruptedException e) { - LOGGER.error("InterruptedException", e); - } catch (Exception e) { - LOGGER.error("Exception", e); - } - return; - } - - public File getTarget() { - return target; - } - - public String[] getPackages() { - return packages; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsHiveResourceGeneratorTest.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsHiveResourceGeneratorTest.java b/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsHiveResourceGeneratorTest.java new file mode 100644 index 0000000..cd80cf8 --- /dev/null +++ b/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsHiveResourceGeneratorTest.java @@ -0,0 +1,55 @@ +package org.apache.streams.plugins.test; + +import org.apache.streams.plugins.StreamsHiveResourceGenerator; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileFilter; + +/** + * Test that Activity beans are compatible with the example activities in the spec. + */ +public class StreamsHiveResourceGeneratorTest { + + private final static Logger LOGGER = LoggerFactory.getLogger(StreamsHiveResourceGeneratorTest.class); + + /** + * Tests that all example activities can be loaded into Activity beans + * + * @throws Exception + */ + @Test + public void testStreamsPojoHive() throws Exception { + StreamsHiveResourceGenerator streamsHiveResourceGenerator = new StreamsHiveResourceGenerator(); + streamsHiveResourceGenerator.main(new String[0]); + + File testOutput = new File( "./target/generated-sources/hive/org/apache/streams/hive"); + FileFilter hqlFilter = new FileFilter() { + @Override + public boolean accept(File pathname) { + if( pathname.getName().endsWith(".hql") ) + return true; + return false; + } + }; + + assert( testOutput != null ); + assert( testOutput.exists() == true ); + assert( testOutput.isDirectory() == true ); + assert( testOutput.listFiles(hqlFilter).length == 11 ); +// assert( new File(testOutput + "/traits").exists() == true ); +// assert( new File(testOutput + "/traits").isDirectory() == true ); +// assert( new File(testOutput + "/traits").listFiles(scalaFilter) != null ); +// assert( new File(testOutput + "/traits").listFiles(scalaFilter).length == 4 ); +// assert( new File(testOutput + "/objectTypes").exists() == true ); +// assert( new File(testOutput + "/objectTypes").isDirectory() == true ); +// assert( new File(testOutput + "/objectTypes").listFiles(scalaFilter) != null ); +// assert( new File(testOutput + "/objectTypes").listFiles(scalaFilter).length == 43 ); +// assert( new File(testOutput + "/verbs").exists() == true ); +// assert( new File(testOutput + "/verbs").isDirectory() == true ); +// assert( new File(testOutput + "/verbs").listFiles(scalaFilter) != null ); +// assert( new File(testOutput + "/verbs").listFiles(scalaFilter).length == 89 ); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsPojoHiveTest.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsPojoHiveTest.java b/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsPojoHiveTest.java deleted file mode 100644 index e5a7abd..0000000 --- a/streams-plugins/streams-plugin-hive/src/test/java/org/apache/streams/plugins/test/StreamsPojoHiveTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.apache.streams.plugins.test; - -import org.apache.streams.plugins.StreamsPojoHive; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.FileFilter; - -/** - * Test that Activity beans are compatible with the example activities in the spec. - */ -public class StreamsPojoHiveTest { - - private final static Logger LOGGER = LoggerFactory.getLogger(StreamsPojoHiveTest.class); - - /** - * Tests that all example activities can be loaded into Activity beans - * - * @throws Exception - */ - @Test - public void testStreamsPojoHive() throws Exception { - StreamsPojoHive streamsPojoHive = new StreamsPojoHive(); - streamsPojoHive.main(new String[0]); - - File testOutput = new File( "./target/generated-sources/hive/org/apache/streams/hive"); - FileFilter hqlFilter = new FileFilter() { - @Override - public boolean accept(File pathname) { - if( pathname.getName().endsWith(".hql") ) - return true; - return false; - } - }; - - assert( testOutput != null ); - assert( testOutput.exists() == true ); - assert( testOutput.isDirectory() == true ); - assert( testOutput.listFiles(hqlFilter).length == 11 ); -// assert( new File(testOutput + "/traits").exists() == true ); -// assert( new File(testOutput + "/traits").isDirectory() == true ); -// assert( new File(testOutput + "/traits").listFiles(scalaFilter) != null ); -// assert( new File(testOutput + "/traits").listFiles(scalaFilter).length == 4 ); -// assert( new File(testOutput + "/objectTypes").exists() == true ); -// assert( new File(testOutput + "/objectTypes").isDirectory() == true ); -// assert( new File(testOutput + "/objectTypes").listFiles(scalaFilter) != null ); -// assert( new File(testOutput + "/objectTypes").listFiles(scalaFilter).length == 43 ); -// assert( new File(testOutput + "/verbs").exists() == true ); -// assert( new File(testOutput + "/verbs").isDirectory() == true ); -// assert( new File(testOutput + "/verbs").listFiles(scalaFilter) != null ); -// assert( new File(testOutput + "/verbs").listFiles(scalaFilter).length == 89 ); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-pojo/pom.xml ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-pojo/pom.xml b/streams-plugins/streams-plugin-pojo/pom.xml new file mode 100644 index 0000000..399a371 --- /dev/null +++ b/streams-plugins/streams-plugin-pojo/pom.xml @@ -0,0 +1,239 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.streams.plugins</groupId> + <artifactId>streams-plugin-pojo</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + + <parent> + <groupId>org.apache.streams</groupId> + <artifactId>streams-plugins</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <dependencies> + <dependency> + <groupId>org.apache.streams</groupId> + <artifactId>streams-config</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.streams</groupId> + <artifactId>streams-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.jsonschema2pojo</groupId> + <artifactId>jsonschema2pojo-core</artifactId> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.jsonschema2pojo</groupId> + <artifactId>jsonschema2pojo-maven-plugin</artifactId> + </dependency> + <dependency> + <groupId>org.apache.streams</groupId> + <artifactId>streams-pojo</artifactId> + <version>${project.version}</version> + <scope>test</scope> + <type>test-jar</type> + </dependency> + <dependency> + <groupId>org.reflections</groupId> + <artifactId>reflections</artifactId> + <version>0.9.9</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-aether-provider</artifactId> + <version>3.3.3</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <version>3.3.3</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-compat</artifactId> + <version>3.3.3</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-core</artifactId> + <version>3.3.3</version> + <exclusions> + <exclusion> + <groupId>org.sonatype.aether</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>3.3.3</version> + </dependency> + <dependency> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-resources-plugin</artifactId> + <version>2.7</version> + <exclusions> + <exclusion> + <groupId>org.apache.maven</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + <version>3.0.15</version> + </dependency> + <dependency> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>3.4</version> + <exclusions> + <exclusion> + <artifactId>commons-logging</artifactId> + <groupId>commons-logging</groupId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.maven.plugin-tools</groupId> + <artifactId>maven-plugin-tools-api</artifactId> + <version>3.3</version> + </dependency> + <dependency> + <groupId>org.apache.maven.plugin-tools</groupId> + <artifactId>maven-plugin-annotations</artifactId> + <version>3.3</version> + </dependency> + <dependency> + <groupId>org.apache.maven.plugin-tools</groupId> + <artifactId>maven-plugin-tools-generators</artifactId> + <version>3.3</version> + </dependency> + <dependency> + <groupId>org.apache.maven.plugin-testing</groupId> + <artifactId>maven-plugin-testing-harness</artifactId> + <version>3.3.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.6</version> + </dependency> + <dependency> + <groupId>joda-time</groupId> + <artifactId>joda-time</artifactId> + <version>2.2</version> + </dependency> + <dependency> + <groupId>org.joda</groupId> + <artifactId>joda-convert</artifactId> + <version>1.8.1</version> + </dependency> + </dependencies> + + <build> + <sourceDirectory>src/main/java</sourceDirectory> + <testSourceDirectory>src/test/java</testSourceDirectory> + <resources> + <resource> + <directory>src/main/resources</directory> + </resource> + </resources> + <testResources> + <testResource> + <directory>src/test/resources</directory> + </testResource> + </testResources> + <plugins> + <plugin> + <artifactId>maven-plugin-plugin</artifactId> + <version>3.4</version> + <configuration> + <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> + </configuration> + <executions> + <execution> + <id>mojo-descriptor</id> + <phase>process-classes</phase> + <goals> + <goal>descriptor</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.reflections</groupId> + <artifactId>reflections-maven</artifactId> + <executions> + <execution> + <goals> + <goal>reflections</goal> + </goals> + <phase>process-classes</phase> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <version>2.4</version> + <executions> + <execution> + <id>resource-dependencies</id> + <phase>process-test-resources</phase> + <goals> + <goal>unpack-dependencies</goal> + </goals> + <configuration> + <includeArtifactIds>streams-pojo</includeArtifactIds> + <includes>**/*.json</includes> + <outputDirectory>${project.build.directory}/test-classes</outputDirectory> + </configuration> + </execution> + </executions> + </plugin> + + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoGenerationConfig.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoGenerationConfig.java b/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoGenerationConfig.java new file mode 100644 index 0000000..b42ccf9 --- /dev/null +++ b/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoGenerationConfig.java @@ -0,0 +1,198 @@ +package org.apache.streams.plugins; + +import org.jsonschema2pojo.AnnotationStyle; +import org.jsonschema2pojo.Annotator; +import org.jsonschema2pojo.GenerationConfig; +import org.jsonschema2pojo.SourceType; +import org.jsonschema2pojo.rules.RuleFactory; + +import java.io.File; +import java.io.FileFilter; +import java.net.URL; +import java.util.Iterator; + +/** + * Created by sblackmon on 3/27/16. + */ +public class StreamsPojoGenerationConfig implements GenerationConfig { + + @Override + public boolean isGenerateBuilders() { + return false; + } + + @Override + public boolean isUsePrimitives() { + return false; + } + + @Override + public Iterator<URL> getSource() { + return null; + } + + @Override + public File getTargetDirectory() { + return null; + } + + @Override + public String getTargetPackage() { + return null; + } + + @Override + public char[] getPropertyWordDelimiters() { + return new char[0]; + } + + @Override + public boolean isUseLongIntegers() { + return false; + } + + @Override + public boolean isUseDoubleNumbers() { + return false; + } + + @Override + public boolean isUseBigDecimals() { + return false; + } + + @Override + public boolean isIncludeHashcodeAndEquals() { + return false; + } + + @Override + public boolean isIncludeToString() { + return false; + } + + @Override + public AnnotationStyle getAnnotationStyle() { + return null; + } + + @Override + public Class<? extends Annotator> getCustomAnnotator() { + return null; + } + + @Override + public Class<? extends RuleFactory> getCustomRuleFactory() { + return null; + } + + @Override + public boolean isIncludeJsr303Annotations() { + return false; + } + + @Override + public SourceType getSourceType() { + return null; + } + + @Override + public boolean isRemoveOldOutput() { + return false; + } + + @Override + public String getOutputEncoding() { + return null; + } + + @Override + public boolean isUseJodaDates() { + return false; + } + + @Override + public boolean isUseJodaLocalDates() { + return false; + } + + @Override + public boolean isUseJodaLocalTimes() { + return false; + } + + @Override + public boolean isUseCommonsLang3() { + return false; + } + + @Override + public boolean isParcelable() { + return false; + } + + @Override + public FileFilter getFileFilter() { + return null; + } + + @Override + public boolean isInitializeCollections() { + return false; + } + + @Override + public String getClassNamePrefix() { + return null; + } + + @Override + public String getClassNameSuffix() { + return null; + } + + @Override + public boolean isIncludeConstructors() { + return false; + } + + @Override + public boolean isConstructorsRequiredPropertiesOnly() { + return false; + } + + @Override + public boolean isIncludeAdditionalProperties() { + return false; + } + + @Override + public boolean isIncludeAccessors() { + return false; + } + + @Override + public String getTargetVersion() { + return null; + } + + @Override + public boolean isIncludeDynamicAccessors() { + return false; + } + + @Override + public String getDateTimeType() { + return null; + } + + @Override + public String getDateType() { + return null; + } + + @Override + public String getTimeType() { + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoSourceGeneratorMojo.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoSourceGeneratorMojo.java b/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoSourceGeneratorMojo.java new file mode 100644 index 0000000..8980daa --- /dev/null +++ b/streams-plugins/streams-plugin-pojo/src/main/java/org/apache/streams/plugins/StreamsPojoSourceGeneratorMojo.java @@ -0,0 +1,272 @@ +package org.apache.streams.plugins; + +import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.Execute; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; +import org.jsonschema2pojo.Jsonschema2Pojo; +import org.jsonschema2pojo.maven.Jsonschema2PojoMojo; +import org.jsonschema2pojo.maven.ProjectClasspath; +import org.jsonschema2pojo.util.URLUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import static org.apache.commons.lang.StringUtils.isNotBlank; + +@Mojo( name = "hive", + defaultPhase = LifecyclePhase.GENERATE_SOURCES +) +@Execute( goal = "hive", + phase = LifecyclePhase.GENERATE_SOURCES +) +public class StreamsPojoSourceGeneratorMojo extends AbstractMojo { + + private final static Logger LOGGER = LoggerFactory.getLogger(StreamsPojoSourceGeneratorMojo.class); + + @Component + public MavenProject project; + +// @Component +// private Settings settings; +// +// @Parameter( defaultValue = "${localRepository}", readonly = true, required = true ) +// protected ArtifactRepository localRepository; +// +// @Parameter( defaultValue = "${plugin}", readonly = true ) // Maven 3 only +// private PluginDescriptor plugin; +// + + @Parameter( defaultValue = "${project.basedir}", readonly = true ) + public File basedir; + + @Parameter( defaultValue = "${jsonschema2pojo.sourceDirectory}", readonly = true ) // Maven 3 only + public String sourceDirectory; + + @Parameter( defaultValue = "${jsonschema2pojo.sourcePaths}", readonly = true ) // Maven 3 only + public String[] sourcePaths; + + @Parameter(defaultValue = "${project.build.directory}", readonly = true) + public File target; + + public void execute() throws MojoExecutionException { + + addProjectDependenciesToClasspath(); + + // verify source directories + if (isNotBlank(sourceDirectory)) { + // verify sourceDirectory + try { + URLUtil.parseURL(sourceDirectory); + } catch (IllegalArgumentException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } else if (sourcePaths != null) { + // verify individual source paths + for (String source : sourcePaths) { + try { + URLUtil.parseURL(source); + } catch (IllegalArgumentException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } + } else { + throw new MojoExecutionException("One of sourceDirectory or sourcePaths must be provided"); + } + + try { + Jsonschema2Pojo.generate(new StreamsPojoGenerationConfig()); + } catch (IOException e) { + throw new MojoExecutionException("Error generating classes from JSON Schema file(s) " + sourceDirectory, e); + } + +// List<Class<?>> serializableClasses = detectSerializableClasses(); +// +// LOGGER.info("Detected {} serialiables:", serializableClasses.size()); +// for( Class clazz : serializableClasses ) +// LOGGER.debug(clazz.toString()); +// +// List<Class<?>> pojoClasses = detectPojoClasses(serializableClasses); +// +// LOGGER.info("Detected {} pojos:", pojoClasses.size()); +// for( Class clazz : pojoClasses ) { +// LOGGER.debug(clazz.toString()); +// +// } +// +// +// for( Class clazz : pojoClasses ) { +// String pojoPath = clazz.getPackage().getName().replace(".pojo.json", ".hive").replace(".","/")+"/"; +// String pojoName = clazz.getSimpleName()+".hql"; +// String pojoHive = renderPojo(clazz); +// writeFile(outDir+"/"+pojoPath+pojoName, pojoHive); +// } + + } + + private void addProjectDependenciesToClasspath() { + + try { + + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader newClassLoader = new ProjectClasspath().getClassLoader(project, oldClassLoader, getLog()); + Thread.currentThread().setContextClassLoader(newClassLoader); + + } catch (DependencyResolutionRequiredException e) { + LOGGER.info("Skipping addition of project artifacts, there appears to be a dependecy resolution problem", e); + } + + } + + private void writeFile(String pojoFile, String pojoHive) { + try { + File path = new File(pojoFile); + File dir = path.getParentFile(); + if( !dir.exists() ) + dir.mkdirs(); + Files.write(Paths.get(pojoFile), pojoHive.getBytes(), StandardOpenOption.CREATE_NEW); + } catch (Exception e) { + LOGGER.error("Write Exception: {}", e); + } + } + + public Iterator<URL> getSource() { + if (null != sourceDirectory) { + return Collections.singleton(URLUtil.parseURL(sourceDirectory)).iterator(); + } + List<URL> sourceURLs = new ArrayList<URL>(); + for (String source : sourcePaths) { + sourceURLs.add(URLUtil.parseURL(source)); + } + return sourceURLs.iterator(); + } + +// public List<Class<?>> detectSerializableClasses() { +// +// Set<Class<? extends Serializable>> classes = +// reflections.getSubTypesOf(java.io.Serializable.class); +// +// List<Class<?>> result = Lists.newArrayList(); +// +// for( Class clazz : classes ) { +// result.add(clazz); +// } +// +// return result; +// } +// +// public List<Class<?>> detectPojoClasses(List<Class<?>> classes) { +// +// List<Class<?>> result = Lists.newArrayList(); +// +// for( Class clazz : classes ) { +// try { +// clazz.newInstance().toString(); +// } catch( Exception e) {} +// // super-halfass way to know if this is a jsonschema2pojo +// if( clazz.getAnnotations().length >= 1 ) +// result.add(clazz); +// } +// +// return result; +// } +// +// public String renderPojo(Class<?> pojoClass) { +// StringBuffer stringBuffer = new StringBuffer(); +// stringBuffer.append("CREATE TABLE "); +// stringBuffer.append(pojoClass.getPackage().getName().replace(".pojo.json", ".hive")); +// stringBuffer.append(LS); +// stringBuffer.append("("); +// stringBuffer.append(LS); +// +// Set<Field> fields = ReflectionUtils.getAllFields(pojoClass); +// appendFields(stringBuffer, fields, "", ","); +// +// stringBuffer.append(")"); +// +// return stringBuffer.toString(); +// } +// +// private void appendFields(StringBuffer stringBuffer, Set<Field> fields, String varDef, String fieldDelimiter) { +// if( fields.size() > 0 ) { +// stringBuffer.append(LS); +// Map<String,Field> fieldsToAppend = uniqueFields(fields); +// for( Iterator<Field> iter = fieldsToAppend.values().iterator(); iter.hasNext(); ) { +// Field field = iter.next(); +// stringBuffer.append(name(field)); +// stringBuffer.append(": "); +// stringBuffer.append(type(field)); +// if( iter.hasNext()) stringBuffer.append(fieldDelimiter); +// stringBuffer.append(LS); +// } +// } else { +// stringBuffer.append(LS); +// } +// } +// +// private String value(Field field) { +// if( field.getName().equals("verb")) { +// return "\"post\""; +// } else if( field.getName().equals("objectType")) { +// return "\"application\""; +// } else return null; +// } +// +// private String type(Field field) { +// if( field.getType().equals(java.lang.String.class)) { +// return "STRING"; +// } else if( field.getType().equals(java.lang.Integer.class)) { +// return "INT"; +// } else if( field.getType().equals(org.joda.time.DateTime.class)) { +// return "DATE"; +// }else if( field.getType().equals(java.util.Map.class)) { +// return "MAP"; +// } else if( field.getType().equals(java.util.List.class)) { +// return "ARRAY"; +// } +// return field.getType().getCanonicalName().replace(".pojo.json", ".scala"); +// } +// +// private Map<String,Field> uniqueFields(Set<Field> fieldset) { +// Map<String,Field> fields = Maps.newTreeMap(); +// Field item = null; +// for( Iterator<Field> it = fieldset.iterator(); it.hasNext(); item = it.next() ) { +// if( item != null && item.getName() != null ) { +// Field added = fields.put(item.getName(), item); +// } +// // ensure right class will get used +// } +// return fields; +// } +// +// private String name(Field field) { +// if( field.getName().equals("object")) +// return "obj"; +// else return field.getName(); +// } +// +// private boolean override(Field field) { +// try { +// if( field.getDeclaringClass().getSuperclass().getField(field.getName()) != null ) +// return true; +// else return false; +// } catch( Exception e ) { +// return false; +// } +// } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-pojo/src/test/java/org/apache/streams/plugins/test/StreamsPojoSourceGeneratorTest.java ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-pojo/src/test/java/org/apache/streams/plugins/test/StreamsPojoSourceGeneratorTest.java b/streams-plugins/streams-plugin-pojo/src/test/java/org/apache/streams/plugins/test/StreamsPojoSourceGeneratorTest.java new file mode 100644 index 0000000..63d4995 --- /dev/null +++ b/streams-plugins/streams-plugin-pojo/src/test/java/org/apache/streams/plugins/test/StreamsPojoSourceGeneratorTest.java @@ -0,0 +1,54 @@ +package org.apache.streams.plugins.test; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileFilter; + +/** + * Test that Activity beans are compatible with the example activities in the spec. + */ +public class StreamsPojoSourceGeneratorTest { + + private final static Logger LOGGER = LoggerFactory.getLogger(StreamsPojoSourceGeneratorTest.class); + + /** + * Tests that all example activities can be loaded into Activity beans + * + * @throws Exception + */ + @Test + public void testStreamsPojoSourceGenerator() throws Exception { +// StreamsPojoSourceGenerator streamsPojoSourceGenerator = new StreamsPojoSourceGenerator(); +// StreamsPojoSourceGenerator.main(new String[0]); + + File testOutput = new File( "./target/generated-sources/streams-pojo"); + FileFilter javaFilter = new FileFilter() { + @Override + public boolean accept(File pathname) { + if( pathname.getName().endsWith(".java") ) + return true; + return false; + } + }; + + assert( testOutput != null ); + assert( testOutput.exists() == true ); + assert( testOutput.isDirectory() == true ); + assert( testOutput.listFiles(javaFilter).length == 11 ); +// assert( new File(testOutput + "/traits").exists() == true ); +// assert( new File(testOutput + "/traits").isDirectory() == true ); +// assert( new File(testOutput + "/traits").listFiles(scalaFilter) != null ); +// assert( new File(testOutput + "/traits").listFiles(scalaFilter).length == 4 ); +// assert( new File(testOutput + "/objectTypes").exists() == true ); +// assert( new File(testOutput + "/objectTypes").isDirectory() == true ); +// assert( new File(testOutput + "/objectTypes").listFiles(scalaFilter) != null ); +// assert( new File(testOutput + "/objectTypes").listFiles(scalaFilter).length == 43 ); +// assert( new File(testO`utput + "/verbs").exists() == true ); +// assert( new File(testOutput + "/verbs").isDirectory() == true ); +// assert( new File(testOutput + "/verbs").listFiles(scalaFilter) != null ); +// assert( new File(testOutput + "/verbs").listFiles(scalaFilter).length == 89 ); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/1c78ecc7/streams-plugins/streams-plugin-pojo/src/test/resources/streams-plugin-pojo/pom.xml ---------------------------------------------------------------------- diff --git a/streams-plugins/streams-plugin-pojo/src/test/resources/streams-plugin-pojo/pom.xml b/streams-plugins/streams-plugin-pojo/src/test/resources/streams-plugin-pojo/pom.xml new file mode 100644 index 0000000..b1de7b8 --- /dev/null +++ b/streams-plugins/streams-plugin-pojo/src/test/resources/streams-plugin-pojo/pom.xml @@ -0,0 +1,42 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.streams.plugins</groupId> + <artifactId>streams-plugin-hive-test</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <packaging>jar</packaging> + <name>Test StreamsPojoHiveMojo</name> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.streams.plugins</groupId> + <artifactId>streams-plugin-hive</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <configuration> + <packages> + <package>org.apache.streams.pojo.json</package> + </packages> + <target>target/test-classes/streams-hive-plugin/</target> + </configuration> + <executions> + <execution> + <goals> + <goal>hive</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file