[seam-commits] Seam SVN: r11480 - in branches/community/Seam_2_2/src: flex and 5 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Sep 11 15:39:06 EDT 2009


Author: norman.richards at jboss.com
Date: 2009-09-11 15:39:06 -0400 (Fri, 11 Sep 2009)
New Revision: 11480

Added:
   branches/community/Seam_2_2/src/flex/
   branches/community/Seam_2_2/src/flex/META-INF/
   branches/community/Seam_2_2/src/flex/META-INF/seam-deployment.properties
   branches/community/Seam_2_2/src/flex/org/
   branches/community/Seam_2_2/src/flex/org/jboss/
   branches/community/Seam_2_2/src/flex/org/jboss/seam/
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexAuthenticationBridge.java
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexFilter.java
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexRemote.java
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexSeamFactory.java
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/MessageBrokerManager.java
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/Remote.java
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/SeamAdapter.java
   branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/package-info.java
   branches/community/Seam_2_2/src/flex/seam.properties
Log:
flex support

Added: branches/community/Seam_2_2/src/flex/META-INF/seam-deployment.properties
===================================================================
--- branches/community/Seam_2_2/src/flex/META-INF/seam-deployment.properties	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/META-INF/seam-deployment.properties	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1 @@
+org.jboss.seam.deployment.annotationTypes=org.jboss.seam.flex.FlexRemote
\ No newline at end of file

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexAuthenticationBridge.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexAuthenticationBridge.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexAuthenticationBridge.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,37 @@
+package org.jboss.seam.flex;
+
+import org.jboss.seam.annotations.In;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.security.Credentials;
+import org.jboss.seam.security.Identity;
+
+
+ at Name("org.jboss.seam.flex.login")
+ at Install(false)
+ at FlexRemote(name="login")
+public class FlexAuthenticationBridge
+{
+   private static final LogProvider log = Logging.getLogProvider(FlexAuthenticationBridge.class);
+
+     @In Identity identity;
+     @In Credentials credentials;
+     
+     public String login(String username, String password) {
+         log.info("*LOGIN " + username + " " + password);
+         credentials.setUsername(username);
+         credentials.setPassword(password);
+         
+         String result = identity.login();
+         
+         log.info("*LOGIN RESULT " + result);
+         return result;
+     }
+     
+     public void logout() {
+         log.info("*LOGOUT ");
+         identity.logout();  
+     }
+}

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexFilter.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexFilter.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexFilter.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,130 @@
+package org.jboss.seam.flex;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.*;
+import java.util.*;
+
+import org.jboss.seam.*;
+
+import org.jboss.seam.annotations.*;
+import org.jboss.seam.annotations.intercept.*;
+import org.jboss.seam.deployment.AnnotationDeploymentHandler;
+import org.jboss.seam.deployment.DeploymentStrategy;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.servlet.ContextualHttpServletRequest;
+import org.jboss.seam.web.AbstractFilter;
+
+ at Scope(ScopeType.APPLICATION)
+ at Name("org.jboss.seam.flex.flexFilter")
+ at Startup
+ at Install(precedence=Install.BUILT_IN)
+ at BypassInterceptors
+ at org.jboss.seam.annotations.web.Filter //within={"org.jboss.seam.????"}
+public class FlexFilter 
+    extends AbstractFilter
+{
+   private static final LogProvider log = Logging.getLogProvider(FlexFilter.class);
+
+   MessageBrokerManager messageBrokerManager;
+   List<Class<?>> scanned = new ArrayList<Class<?>>();
+   
+   private AnnotationDeploymentHandler annotationDeploymentHandler() {
+      DeploymentStrategy deployment = (DeploymentStrategy) Component.getInstance("deploymentStrategy");                   
+      
+      if (deployment != null) {
+          return (AnnotationDeploymentHandler) 
+             deployment.getDeploymentHandlers().get(AnnotationDeploymentHandler.NAME);
+      }
+      
+      return null;
+   }
+   
+   private Collection<Class<?>> scannedClasses() {
+      Collection<Class<?>> result = null;
+      
+      AnnotationDeploymentHandler handler = annotationDeploymentHandler();
+      if (handler !=null) {
+         result = handler.getClassMap().get(FlexRemote.class.getName());
+      }
+      
+      return result != null ? result : new ArrayList<Class<?>>(0);
+   }
+   
+   @Create
+   public void seamInit() {
+      // deployment handler only knows about scanned classes during startup
+      // so we need to get them now and save them
+      scanned.addAll(scannedClasses());
+   }
+   
+   @Override
+   public void init(FilterConfig filterConfig) 
+      throws ServletException
+   {
+      super.init(filterConfig);
+      
+      messageBrokerManager = new MessageBrokerManager();
+      messageBrokerManager.init(new FlexServletConfig(filterConfig.getServletContext()));      
+      messageBrokerManager.addDestinations(scanned);
+   }
+   
+
+   public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain) 
+       throws IOException, ServletException
+   {
+      if (isMappedToCurrentRequestPath(request)) {         
+         new ContextualHttpServletRequest((HttpServletRequest) request)
+         {
+            @Override
+            public void process() 
+                throws ServletException, IOException
+            {
+               messageBrokerManager.service((HttpServletRequest)request,
+                               (HttpServletResponse)response);
+            }
+         }.run();
+      } else { 
+         chain.doFilter(request, response);
+      }
+   }
+   
+   
+   private static class FlexServletConfig 
+       implements ServletConfig 
+   {
+      Map<String,String> params;
+      ServletContext context;
+      
+      public FlexServletConfig(ServletContext context) {
+         this(context, null);
+      }
+      
+      public FlexServletConfig(ServletContext context, Map<String,String> params) {
+         this.context = context;
+         this.params = (params!=null) ? params : new HashMap<String,String>();
+      }
+      
+      public ServletContext getServletContext() {
+         return context;
+      }
+      
+      public String getServletName() {
+         return "FlexServlet";
+      }
+      
+      public String getInitParameter(String param) {
+         log.info("*** FSC getInitParam " + param);
+         return params.get(param);
+      }
+      
+      @SuppressWarnings("unchecked")        
+      public Enumeration getInitParameterNames() {
+         return Collections.enumeration(params.keySet());
+      }
+      
+   }
+}
\ No newline at end of file

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexRemote.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexRemote.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexRemote.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,16 @@
+package org.jboss.seam.flex;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+ at Target(ElementType.TYPE)
+ at Documented
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface FlexRemote
+{
+    abstract String name();
+}

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexSeamFactory.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexSeamFactory.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/FlexSeamFactory.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,45 @@
+package org.jboss.seam.flex;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+import flex.messaging.FlexFactory;
+import flex.messaging.FactoryInstance;
+import flex.messaging.config.ConfigMap;
+
+
+public class FlexSeamFactory 
+    implements FlexFactory
+{  
+   private static final LogProvider log = Logging.getLogProvider(FlexSeamFactory.class);
+   
+   String destinationName;
+   String componentName;
+   
+   public FlexSeamFactory(String destinationName, String componentName) {
+      this.componentName = componentName;
+      this.destinationName = destinationName;
+   }
+   
+   public void initialize(String id, ConfigMap configMap) {
+      log.info("!FSF init " + id + " props=" + configMap);
+   }
+   
+   public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
+      log.info("!FSF create factory " + id + " props=" + properties);
+      return new FactoryInstance(this, id, properties);
+   }
+   
+   public Object lookup(FactoryInstance factory) {  
+      log.info("!FSF lookup " + factory);
+      
+      try {
+          Object instance = Component.getInstance(componentName, true);      
+          return instance;
+      } catch (Exception e) {
+          log.error(e);
+          return null;
+      }
+   }
+}

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/MessageBrokerManager.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/MessageBrokerManager.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/MessageBrokerManager.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,246 @@
+package org.jboss.seam.flex;
+
+import java.io.*;
+import java.lang.reflect.Method;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.Collection;
+
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import flex.messaging.*;
+import flex.messaging.config.*;
+import flex.messaging.endpoints.Endpoint;
+import flex.messaging.log.ServletLogTarget;
+import flex.messaging.services.RemotingService;
+import flex.messaging.services.remoting.*;
+
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.util.Reflections;
+import org.jboss.seam.util.Resources;
+
+
+public class MessageBrokerManager
+{
+   private static final LogProvider log = Logging.getLogProvider(MessageBrokerManager.class);
+
+   private static String FLEXDIR = "/WEB-INF/flex/";
+
+   private flex.messaging.MessageBroker broker;
+   
+   private ServletConfig servletConfig;
+   
+   
+   public void init(ServletConfig servletConfig)
+      throws ServletException
+   {
+      this.servletConfig = servletConfig;
+      
+      // allocate thread local variables
+      createThreadLocals();
+      
+      try
+      {
+         FlexContext.setThreadLocalObjects(null, null, null, null, null, servletConfig);         
+         ServletLogTarget.setServletContext(servletConfig.getServletContext());
+                 
+         FlexConfigurationManager configManager = new FlexConfigurationManager();
+         MessagingConfiguration config = configManager.getMessagingConfiguration(servletConfig);
+         
+         config.createLogAndTargets();         
+         broker = config.createBroker(servletConfig.getInitParameter("messageBrokerId"), 
+                                      Thread.currentThread().getContextClassLoader());
+         
+         // Set the servlet config as thread local
+         FlexContext.setThreadLocalObjects(null, null, broker, null, null, servletConfig);
+         
+         setupInternalPathResolver(servletConfig.getServletContext());
+         setInitServletContext(broker, servletConfig.getServletContext());
+         
+         // Create endpoints, services, security, and logger on the broker based on configuration
+         config.configureBroker(broker);
+         
+         //initialize the httpSessionToFlexSessionMap
+         synchronized(HttpFlexSession.mapLock)
+         {
+            if (servletConfig.getServletContext().getAttribute(HttpFlexSession.SESSION_MAP) == null) {
+               servletConfig.getServletContext().setAttribute(HttpFlexSession.SESSION_MAP, new ConcurrentHashMap());
+            }
+         }
+         
+         broker.start();
+         
+         configManager.reportTokens();
+         config.reportUnusedProperties();
+         
+         // clear the broker and servlet config as this thread is done
+         FlexContext.clearThreadLocalObjects();
+      
+      } catch (Throwable t){
+         log.error("MessageBrokerServlet failed to initialize due to runtime exception");
+         destroy();
+         throw new ServletException(t);
+      }
+   }
+   
+   
+   private void setInitServletContext(flex.messaging.MessageBroker broker, ServletContext ctx)    
+      throws Exception
+   {
+      Method setMethod = flex.messaging.MessageBroker.class.
+      getDeclaredMethod("setInitServletContext", ServletContext.class);
+      setMethod.setAccessible(true);
+      Reflections.invoke(setMethod, broker, ctx);
+   }
+   
+   private void setupInternalPathResolver(final ServletContext servletContext)
+   {
+      broker.setInternalPathResolver(
+            new flex.messaging.MessageBroker.InternalPathResolver()
+            {
+               public InputStream resolve(String filename)
+               {
+                  log.info("internal path resolver " + filename);
+                  return Resources.getResourceAsStream(FLEXDIR + filename, servletContext);
+               }
+            }
+      );
+   }
+   
+   public void destroy()
+   {
+      if (broker != null) {
+         broker.stop();
+         // release static thread locals
+         destroyThreadLocals();
+      }
+   }
+   
+   public void service(HttpServletRequest req, HttpServletResponse res)
+   {
+      log.info("=========== START FLEX REQUEST");
+      try {
+         broker.initThreadLocals();
+         
+         FlexContext.setThreadLocalObjects(null, null, broker, req, res, servletConfig);
+         
+         // necessary to create for later
+         HttpFlexSession fs = HttpFlexSession.getFlexSession(req);
+         log.info("flex session is " + fs);
+         
+         Endpoint endpoint = findEndpoint(req, res);            
+         log.info("Endpoint: " + endpoint.describeEndpoint());
+         
+         endpoint.service(req, res);
+      } catch (UnsupportedOperationException ue) {
+         sendError(res);
+      } finally {
+         FlexContext.clearThreadLocalObjects();
+      }
+   }
+   
+   
+   private void sendError(HttpServletResponse res)
+   {
+      if (!res.isCommitted()) {
+         try {                        
+            res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+         } catch (IOException ignored) {
+            
+         }
+      }
+   }
+   
+   
+   private Endpoint findEndpoint(HttpServletRequest req, HttpServletResponse res)
+   {
+      String contextPath = req.getContextPath();
+      String pathInfo = req.getPathInfo();
+      String endpointPath = req.getServletPath();
+      if (pathInfo != null) {
+         endpointPath = endpointPath + pathInfo;
+      }
+      
+      log.info("flex request for cp=" + contextPath + " ep=" + endpointPath);
+      try {
+         return broker.getEndpoint(endpointPath, contextPath);
+      } catch (MessageException me) {
+         if (!res.isCommitted()) {
+            try {                    
+               res.sendError(HttpServletResponse.SC_NOT_FOUND);
+            } catch (IOException ignore) {
+               // ignore
+            }
+         }                
+         return null;
+      }
+   }
+   
+   
+   // Call ONLY on servlet startup
+   public void createThreadLocals()
+   {
+      // allocate static thread local objects
+      flex.messaging.MessageBroker.createThreadLocalObjects();
+      FlexContext.createThreadLocalObjects();
+      flex.messaging.io.SerializationContext.createThreadLocalObjects();
+      flex.messaging.io.TypeMarshallingContext.createThreadLocalObjects();
+   }
+   
+   protected void destroyThreadLocals()
+   {
+      // Destroy static thread local objects
+      flex.messaging.MessageBroker.releaseThreadLocalObjects();
+      FlexContext.releaseThreadLocalObjects();
+      flex.messaging.io.SerializationContext.releaseThreadLocalObjects();
+      flex.messaging.io.TypeMarshallingContext.releaseThreadLocalObjects();
+   }
+   
+   
+   private RemotingService findRemotingService() {
+      return (RemotingService) broker.getServiceByType(RemotingService.class.getName());
+   }
+   
+   private void registerSeamAdapter(RemotingService remotingService) {
+      if (remotingService.getRegisteredAdapters().get(SeamAdapter.SEAM_ADAPTER_ID) == null) {
+         remotingService.registerAdapter(SeamAdapter.SEAM_ADAPTER_ID,SeamAdapter.class.getName());
+      }
+   }
+   
+   private Destination createDestination(String destinationName, String componentName) {           
+      RemotingService remotingService = findRemotingService();
+      
+      RemotingDestination destination = 
+         (RemotingDestination) remotingService.createDestination(destinationName);
+      
+      destination.setFactory(new FlexSeamFactory(destinationName, componentName));
+      
+      
+      // configure adapter       
+      registerSeamAdapter(remotingService);
+      destination.createAdapter(SeamAdapter.SEAM_ADAPTER_ID);
+      
+      // configure channel?
+      //System.out.println("-channels " + destination.getChannels());
+      
+      return destination;
+   }
+   
+   public void addDestinations(Collection<Class<?>> destinations) {                            
+      for (Class<?> annotatedClass: destinations) {
+         log.info("Adding scanned flex desitionation for class " + annotatedClass);
+         FlexRemote fr = annotatedClass.getAnnotation(FlexRemote.class);
+         
+         Name name = annotatedClass.getAnnotation(Name.class);
+         
+         String destinationName = fr.name();
+         String componentName = name.value();
+         Destination destination = createDestination(destinationName, componentName);
+         
+         destination.start();   
+      }
+   }
+   
+}

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/Remote.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/Remote.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/Remote.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,41 @@
+package org.jboss.seam.flex;
+
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Startup;
+
+ at Name("org.jboss.seam.flex.remote")
+ at Startup
+ at Install(false)
+ at Scope(ScopeType.APPLICATION)
+public class Remote 
+{
+   String destinationName;
+   String componentName;
+   
+   public void setDestinationName(String destionationName) {
+      this.destinationName = destionationName;
+   }
+   
+   public String getDestinationName() {
+      return destinationName;
+   }
+   
+   public void setComponentName(String componentName) {
+      this.componentName = componentName;
+   }
+   
+   public String getComponentName() {
+      return componentName;
+   }
+   
+   
+   @Create
+   public void init() {
+      //System.out.println("** remoting destination " + destinationName + " for " + componentName);
+   }
+
+}

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/SeamAdapter.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/SeamAdapter.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/SeamAdapter.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,27 @@
+package org.jboss.seam.flex;
+
+
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+import flex.messaging.services.remoting.adapters.JavaAdapter;
+import flex.messaging.messages.Message;
+
+/**
+ * The Seam adaptor should translate seam exceptions and do any other additional 
+ * management needed  
+ */
+public class SeamAdapter
+    extends JavaAdapter
+{
+   public static final String SEAM_ADAPTER_ID = "seam-adapter";
+   
+   private static final LogProvider log = Logging.getLogProvider(SeamAdapter.class);
+
+   @Override
+   public Object invoke(Message message) {
+      log.info("SeamAdapter: " + message);
+      return super.invoke(message);      
+   }
+   
+}

Added: branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/package-info.java
===================================================================
--- branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/package-info.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/flex/org/jboss/seam/flex/package-info.java	2009-09-11 19:39:06 UTC (rev 11480)
@@ -0,0 +1,7 @@
+ at Namespace(value="http://jboss.com/products/seam/flex", prefix="org.jboss.seam.flex")
+ at AutoCreate
+package org.jboss.seam.flex;
+
+import org.jboss.seam.annotations.AutoCreate;
+import org.jboss.seam.annotations.Namespace;
+

Added: branches/community/Seam_2_2/src/flex/seam.properties
===================================================================



More information about the seam-commits mailing list