polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nic...@apache.org
Subject [04/15] polygene-java git commit: Working on new Envisage over http.
Date Mon, 18 Sep 2017 15:21:00 GMT
Working on new Envisage over http.


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/2bef7120
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/2bef7120
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/2bef7120

Branch: refs/heads/es-sql
Commit: 2bef7120f17475e547234f35f6aae2a7f9e6a24b
Parents: 96f0713
Author: niclas <niclas@hedhman.org>
Authored: Mon Sep 4 13:06:16 2017 +0800
Committer: niclas <niclas@hedhman.org>
Committed: Mon Sep 4 13:06:16 2017 +0800

----------------------------------------------------------------------
 .../runtime/activation/ActivationDelegate.java  |   4 +-
 .../serialization/JsonRepresentation.java       |   5 +
 .../HttpServerModule/bootstrap.tmpl             |   7 +-
 .../RestAPIApplication/Launcher.java.tmpl       |  24 +++-
 .../RestAPIApplication/bootstrap-test.tmpl      |  22 +---
 tools/model-detail/build.gradle                 |   1 +
 .../polygene/tools/model/EnvisageServlet.java   | 113 +++++++++++++++++++
 .../descriptor/ApplicationDetailDescriptor.java |  52 ++++++++-
 .../model/descriptor/LayerDetailDescriptor.java |  23 ++++
 .../model-detail/src/main/resources/index.html  |  11 ++
 10 files changed, 233 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/core/runtime/src/main/java/org/apache/polygene/runtime/activation/ActivationDelegate.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/activation/ActivationDelegate.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/activation/ActivationDelegate.java
index af3a812..662b1c2 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/activation/ActivationDelegate.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/activation/ActivationDelegate.java
@@ -157,7 +157,7 @@ public final class ActivationDelegate
             {
                 throw ( (ActivationException) e );
             }
-            throw new ActivationException( "Unable to Activate application.", e );
+            throw new ActivationException( "Unable to Activate application: " + target, e
);
         }
     }
 
