KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > MainWebSite


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.servlet.*;
28 import org.meshcms.util.*;
29
30 public class MainWebSite extends WebSite {
31   private SortedMap virtualSitesMap;
32   private MultiSiteManager multiSiteManager;
33
34   /**
35    * Creates a new main website.
36    */

37   protected static WebSite create(ServletContext sc,
38       String JavaDoc[] welcomeFiles, File rootFile, Path rootPath, Path cmsPath) {
39     MainWebSite mainWebSite = new MainWebSite();
40     mainWebSite.init(sc, welcomeFiles, rootFile, rootPath, cmsPath);
41     return mainWebSite;
42   }
43
44   /**
45    * Initializes the website. After calling the method of the superclass,
46    * initializes the virtual websites.
47    */

48   protected void init(ServletContext sc, String JavaDoc[] welcomeFiles, File rootFile,
49       Path rootPath, Path cmsPath) {
50     super.init(sc, welcomeFiles, rootFile, rootPath, cmsPath);
51
52     if (virtualSitesMap == null) {
53       virtualSitesMap = new TreeMap();
54     }
55
56     if (multiSiteManager == null) {
57       multiSiteManager = MultiSiteManager.load(this);
58     }
59
60     multiSiteManager.initDomainsMap();
61   }
62
63   /**
64    * Returns the right website for the given request. Since this is a main
65    * website, it will return the website itself or a virtual website, according
66    * to the requested host name.
67    */

68   public WebSite getWebSite(ServletRequest request) {
69     return multiSiteManager.getWebSite(request.getServerName());
70   }
71
72   public String JavaDoc getTypeDescription() {
73     return "main web site";
74   }
75
76   /**
77    * Returns the virtual website instance related to the given directory name.
78    * That instance will be created if not found, and will not fail if the
79    * directory does not exist (this is subject to change).
80    */

81   public VirtualWebSite getVirtualSite(String JavaDoc dirName) {
82     VirtualWebSite vws = (VirtualWebSite) virtualSitesMap.get(dirName);
83
84     if (vws == null) {
85       Path sitePath = virtualSitesPath.add(dirName);
86       File rootFile = getFile(sitePath);
87       Path cmsPath = new CMSDirectoryFinder(rootFile, true).getCMSPath();
88       vws = VirtualWebSite.create(this, sitePath, cmsPath);
89       virtualSitesMap.put(dirName, vws);
90     }
91
92     return vws;
93   }
94
95   /**
96    * @return the MultiSiteManager instance.
97    */

98   public MultiSiteManager getMultiSiteManager() {
99     return multiSiteManager;
100   }
101
102   public void updateSiteMap(boolean force) {
103     super.updateSiteMap(force);
104
105     if (multiSiteManager != null) {
106       multiSiteManager.initDomainsMap();
107     }
108   }
109
110   /* public String getHost(String dirName) {
111     WebSite site = multiSiteManager.getWebSite(dirName);
112
113     if (site != null) {
114       String host = site.getConfiguration().getSiteHost();
115
116       if (!Utils.isNullOrEmpty(host) &&
117           multiSiteManager.getWebSite(host).equals(site)) {
118         return host;
119       }
120
121       if (multiSiteManager.isUseDirsAsDomains()) {
122         return dirName;
123       }
124
125       StringTokenizer st =
126           new StringTokenizer(multiSiteManager.getDomains(dirName), ";:, \t");
127
128       if (st.hasMoreTokens()) {
129         return st.nextToken();
130       }
131     }
132
133     return null;
134   } */

135 }
136
Popular Tags