first take at STREAMS-389: streams-project/streams-plugins/streams-plugin-scala


Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/57f17d76
Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/57f17d76
Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/57f17d76

Branch: refs/feature/STREAMS-389,398
Commit: 57f17d765a5e0f4767a998e47e8afb096a8ff792
Parents: 95a02d7
Author: Steve Blackmon (@steveblackmon) <sblack...@apache.org>
Authored: Mon Dec 7 13:48:38 2015 -0700
Committer: Steve Blackmon @steveblackmon <sblack...@apache.org>
Committed: Sat May 7 18:53:32 2016 -0500

----------------------------------------------------------------------
 streams-plugins/pom.xml                         |   1 +
 streams-plugins/streams-plugin-scala/pom.xml    | 243 ++++++++++++
 .../streams/plugins/StreamsPojoScala.java       | 371 +++++++++++++++++++
 .../streams/plugins/StreamsPojoScalaMojo.java   |  71 ++++
 .../plugins/test/StreamsPojoScalaTest.java      |  55 +++
 .../test/resources/streams-plugin-scala/pom.xml |  40 ++
 6 files changed, 781 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/57f17d76/streams-plugins/pom.xml
----------------------------------------------------------------------
diff --git a/streams-plugins/pom.xml b/streams-plugins/pom.xml
index 5d403ac..f2c8999 100644
--- a/streams-plugins/pom.xml
+++ b/streams-plugins/pom.xml
@@ -42,6 +42,7 @@
         <module>streams-plugin-hive</module>
         <module>streams-plugin-pig</module>
         <module>streams-plugin-pojo</module>
+        <module>streams-plugin-scala</module>
        </modules>
 
     <dependencyManagement>

http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/57f17d76/streams-plugins/streams-plugin-scala/pom.xml
----------------------------------------------------------------------
diff --git a/streams-plugins/streams-plugin-scala/pom.xml 
b/streams-plugins/streams-plugin-scala/pom.xml
new file mode 100644
index 0000000..f718260
--- /dev/null
+++ b/streams-plugins/streams-plugin-scala/pom.xml
@@ -0,0 +1,243 @@
+<?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-scala</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>
+    </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>
+        </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>
+        </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>streams-pojo-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>
+                            
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>net.alchim31.maven</groupId>
+                <artifactId>scala-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>scala-compile-generated-sources</id>
+                        <phase>integration-test</phase>
+                        <goals>
+                            <goal>compile</goal>
+                        </goals>
+                        <configuration>
+                            
<sourceDir>target/generated-sources/scala</sourceDir>
+                            <target>target/testClasses/scala</target>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/57f17d76/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScala.java
----------------------------------------------------------------------
diff --git 
a/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScala.java
 
