// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 6/15/2001 17:43:09
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/app/CacheManager

package com.macromedia.generator.app;

import com.macromedia.generator.api.*;
import com.macromedia.generator.platform.Mac;
import com.macromedia.generator.utils.GenUtils;
import com.macromedia.generator.utils.QueryStringParser;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

// Referenced classes of package com.macromedia.generator.app:
//            CacheLog, CacheProperties, FileCache, FlashEnvironment, 
//            FontCache, GenCache, GenLog, MediaCache, 
//            RequestCache, URLCache

public class CacheManager
{

    public CacheManager()
    {
        C2 = true;
        C8 = null;
        C9 = null;
        CA = null;
        CB = null;
        CC = null;
        CD = false;
        CE = 0L;
        CF = 0L;
        CacheProperties cacheproperties = new CacheProperties();
        CA(cacheproperties);
    }

    public CacheManager(CacheProperties cacheproperties)
    {
        C2 = true;
        C8 = null;
        C9 = null;
        CA = null;
        CB = null;
        CC = null;
        CD = false;
        CE = 0L;
        CF = 0L;
        CA(cacheproperties);
    }

    public boolean addRequest(String s, Buffer buffer)
    {
        if(C8 == null)
            return false;
        boolean flag = false;
        long l = 0L;
        QueryStringParser querystringparser = new QueryStringParser();
        Hashtable hashtable = querystringparser.parse(s);
        if(hashtable != null && hashtable.size() > 0)
        {
            String s1 = null;
            s1 = (String)hashtable.get("grc");
            if(s1 != null)
                if(s1.equalsIgnoreCase("true"))
                    flag = true;
                else
                if(s1.equalsIgnoreCase("false"))
                    flag = false;
            s1 = (String)hashtable.get("gre");
            if(s1 != null)
                l = Long.parseLong(s1);
            s = stripGeneratorParams(s);
        }
        return C8.addRequest(s, buffer, flag, l);
    }

    private File C2(FlashEnvironment flashenvironment, String s)
    {
        if(s == null)
            return null;
        Mac mac = Mac.getMac();
        File file = null;
        if(mac != null)
        {
            String s1 = mac.makeJavaPretty(s);
            file = new File(s1);
            if(!file.exists() || !file.isFile())
                file = new File(s1.substring(1));
        } else
        {
            file = new File(s);
        }
        if(file != null)
        {
            if(!file.isAbsolute())
            {
                File file1 = null;
                if(flashenvironment != null)
                    file1 = flashenvironment.getDirectory();
                if(file1 != null)
                    file = new File(file1, file.getPath());
            }
            if(!file.exists() || !file.isFile())
                file = null;
        }
        return file;
    }

    private String C3(FlashEnvironment flashenvironment, String s)
    {
        String s1 = null;
        File file = new File(s);
        if(!file.isAbsolute())
        {
            File file1 = null;
            if(flashenvironment != null)
                file1 = flashenvironment.getDirectory();
            if(file1 != null)
                file = new File(file1, file.getPath());
            else
                CD(flashenvironment, C7("ExtFontErr") + file.getPath());
        }
        if(file.exists() && file.isFile())
        {
            s1 = file.getPath();
        } else
        {
            String s2 = null;
            if(flashenvironment != null)
                s2 = flashenvironment.getExternalFontLocation();
            if(s2 != null)
            {
                s2 = CF(s2);
                String s3 = System.getProperty("file.separator");
                String s4 = s2 + s3 + s;
                file = new File(s4);
                if(file.exists() && file.isFile())
                {
                    s1 = file.getPath();
                } else
                {
                    File file2 = flashenvironment.getDirectory();
                    file = new File(file2, s4);
                    if(file.exists() && file.isFile())
                        s1 = file.getPath();
                    else
                    if(s4.startsWith("\\"))
                    {
                        s1 = "http:" + s4;
                        s1 = s1.replace('\\', '/');
                    } else
                    {
                        CD(flashenvironment, C7("ExtFontErr") + file.getPath());
                    }
                }
            } else
            {
                CD(flashenvironment, C7("ExtFontErr") + file.getPath());
            }
        }
        return s1;
    }

