Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

RootDirectory.java

00001 package edu.virtualschool.jwaa.xml;
00002 
00003 import java.io.File;
00004 import java.util.HashMap;
00005 import java.util.Iterator;
00006 
00007 import org.apache.log4j.Logger;
00008 
00009 import edu.virtualschool.jwaa.Fault;
00010 import edu.virtualschool.jwaa.GenericIdentifiable;
00011 import edu.virtualschool.jwaa.field.*;
00012 
00023 public final class RootDirectory extends GenericIdentifiable
00024 {
00025   final FileField fileMonitor;
00026   final HashMap appMap = new HashMap();
00027   
00028   final static Logger logger = Logger.getLogger(RootDirectory.class.getName());
00029   
00030   RootDirectory(File file, String servletUrlPrefix)
00031   {
00032     super(servletUrlPrefix);
00033     this.fileMonitor = new FileField(file);
00034   }
00035   void update() throws Fault
00036   {
00037     long timeNow = System.currentTimeMillis();
00038     if (!fileMonitor.isUpToDate())
00039     { 
00040       load();
00041       fileMonitor.update();
00042     }
00043     for (Iterator it = appMap.values().iterator(); it.hasNext(); )
00044     {
00045       ApplicationElement app = (ApplicationElement)it.next();
00046       app.update();
00047       appMap.put(app.id, app);
00048     }
00049   }
00050   void load() throws Fault
00051   {
00052     logger.debug("loading root from "+fileMonitor);
00053     String[] rootNames = fileMonitor.file.list();
00054     for (int i = 0; i < rootNames.length; i++)
00055     {
00056       String name = rootNames[i];
00057       if (name.startsWith("."))
00058         continue;
00059       File courseDirectory = new File(fileMonitor.file, name);
00060       if (courseDirectory.isDirectory())
00061       {
00062         String[] contents = courseDirectory.list();
00063         File file = new File(courseDirectory, "application.xml");
00064         ApplicationElement course = new ApplicationElement(this, file);
00065         appMap.put(course.getIdentity(), course);
00066       }
00067     }
00068   }
00069   final PageElement findPath(String path)
00070     throws Fault
00071   {
00072     update();
00073     if (path == null)
00074       return null;
00075     String[] array = path.split("/");
00076     if (array.length < 1)
00077       return null;
00078     ApplicationElement app = findApplication(array[1]);
00079     PageElement page = null;
00080     if (app != null)
00081     {
00082       if (array.length <= 2)
00083         return app.getOuterHomePage();
00084       page = app.findPage(array[2]);
00085       if (page == null)
00086         page = app.getNotFoundPage();
00087     }
00088     return page;
00089   }
00090   public ApplicationElement findApplication(String id) 
00091     throws PageNotFoundFault
00092   {
00093     Object result = appMap.get(id);
00094     return (ApplicationElement)result;
00095   }
00096 }