b/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScala.java
new file mode 100644
index 0000000..1c3e317
--- /dev/null
+++ 
b/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScala.java
@@ -0,0 +1,371 @@
+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.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 StreamsPojoScala implements Runnable {
+
+    private final static Logger LOGGER = 
LoggerFactory.getLogger(StreamsPojoScala.class);
+
+    private final static String LS = System.getProperty("line.separator");
+
+    private StreamsPojoScalaMojo mojo;
+
+    String outDir = "./target/generated-sources/scala";
+    String packages = "org.apache.streams.pojo.json";
+
+    private final Reflections reflections = new Reflections(
+            new ConfigurationBuilder()
+                    // TODO
+                    .forPackages(packages)
+                    .setScanners(
+                            new SubTypesScanner(),
+                            new TypeAnnotationsScanner()));
+
+    public void main(String[] args) {
+        StreamsPojoScala streamsPojoScala = new StreamsPojoScala();
+        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 StreamsPojoScala(StreamsPojoScalaMojo 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 &&
+                !Strings.isNullOrEmpty(mojo.getPackages())
+            )
+            packages = mojo.getPackages();
+    }
+
+    public StreamsPojoScala() {
+    }
+
+    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());
+
+        List<Class<?>> traits = detectTraits(pojoClasses);
+
+        LOGGER.info("Detected {} traits:", traits.size());
+        for( Class clazz : traits )
+            LOGGER.debug(clazz.toString());
+
+        List<Class<?>> cases = detectCases(pojoClasses);
+
+        LOGGER.info("Detected {} cases:", cases.size());
+        for( Class clazz : cases )
+            LOGGER.debug(clazz.toString());
+
+
+        for( Class clazz : traits ) {
+            String pojoPath = 
clazz.getPackage().getName().replace(".pojo.json", 
".scala").replace(".","/")+"/traits/";
+            String pojoName = clazz.getSimpleName()+".scala";
+            String pojoScala = renderTrait(clazz);
+            writeFile(outDir+"/"+pojoPath+pojoName, pojoScala);
+        }
+
+        for( Class clazz : traits ) {
+            String pojoPath = 
clazz.getPackage().getName().replace(".pojo.json", 
".scala").replace(".","/")+"/";
+            String pojoName = clazz.getSimpleName()+".scala";
+            String pojoScala = renderClass(clazz);
+            writeFile(outDir+"/"+pojoPath+pojoName, pojoScala);
+        }
+
+        for( Class clazz : cases ) {
+            String pojoPath = 
clazz.getPackage().getName().replace(".pojo.json", 
".scala").replace(".","/")+"/";
+            String pojoName = clazz.getSimpleName()+".scala";
+            String pojoScala = renderCase(clazz);
+            writeFile(outDir+"/"+pojoPath+pojoName, pojoScala);
+        }
+
+    }
+
+    private void writeFile(String pojoFile, String pojoScala) {
+        try {
+            File path = new File(pojoFile);
+            File dir = path.getParentFile();
+            if( !dir.exists() )
+                dir.mkdirs();
+            Files.write(Paths.get(pojoFile), pojoScala.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 List<Class<?>> detectTraits(List<Class<?>> classes) {
+
+        List<Class<?>> traits = Lists.newArrayList();
+
+        for( Class clazz : classes ) {
+            if (reflections.getSubTypesOf(clazz).size() > 0)
+                traits.add(clazz);
+        }
+
+        return traits;
+    }
+
+    public List<Class<?>> detectCases(List<Class<?>> classes) {
+
+        List<Class<?>> cases = Lists.newArrayList();
+
+        for( Class clazz : classes ) {
+            if (reflections.getSubTypesOf(clazz).size() == 0)
+                cases.add(clazz);
+        }
+
+        return cases;
+    }
+
+
+    public String renderTrait(Class<?> pojoClass) {
+        StringBuffer stringBuffer = new StringBuffer();
+        stringBuffer.append("package ");
+        
stringBuffer.append(pojoClass.getPackage().getName().replace(".pojo.json", 
".scala"));
+        stringBuffer.append(".traits");
+        stringBuffer.append(LS);
+        stringBuffer.append("trait "+pojoClass.getSimpleName());
+        stringBuffer.append(" extends Serializable");
+        stringBuffer.append(" {");
+
+        Set<Field> fields = ReflectionUtils.getAllFields(pojoClass);
+        appendFields(stringBuffer, fields, "def", ";");
+
+        stringBuffer.append("}");
+
+        return stringBuffer.toString();
+    }
+
+    public String renderClass(Class<?> pojoClass) {
+        StringBuffer stringBuffer = new StringBuffer();
+        stringBuffer.append("package ");
+        
stringBuffer.append(pojoClass.getPackage().getName().replace(".pojo.json", 
".scala"));
+        stringBuffer.append(LS);
+        stringBuffer.append("import 
org.apache.commons.lang.builder.{HashCodeBuilder, EqualsBuilder, 
ToStringBuilder}");
+        stringBuffer.append(LS);
+        stringBuffer.append("class "+pojoClass.getSimpleName());
+        stringBuffer.append(" (");
+
+        Set<Field> fields = ReflectionUtils.getAllFields(pojoClass);
+        appendFields(stringBuffer, fields, "var", ",");
+
+        stringBuffer.append(")");
+        stringBuffer.append(" extends 
"+pojoClass.getPackage().getName().replace(".pojo.json", 
".scala")+".traits."+pojoClass.getSimpleName());
+        stringBuffer.append(" with Serializable ");
+        stringBuffer.append("{ ");
+        stringBuffer.append(LS);
+        stringBuffer.append("override def equals(obj: Any) = obj match { ");
+        stringBuffer.append(LS);
+        stringBuffer.append("  case other: ");
+        stringBuffer.append(pojoClass.getSimpleName());
+        stringBuffer.append(" => other.getClass == getClass && 
EqualsBuilder.reflectionEquals(this,obj)");
+        stringBuffer.append(LS);
+        stringBuffer.append("  case _ => false");
+        stringBuffer.append(LS);
+        stringBuffer.append("}");
+        stringBuffer.append(LS);
+        stringBuffer.append("override def hashCode = new 
HashCodeBuilder().hashCode");
+        stringBuffer.append(LS);
+        stringBuffer.append("}");
+
+        return stringBuffer.toString();
+    }
+
+    public String renderCase(Class<?> pojoClass) {
+        StringBuffer stringBuffer = new StringBuffer();
+        stringBuffer.append("package ");
+        
stringBuffer.append(pojoClass.getPackage().getName().replace(".pojo.json", 
".scala"));
+        stringBuffer.append(LS);
+        stringBuffer.append("case class "+pojoClass.getSimpleName());
+        stringBuffer.append("(");
+        Set<Field> fields = ReflectionUtils.getAllFields(pojoClass);
+        appendFields(stringBuffer, fields, "var", ",");
+        stringBuffer.append(")");
+        if( pojoClass.getSuperclass() != null && 
!pojoClass.getSuperclass().equals(java.lang.Object.class)) {
+            stringBuffer.append(" extends 
"+pojoClass.getSuperclass().getPackage().getName().replace(".pojo.json", 
".scala")+".traits."+pojoClass.getSuperclass().getSimpleName());
+        }
+        stringBuffer.append(LS);
+
+        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();
+                if( override( field ) )
+                    stringBuffer.append("override ");
+                stringBuffer.append(varDef);
+                stringBuffer.append(" ");
+                stringBuffer.append(name(field));
+                stringBuffer.append(": ");
+                if( option(field) ) {
+                    stringBuffer.append("scala.Option[");
+                    stringBuffer.append(type(field));
+                    stringBuffer.append("]");
+                } else {
+                    stringBuffer.append(type(field));
+                }
+                if( !fieldDelimiter.equals(";") && value(field) != null) {
+                    stringBuffer.append(" = ");
+                    if( option(field) ) {
+                        stringBuffer.append("scala.Some(");
+                        stringBuffer.append(value(field));
+                        stringBuffer.append(")");
+                    } else {
+                        stringBuffer.append(value(field));
+                    }
+                }
+                if( iter.hasNext()) stringBuffer.append(fieldDelimiter);
+                stringBuffer.append(LS);
+            }
+        } else {
+            stringBuffer.append(LS);
+        }
+    }
+
+    private boolean option(Field field) {
+        if( field.getName().equals("verb")) {
+            return false;
+        } else if( field.getType().equals(java.util.Map.class)) {
+            return false;
+        } else if( field.getType().equals(java.util.List.class)) {
+            return false;
+        } else return true;
+    }
+
+    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.util.Map.class)) {
+            return "scala.collection.mutable.Map[String,Any]";
+        } else if( field.getType().equals(java.util.List.class)) {
+            return "scala.collection.mutable.MutableList[Any]";
+        }
+        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/57f17d76/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScalaMojo.java
----------------------------------------------------------------------
diff --git 
a/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScalaMojo.java
 
