001: /*
002: * MeshCMS - A simple CMS based on SiteMesh
003: * Copyright (C) 2004-2007 Luciano Vernaschi
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * You can contact the author at http://www.cromoteca.com
020: * and at info@cromoteca.com
021: */
022:
023: package org.meshcms.core;
024:
025: import java.io.*;
026: import java.util.*;
027: import javax.servlet.*;
028: import org.meshcms.util.*;
029:
030: public class MainWebSite extends WebSite {
031: private SortedMap virtualSitesMap;
032: private MultiSiteManager multiSiteManager;
033:
034: /**
035: * Creates a new main website.
036: */
037: protected static WebSite create(ServletContext sc,
038: String[] welcomeFiles, File rootFile, Path rootPath,
039: Path cmsPath) {
040: MainWebSite mainWebSite = new MainWebSite();
041: mainWebSite.init(sc, welcomeFiles, rootFile, rootPath, cmsPath);
042: return mainWebSite;
043: }
044:
045: /**
046: * Initializes the website. After calling the method of the superclass,
047: * initializes the virtual websites.
048: */
049: protected void init(ServletContext sc, String[] welcomeFiles,
050: File rootFile, Path rootPath, Path cmsPath) {
051: super .init(sc, welcomeFiles, rootFile, rootPath, cmsPath);
052:
053: if (virtualSitesMap == null) {
054: virtualSitesMap = new TreeMap();
055: }
056:
057: if (multiSiteManager == null) {
058: multiSiteManager = MultiSiteManager.load(this );
059: }
060:
061: multiSiteManager.initDomainsMap();
062: }
063:
064: /**
065: * Returns the right website for the given request. Since this is a main
066: * website, it will return the website itself or a virtual website, according
067: * to the requested host name.
068: */
069: public WebSite getWebSite(ServletRequest request) {
070: return multiSiteManager.getWebSite(request.getServerName());
071: }
072:
073: public String getTypeDescription() {
074: return "main web site";
075: }
076:
077: /**
078: * Returns the virtual website instance related to the given directory name.
079: * That instance will be created if not found, and will not fail if the
080: * directory does not exist (this is subject to change).
081: */
082: public VirtualWebSite getVirtualSite(String dirName) {
083: VirtualWebSite vws = (VirtualWebSite) virtualSitesMap
084: .get(dirName);
085:
086: if (vws == null) {
087: Path sitePath = virtualSitesPath.add(dirName);
088: File rootFile = getFile(sitePath);
089: Path cmsPath = new CMSDirectoryFinder(rootFile, true)
090: .getCMSPath();
091: vws = VirtualWebSite.create(this , sitePath, cmsPath);
092: virtualSitesMap.put(dirName, vws);
093: }
094:
095: return vws;
096: }
097:
098: /**
099: * Returns the MultiSiteManager instance.
100: */
101: public MultiSiteManager getMultiSiteManager() {
102: return multiSiteManager;
103: }
104:
105: public void updateSiteMap(boolean force) {
106: super .updateSiteMap(force);
107:
108: if (multiSiteManager != null) {
109: multiSiteManager.initDomainsMap();
110: }
111: }
112:
113: /* public String getHost(String dirName) {
114: WebSite site = multiSiteManager.getWebSite(dirName);
115:
116: if (site != null) {
117: String host = site.getConfiguration().getSiteHost();
118:
119: if (!Utils.isNullOrEmpty(host) &&
120: multiSiteManager.getWebSite(host).equals(site)) {
121: return host;
122: }
123:
124: if (multiSiteManager.isUseDirsAsDomains()) {
125: return dirName;
126: }
127:
128: StringTokenizer st =
129: new StringTokenizer(multiSiteManager.getDomains(dirName), ";:, \t");
130:
131: if (st.hasMoreTokens()) {
132: return st.nextToken();
133: }
134: }
135:
136: return null;
137: } */
138: }
|