GWT

This section describes the core Jetty and Servlet layers that are used in the StrataCode web framework.

Jetty and Servlets

Jetty is a lightweight servlet engine exposed in StrataCode as a set of layers in the jetty layer group. To use jetty with servlets, extend the jetty.servlet layer. There's also a servlet.schtml layer which defines a new extension "schtml" which compiles StrataCode templates into servlets and adds them automatically to web.xml.

See the examples layer called servletHelloWorld:

public example.servletHelloWorld extends jetty.servlet, servlet.schtml {

}

When you extend the jetty.servlet layer, your layer's top level directory can contain web/WEB-INF/web.xml. When you specify this file, it gets put into your build directory. When it is not specified, a default one is generated. This happens because a previous layer defines a web.scxml file - a dynamically generated web.xml. These files the normal rules for layering: a web.scxml file can override a web.xml file and vice versa.

If you want to add a servlet at a specific path, you can use the @PathServlet annotation on any servlet:

file: example/servletHelloWorld/HelloWorld.sc
import java.io.IOException;

@PathServlet(path="/hello")
class HelloWorld extends HttpServlet {
   void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
      response.getOutputStream().print("<body>Hello world</body>");
   }
}
Or add a schtml file - it will be registered in web.xml using a path based on its generated type name:
file: example/servletHelloWorld/Hello.schtml
<%! class Hello {
    String message = "Hello World";
} %>
<html><body><%= message %></body></html>

In this case, you request the servlet with http://localhost:8080/example/servletHelloWorld.

Now, in another layer you can extend that layer but change the message property:

file: example/extendedHelloWorld/Hello.schtml
<%! Hello { 
  message = "Hello StrataCode";
} %>
<%= super.output() %>
or do the same thing from a StrataCode file instead of a schtml file:
file: example/extendedHelloWorld2/Hello.sc
Hello {
   message = "Hello StrataCode";
}

The web.xml file is generated by adding tags for all servlets, servlet filters, and template pages in the application. When you extend the wicket layer, wicket applications are further inserted into the web.xml file.

This is accomplished by layering of web.scxml files. First in the servlet/webApp layer, the template language is used to define a structured template matching the web.xml file structure of servlets, servlet-mappings, and servlet-filters, filter-mappings.

file: servlet/webApp/web/WEB-INF/web.scxml
<!DOCTYPE web-app
          PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd">

<%! 
   public String displayName = "Servlets on StrataCode";


   public StringBuilder outputServletTags() { 
      StringBuilder out = new StringBuilder();
      sc.layer.LayeredSystem system = sc.layer.LayeredSystem.getCurrent();
      sc.layer.BuildInfo bi = system.currentBuildLayer.buildInfo;
      java.util.List<sc.layer.TypeGroupMember> servlets = bi.getTypeGroupMembers("servlets");

      for (sc.layer.TypeGroupMember servlet:servlets) { %>
        <servlet>
          <servlet-name><%= servlet.typeName %></servlet-name>
          <servlet-class><%= servlet.typeName %></servlet-class>
        </servlet>
      <% } 
      return out;
   }

   public StringBuilder outputServletMappingTags() { 
      StringBuilder out = new StringBuilder();
      sc.layer.LayeredSystem system = sc.layer.LayeredSystem.getCurrent();
      sc.layer.BuildInfo bi = system.currentBuildLayer.buildInfo;
      java.util.List<sc.layer.TypeGroupMember> servlets = bi.getTypeGroupMembers("servlets");

      for (sc.layer.TypeGroupMember servlet:servlets) { %>
        <servlet-mapping>
           <servlet-name><%= servlet.typeName %></servlet-name>
           <url-pattern><%= servlet.getAnnotationValue("sc.servlet.PathServlet","path") %></url-pattern>
        </servlet-mapping>
      <% } 
      return out;
   }

   public StringBuilder outputServletFilterTags() { 
      StringBuilder out = new StringBuilder();
      sc.layer.LayeredSystem system = sc.layer.LayeredSystem.getCurrent();
      sc.layer.BuildInfo bi = system.currentBuildLayer.buildInfo;
      java.util.List<sc.layer.TypeGroupMember> filters = bi.getTypeGroupMembers("servletFilters");

      for (sc.layer.TypeGroupMember filter:filters) { %>
         <filter>
              <filter-name><%=filter.typeName %></filter-name>
              <filter-class><%=filter.typeName%></filter-class>
         </filter>
      <% }
      return out;
   }

   public StringBuilder outputServletFilterMappingTags() { 
      StringBuilder out = new StringBuilder();
      sc.layer.LayeredSystem system = sc.layer.LayeredSystem.getCurrent();
      sc.layer.BuildInfo bi = system.currentBuildLayer.buildInfo;
      java.util.List<sc.layer.TypeGroupMember> filters = bi.getTypeGroupMembers("servletFilters");

      for (sc.layer.TypeGroupMember filter:filters) { %>
         <filter-mapping>
              <filter-name><%=filter.typeName %></filter-name>
              <url-pattern><%= filter.getAnnotationValue("sc.servlet.PathServletFilter", "path")%></url-pattern>
         </filter-mapping>
      <% }
      return out;
   }

   public StringBuilder outputListeners() { 
      return new StringBuilder();
   }
