[jboss-cvs] JBossAS SVN: r90928 - in projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher: map and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jul 8 08:00:05 EDT 2009


Author: alesj
Date: 2009-07-08 08:00:05 -0400 (Wed, 08 Jul 2009)
New Revision: 90928

Added:
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/DefaultGraphCreatorMapper.java
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/GraphCreatorMapper.java
Modified:
   projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java
Log:
Move graph creation mapping to interface.

Modified: projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java
===================================================================
--- projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java	2009-07-08 11:56:32 UTC (rev 90927)
+++ projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/GrapherServlet.java	2009-07-08 12:00:05 UTC (rev 90928)
@@ -35,9 +35,9 @@
 import org.jboss.kernel.spi.dependency.KernelController;
 import org.jboss.logging.Logger;
 import org.jboss.mc.servlet.vdf.api.KernelControllerVDFConnector;
-import org.jboss.mctools.grapher.graph.AllGraphCreator;
-import org.jboss.mctools.grapher.graph.BeanGraphCreator;
 import org.jboss.mctools.grapher.graph.GraphCreator;
+import org.jboss.mctools.grapher.map.GraphCreatorMapper;
+import org.jboss.mctools.grapher.map.DefaultGraphCreatorMapper;
 import org.jboss.mctools.grapher.render.GIFRenderer;
 import org.jboss.mctools.grapher.render.JPGRenderer;
 import org.jboss.mctools.grapher.render.Renderer;
@@ -61,6 +61,7 @@
    private Logger log = Logger.getLogger(getClass());
 
    private KernelController controller;
+   private GraphCreatorMapper mapper;
 
    private static final String DEFAULT_FORMAT = "gif";
    /**
@@ -68,7 +69,6 @@
     */
    private Renderer renderer;
    private Map<String, Renderer> renderers;
-   private GraphCreator ALL = new AllGraphCreator();
 
    @Override
    public void init(ServletConfig config) throws ServletException
@@ -80,17 +80,21 @@
 
       controller = connector.getUtility();
 
+      mapper = createInstance(config.getInitParameter("mapper"), new DefaultGraphCreatorMapper(), GraphCreatorMapper.class);
+
       renderers = new HashMap<String, Renderer>();
       renderers.put("svg", new SVGRenderer());
       renderers.put("jpg", new JPGRenderer());
       renderers.put("gif", new GIFRenderer());
 
+      // do we have short extension
       String defaultRenderer = config.getInitParameter("renderer");
       if (defaultRenderer != null)
          renderer = renderers.get(defaultRenderer);
 
+      // or is this a new impl
       if (renderer == null)
-         renderer = renderers.get(DEFAULT_FORMAT);
+         renderer = createInstance(defaultRenderer, renderers.get(DEFAULT_FORMAT), Renderer.class);
    }
 
    /**
@@ -121,12 +125,7 @@
     */
    protected void doRenderGraph(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
-      String bean = request.getParameter("bean");
-      GraphCreator creator;
-      if (bean != null)
-         creator = new BeanGraphCreator(bean);
-      else
-         creator = ALL;
+      GraphCreator creator = mapGraphCreator(request);
 
       //if (log.isTraceEnabled())
          log.info("Invoking creator: " + creator);
@@ -147,6 +146,17 @@
       }
    }
 
+   /**
+    * Map graph creator from request.
+    *
+    * @param request the curent http request
+    * @return graph creator instance
+    */
+   protected GraphCreator mapGraphCreator(HttpServletRequest request)
+   {
+      return mapper.mapFromRequest(request);
+   }
+
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
@@ -158,4 +168,31 @@
    {
       doRenderGraph(request, response);
    }
-}
+
+   /**
+    * Create new instance.
+    *
+    * @param reference the class name reference
+    * @param defaultInstance the default instance
+    * @param expectedClass the expected class
+    * @param <T> the expected type
+    * @return new instance or default for any error
+    */
+   protected <T> T createInstance(String reference, T defaultInstance, Class<T> expectedClass)
+   {
+      if (reference == null)
+         return defaultInstance;
+
+      try
+      {
+         Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(reference);
+         Object object = clazz.newInstance();
+         return expectedClass.cast(object);
+      }
+      catch (Exception e)
+      {
+         log.warn("Exception instantiating new " + expectedClass.getName() + " instance: " + e);
+      }
+      return defaultInstance;
+   }
+ }

Added: projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/DefaultGraphCreatorMapper.java
===================================================================
--- projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/DefaultGraphCreatorMapper.java	                        (rev 0)
+++ projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/DefaultGraphCreatorMapper.java	2009-07-08 12:00:05 UTC (rev 90928)
@@ -0,0 +1,44 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.mctools.grapher.map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.jboss.mctools.grapher.graph.GraphCreator;
+import org.jboss.mctools.grapher.graph.BeanGraphCreator;
+
+/**
+ * Default graph creator mapper.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class DefaultGraphCreatorMapper implements GraphCreatorMapper
+{
+   public GraphCreator mapFromRequest(HttpServletRequest request)
+   {
+      String bean = request.getParameter("bean");
+      if (bean != null)
+         return new BeanGraphCreator(bean);
+
+      return ALL;
+   }
+}

Added: projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/GraphCreatorMapper.java
===================================================================
--- projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/GraphCreatorMapper.java	                        (rev 0)
+++ projects/mc-tools/grapher/trunk/src/main/java/org/jboss/mctools/grapher/map/GraphCreatorMapper.java	2009-07-08 12:00:05 UTC (rev 90928)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.mctools.grapher.map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.jboss.mctools.grapher.graph.GraphCreator;
+import org.jboss.mctools.grapher.graph.AllGraphCreator;
+
+/**
+ * Map servlet request to GraphCreator instance.
+ * 
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface GraphCreatorMapper
+{
+   /** The default grapf creator */
+   static final GraphCreator ALL = new AllGraphCreator();
+
+   /**
+    * Map graph creator from request. 
+    *
+    * @param request current servlet request
+    * @return grapf creator instance
+    */
+   GraphCreator mapFromRequest(HttpServletRequest request);
+}




More information about the jboss-cvs-commits mailing list