work=new File(System.getProperty("jetty.home"),"work"); if (!work.exists() || !work.canWrite() || !work.isDirectory())
File w=new File(System.getProperty("jetty.home"),"work"); if (w.exists() && w.canWrite() && w.isDirectory()) work=w; else if (getBaseResource()!=null)
    public File getTempDirectory()    {        if (_tmpDir!=null)            return _tmpDir;        // Initialize temporary directory        //        // I'm afraid that this is very much black magic.        // but if you can think of better....        Object t = getAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR);        if (t!=null && (t instanceof File))        {            _tmpDir=(File)t;            if (_tmpDir.isDirectory() && _tmpDir.canWrite())                return _tmpDir;        }        if (t!=null && (t instanceof String))        {            try            {                _tmpDir=new File((String)t);                if (_tmpDir.isDirectory() && _tmpDir.canWrite())                {                    if(Log.isDebugEnabled())Log.debug("Converted to File "+_tmpDir+" for "+this);                    setAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR,_tmpDir);                    return _tmpDir;                }            }            catch(Exception e)            {                Log.warn(Log.EXCEPTION,e);            }        }        // No tempdir so look for a work directory to use as tempDir base        File work=null;        try        {            work=new File(System.getProperty("jetty.home"),"work");            if (!work.exists() || !work.canWrite() || !work.isDirectory())            {                work=new File(getWebInf().getFile(),"work");                if (!work.exists() || !work.canWrite() || !work.isDirectory())                    work=null;            }        }        catch(Exception e)        {            Log.ignore(e);        }        // No tempdir set so make one!        try        {            String temp="Jetty_"+getContextPath();            temp=temp.replace('/','_');            temp=temp.replace('.','_');            temp=temp.replace('\\','_');                        if (work!=null)                _tmpDir=new File(work,temp);            else            {                _tmpDir=new File(System.getProperty("java.io.tmpdir"),temp);                                if (_tmpDir.exists())                {                    if(Log.isDebugEnabled())Log.debug("Delete existing temp dir "+_tmpDir+" for "+this);                    if (!IO.delete(_tmpDir))                    {                        if(Log.isDebugEnabled())Log.debug("Failed to delete temp dir "+_tmpDir);                    }                                    if (_tmpDir.exists())                    {                        String old=_tmpDir.toString();                        _tmpDir=File.createTempFile(temp+"_","");                        if (_tmpDir.exists())                            _tmpDir.delete();                        Log.warn("Can't reuse "+old+", using "+_tmpDir);                    }                }            }            if (!_tmpDir.exists())                _tmpDir.mkdir();                        if (!"work".equals(_tmpDir.getName()))                _tmpDir.deleteOnExit();            if(Log.isDebugEnabled())Log.debug("Created temp dir "+_tmpDir+" for "+this);        }        catch(Exception e)        {            _tmpDir=null;            Log.ignore(e);        }        if (_tmpDir==null)        {            try{                // that didn't work, so try something simpler (ish)                _tmpDir=File.createTempFile("JettyContext","");                if (_tmpDir.exists())                    _tmpDir.delete();                _tmpDir.mkdir();                _tmpDir.deleteOnExit();                if(Log.isDebugEnabled())Log.debug("Created temp dir "+_tmpDir+" for "+this);            }            catch(IOException e)            {                Log.warn("tmpdir",e); System.exit(1);            }        }        setAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR,_tmpDir);        return _tmpDir;    }