b/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScalaMojo.java
new file mode 100644
index 0000000..baa0f50
--- /dev/null
+++ 
b/streams-plugins/streams-plugin-scala/src/main/java/org/apache/streams/plugins/StreamsPojoScalaMojo.java
@@ -0,0 +1,71 @@
+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 = "scala",
+        defaultPhase = LifecyclePhase.GENERATE_SOURCES
+)
+@Execute(   goal = "scala",
+            phase = LifecyclePhase.GENERATE_SOURCES
+)
+public class StreamsPojoScalaMojo extends AbstractMojo {
+
+    private final static Logger LOGGER = 
LoggerFactory.getLogger(StreamsPojoScalaMojo.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 {
+        StreamsPojoScala streamsPojoScala = new StreamsPojoScala(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/57f17d76/streams-plugins/streams-plugin-scala/src/test/java/org/apache/streams/plugins/test/StreamsPojoScalaTest.java
----------------------------------------------------------------------
diff --git 
a/streams-plugins/streams-plugin-scala/src/test/java/org/apache/streams/plugins/test/StreamsPojoScalaTest.java
 
b/streams-plugins/streams-plugin-scala/src/test/java/org/apache/streams/plugins/test/StreamsPojoScalaTest.java
new file mode 100644
index 0000000..4e5e7e3
--- /dev/null
+++ 
b/streams-plugins/streams-plugin-scala/src/test/java/org/apache/streams/plugins/test/StreamsPojoScalaTest.java
@@ -0,0 +1,55 @@
+package org.apache.streams.plugins.test;
+
+import org.apache.streams.plugins.StreamsPojoScala;
+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 StreamsPojoScalaTest {
+
+    private final static Logger LOGGER = 
LoggerFactory.getLogger(StreamsPojoScalaTest.class);
+
+    /**
+     * Tests that all example activities can be loaded into Activity beans
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testDetectPojoScala() throws Exception {
+        StreamsPojoScala streamsPojoScala = new StreamsPojoScala();
+        streamsPojoScala.main(new String[0]);
+
+        File testOutput = new File( 
"./target/generated-sources/scala/org/apache/streams/scala");
+        FileFilter scalaFilter = new FileFilter() {
+            @Override
+            public boolean accept(File pathname) {
+                if( pathname.getName().endsWith(".scala") )
+                    return true;
+                return false;
+            }
+        };
+
+        assert( testOutput != null );
+        assert( testOutput.exists() == true );
+        assert( testOutput.isDirectory() == true );
+        assert( testOutput.listFiles(scalaFilter).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/57f17d76/streams-plugins/streams-plugin-scala/src/test/resources/streams-plugin-scala/pom.xml
----------------------------------------------------------------------
diff --git 
a/streams-plugins/streams-plugin-scala/src/test/resources/streams-plugin-scala/pom.xml
 
b/streams-plugins/streams-plugin-scala/src/test/resources/streams-plugin-scala/pom.xml
new file mode 100644
index 0000000..b4867a0
--- /dev/null
+++ 
b/streams-plugins/streams-plugin-scala/src/test/resources/streams-plugin-scala/pom.xml
@@ -0,0 +1,40 @@
+<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-scala-test</artifactId>
+    <version>0.3-incubating-SNAPSHOT</version>
+    <packaging>jar</packaging>
+    <name>Test StreamsPojoScalaMojo</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-scala</artifactId>
+                <version>0.3-incubating-SNAPSHOT</version>
+                <configuration>
+                    <packages>org.apache.streams.pojo.json</packages>
+                    <target>target/test-classes/streams-scala-plugin/</target>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>scala</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file

Reply via email to