@@ -308,7 +308,7 @@ public final class ActivationDelegate
         public Object get()
         {
             throw new IllegalStateException( "Service is passive, either activating and"
-                                             + " cannot be used yet or passivating and cannot
be used anymore." );
+                                             + " cannot be used yet or passivating and cannot
be used anymore: " + reference );
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/JsonRepresentation.java
----------------------------------------------------------------------
diff --git a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/JsonRepresentation.java
b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/JsonRepresentation.java
index fafabe4..01947cf 100644
--- a/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/JsonRepresentation.java
+++ b/libraries/restlet/src/main/java/org/apache/polygene/library/restlet/serialization/JsonRepresentation.java
@@ -32,6 +32,7 @@ import org.apache.polygene.api.serialization.Serializer;
 import org.apache.polygene.api.structure.ModuleDescriptor;
 import org.apache.polygene.spi.PolygeneSPI;
 import org.restlet.data.MediaType;
+import org.restlet.engine.application.StatusInfo;
 import org.restlet.representation.OutputRepresentation;
 import org.restlet.representation.Representation;
 
@@ -121,6 +122,10 @@ public class JsonRepresentation<T> extends OutputRepresentation
         {
             representation.write( outputStream );
         }
+        else if( object instanceof StatusInfo )
+        {
+            outputStream.write(((StatusInfo) object).getReasonPhrase().getBytes());
+        }
         else if( object != null )
         {
             stateSerialization.serialize( Serializer.Options.NO_TYPE_INFO, outputStream,
object );

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/generator-polygene/app/templates/ConnectivityLayer/HttpServerModule/bootstrap.tmpl
----------------------------------------------------------------------
diff --git a/tools/generator-polygene/app/templates/ConnectivityLayer/HttpServerModule/bootstrap.tmpl
b/tools/generator-polygene/app/templates/ConnectivityLayer/HttpServerModule/bootstrap.tmpl
index addab42..2018bfa 100644
--- a/tools/generator-polygene/app/templates/ConnectivityLayer/HttpServerModule/bootstrap.tmpl
+++ b/tools/generator-polygene/app/templates/ConnectivityLayer/HttpServerModule/bootstrap.tmpl
@@ -37,7 +37,9 @@ import org.apache.polygene.bootstrap.LayerAssembly;
 import org.apache.polygene.bootstrap.ModuleAssembly;
 import org.apache.polygene.bootstrap.layered.ModuleAssembler;
 import org.apache.polygene.library.restlet.PolygeneServerServlet;
-
+<% if( hasFeature( 'envisage' ) ) {
+%>import org.apache.polygene.tools.model.EnvisageServlet;
+<% } %>
 import <%= polygene.packageName %>.rest.<%= polygene.name %>RestApplication;
 
 import static org.apache.polygene.library.http.Servlets.addServlets;
@@ -97,6 +99,9 @@ public class HttpServerModule
         defaults.port().set( DEFAULT_PORT );
         HashMap<String, String> initParams = new HashMap<>();
         initParams.put("org.restlet.application", <%= polygene.name %>RestApplication.class.getName()
);
+<% if( hasFeature( 'envisage' ) ) {
+%>        addServlets( serve( "/envisage/*" ).with( EnvisageServlet.class ) ).to( module
);
+<% } %>
         addServlets( serve( "/*" ).with(PolygeneServerServlet.class ).withInitParams( initParams
) ).to( module );
         return module;
     }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/generator-polygene/app/templates/RestAPIApplication/Launcher.java.tmpl
----------------------------------------------------------------------
diff --git a/tools/generator-polygene/app/templates/RestAPIApplication/Launcher.java.tmpl
b/tools/generator-polygene/app/templates/RestAPIApplication/Launcher.java.tmpl
index ad03b15..ad1909f 100644
--- a/tools/generator-polygene/app/templates/RestAPIApplication/Launcher.java.tmpl
+++ b/tools/generator-polygene/app/templates/RestAPIApplication/Launcher.java.tmpl
@@ -23,29 +23,45 @@ import <%= polygene.packageName %>.bootstrap.<%= polygene.name
%>ApplicationAsse
 import <%= polygene.packageName %>.bootstrap.connectivity.ConnectivityLayer;
 import <%= polygene.packageName %>.bootstrap.connectivity.RestApiModule;
 
+import java.util.function.Consumer;
+
 import org.apache.polygene.api.structure.Application;
+import org.apache.polygene.bootstrap.ApplicationAssembly;
 import org.apache.polygene.bootstrap.AssemblyException;
 import org.apache.polygene.bootstrap.layered.LayeredApplicationAssembler;
 import org.apache.polygene.library.restlet.PolygeneRestApplicationLauncher;
+<% if( hasFeature( 'envisage' ) ) {
+%>import org.apache.polygene.tools.model.descriptor.ApplicationDetailDescriptor;
+<% } %>
 
 public class <%= polygene.name %>Launcher extends PolygeneRestApplicationLauncher
 {
     private static final String name = "<%= polygene.name %>";
     private static final String version = "0.1";
-    private Application.Mode mode = getApplicationMode();
+    private Application.Mode mode;
+    private Consumer<ApplicationAssembly> customize;
+<% if( hasFeature( 'envisage' ) ) {
+%>    private ApplicationDetailDescriptor detailedModel;
+<% } %>
 
     public static void main( String[] args )
         throws Exception
     {
-        <%= polygene.name %>Launcher app = new <%= polygene.name %>Launcher();
+        <%= polygene.name %>Launcher app = new <%= polygene.name %>Launcher(getApplicationMode(),
none -> {} );
         app.installShutdownHook();
         app.initialize();
     }
 
+    public <%= polygene.name %>Launcher(Application.Mode mode, Consumer<ApplicationAssembly>
customize)
+    {
+        this.mode = mode;
+        this.customize = customize;
+    }
+
     protected LayeredApplicationAssembler createApplicationAssembler()
         throws AssemblyException
     {
-        return new <%= polygene.name %>ApplicationAssembler( name, version, mode, none
-> {} );
+        return new <%= polygene.name %>ApplicationAssembler( name, version, mode, customize
);
     }
 
     @Override
@@ -64,7 +80,7 @@ public class <%= polygene.name %>Launcher extends PolygeneRestApplicationLaunche
         return RestApiModule.NAME;
     }
 
-    private Application.Mode getApplicationMode()
+    private static Application.Mode getApplicationMode()
     {
         String mode = System.getenv( "APP_MODE" );
         if( mode == null )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/generator-polygene/app/templates/RestAPIApplication/bootstrap-test.tmpl
----------------------------------------------------------------------
diff --git a/tools/generator-polygene/app/templates/RestAPIApplication/bootstrap-test.tmpl
b/tools/generator-polygene/app/templates/RestAPIApplication/bootstrap-test.tmpl
index a0cad7a..1e50b00 100644
--- a/tools/generator-polygene/app/templates/RestAPIApplication/bootstrap-test.tmpl
+++ b/tools/generator-polygene/app/templates/RestAPIApplication/bootstrap-test.tmpl
@@ -85,27 +85,7 @@ public class BootstrapTest
     public void applicationBootstrapSucceeds()
         throws Exception
     {
-        <%= polygene.name %>Launcher launcher = new <%= polygene.name %>Launcher()
-        {
-            @Override
-            protected LayeredApplicationAssembler createApplicationAssembler()
-                throws AssemblyException
-            {
-                return new <%= polygene.name %>ApplicationAssembler( "LaunchTest",
"0", Application.Mode.development, BootstrapTest.this::setupTest )
-                {
-                    @Override
-                    protected void onModelCreated( ApplicationDescriptor model )
-                    {
-                        ApplicationDetailDescriptor modelDescription = ApplicationDetailDescriptorBuilder.createApplicationDetailDescriptor(
model );
-                        System.out.println( "Application Model" );
-                        JsonWriter writer = Json.createWriter( System.out );
-                        System.out.println();
-                        writer.writeObject( modelDescription.toJson() );
-                        super.onModelCreated( model );
-                    }
-                };
-            }
-        };
+        <%= polygene.name %>Launcher launcher = new <%= polygene.name %>Launcher(Application.Mode.development,
this::setupTest);
         launcher.initialize();
         System.out.println("Application Launched...");
         // Thread.sleep( 3600000L );

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/model-detail/build.gradle
----------------------------------------------------------------------
diff --git a/tools/model-detail/build.gradle b/tools/model-detail/build.gradle
index c6cd684..20dbc15 100644
--- a/tools/model-detail/build.gradle
+++ b/tools/model-detail/build.gradle
@@ -26,6 +26,7 @@ jar { manifest { name = "Apache Polygeneâ„¢ Model Detail" }}
 
 dependencies {
   api polygene.core.bootstrap
+  implementation libraries.servlet_api
 
   runtimeOnly polygene.core.runtime
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/model-detail/src/main/java/org/apache/polygene/tools/model/EnvisageServlet.java
----------------------------------------------------------------------
diff --git a/tools/model-detail/src/main/java/org/apache/polygene/tools/model/EnvisageServlet.java
b/tools/model-detail/src/main/java/org/apache/polygene/tools/model/EnvisageServlet.java
new file mode 100644
index 0000000..a72d2b2
--- /dev/null
+++ b/tools/model-detail/src/main/java/org/apache/polygene/tools/model/EnvisageServlet.java
@@ -0,0 +1,113 @@
+package org.apache.polygene.tools.model;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonWriter;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.polygene.api.activation.Activation;
+import org.apache.polygene.api.activation.ActivationException;
+import org.apache.polygene.api.activation.PassivationException;
+import org.apache.polygene.api.injection.scope.Structure;
+import org.apache.polygene.api.mixin.Mixins;
+import org.apache.polygene.api.structure.ApplicationDescriptor;
+
+import static org.apache.polygene.tools.model.descriptor.ApplicationDetailDescriptorBuilder.createApplicationDetailDescriptor;
+
+@Mixins( EnvisageServlet.Mixin.class )
+public interface EnvisageServlet extends Servlet
+{
+    class Mixin extends HttpServlet
+        implements EnvisageServlet
+    {
+        private JsonObject model;
+
+        public Mixin( @Structure ApplicationDescriptor descriptor )
+        {
+            model = createApplicationDetailDescriptor( descriptor ).toJson();
+        }
+
+        @Override
+        protected void doGet( HttpServletRequest req, HttpServletResponse resp )
+            throws ServletException, IOException
+        {
+            String pathInfo = req.getPathInfo();
+            log( "Fetch " + pathInfo );
+            if( isStatic( pathInfo ) )
+            {
+                serviceStatic( pathInfo, resp );
+            }
+            if( isJson( pathInfo ) )
+            {
+                serviceJson( resp );
+            }
+        }
+
+        private boolean isStatic( String pathInfo )
+        {
+            return (pathInfo.equals( "/index.html" )
+                   || pathInfo.startsWith( "/js/" )
+                   || pathInfo.startsWith( "/css/" )
+                   || pathInfo.startsWith( "/images/" ) )
+                   && !pathInfo.contains( ".." )
+                ;
+        }
+
+        private boolean isJson( String pathInfo )
+        {
+            return pathInfo.equals( "/model/" );
+        }
+
+        private void serviceStatic( String pathInfo, HttpServletResponse resp )
+            throws IOException
+        {
+            ServletOutputStream out = resp.getOutputStream();
+            try( InputStream resource = getClass().getClassLoader().getResourceAsStream(
pathInfo ) )
+            {
+                if( resource == null )
+                {
+                    resp.setStatus( HttpServletResponse.SC_NOT_FOUND );
+                }
+                else
+                {
+                    copy( resource, out );
+                }
+            }
+        }
+
+        private void serviceJson( HttpServletResponse resp )
+            throws IOException
+        {
+            if( model == null )
+            {
+                resp.setStatus( HttpServletResponse.SC_NO_CONTENT );
+            }
+            else
+            {
+                PrintWriter out = resp.getWriter();
+                JsonWriter writer = Json.createWriter( out );
+                writer.writeObject( model );
+                writer.close();
+                out.flush();
+            }
+        }
+
+        private void copy( InputStream resource, ServletOutputStream out )
+            throws IOException
+        {
+            int b;
+            while( ( b = resource.read() ) >= 0 )
+            {
+                out.write( b );
+            }
+            out.flush();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/ApplicationDetailDescriptor.java
----------------------------------------------------------------------
diff --git a/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/ApplicationDetailDescriptor.java
b/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/ApplicationDetailDescriptor.java
index e23247f..d6c18fd 100644
--- a/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/ApplicationDetailDescriptor.java
+++ b/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/ApplicationDetailDescriptor.java
@@ -19,9 +19,12 @@
  */
 package org.apache.polygene.tools.model.descriptor;
 
+import java.util.Comparator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Objects;
+import java.util.SortedSet;
+import java.util.TreeSet;
 import javax.json.Json;
 import javax.json.JsonArrayBuilder;
 import javax.json.JsonObject;
@@ -41,13 +44,14 @@ public final class ApplicationDetailDescriptor
 {
     private final ApplicationDescriptor descriptor;
     private final List<ActivatorDetailDescriptor> activators = new LinkedList<>();
-    private final List<LayerDetailDescriptor> layers = new LinkedList<>();
+    private final SortedSet<LayerDetailDescriptor> layers;
 
     ApplicationDetailDescriptor( ApplicationDescriptor descriptor )
         throws IllegalArgumentException
     {
         Objects.requireNonNull( descriptor, "ApplicationDescriptor" );
         this.descriptor = descriptor;
+        layers = new TreeSet<>( new UsedLayerComparator() );
     }
 
     /**
@@ -81,6 +85,7 @@ public final class ApplicationDetailDescriptor
 
     final void addLayer( LayerDetailDescriptor descriptor )
     {
+        System.out.println("NICLAS!!!! Layer:" + descriptor.toString());
         Objects.requireNonNull( descriptor, "LayerDetailDescriptor" );
         descriptor.setApplication( this );
         layers.add( descriptor );
@@ -136,4 +141,49 @@ public final class ApplicationDetailDescriptor
 
         return appBuilder.build();
     }
+
+    private static class UsedLayerComparator
+        implements Comparator<LayerDetailDescriptor>
+    {
+
+        @Override
+        public int compare( LayerDetailDescriptor d1, LayerDetailDescriptor d2 )
+        {
+            if( d1.equals( d2 ))
+            {
+                return 0;
+            }
+            if( uses( d1, d2 ) )
+            {
+                return -1;
+            }
+            if( uses(d2, d1) )
+            {
+                return 1;
+            }
+            return 0;
+        }
+
+        // 0 = same layer
+        // 1 = user uses used
+        // 2 = not determination
+        private boolean uses( LayerDetailDescriptor user, LayerDetailDescriptor used )
+        {
+            System.out.println("Compare " + user.usedLayers() + " : " + used.usedLayers());
+            System.out.println("Compare " + user.usedBy() + " : " + used.usedBy());
+            System.out.println("---");
+            if( user.equals( used ))
+            {
+                return true;
+            }
+            for( LayerDetailDescriptor usedLayer : user.usedLayers() )
+            {
+                if( uses( usedLayer, used ) )
+                {
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/LayerDetailDescriptor.java
----------------------------------------------------------------------
diff --git a/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/LayerDetailDescriptor.java
b/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/LayerDetailDescriptor.java
index d96f12d..ac4d4b5 100644
--- a/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/LayerDetailDescriptor.java
+++ b/tools/model-detail/src/main/java/org/apache/polygene/tools/model/descriptor/LayerDetailDescriptor.java
@@ -160,6 +160,29 @@ public final class LayerDetailDescriptor
     }
 
     @Override
+    public boolean equals( Object o )
+    {
+        if( this == o )
+        {
+            return true;
+        }
+        if( o == null || getClass() != o.getClass() )
+        {
+            return false;
+        }
+
+        LayerDetailDescriptor that = (LayerDetailDescriptor) o;
+
+        return descriptor.equals( that.descriptor );
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return descriptor.hashCode();
+    }
+
+    @Override
     public final String toString()
     {
         return descriptor.name();

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/2bef7120/tools/model-detail/src/main/resources/index.html
----------------------------------------------------------------------
diff --git a/tools/model-detail/src/main/resources/index.html b/tools/model-detail/src/main/resources/index.html
new file mode 100644
index 0000000..486feab
--- /dev/null
+++ b/tools/model-detail/src/main/resources/index.html
@@ -0,0 +1,11 @@
+<html>
+<body>
+<h1>Envisage</h1>
+<div class="header"></div>
+<div class="menu"></div>
+<div class="type-tree"></div>
+<div class="app-structure"></div>
+<div class="details"></div>
+<div class="footer"></div>
+</body>
+</html>
\ No newline at end of file


Mime
View raw message