    private URL C4(FlashEnvironment flashenvironment, String s)
    {
        URL url = null;
        try
        {
            url = new URL(s);
        }
        catch(MalformedURLException _ex)
        {
            File file = C2(flashenvironment, s);
            if(file == null)
                url = null;
            else
                try
                {
                    s = file.getAbsolutePath();
                    if(s.startsWith("\\"))
                    {
                        if(!GenUtils.isJava2())
                            url = GenUtils.toURL(file);
                        else
                            url = file.toURL();
                    } else
                    {
                        String s1 = System.getProperty("file.separator");
                        if(s1.length() == 1)
                        {
                            char c = s1.charAt(0);
                            if(c != '/')
                                s = s.replace(c, '/');
                            if(s.charAt(0) != '/')
                                s = '/' + s;
                        }
                        s = "file://" + s;
                        url = new URL(s);
                    }
                }
                catch(MalformedURLException _ex2)
                {
                    url = null;
                }
        }
        return url;
    }

    public void dispose()
    {
        CC(C5("DisposeCache"), 2);
        if(C8 != null)
        {
            C8.dispose();
            C8 = null;
        }
        if(C9 != null)
        {
            C9.dispose();
            C9 = null;
        }
        if(CA != null)
        {
            CA.dispose();
            CA = null;
        }
        if(CB != null)
        {
            CB.dispose();
            CB = null;
        }
    }

    protected void finalize()
        throws Throwable
    {
        dispose();
    }

    public CacheLog getCacheLog()
    {
        return CC;
    }

    private String C5(String s)
    {
        return C9("com.macromedia.generator.app.CacheData", s);
    }

    public FileCache getFileCache()
    {
        return CB;
    }

    public FontCache getFontCache()
    {
        return C9;
    }

    private Object C6(FlashEnvironment flashenvironment, String s)
    {
        if(s == null || C9 == null)
            return null;
        String s1 = null;
        s1 = C3(flashenvironment, s);
        if(s1 == null)
            return null;
        URL url = null;
        url = C4(flashenvironment, s1);
        if(url == null)
            return null;
        Object obj = null;
        try
        {
            if(flashenvironment.getFontCache() != null)
            {
                obj = flashenvironment.getFontCache().getURLData(url);
                if(obj == null)
                    obj = flashenvironment.getFontCache().fetchURLData(url, true, 0L);
            } else
            {
                obj = C9.getURLData(url);
                if(obj == null)
                    obj = C9.fetchURLData(url, true, 0L);
            }
            if(obj == null)
                CD(flashenvironment, C7("ExtFontErr") + url.toString());
        }
        catch(IOException ioexception)
        {
            CD(flashenvironment, C7("ExtFontErr") + url.toString() + ": " + ioexception);
            return null;
        }
        return obj;
    }

    private String C7(String s)
    {
        return C9("com.macromedia.generator.resources.ResourceData", s);
    }

    public long getInternalFontCacheMaxSize()
    {
        return CE;
    }

    public long getInternalMediaCacheMaxSize()
    {
        return CF;
    }

    public MediaCache getMediaCache()
    {
        return CA;
    }

    public Object getMediaData(FlashEnvironment flashenvironment, String s, boolean flag)
    {
        URL url = null;
        url = C4(flashenvironment, s);
        if(url == null)
            return null;
        long l = 0L;
        QueryStringParser querystringparser = new QueryStringParser();
        Hashtable hashtable = querystringparser.parse(s);
        if(hashtable != null && hashtable.size() > 0)
        {
            String s1 = null;
            s1 = (String)hashtable.get("gmc");
            if(s1 != null)
                if(s1.equalsIgnoreCase("true"))
                    flag = true;
                else
                if(s1.equalsIgnoreCase("false"))
                    flag = false;
            s1 = (String)hashtable.get("gme");
            if(s1 != null)
                l = Long.parseLong(s1);
        }
        Object obj = null;
        obj = C8(flashenvironment, url, flag, l);
        return obj;
    }