%>
   
<web-app>
  <display-name><%= displayName %></display-name>

  <%= outputServletFilterTags() %>
  <%= outputServletFilterMappingTags() %>
  <%= outputListeners() %>
  <%= outputServletTags() %>
  <%= outputServletMappingTags() %>

</web-app>
Wicket needs to add its own servlet filters to the list. It defines its own template which modifies the web component, overriding the two methods used to add a new servlet filter:
file: wicket/core/web/WEB-INF/web.scxml
<%! web {
   public StringBuilder outputServletFilterTags() {
      StringBuilder out = super.outputServletFilterTags();

      sc.layer.LayeredSystem system = sc.layer.LayeredSystem.getCurrent();
      sc.layer.BuildInfo bi = system.currentBuildLayer.buildInfo;
      java.util.List<sc.layer.TypeGroupMember> wicketApps = bi.getTypeGroupMembers("wicketApplications");

      for (sc.layer.TypeGroupMember wicketApp:wicketApps) { %>
        <filter>
           <filter-name><%= wicketApp.typeName%></filter-name>
           <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
           <init-param>
              <param-name>applicationClassName</param-name>
              <param-value><%= wicketApp.typeName%></param-value>
           </init-param>
        </filter>
      <% }
      return out;
   }

   public StringBuilder outputServletFilterMappingTags() {
      StringBuilder out = super.outputServletFilterMappingTags();

      sc.layer.LayeredSystem system = sc.layer.LayeredSystem.getCurrent();
      sc.layer.BuildInfo bi = system.currentBuildLayer.buildInfo;
      java.util.List<sc.layer.TypeGroupMember> wicketApps = bi.getTypeGroupMembers("wicketApplications");

      for (sc.layer.TypeGroupMember wicketApp:wicketApps) { %>
         <filter-mapping>
            <filter-name><%= wicketApp.typeName%></filter-name>
            <url-pattern><%= wicketApp.getAnnotationValue("sc.wicket.ApplicationPath")%></url-pattern>
         </filter-mapping>
      <% }
      return out;
   }
} %>

<%= super.output() %>

The ability to gather a list of all the objects with a specific annotation or that implement a class is built into the StrataCode build system - accessible to template files at build time. In these templates, you can use various utility libraries to get annotation values, or introspect the types so that this code generation is as informed as necessary. This is all done in Java by framework developers. This work helps keep framework details out of application layers. But since it's using StrataCode code, its easy to use these templates to trace down problems when things go wrong. All project definition files are also StrataCode so when an annotation or other construct causes an error, you can trace those errors as well.

Configuring Jetty

When you extend the jetty/serve layer, you get a default Jetty configuration of a CServer component defined as:

file: jetty/serve/CServer.sc
object CServer extends Server {
   object httpConnector extends ServerConnector {
      // TODO: should this value be derived from system.URL at some point?
      port = 8080;
   }
   
   object handlerList extends HandlerList {
      object resourceHandler extends ResourceHandler {
         welcomeFiles = {"index.html"};
         // Run the server from the web subdirectory. 
         resourceBase = "./web";
      }
      object defaultHandler extends DefaultHandler {
      }
   }

   boolean sync = false;
   
   @sc.obj.MainSettings(produceScript = true, execName = "startSCJetty", debug = false)
   static void main(String[] args) throws Exception {
      CServer s = CServer;
      if (s.sync)
         s.join();
   }
}
To change the port just extend this layer and override the httpConnector.port property:
file: jetty/examp/CServer.sc
CServer {
   httpConnector {
      port = 8080;
   }
   handlerList {
      webAppHandler {
         contextPath = "/";
         war="./web";
      }
   }
}

JUnit

Extend the junit.main layer. Add the normal @Test annotations for junit. To run your tests, use:

scc yourLayer -t "<testclasspattern>"

for example:

scc test/GetSet -t "sc.*"

or just use -ta to run all tests:

scc test/GetSet -ta

StrataCode provides facilities to gather all tests registered. In this case, junit registers itself as a test provider when you include the junit layer. It's annotation processor for Test accumulates test objects at build time. This index is used to make running all tests easy - a one step compile and run process.