1 //Copyright (C) 2002 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2 //All rights reserved.  Use of this class is limited.
3 //Please see the LICENSE for more information.
4 
5 package com.borneo.util;
6 
7 import java.io.File;
8 
9 import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpSession;
11
12import org.apache.log4j.Logger;
13import org.apache.log4j.PropertyConfigurator;
14
15/**
16 * Implements a renaming policy that ties files to a SessionId by Prefixing
17 * the sessId to the file name. Since this is in a servlet context,
18 * use the container's temp dir as a Directory for the files.
19 * These files are assumed Transient and will be deleted by a SessionTimeOUT activity
20 * 
21 * @author Jason Hunter
22 * @version 1.1, 2002/11/05, making thread safe with createNewFile()
23 * @version 1.0, 2002/04/30, initial revision, thanks to Yoonjung Lee
24 *                           for this idea
25 */
26
27public class DefaultFileRenamePolicySesn
28    implements FileRenamePolicySesn
29{
30    // This method does not need to be synchronized because createNewFile()
31    // is atomic and used here to mark when a file name is chosen
32    public DefaultFileRenamePolicySesn()
33    {
34    }
35
36    public File rename(File f, HttpServletRequest request, boolean flag)
37    {
38        if((new File((File)request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), request.getSession().getId().substring(0, 15) + f.getName())).exists() == flag)
39        {
40            log.debug("boolean vrsn : hit substring");
41            return new File((File)request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), request.getSession().getId().substring(0, 15) + f.getName());
42        } else
43        {
44            log.debug("boolean vrsn : NOhit substring");
45            return new File((File)request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), f.getName());
46        }
47    }
48
49    public File rename(File f, HttpServletRequest request)
50    {
51        log.debug("plain vrsn ");
52        return new File((File)request.getSession().getServletContext().getAttribute("javax.servlet.context.tempdir"), request.getSession().getId().substring(0, 15) + f.getName());
53    }
54
55    public File rename(File f, HttpSession session)
56    {
57        log.debug("plain vrsn ");
58        return new File((File)session.getServletContext().getAttribute("javax.servlet.context.tempdir"), session.getId().substring(0, 15) + f.getName());
59    }
60
61    protected static Logger log;
62
63    static 
64    {
65        log = Logger.getLogger(DefaultFileRenamePolicySesn.class.getName());
66        PropertyConfigurator.configure(com.borneo.util.DefaultFileRenamePolicySesn.class.getClassLoader().getResource("config/log4j.properties"));
67        
68  }
69}
70