    private Object C8(FlashEnvironment flashenvironment, URL url, boolean flag, long l)
    {
        if(url == null || CA == null || CB == null)
            return null;
        Object obj = null;
        try
        {
            if(flashenvironment.getMediaCache() != null)
            {
                obj = flashenvironment.getMediaCache().getURLData(url);
                if(obj == null)
                    obj = flashenvironment.getMediaCache().fetchURLData(url, true, 0L);
            } else
            {
                obj = CA.getURLData(url);
                if(obj == null)
                {
                    obj = CB.getURLData(url);
                    if(obj == null)
                    {
                        obj = CB.fetchURLData(url, flag, l);
                        if(obj == null)
                            obj = CA.fetchURLData(url, flag, l);
                        else
                            CA.setCacheURL(url, obj, ((byte[])obj).length, l);
                    } else
                    {
                        CA.setCacheURL(url, obj, ((byte[])obj).length, l);
                    }
                }
            }
        }
        catch(IOException ioexception)
        {
            CD(flashenvironment, C5("ErrorReadingMediaData") + url.toString() + ": " + ioexception);
            return null;
        }
        if(obj == null)
            CC(C5("UnableToGetURLData") + url.toString(), 2);
        return obj;
    }

    public CacheProperties getProperties()
    {
        CacheProperties cacheproperties = new CacheProperties();
        if(CC != null)
        {
            cacheproperties.logLevel = CC.getLevel();
            cacheproperties.logCycle = CC.getCycle();
            cacheproperties.logsMax = CC.getMaxLogFiles();
        }
        if(C8 != null)
        {
            cacheproperties.rcMaxSize = C8.getMaxSize();
            cacheproperties.rcForce = C8.getForce();
            cacheproperties.rcDefaultExpire = C8.getDefaultExpire();
            cacheproperties.rcRecycle = C8.getRecycle();
        }
        if(C9 != null)
        {
            cacheproperties.tcMaxSize = C9.getMaxSize();
            cacheproperties.tcForce = C9.getForce();
            cacheproperties.tcDefaultExpire = C9.getDefaultExpire();
            cacheproperties.tcRecycle = C9.getRecycle();
            cacheproperties.tcCheckModifiedSince = C9.getCheckModifiedSince();
        }
        if(CA != null)
        {
            cacheproperties.mcMaxSize = CA.getMaxSize();
            cacheproperties.mcForce = CA.getForce();
            cacheproperties.mcDefaultExpire = CA.getDefaultExpire();
            cacheproperties.mcRecycle = CA.getRecycle();
            cacheproperties.mcCheckModifiedSince = CA.getCheckModifiedSince();
        }
        if(CB != null)
        {
            cacheproperties.fcMaxSize = CB.getMaxSize();
            cacheproperties.fcForce = CB.getForce();
            cacheproperties.fcDefaultExpire = CB.getDefaultExpire();
            cacheproperties.fcRecycle = CB.getRecycle();
            cacheproperties.fcCheckModifiedSince = CB.getCheckModifiedSince();
            cacheproperties.fcLoad = CB.getLoad();
            cacheproperties.diskBufferSize = CB.getDiskBufferSize();
            cacheproperties.netBufferSize = CB.getNetBufferSize();
        }
        return cacheproperties;
    }

    public Buffer getRequestBuffer(String s)
    {
        if(C8 == null || s == null)
        {
            return null;
        } else
        {
            s = stripGeneratorParams(s);
            return C8.getMatchingRequestBuffer(s);
        }
    }

    public RequestCache getRequestCache()
    {
        return C8;
    }

    private String C9(String s, String s1)
    {
        ResourceBundle resourcebundle = ResourceBundle.getBundle(s, Locale.getDefault());
        return resourcebundle.getString(s1);
    }

