001    /*
002      Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.gui.web;
019    
020    import java.io.File;
021    import java.io.FileInputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    import java.net.JarURLConnection;
026    import java.net.URL;
027    import java.net.URLConnection;
028    import java.util.Date;
029    import org.apache.log4j.Logger;
030    import org.mortbay.util.Resource;
031    import org.objectweb.jac.util.ExtArrays;
032     
033    public class ClasspathResource extends Resource {
034        static Logger logger = Logger.getLogger("web");
035    
036        long lastModified = new Date().getTime();
037        String path;
038        URLConnection connection;
039        File file;
040        boolean isFile;
041        public ClasspathResource(String path) throws IOException {
042            if (path.startsWith("/")) {
043                path = path.substring(1);
044            }
045            this.path = path;
046            URL url = getClass().getClassLoader().getResource(path);
047            if (url!=null) {
048                file = new File(url.getFile());
049                connection = url.openConnection();
050                if (connection instanceof JarURLConnection) {
051                    file = new File(((JarURLConnection)connection).getJarFileURL().getFile());
052                } 
053                logger.debug("New classpath resource: "+url+" isFile="+file.isFile());
054            } else {
055                //throw new RuntimeException("Resource not found: "+path);
056                logger.debug("Resource not found: "+path);
057            }
058        }
059        public ClasspathResource() {
060            path = null;
061        }
062        public void release() {
063        }
064        public boolean exists() {
065            boolean exists =  isFile ? file.exists() : getInputStream()!=null;
066            logger.debug("exists "+path+"? -> "+exists);
067            return exists;            
068        }
069        public boolean isDirectory() {
070            return false;
071        }
072        public long lastModified() {
073            if (file.exists())
074                return file.lastModified();
075            else
076                return lastModified;
077        }
078        public long length() {
079            try {
080                if (isFile) {
081                    return file.length();
082                } else {
083                    InputStream is = getInputStream();
084                    if (is!=null)
085                        return getInputStream().available();
086                    else 
087                        return 0;
088                }
089            } catch(Exception e) {
090                logger.error("Failed to get length of resource: "+path,e);
091                return 0;
092            }
093        }
094        public URL getURL() {
095            return null;
096        }
097        public File getFile() {
098            return file;
099        }
100        public String getName() {
101            return path;
102        }
103        public InputStream getInputStream() {
104            logger.debug("getInputStream "+file);
105            if (isFile) {
106                try {
107                    return new FileInputStream(file);
108                } catch (IOException e) {
109                    logger.error("getInputStream "+path,e);
110                    return null;
111                }
112            } else {
113                return getClass().getClassLoader().getResourceAsStream(path);
114            }
115        }
116        public OutputStream getOutputStream() {
117            return null;
118        }
119        public boolean delete() {
120            return false;
121        }
122        public boolean renameTo(Resource newName) {
123            return false;
124        }
125        public String[] list() {
126            return ExtArrays.emptyStringArray;
127        }
128        public Resource addPath(String addedPath) throws IOException {
129            if (path==null)
130                return new ClasspathResource(addedPath);
131            else 
132                return this;
133        }
134        public String toString() {
135            return path;
136        }
137    
138    }