package org.rythmengine.spring;

import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.rythmengine.extension.ITemplateResourceLoader;
import org.rythmengine.resource.ITemplateResource;
import org.rythmengine.resource.TemplateResourceBase;
import org.rythmengine.utils.IO;
import org.springframework.core.io.*;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: luog
 * Date: 25/11/13
 * Time: 11:44 AM
 * To change this template use File | Settings | File Templates.
 */
class SpringTemplateResource extends TemplateResourceBase implements ITemplateResource {

    Logger logger = L.get(ITemplateResource.class);

    private Resource springResource = null;
    private String key;
    private boolean isFile;
    private boolean isValid = true;

    SpringTemplateResource(Resource rsrc, ITemplateResourceLoader loader) {
        super(loader);
        if (null == rsrc) {
            throw new NullPointerException();
        }
        springResource = rsrc;
        isFile = (rsrc instanceof AbstractFileResolvingResource) ||
            (rsrc instanceof FileSystemResource) || (rsrc instanceof VfsResource);

        if (!isFile) {
            key = reload();
        } else {
            try {
                key = getPath(rsrc);
                if (!key.startsWith("/")) {
                    key = ("/" + key).replace(loader.getResourceLoaderRoot(), "");
                } else {
                    key = key.replace(loader.getResourceLoaderRoot(), "");
                }
                isValid = rsrc.isReadable();
            } catch (IOException e) {
                logger.warn(e, "error loading spring resource: %s", rsrc.getFilename());
                try {
                    key = reload();
                } catch (RuntimeException e1) {
                    isValid = false;
                }
                isFile = false;
            }
        }
    }

    private String getPath(Resource rsrc) throws IOException {
        if (rsrc instanceof ContextResource) {
            return ((ContextResource)rsrc).getPathWithinContext();
        }
        if (rsrc instanceof ClassPathResource) {
            return ((ClassPathResource)rsrc).getPath();
        }
        if (rsrc instanceof AbstractFileResolvingResource) {
            return rsrc.getFile().getPath();
        }
        if (rsrc instanceof FileSystemResource) {
            return ((FileSystemResource)rsrc).getPath();
        }
        throw new IOException("Cannot find resource path");
    }

    @Override
    public Object getKey() {
        return key;
    }

    @Override
    public boolean isValid() {
        return isValid;
    }

    @Override
    protected long defCheckInterval() {
        return isFile ? 1000 * 5 : -1;
    }

    @Override
    protected long lastModified() {
        if (!isFile) return 0;
        try {
            return springResource.lastModified();
        } catch (IOException e) {
            logger.warn(e, "error refreshing spring resource: %s", springResource.getFilename());
            return -1;
        }
    }

    @Override
    protected String reload() {
        if (!isFile) {
            return key;
        }
        try {
            return IO.readContentAsString(springResource.getFile());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getSuggestedClassName() {
        return path2CN(key);
    }
}

@@ -318,16 +318,63 @@
 re.io.*;
+%0Aimport org.springframework.util.ResourceUtils;
 %0A%0Aimport
@@ -394,16 +394,37 @@
 ception;
+%0Aimport java.net.URL;
 %0A%0A/**%0A *
@@ -3275,60 +3275,487 @@
     
-return IO.readContentAsString(springResource.getFile
+if (getEngine().isDevMode()) %7B%0A                URL url = springResource.getURL();%0A                if (ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())) %7B%0A                    // this ensures it always load the file changes%0A                    // instead of cached content in the inputstream%0A                    return IO.readContentAsString(springResource.getFile());%0A                %7D%0A            %7D%0A            return IO.readContentAsString(springResource.getInputStream
 ());

fix resource loading issue