    private void CA(CacheProperties cacheproperties)
    {
        CC = new CacheLog(cacheproperties.logLevel, cacheproperties.logCycle, cacheproperties.logsMax);
        if(CC != null && cacheproperties.rcMaxSize == 0L)
            CC.setLevel(0);
        CB();
        C8 = new RequestCache(cacheproperties.rcMaxSize, cacheproperties.rcForce, cacheproperties.rcRecycle, cacheproperties.rcDefaultExpire, CC);
        if(CC != null)
        {
            CC.setLevel(cacheproperties.logLevel);
            if(cacheproperties.tcMaxSize == 0L)
                CC.setLevel(0);
        }
        C9 = new FontCache(cacheproperties.tcMaxSize, cacheproperties.tcForce, cacheproperties.tcRecycle, cacheproperties.tcDefaultExpire, cacheproperties.tcCheckModifiedSince, cacheproperties.diskBufferSize, cacheproperties.netBufferSize, CC);
        if(CC != null)
        {
            CC.setLevel(cacheproperties.logLevel);
            if(cacheproperties.mcMaxSize == 0L)
                CC.setLevel(0);
        }
        CA = new MediaCache(cacheproperties.mcMaxSize, cacheproperties.mcForce, cacheproperties.mcRecycle, cacheproperties.mcDefaultExpire, cacheproperties.mcCheckModifiedSince, cacheproperties.diskBufferSize, cacheproperties.netBufferSize, CC);
        if(CC != null)
        {
            CC.setLevel(cacheproperties.logLevel);
            if(cacheproperties.fcMaxSize == 0L)
                CC.setLevel(0);
        }
        CB = new FileCache(cacheproperties.fcMaxSize, cacheproperties.fcForce, cacheproperties.fcRecycle, cacheproperties.fcDefaultExpire, cacheproperties.fcCheckModifiedSince, cacheproperties.fcLoad, cacheproperties.diskBufferSize, cacheproperties.netBufferSize, CC);
        if(CC != null)
            CC.setLevel(cacheproperties.logLevel);
        CD = cacheproperties.useInternalCaches;
        CE = cacheproperties.itcMaxSize;
        CF = cacheproperties.imcMaxSize;
    }

    public void loadExternalFonts(FlashEnvironment flashenvironment, Script script)
        throws GeneratorApiException
    {
        try
        {
            int i = script.getExternalFontCount();
            if(i > 0)
                CE(flashenvironment, C7("ExtFontWarn"));
            for(int j = 0; j < i; j++)
            {
                String s = script.getExternalFontFile(j);
                Object obj = C6(flashenvironment, s);
                if(obj != null)
                {
                    Buffer buffer = new Buffer();
                    buffer.write((byte[])obj);
                    script.loadExternalFontBuffer(j, buffer);
                    buffer.release();
                }
            }

        }
        catch(GeneratorApiException generatorapiexception)
        {
            CD(flashenvironment, C7("ExtFontErrProcess"));
            throw generatorapiexception;
        }
    }

    private void CB()
    {
        CB(false);
    }

    private void CB(boolean flag)
    {
        if(CC != null)
        {
            if(flag)
                CC.setForce(true);
            CC.logMessage("", false);
            if(flag)
                CC.setForce(false);
        }
    }

    private void CC(String s, int i)
    {
        if(CC != null && i <= CC.getLevel())
            CC.logMessage(s);
    }

    private void CD(FlashEnvironment flashenvironment, String s)
    {
        if(flashenvironment != null)
            flashenvironment.logError(s);
        else
            System.out.println(s);
    }

    public void logHitCounts()
    {
        CB(true);
        if(C8 != null)
            C8.logHitCounts();
        if(C9 != null)
            C9.logHitCounts();
        if(CA != null)
            CA.logHitCounts();
        if(CB != null)
            CB.logHitCounts();
        CB(true);
    }

    private void CE(FlashEnvironment flashenvironment, String s)
    {
        if(flashenvironment != null)
            flashenvironment.logWarning(s);
        else
            System.out.println(s);
    }

