| 1 | |
package org.webslinger; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.util.Arrays; |
| 6 | |
import java.util.Collection; |
| 7 | |
import java.util.logging.Logger; |
| 8 | |
|
| 9 | |
import javax.servlet.ServletException; |
| 10 | |
import javax.servlet.http.HttpServletRequest; |
| 11 | |
|
| 12 | |
import org.apache.commons.vfs.FileObject; |
| 13 | |
import org.apache.commons.vfs.FileSystemManager; |
| 14 | |
import org.apache.commons.vfs.FileSystemOptions; |
| 15 | |
import org.apache.commons.vfs.impl.StandardFileSystemManager; |
| 16 | |
|
| 17 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 18 | |
import org.webslinger.commons.vfs.handlers.ExclusionHandler; |
| 19 | |
import org.webslinger.commons.vfs.handlers.HandlerFactory; |
| 20 | |
import org.webslinger.commons.vfs.flat.FlatConfigBuilder; |
| 21 | |
import org.webslinger.commons.vfs.flat.FlatFileSystem; |
| 22 | |
import org.webslinger.container.WebslingerContainer; |
| 23 | |
import org.webslinger.modules.ModuleState; |
| 24 | |
import org.webslinger.modules.ModuleStateDispatcher; |
| 25 | |
import org.webslinger.modules.ModuleStateXML; |
| 26 | |
import org.webslinger.modules.WebslingerModulesConfigBuilder; |
| 27 | |
import org.webslinger.modules.StandardModuleState; |
| 28 | |
import org.webslinger.servlet.webxml.WebXml; |
| 29 | |
import org.webslinger.util.MimeTypeLookup; |
| 30 | |
import org.webslinger.util.MimeTypes; |
| 31 | |
import org.webslinger.xml.XmlUtil; |
| 32 | |
|
| 33 | 26 | public abstract class AbstractWebslingerServletContextFactory implements WebslingerServletContextFactory { |
| 34 | 1 | private static final Logger logger = Logger.getLogger(AbstractWebslingerServletContextFactory.class.getName()); |
| 35 | |
|
| 36 | |
public void initializeRequest(WebslingerServletContext context, HttpServletRequest request) { |
| 37 | 526 | } |
| 38 | |
|
| 39 | |
protected FileObject resolveFile(FileObject root, String url) throws IOException { |
| 40 | 3 | return root.getFileSystem().getFileSystemManager().resolveFile(url); |
| 41 | |
} |
| 42 | |
|
| 43 | |
protected MimeTypes getDefaultMimeTypes() throws IOException { |
| 44 | 26 | MimeTypes mimeTypes = new MimeTypes(); |
| 45 | 26 | mimeTypes.load(getClass().getClassLoader().getResourceAsStream("org/webslinger/mime.types")); |
| 46 | 26 | return mimeTypes; |
| 47 | |
} |
| 48 | |
|
| 49 | |
protected FileObject getRoot(String rootUrl, String modulesBase) throws IOException { |
| 50 | 26 | FileSystemOptions options = new FileSystemOptions(); |
| 51 | 26 | StandardFileSystemManager sfsm = VFSUtil.createStandardFileSystemManager(); |
| 52 | 26 | FileObject currentDir = VFSUtil.toFileObject(sfsm, new File("."), options); |
| 53 | 26 | sfsm.setBaseFile(currentDir); |
| 54 | 26 | FileObject root = VFSUtil.toFileObject(sfsm, rootUrl, options); |
| 55 | 26 | sfsm.setBaseFile(root); |
| 56 | 26 | FileObject modules = VFSUtil.toFileObject(sfsm, modulesBase, options); |
| 57 | 26 | logger.info("New Root(" + root + ", " + modules + ")"); |
| 58 | 26 | WebslingerModulesConfigBuilder.getInstance().setModulesBase(options, modules); |
| 59 | 26 | FlatConfigBuilder.getInstance().setExclusionHandlerFactory(options, new HandlerFactory<ExclusionHandler, FlatFileSystem>() { |
| 60 | |
public ExclusionHandler getHandler(FlatFileSystem fs, Class<ExclusionHandler> clz) { |
| 61 | 24 | return new ExclusionHandler() { |
| 62 | |
public void excludeNames(Collection<String> names) { |
| 63 | 2617 | names.remove(".svn"); |
| 64 | 2617 | names.remove(".git"); |
| 65 | 2617 | names.remove(".hg"); |
| 66 | 2617 | } |
| 67 | |
}; |
| 68 | |
} |
| 69 | |
}); |
| 70 | 26 | return root; |
| 71 | |
} |
| 72 | |
|
| 73 | |
protected WebslingerServletContext newWebslingerServletContext(String id, FileObject root, FileObject[] bases) throws IOException, ServletException { |
| 74 | 26 | logger.info("newWebslingerServletContext(" + root + ", " + (bases == null ? "null" : Arrays.asList(bases).toString()) + ")"); |
| 75 | 26 | FileObject moduleStateXmlFile = root.resolveFile("Config/ModuleState.xml"); |
| 76 | 26 | if (!moduleStateXmlFile.exists()) moduleStateXmlFile = root.resolveFile("Var/ModuleState.xml"); |
| 77 | 26 | if (!moduleStateXmlFile.exists()) { |
| 78 | 7 | logger.warning("Couldn't find ModuleState.xml, tried Config and Var"); |
| 79 | 7 | moduleStateXmlFile = root.resolveFile("Config/ModuleState.xml"); |
| 80 | |
} |
| 81 | |
FileObject virtualRoot; |
| 82 | 26 | if (moduleStateXmlFile.exists() || (bases != null && bases.length > 0)) { |
| 83 | 21 | logger.info("Using COW filesystem layer"); |
| 84 | 21 | ModuleStateXML moduleStorage = new ModuleStateXML(moduleStateXmlFile); |
| 85 | 21 | StandardModuleState moduleState = new StandardModuleState(root, bases); |
| 86 | 21 | moduleStorage.init(moduleState); |
| 87 | 21 | ModuleState moduleDispatcher = new ModuleStateDispatcher(moduleStorage, moduleState); |
| 88 | 21 | virtualRoot = moduleState.getRoot(); |
| 89 | 21 | } else { |
| 90 | 5 | logger.info("Not using COW filesystem layer"); |
| 91 | 5 | FileSystemManager fsm = root.getFileSystem().getFileSystemManager(); |
| 92 | 5 | virtualRoot = fsm.createFileSystem("flat", root); |
| 93 | |
} |
| 94 | |
WebXml webXml; |
| 95 | 26 | FileObject webXmlFile = virtualRoot.resolveFile("www/WEB-INF/web.xml"); |
| 96 | 26 | if (webXmlFile.exists()) { |
| 97 | |
try { |
| 98 | 21 | webXml = new WebXml(getDefaultMimeTypes(), XmlUtil.read(webXmlFile.getContent().getInputStream())); |
| 99 | 0 | } catch (IOException e) { |
| 100 | 0 | throw e; |
| 101 | 0 | } catch (RuntimeException e) { |
| 102 | 0 | throw e; |
| 103 | 0 | } catch (Exception e) { |
| 104 | 0 | throw (IOException) new IOException(e.getMessage()).initCause(e); |
| 105 | 21 | } |
| 106 | |
} else { |
| 107 | 5 | webXml = new WebXml(getDefaultMimeTypes()); |
| 108 | |
} |
| 109 | 26 | WebslingerContainer container = new WebslingerContainer(this, virtualRoot, webXml); |
| 110 | 26 | WebslingerServletContext context = new WebslingerServletContext(id, container); |
| 111 | 26 | context.init(); |
| 112 | 26 | return context; |
| 113 | |
} |
| 114 | |
} |