[jboss-svn-commits] JBL Code SVN: r21479 - in labs/jbosslabs/trunk/stats-server/sources/kosmos: src/java/hu/midori/kosmos/server and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Aug 12 12:11:50 EDT 2008


Author: wrzep
Date: 2008-08-12 12:11:50 -0400 (Tue, 12 Aug 2008)
New Revision: 21479

Added:
   labs/jbosslabs/trunk/stats-server/sources/kosmos/src/java/hu/midori/kosmos/server/AbstractKosmosService.java
Modified:
   labs/jbosslabs/trunk/stats-server/sources/kosmos/project.xml
Log:
trim lists to improve performance


Modified: labs/jbosslabs/trunk/stats-server/sources/kosmos/project.xml
===================================================================
--- labs/jbosslabs/trunk/stats-server/sources/kosmos/project.xml	2008-08-12 15:45:23 UTC (rev 21478)
+++ labs/jbosslabs/trunk/stats-server/sources/kosmos/project.xml	2008-08-12 16:11:50 UTC (rev 21479)
@@ -27,6 +27,21 @@
 	    <artifactId>jfreechart</artifactId>
 	    <version>1.0.0</version>
 	</dependency>
+	<dependency>
+	    <groupId>commons-logging</groupId>
+	    <artifactId>commons-logging</artifactId>
+	    <version>1.1</version>
+	</dependency>
+	<dependency>
+		    <groupId>commons-lang</groupId>
+		    <artifactId>commons-lang</artifactId>
+		    <version>2.3</version>
+	</dependency>    
+	<dependency>
+		    <groupId>javax.servlet</groupId>
+		    <artifactId>servlet-api</artifactId>
+		    <version>2.4</version>
+	</dependency>     	
     </dependencies>
     <build>
         <sourceDirectory>src/java</sourceDirectory>

