[jboss-svn-commits] JBL Code SVN: r12712 - in labs/jbosslabs/trunk/stats-server/kosmos: src and 4 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jun 20 09:26:43 EDT 2007


Author: wrzep
Date: 2007-06-20 09:26:43 -0400 (Wed, 20 Jun 2007)
New Revision: 12712

Added:
   labs/jbosslabs/trunk/stats-server/kosmos/src/
   labs/jbosslabs/trunk/stats-server/kosmos/src/java/
   labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/
   labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/
   labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/
   labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java
   labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/ShotokuCachedDataStore.java
Log:
JBLAB-868 
alternative data stores for kosmos server


Added: labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java
===================================================================
--- labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java	                        (rev 0)
+++ labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java	2007-06-20 13:26:43 UTC (rev 12712)
@@ -0,0 +1,103 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.kosmos;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Date;
+
+import org.jboss.forge.common.ForgeHelper;
+import org.jboss.forge.common.TempFileDescriptor;
+import org.jboss.shotoku.tools.Tools;
+
+import hu.midori.kosmos.server.store.StaticContentStore;
+
+/**
+ * 
+ * @author Tomasz Szyma??ski (tomasz.szymanski at jboss.com)
+ * @author <a href="mailto:aron.gombas at midori.hu">Aron Gombas</a>
+ */
+public class LabsCachedDataStore implements StaticContentStore {
+
+	public String storeFile(String fileName, InputStream in) throws Exception {
+		
+		ForgeHelper.deleteTempFile(getCachedWildcard(fileName));
+		
+		TempFileDescriptor tfd = ForgeHelper.addTempFile(getCachedFileName(fileName), false);
+		
+		OutputStream out = tfd.getOut();
+		
+		Tools.transfer(in, out);
+		
+		/*int chr = 0;
+		
+		// write the file
+		while ((chr = in.read()) != -1) {
+			out.write(chr);
+		}*/
+		
+		in.close();
+		out.close();
+		
+		return tfd.getTempFileName();
+	}
+
+	/** 
+	 * Returns the part of the filename for a cached item without the timestamp
+	 * prefix. This is used to found the previous versions of the same cache item.
+	 * @throws UnsupportedEncodingException 
+	 */
+	private static String getCachedWildcard(String filename)
+			throws UnsupportedEncodingException {
+		return encodeFilename("", filename);
+	}
+
+	/** 
+	 * Returns the local filename for a cached item, by adding a timestamp prefix
+	 * so that browsers will not cache the item if it actually changes.
+	 * @throws UnsupportedEncodingException 
+	 */
+	private static String getCachedFileName(String filename)
+			throws UnsupportedEncodingException {
+		return encodeFilename(Long.toString(new Date().getTime()), filename);
+	}
+
+	/** 
+	 * Encodes the local filename for a cached item.
+	 * @param filename can contain any character.
+	 * @return the filename which contain only file-system-compatible characters.
+	 * @throws UnsupportedEncodingException 
+	 */
+	private static String encodeFilename(String prefix, String filename)
+			throws UnsupportedEncodingException {
+		return URLEncoder.encode(String.format("%s_%s", prefix, filename),
+				"utf-8");
+	}
+	
+	/** Unimplemented methods from kosmos 0.1.7 - Pawel Wrzeszcz */
+	
+	public void begin() {};
+	
+	public void end() {};
+}

Added: labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/ShotokuCachedDataStore.java
===================================================================
--- labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/ShotokuCachedDataStore.java	                        (rev 0)
+++ labs/jbosslabs/trunk/stats-server/kosmos/src/java/org/jboss/kosmos/ShotokuCachedDataStore.java	2007-06-20 13:26:43 UTC (rev 12712)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt 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.kosmos;
+
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Date;
+
+import org.jboss.shotoku.ContentManager;
+import org.jboss.shotoku.Directory;
+import org.jboss.shotoku.Node;
+import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
+
+import hu.midori.kosmos.server.store.StaticContentStore;
+
+/**
+ * 
+ * @author Adam Warski (adamw at aster.pl)
+ * @author <a href="mailto:aron.gombas at midori.hu">Aron Gombas</a>
+ */
+public class ShotokuCachedDataStore implements StaticContentStore {
+	private String fileAccessUrl;
+
+	private String prefix;
+
+	private ContentManager cm;
+
+	public String storeFile(String fileName, InputStream in) throws Exception {
+		Node file;
+		Directory cacheDir = cm.getRootDirectory();
+
+		// Clearing old cache.
+		String cachedWildcard = getCachedWildcard(fileName);
+		for (Node cacheNode : cacheDir.getNodes()) {
+			if (cacheNode.getName().endsWith(cachedWildcard)) {
+				cacheNode.delete();
+			}
+		}
+		
+		// Saving the new file.
+		String cachedFileName = getCachedFileName(fileName);
+		try {
+			file = cm.getNode(cachedFileName);
+		} catch (ResourceDoesNotExist e) {
+			file = cm.getRootDirectory().newNode(cachedFileName);
+		}
+
+		file.setContent(in);
+		file.save("Kosmos");
+
+		return fileAccessUrl + "/" + prefix + "/" + cachedFileName;
+	}
+
+	public void setFileAccessUrl(String fileAccessUrl) {
+		this.fileAccessUrl = fileAccessUrl;
+	}
+
+	public void setPrefix(String prefix) {
+		cm = ContentManager.getContentManager(prefix);
+		this.prefix = prefix;
+	}
+
+	/** 
+	 * Returns the part of the filename for a cached item without the timestamp
+	 * prefix. This is used to found the previous versions of the same cache item.
+	 * @throws UnsupportedEncodingException 
+	 */
+	private static String getCachedWildcard(String filename)
+			throws UnsupportedEncodingException {
+		return encodeFilename("", filename);
+	}
+
+	/** 
+	 * Returns the local filename for a cached item, by adding a timestamp prefix
+	 * so that browsers will not cache the item if it actually changes.
+	 * @throws UnsupportedEncodingException 
+	 */
+	private static String getCachedFileName(String filename)
+			throws UnsupportedEncodingException {
+		return encodeFilename(Long.toString(new Date().getTime()), filename);
+	}
+
+	/** 
+	 * Encodes the local filename for a cached item.
+	 * @param filename can contain any character.
+	 * @return the filename which contain only file-system-compatible characters.
+	 * @throws UnsupportedEncodingException 
+	 */
+	private static String encodeFilename(String prefix, String filename)
+			throws UnsupportedEncodingException {
+		return URLEncoder.encode(String.format("%s_%s", prefix, filename),
+				"utf-8");
+	}
+	
+	/** Unimplemented methods from kosmos 0.1.7 - Pawel Wrzeszcz */
+	
+	public void begin() {};
+	
+	public void end() {};
+}




More information about the jboss-svn-commits mailing list