From: <jbo...@li...> - 2005-12-14 23:07:13
|
Author: szimano Date: 2005-12-14 18:06:58 -0500 (Wed, 14 Dec 2005) New Revision: 1818 Added: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/VolatileTempFileRemover.java trunk/forge/portal-extensions/forge-kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/ForgeHelper.java trunk/forge/portal-extensions/forge-kosmos/conf/server/kosmos-services-servlet.xml trunk/forge/portal-extensions/forge-kosmos/project.xml trunk/forge/portal-extensions/forge-timetracker/src/java/org/jboss/labs/portlet/timetracker/TimeTrackerPortlet.java Log: added temp file ability for file-access (accessible by ForgeHelper) http://jira.jboss.com/jira/browse/JBLAB-575 changed Kosmos to use temp files instead of shotoku (sorry adam) minor changes in TimeTracker http://jira.jboss.com/jira/browse/JBLAB-550 Modified: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/ForgeHelper.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/ForgeHelper.java 2005-12-14 17:58:33 UTC (rev 1817) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/ForgeHelper.java 2005-12-14 23:06:58 UTC (rev 1818) @@ -29,6 +29,8 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; +import java.util.Set; +import java.util.TreeSet; import java.util.Vector; import javax.activation.MimetypesFileTypeMap; @@ -51,10 +53,12 @@ import org.jboss.shotoku.ContentManager; import org.w3c.dom.Node; + /** * Common constants and functions for the forge. * * @author adamw + * @author szimano */ public class ForgeHelper { /** @@ -124,6 +128,12 @@ private static MimetypesFileTypeMap mimeTypes; private static Vector<String> tempFiles; + + public static final String FILES_PREFIX = "labsTempFile"; + + private static Set<Integer> volatileTempFiles; + + private static VolatileTempFileRemover vtfRemover; static { // Reading forge properties. @@ -134,6 +144,11 @@ .getResourceAsStream("/" + MIME_TYPE_FILENAME)); tempFiles = new Vector<String>(); + + volatileTempFiles = new TreeSet<Integer>(); + + vtfRemover = new VolatileTempFileRemover(tempFiles, volatileTempFiles); + vtfRemover.start(); } /** @@ -537,17 +552,83 @@ return mimeTypes.getContentType(fileName); } - public synchronized static TempFileDescriptor addTempFile(String fileSuffix) + /**Add temporary file. This returns you TempFileDescriptor with opened InputStream to the file and link to access it. + * Remember to close the input after use ! + * @param fileSuffix suffix of the temporary file (to get apropriate content_type) and allow deletion for not volatile files. + * @param volatileFile If true file will be deleted, if false you have to delete it on your own, by using deleteTempFile with apropriate suffix. + * @return + * @throws FileNotFoundException + * @throws IOException + */ + public synchronized static TempFileDescriptor addTempFile(String fileSuffix, boolean volatileFile) throws FileNotFoundException, IOException { - File file = File.createTempFile("labsTempFile", fileSuffix); + File file = File.createTempFile(FILES_PREFIX, fileSuffix); FileOutputStream fos = new FileOutputStream(file); + + int fileNumber = -1; + + // look for available positin in vector + for (int i = 0; (i < tempFiles.size() && i == -1); i++) { + if (tempFiles.get(i) == null) { + fileNumber = i; + } + } + + // if not found - add at the end + if (fileNumber == -1) { + tempFiles.add(file.getAbsolutePath()); + fileNumber = tempFiles.size() - 1; + } + else { + tempFiles.add(fileNumber, file.getAbsolutePath()); + } - tempFiles.add(file.getAbsolutePath()); - + if (volatileFile) { + volatileTempFiles.add(fileNumber); + } + return new TempFileDescriptor(fos, "/file-access/" + TMP_FILES_ACCESS - + (tempFiles.size() - 1)); + + fileNumber); } + /**Deletes all temp files with matching suffix. + * @param fileSuffix end of the filename(s) to delete. + * @throws IOException + */ + public synchronized static void deleteTempFile(String fileSuffix) throws IOException { + File dir = null; + + // get a file and a place where tempfiles are stored on disk + for (int i = 0; (i < tempFiles.size() && dir != null); i++) { + if (tempFiles.get(i) != null) { + File file = new File(tempFiles.get(i)); + dir = new File(file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(File.separatorChar))); + } + } + + if (dir == null) { + // there were no files - choose different way + File tmp = File.createTempFile(FILES_PREFIX, "tmpFile"); + dir = tmp.getParentFile(); + tmp.delete(); + } + + File[] allFiles = dir.listFiles(); + + for (int i = 0; i < allFiles.length; i++) { + File tmpFile = allFiles[i]; + + // if not an directory, labs temp file and matches suffix - delete it + if (!tmpFile.isDirectory() && tmpFile.getName().startsWith(FILES_PREFIX) && tmpFile.getName().endsWith(fileSuffix)) { + tmpFile.delete(); + } + } + } + + /**Get file path to a temp file for a given number. + * @param num Number of the temp file. + * @return Absolute path to temporary file. + */ public static String getFilePath(Integer num) { return tempFiles.get(num); } Added: trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/VolatileTempFileRemover.java =================================================================== --- trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/VolatileTempFileRemover.java 2005-12-14 17:58:33 UTC (rev 1817) +++ trunk/forge/portal-extensions/forge-common/src/java/org/jboss/forge/common/VolatileTempFileRemover.java 2005-12-14 23:06:58 UTC (rev 1818) @@ -0,0 +1,73 @@ +package org.jboss.forge.common; + +import java.io.File; +import java.util.Calendar; +import java.util.Date; +import java.util.Set; +import java.util.Vector; + + +public class VolatileTempFileRemover extends Thread { + + private boolean running = false; + + private final int NUMBER_OF_HOURS = 1; + + private final int FILE_OLDER_THEN_HOURS = 24; + + private Vector<String> tempFiles; + + private Set<Integer> volatileTempFiles; + + public VolatileTempFileRemover(Vector<String> tempFiles, Set<Integer> volatileTempFiles) { + this.tempFiles = tempFiles; + this.volatileTempFiles = volatileTempFiles; + } + + @Override + public void run() { + running = true; + + while (running) { + try { + wait(NUMBER_OF_HOURS * 60 * 60 * 1000 ); + } catch (InterruptedException e) { + } + + deleteVolatileFiles(); + } + } + + private void deleteVolatileFiles() { + for (int i = 0; i < tempFiles.size(); i++) { + if (volatileTempFiles.contains(i) && tempFiles.get(i) != null) { + //this is a volatile file - check it and delete if is older then FILE_OLDER_THEN_HOURS + + File file = new File(tempFiles.get(i)); + + Calendar cal = Calendar.getInstance(); + + cal.setTimeInMillis(file.lastModified()); + + cal.add(Calendar.HOUR, FILE_OLDER_THEN_HOURS); + + Calendar calNow = Calendar.getInstance(); + + calNow.setTime(new Date()); + + if (calNow.after(cal)){ + // file is old - delete it + file.delete(); + tempFiles.add(i, null); + volatileTempFiles.remove(i); + } + } + } + } + + public void stopRemover() { + this.notify(); + running = false; + } + +} Modified: trunk/forge/portal-extensions/forge-kosmos/conf/server/kosmos-services-servlet.xml =================================================================== --- trunk/forge/portal-extensions/forge-kosmos/conf/server/kosmos-services-servlet.xml 2005-12-14 17:58:33 UTC (rev 1817) +++ trunk/forge/portal-extensions/forge-kosmos/conf/server/kosmos-services-servlet.xml 2005-12-14 23:06:58 UTC (rev 1818) @@ -4,7 +4,7 @@ <beans> <!-- CC service --> <bean id="ccService" class="hu.midori.kosmos.server.cc.CcServiceImpl"> - <property name="store" ref="shotokuCachedDataStore"/> + <property name="store" ref="labsCachedDataStore"/> </bean> <bean name="/cc-service" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="ccService"/> @@ -13,7 +13,7 @@ <!-- JIRA service --> <bean id="jiraService" class="hu.midori.kosmos.server.jira.JiraServiceImpl"> - <property name="store" ref="shotokuCachedDataStore"/> + <property name="store" ref="labsCachedDataStore"/> </bean> <bean name="/jira-service" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="jiraService"/> @@ -22,7 +22,7 @@ <!-- SF service --> <bean id="sfService" class="hu.midori.kosmos.server.sf.SfServiceImpl"> - <property name="store" ref="shotokuCachedDataStore"/> + <property name="store" ref="labsCachedDataStore"/> </bean> <bean name="/sf-service" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="sfService"/> @@ -31,16 +31,15 @@ <!-- SVN service --> <bean id="svnService" class="hu.midori.kosmos.server.svn.SvnServiceImpl"> - <property name="store" ref="shotokuCachedDataStore"/> + <property name="store" ref="labsCachedDataStore"/> </bean> <bean name="/svn-service" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="svnService"/> <property name="serviceInterface" value="hu.midori.kosmos.protocol.SvnService"/> </bean> - <!-- Shotoku cached data store --> - <bean id="shotokuCachedDataStore" class="org.jboss.kosmos.ShotokuCachedDataStore"> - <property name="fileAccessUrl" value="http://localhost:8080/file-access"/> - <property name="prefix" value="kosmos/images/kosmos-cache"/> - </bean> + <!-- Labs cached data store --> + <bean id="labsCachedDataStore" class="org.jboss.kosmos.LabsCachedDataStore"> + </bean> + </beans> \ No newline at end of file Modified: trunk/forge/portal-extensions/forge-kosmos/project.xml =================================================================== --- trunk/forge/portal-extensions/forge-kosmos/project.xml 2005-12-14 17:58:33 UTC (rev 1817) +++ trunk/forge/portal-extensions/forge-kosmos/project.xml 2005-12-14 23:06:58 UTC (rev 1818) @@ -10,6 +10,11 @@ <id>forge-kosmos</id> <name>Forge kosmos</name> <dependencies> + <dependency> + <groupId>jboss-forge</groupId> + <artifactId>forge-common</artifactId> + <version>1.0</version> + </dependency> <dependency> <groupId>kosmos</groupId> <artifactId>kosmos-server</artifactId> Added: trunk/forge/portal-extensions/forge-kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java =================================================================== --- trunk/forge/portal-extensions/forge-kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java 2005-12-14 17:58:33 UTC (rev 1817) +++ trunk/forge/portal-extensions/forge-kosmos/src/java/org/jboss/kosmos/LabsCachedDataStore.java 2005-12-14 23:06:58 UTC (rev 1818) @@ -0,0 +1,94 @@ +/* + * 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 hu.midori.kosmos.server.CachedDataStore; + +/** + * + * @author Tomasz Szymański (tom...@jb...) + * @author <a href="mailto:aro...@mi...">Aron Gombas</a> + */ +public class LabsCachedDataStore implements CachedDataStore { + + public String storeFile(String fileName, InputStream in) throws Exception { + + ForgeHelper.deleteTempFile(getCachedWildcard(fileName)); + + TempFileDescriptor tfd = ForgeHelper.addTempFile(getCachedFileName(fileName), false); + + OutputStream out = tfd.getOut(); + + 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"); + } +} Modified: trunk/forge/portal-extensions/forge-timetracker/src/java/org/jboss/labs/portlet/timetracker/TimeTrackerPortlet.java =================================================================== --- trunk/forge/portal-extensions/forge-timetracker/src/java/org/jboss/labs/portlet/timetracker/TimeTrackerPortlet.java 2005-12-14 17:58:33 UTC (rev 1817) +++ trunk/forge/portal-extensions/forge-timetracker/src/java/org/jboss/labs/portlet/timetracker/TimeTrackerPortlet.java 2005-12-14 23:06:58 UTC (rev 1818) @@ -67,7 +67,7 @@ plot.draw(g2d, new Rectangle2D.Double(0,0,500,500),null,null,null); - TempFileDescriptor tmpD = ForgeHelper.addTempFile("timetracker.png"); + TempFileDescriptor tmpD = ForgeHelper.addTempFile("timetracker.png", true); FileOutputStream out = tmpD.getOut(); |