Added: labs/jbosslabs/trunk/stats-server/sources/kosmos/src/java/hu/midori/kosmos/server/AbstractKosmosService.java
===================================================================
--- labs/jbosslabs/trunk/stats-server/sources/kosmos/src/java/hu/midori/kosmos/server/AbstractKosmosService.java	                        (rev 0)
+++ labs/jbosslabs/trunk/stats-server/sources/kosmos/src/java/hu/midori/kosmos/server/AbstractKosmosService.java	2008-08-12 16:11:50 UTC (rev 21479)
@@ -0,0 +1,182 @@
+/*
+ * Kosmos.
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package hu.midori.kosmos.server;
+
+import hu.midori.kosmos.model.util.ZoomableImageLocator;
+import hu.midori.kosmos.server.store.StaticContentStore;
+import hu.midori.kosmos.server.util.ChartUtils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jfree.chart.JFreeChart;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.web.context.WebApplicationContext;
+
+/**
+ * Each Kosmos service must extend this baseclass.
+ * Services are implemented as POJOs.
+ *
+ * @author <a href="mailto:aron.gombas at midori.hu">Aron Gombas</a>
+ * @version $Id$
+ */
+public abstract class AbstractKosmosService implements ApplicationContextAware {
+    private final static Log log = LogFactory.getLog(AbstractKosmosService.class);
+
+    /**
+     * Heavy lists will be trimmed to this size or set to -1 if you want to disable trimming.
+     * @see #trimList(List)
+     */
+    protected static final int MAX_LIST_SIZE = 8;
+
+    /** Horizontal resolution of small chart images. */
+    private static final int CHART_SMALL_RESOLUTION_X = 320;
+    /** Vertical resolution of small chart images. */
+    private static final int CHART_SMALL_RESOLUTION_Y = 240;
+    /** Horizontal resolution of small chart images. */
+    private static final int CHART_LARGE_RESOLUTION_X = 640;
+    /** Vertical resolution of small chart images. */
+    private static final int CHART_LARGE_RESOLUTION_Y = 480;
+
+    /** Spring web-application context. */
+    protected WebApplicationContext applicationContext;
+
+    /** The timestamp when the cache was most recently updated. */
+    private Date timestamp;
+    /** Localized resources. */
+    private ResourceBundle resources = null;
+    /** Pluggable static content store implementation. */
+    private StaticContentStore store;
+
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        this.applicationContext = (WebApplicationContext)applicationContext;
+    }
+
+    public void setStore(StaticContentStore store) {
+        this.store = store;
+    }
+
+    /**
+     * Returns the timestamp when the cache was most recently updated.
+     * This can be invoked only after the service method was called.
+     */
+    public Date getTimestamp() {
+        log.debug("Service invoked: returning timestamp...");
+        // init if timestamp wasn't initialized yet
+        if(timestamp == null)
+            timestamp = new Date();
+
+        return timestamp;
+    }
+
+    /** Updates the timestamp at service method calls. */
+    protected void updateTimestamp() {
+        this.timestamp = new Date();
+    }
+
+    /**
+     * Returns the localized string resource from the given resource bundle.
+     * For caching, it assumes that only one bundle is used per service.
+     * The locale is defined globally for the servlet, not as method parameter.
+     */
+    protected String getResourceString(String baseName, String key) {
+        // lazy-init the bundle
+        if(resources == null) {
+            String locale = applicationContext.getServletContext().getInitParameter("locale");
+            log.info(String.format("Loading resources \"%s\" for locale \"%s\"...", baseName, locale));
+            resources = ResourceBundle.getBundle(baseName);
+            	//StringUtils.isBlank(locale) ? ResourceBundle.getBundle(baseName) : ResourceBundle.getBundle(baseName, new Locale(locale));
+        }
+
+        return resources.getString(key);
+    }
+
+    /**
+     * Must be called before the first file gets stored.
+     * @see StaticContentStore
+     */
+    protected void beginStoreSession() {
+        log.debug(String.format("Beginning store session...."));
+        store.begin();
+    }
+
+    /**
+     * Must be called after the last file got stored.
+     * @see StaticContentStore
+     */
+    protected void endStoreSession() {
+        log.debug(String.format("Ending store session..."));
+        store.end();
+    }
+
+    /**
+     * Stores the passed stream to an implementation-dependent "storage".
+     * @return the absolute URL that points to the resulted file.
+     * @see StaticContentStore
+     */
+    protected String storeFile(String fileName, InputStream in) {
+        log.debug(String.format("Storing \"%s\"...", fileName));
+
+        try {
+            // delegate to the concrete store implementation
+            return store.storeFile(fileName, in);
+        } catch (Exception ex) {
+            log.error("Unable to store cached file", ex);
+            return "";
+        }
+    }
+
+    /**
+     * Stores the images generated from the passed chart in 2 resolutions.
+     * @param fileName must <strong>not</strong> contain the file extension.
+     * @return the locator with absolute URLs pointing to the resulted images.
+     * @see #storeFile(String, InputStream)
+     */
+    protected ZoomableImageLocator storeChart(String fileName, JFreeChart chart) {
+        String loresUrl = "";
+        try {
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            ChartUtils.writeChartAsPng(chart, out, CHART_SMALL_RESOLUTION_X, CHART_SMALL_RESOLUTION_Y);
+            loresUrl = storeFile(fileName + "_small.png", new ByteArrayInputStream(out.toByteArray()));
+        } catch(IOException ex) {
+            log.error("Unable to write lo-res version of chart image", ex);
+        }
+
+        String hiresUrl = "";
+        try {
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            ChartUtils.writeChartAsPng(chart, out, CHART_LARGE_RESOLUTION_X, CHART_LARGE_RESOLUTION_Y);
+            hiresUrl = storeFile(fileName + "_large.png", new ByteArrayInputStream(out.toByteArray()));
+        } catch(IOException ex) {
+            log.error("Unable to write hi-res version of chart image", ex);
+        }
+
+        return new ZoomableImageLocator(loresUrl, hiresUrl);
+    }
+
+    /**
+     * Trims long lists to some limited size to avoid large datasets that would result in
+     * huge network transfers and unuseable web pages.
+     */
+    protected List trimList(List data) {
+        if((MAX_LIST_SIZE != -1) && (data.size() > MAX_LIST_SIZE))
+            return data.subList(0, MAX_LIST_SIZE);
+
+        return data;
+    }
+}




More information about the jboss-svn-commits mailing list