    public String makeRequestString(String s, String s1)
    {
        if(s == null)
        {
            return null;
        } else
        {
            String s2 = s + (s1 == null ? "" : "?" + s1);
            return s2;
        }
    }

    public void purgeCaches()
    {
        if(C8 != null)
            C8.purge();
        if(C9 != null)
            C9.purge();
        if(CA != null)
            CA.purge();
        if(CB != null)
            CB.purge();
    }

    public void setProperties(CacheProperties cacheproperties)
    {
        if(CC != null)
        {
            CB();
            CC(C5("SettingCacheProps"), 2);
            CC.setLevel(cacheproperties.logLevel);
            CC.setCycle(cacheproperties.logCycle, cacheproperties.logsMax);
        }
        if(C8 != null)
        {
            C8.setMaxSize(cacheproperties.rcMaxSize);
            C8.setForce(cacheproperties.rcForce);
            C8.setDefaultExpire(cacheproperties.rcDefaultExpire);
            C8.setRecycle(cacheproperties.rcRecycle);
        }
        if(C9 != null)
        {
            C9.setMaxSize(cacheproperties.tcMaxSize);
            C9.setForce(cacheproperties.tcForce);
            C9.setDefaultExpire(cacheproperties.tcDefaultExpire);
            C9.setRecycle(cacheproperties.tcRecycle);
            C9.setCheckModifiedSince(cacheproperties.tcCheckModifiedSince);
            C9.setBufferSize(cacheproperties.diskBufferSize, cacheproperties.netBufferSize);
        }
        if(CA != null)
        {
            CA.setMaxSize(cacheproperties.mcMaxSize);
            CA.setForce(cacheproperties.mcForce);
            CA.setDefaultExpire(cacheproperties.mcDefaultExpire);
            CA.setRecycle(cacheproperties.mcRecycle);
            CA.setCheckModifiedSince(cacheproperties.mcCheckModifiedSince);
            CA.setBufferSize(cacheproperties.diskBufferSize, cacheproperties.netBufferSize);
        }
        if(CB != null)
        {
            CB.setMaxSize(cacheproperties.fcMaxSize);
            CB.setForce(cacheproperties.fcForce);
            CB.setDefaultExpire(cacheproperties.fcDefaultExpire);
            CB.setRecycle(cacheproperties.fcRecycle);
            CB.setCheckModifiedSince(cacheproperties.fcCheckModifiedSince);
            CB.setBufferSize(cacheproperties.diskBufferSize, cacheproperties.netBufferSize);
            CB.setLoad(cacheproperties.fcLoad);
        }
        if(CC != null)
            CB();
    }

    public String stripGeneratorParams(String s)
    {
        if(s == null)
            return null;
        String s1 = s;
        int i = s.indexOf(63);
        if(i > -1)
        {
            s1 = s.substring(0, i + 1);
            String s2 = s.substring(i + 1, s.length());
            StringTokenizer stringtokenizer = new StringTokenizer(s2, "&");
            boolean flag = true;
            while(stringtokenizer.hasMoreTokens()) 
            {
                String s3 = stringtokenizer.nextToken();
                if(i > 0 && !s3.startsWith("grc") && !s3.startsWith("gre"))
                {
                    if(!flag)
                        s1 = s1 + "&";
                    s1 = s1 + s3;
                    flag = false;
                }
            }
            if(s1.endsWith("?"))
                s1 = s1.substring(0, s1.length() - 1);
        }
        return s1;
    }

    private String CF(String s)
    {
        if(s.startsWith("\""))
            s = s.substring(1);
        if(s.endsWith("\""))
            s = s.substring(0, s.length() - 1);
        return s;
    }

    public boolean useInternalCache()
    {
        return CD;
    }

    private boolean C2;
    private final String C3 = "grc";
    private final String C4 = "gre";
    private final String C5 = "gmc";
    private final String C6 = "gme";
    private final String C7 = "\r\n";
    private RequestCache C8;
    private FontCache C9;
    private MediaCache CA;
    private FileCache CB;
    private CacheLog CC;
    private boolean CD;
    private long CE;
    private long CF;
}