Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawServletContext.java, revision 1.17

1.1       bmahe       1: // JigsawServletContext.java
1.17    ! bmahe       2: // $Id: JigsawServletContext.java,v 1.16 1998/09/28 12:36:50 bmahe Exp $
1.1       bmahe       3: // (c) COPYRIGHT MIT and INRIA, 1998.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5: 
                      6: package org.w3c.jigsaw.servlet;
                      7: 
                      8: import java.io.*;
                      9: import java.net.*;
                     10: import java.util.*;
                     11: 
                     12: import javax.servlet.*;
                     13: 
1.11      bmahe      14: import org.w3c.util.*;
1.1       bmahe      15: import org.w3c.tools.resources.store.*;
1.5       bmahe      16: import org.w3c.tools.resources.event.*;
1.1       bmahe      17: import org.w3c.tools.resources.*;
                     18: import org.w3c.jigsaw.frames.*;
                     19: 
                     20: /**
1.17    ! bmahe      21:  * @version $Revision: 1.16 $
1.1       bmahe      22:  * @author  Benoît Mahé (bmahe@w3.org)
                     23:  */
1.5       bmahe      24: public class JigsawServletContext extends StructureChangedAdapter
                     25:                                   implements ServletContext,  
1.11      bmahe      26:                                             PropertyMonitoring
1.5       bmahe      27: {
                     28: 
                     29:     class Logger {
                     30:        File             logfile  = null;
                     31:        RandomAccessFile log      = null ;
                     32:        byte             msgbuf[] = null ;
                     33:        boolean          closed   = true;      
                     34: 
                     35:        private final String monthnames[] = {
                     36:            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                     37:            "Jui", "Aug", "Sep", "Oct", "Nov", "Dec"
                     38:        };
                     39: 
                     40:        String getDate() {
                     41:            Date now = new Date();
                     42:            return (now.getDate()
                     43:                    + "/" + monthnames[now.getMonth()]
                     44:                    + "/" + (now.getYear() + 1900)
                     45:                    + ((now.getHours() < 10)
                     46:                       ? (":0" + now.getHours())
                     47:                       : (":" + now.getHours()))
                     48:                    + ((now.getMinutes() < 10)
                     49:                       ? (":0" + now.getMinutes())
                     50:                       : (":" + now.getMinutes()))
                     51:                    + ((now.getSeconds() < 10)
                     52:                       ? (":0" + now.getSeconds())
                     53:                       : (":" + now.getSeconds()))
                     54:                    + ((now.getTimezoneOffset() < 0)
                     55:                       ? " " + (now.getTimezoneOffset() / 60)
                     56:                       : " +" + (now.getTimezoneOffset() / 60)));
                     57:        }
                     58: 
                     59:        void log(String msg) {
                     60:            msg = "["+getDate()+"] "+msg+"\n";
                     61:            try {
                     62:                if ( log == null || closed)
                     63:                    openLogFile();
                     64:                if ( log != null ) {
                     65:                    int len = msg.length() ;
                     66:                    if ( len > msgbuf.length ) 
                     67:                        msgbuf = new byte[len] ;
                     68:                    msg.getBytes (0, len, msgbuf, 0) ;
                     69:                    log.write (msgbuf, 0, len) ;
                     70:                }
                     71:            } catch (IOException ex) {
1.7       bmahe      72:                System.out.println("Can't write ("+
                     73:                                   msg+") to logfile ["+
                     74:                                   logfile+"] : "+ex.getMessage());
1.5       bmahe      75:            }
                     76:        }
                     77:  
                     78:        void log(Exception ex, String msg) {
1.15      bmahe      79:            log(msg+" : "+ex.getClass().getName()+" ("+ex.getMessage()+")");
1.5       bmahe      80:        }
                     81: 
                     82:        void openLogFile() 
                     83:            throws IOException
                     84:        {
                     85:            RandomAccessFile old = log ;
                     86:            log = new RandomAccessFile (logfile, "rw") ;
                     87:            log.seek (log.length()) ;
                     88:            closed = false;
                     89:            if ( old != null )
                     90:                old.close () ;
                     91:        }
                     92: 
                     93:        void close() {
                     94:            try {
                     95:                if (log != null)
                     96:                    log.close();
                     97:                closed = true;
                     98:            } catch (IOException ex) {
                     99:                ex.printStackTrace();
                    100:            }
                    101:        }
                    102: 
                    103:        Logger(File logfile) {
                    104:            this.logfile = logfile;
                    105:            this.msgbuf  = new byte[128] ;
                    106:            log("Servlet Logger started");
                    107:        }
                    108:     }
1.9       bmahe     109: 
1.1       bmahe     110:     private ResourceReference reference = null;
                    111: 
1.5       bmahe     112:     private Logger logger = null;
                    113: 
1.11      bmahe     114:     private ObservableProperties props = null;
                    115: 
1.15      bmahe     116:     private File directory = null;
                    117: 
1.12      bmahe     118:     protected static String logdir     = "logs" ;
                    119: 
                    120:     protected static String deflogfile = "servlets";
                    121: 
1.11      bmahe     122:     public boolean propertyChanged (String name) {
1.13      bmahe     123:        if (name.equals(ServletProps.SERVLET_LOG_FILE_P)) {
1.5       bmahe     124:            if (logger != null) {
                    125:                logger.close();
1.11      bmahe     126:                File newlogfile = 
                    127:                    new File((String) props.get(
1.13      bmahe     128:                                            ServletProps.SERVLET_LOG_FILE_P));
1.11      bmahe     129:                if (newlogfile.getPath().length() < 1) 
1.7       bmahe     130:                    newlogfile = getServletLogFile();
                    131:                logger = new Logger(newlogfile);
1.5       bmahe     132:            }
                    133:        }
1.11      bmahe     134:        return true;
1.5       bmahe     135:     }
                    136: 
                    137:     public void resourceUnloaded(StructureChangedEvent evt){
                    138:        if (logger != null) {
                    139:            logger.close();
                    140:        }
                    141:     }
                    142: 
1.1       bmahe     143:     /**
                    144:      * A useful utility routine that tries to guess the content-type
                    145:      * of an object based upon its extension.
                    146:      */
                    147:     protected static String guessContentTypeFromName(String fname) {
1.17    ! bmahe     148:        return org.w3c.www.mime.Utils.guessContentTypeFromName(fname);
1.1       bmahe     149:     }
                    150: 
                    151:     /**
                    152:      * ServletContext implementation - Get the MIME type for given file.
                    153:      */
                    154: 
                    155:     public String getMimeType(String filename) {
                    156:        return guessContentTypeFromName(filename);
1.7       bmahe     157:     }
                    158: 
1.11      bmahe     159:     protected ServerInterface getServer() {
1.7       bmahe     160:        try {
                    161:            Resource res = reference.lock();
1.11      bmahe     162:            return ((ServletDirectoryFrame)res).getServer();
1.7       bmahe     163:        } catch(InvalidResourceException ex) {
                    164:            ex.printStackTrace();
                    165:            return null;
                    166:        } finally {
                    167:            reference.unlock();
                    168:        }
1.1       bmahe     169:     }
                    170: 
1.11      bmahe     171:     public File getServletLogFile() {
                    172:        ServerInterface server = getServer();
                    173:        File logfile = null;
                    174:        String file = (String)
1.14      bmahe     175:            server.getProperties().getString(ServletProps.SERVLET_LOG_FILE_P,
                    176:                                             null);
1.11      bmahe     177:        if (file != null)
                    178:            logfile = new File(file);
                    179:        if ((logfile != null) && (logfile.getPath().length() < 1))
                    180:            logfile = null;
                    181:        if (logfile == null) {
                    182:            File root_dir = server.getRootDirectory();
                    183:            if (root_dir == null) {
                    184:                throw new RuntimeException("unable to build a default "+
                    185:                                           "value for the servlet log file.");
                    186:            }
                    187:            logfile = new File(new File(root_dir, logdir), deflogfile);
1.16      bmahe     188:            server.getProperties().putValue(ServletProps.SERVLET_LOG_FILE_P,
                    189:                                            logfile.getAbsolutePath());
1.11      bmahe     190:        }
                    191:        return logfile;
                    192:     }
                    193: 
1.1       bmahe     194:     /**
                    195:      * ServletContext implementation - Lookup a given servlet.
                    196:      */
                    197: 
                    198:     public Servlet getServlet(String name) {
                    199:        try {
                    200:            Resource res = reference.lock();
                    201:            return ((ServletDirectoryFrame)res).getServlet(name);
                    202:        } catch(InvalidResourceException ex) {
                    203:            ex.printStackTrace();
                    204:            return null;
                    205:        } finally {
                    206:            reference.unlock();
                    207:        }
                    208:     }
                    209: 
                    210:     /**
                    211:      * ServletContext implementation - Enumerate all servlets within context.
                    212:      */
                    213: 
                    214:     public Enumeration getServlets() {
                    215:        try {
                    216:            Resource res = reference.lock();
                    217:            return ((ServletDirectoryFrame)res).getServlets();
                    218:        } catch(InvalidResourceException ex) {
                    219:            ex.printStackTrace();
                    220:            return null;
                    221:        } finally {
                    222:            reference.unlock();
                    223:        }
                    224:     }
1.4       bmahe     225: 
                    226:     /**
                    227:      * ServletContext implementation - Enumerate all servlets names 
                    228:      * within context.
                    229:      */
1.1       bmahe     230: 
                    231:     public Enumeration getServletNames() {
1.2       bmahe     232:        try {
                    233:            Resource res = reference.lock();
                    234:            return ((ServletDirectoryFrame)res).getServletNames();
                    235:        } catch(InvalidResourceException ex) {
                    236:            ex.printStackTrace();
                    237:            return null;
                    238:        } finally {
                    239:            reference.unlock();
                    240:        }
1.1       bmahe     241:     }
                    242: 
                    243:     /**
                    244:      * ServletContext implementation - Log a message.
                    245:      */
                    246:     
                    247:     public void log(String msg) {
1.5       bmahe     248:        logger.log(msg);
1.1       bmahe     249:     }
                    250:     
                    251:     public void log(Exception ex, String msg) {
1.5       bmahe     252:        logger.log(ex,msg);
1.1       bmahe     253:     }
                    254: 
                    255:     /**
                    256:      * ServletContext implementation - Translate a piece of path.
                    257:      */
                    258: 
                    259:     public String getRealPath(String path) {
                    260:        return path;
                    261:     }
                    262: 
                    263:     /**
                    264:      * ServletContext implementation - Get server informations.
                    265:      */
                    266: 
                    267:     public String getServerInfo() {
                    268:        try {
                    269:            Resource res = reference.lock();
                    270:            return ((ServletDirectoryFrame)res).getServerInfo();
                    271:        } catch(InvalidResourceException ex) {
                    272:            ex.printStackTrace();
                    273:            return null;
                    274:        } finally {
                    275:            reference.unlock();
                    276:        }       
                    277:     }
                    278: 
                    279:     /**
                    280:      * ServletContext implementation - Get an attribute value.
                    281:      * We map this into the ServletWrapper attributes, without
                    282:      * support for name clashes though.
                    283:      * @param name The attribute name.
                    284:      */
                    285:     
                    286:     public Object getAttribute(String name) {
                    287:        try {
                    288:            Resource res = reference.lock();
                    289:            return ((ServletDirectoryFrame)res).getAttribute(name);
                    290:        } catch(InvalidResourceException ex) {
                    291:            ex.printStackTrace();
                    292:            return null;
                    293:        } finally {
                    294:            reference.unlock();
                    295:        }
                    296:     }
                    297: 
1.15      bmahe     298:     private AutoReloadServletLoader loader = null;
                    299: 
                    300:     /** 
                    301:      * Get or create a suitable LocalServletLoader instance to load 
                    302:      * that servlet.
                    303:      * @return A LocalServletLoader instance.
                    304:      */
                    305:     protected synchronized AutoReloadServletLoader getLocalServletLoader() {
                    306:        if ( loader == null ) {
                    307:            loader = new AutoReloadServletLoader(this);
                    308:        }
                    309:        return loader;
                    310:     }
                    311: 
                    312:     protected synchronized 
                    313:        AutoReloadServletLoader createNewLocalServletLoader (boolean keepold) 
                    314:     {
                    315:        if ((loader != null) && keepold)
                    316:            loader = new AutoReloadServletLoader(loader);
                    317:        else
                    318:            loader = new AutoReloadServletLoader(this);
                    319:        return loader;
                    320:     }
                    321: 
                    322:     public File getServletDirectory() {
                    323:        return directory;
                    324:     }
                    325: 
1.1       bmahe     326:     /**
                    327:      * Create a new ServletContext.
                    328:      * @param ref a ResourceReference pointing on a ServletDirectoryFrame.
                    329:      */
1.11      bmahe     330:     protected JigsawServletContext(ResourceReference ref, 
                    331:                                   ObservableProperties props) 
                    332:     {
1.1       bmahe     333:        this.reference = ref;
1.11      bmahe     334:        this.props     = props;
                    335:        props.registerObserver(this);
                    336:        this.logger = new Logger(getServletLogFile());
1.15      bmahe     337:        this.loader = new AutoReloadServletLoader(this);
1.3       bmahe     338:        try {
                    339:            Resource res = reference.lock();
                    340:            if (! (res instanceof ServletDirectoryFrame)) {
                    341:                throw new RuntimeException("This reference is not pointing on"+
                    342:                                           " a ServletDirectoryFrame.");
1.5       bmahe     343:            } else {
                    344:                ServletDirectoryFrame sframe = (ServletDirectoryFrame)res;
                    345:                FramedResource resource = (FramedResource)sframe.getResource();
                    346:                resource.addStructureChangedListener(this);
1.15      bmahe     347:                if (resource.definesAttribute("directory"))
                    348:                    this.directory = 
                    349:                        (File) resource.getValue("directory", null);
1.3       bmahe     350:            }
                    351:        } catch(InvalidResourceException ex) {
                    352:            throw new RuntimeException("This reference is pointing on"+
                    353:                                       " an Invalid ServletDirectoryFrame.");
                    354:        } finally {
                    355:            reference.unlock();
                    356:        }
1.1       bmahe     357:     }
                    358: 
                    359: }

Webmaster