KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > site > CmsSiteManager


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/site/CmsSiteManager.java,v $
3  * Date : $Date: 2006/04/28 15:20:52 $
4  * Version: $Revision: 1.52 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.site;
33
34 import org.opencms.configuration.CmsConfigurationException;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.main.CmsRuntimeException;
41 import org.opencms.main.OpenCms;
42 import org.opencms.security.CmsPermissionSet;
43 import org.opencms.security.CmsRole;
44 import org.opencms.util.CmsStringUtil;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collections JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.HashSet JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53 import java.util.Set JavaDoc;
54
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56
57 import org.apache.commons.logging.Log;
58
59 /**
60  * Manages all configured sites in OpenCms.<p>
61  *
62  * @author Alexander Kandzior
63  *
64  * @version $Revision: 1.52 $
65  *
66  * @since 6.0.0
67  */

68 public final class CmsSiteManager implements Cloneable JavaDoc {
69
70     /** The static log object for this class. */
71     private static final Log LOG = CmsLog.getLog(CmsSiteManager.class);
72
73     /** The site that is configured at the moment, need to be recorded for the case that
74      * alias server are added during configuration. */

75     private List JavaDoc m_aliases;
76
77     /** The default site root. */
78     private CmsSite m_defaultSite;
79
80     /** The default uri. */
81     private String JavaDoc m_defaultUri;
82
83     /** Indicates if the configuration is finalized (frozen). */
84     private boolean m_frozen;
85
86     /** The set of all configured site root paths (as String). */
87     private Set m_siteRoots;
88
89     /** The map of configured sites. */
90     private Map JavaDoc m_sites;
91
92     /** The workplace server. */
93     private String JavaDoc m_workplaceServer;
94
95     /** The site matcher that matches the workplace site. */
96     private CmsSiteMatcher m_workplaceSiteMatcher;
97
98     /**
99      * Creates a new CmsSiteManager.<p>
100      *
101      */

102     public CmsSiteManager() {
103
104         m_sites = new HashMap JavaDoc();
105         m_siteRoots = new HashSet JavaDoc();
106         m_aliases = new ArrayList JavaDoc();
107
108         if (CmsLog.INIT.isInfoEnabled()) {
109             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_START_SITE_CONFIG_0));
110         }
111     }
112
113     /**
114      * Returns a list of all site available for the current user.<p>
115      *
116      * @param cms the current cms context
117      * @param workplaceMode if true, the root and current site is included for the admin user
118      * and the view permission is required to see the site root
119      * @return a list of all site available for the current user
120      */

121     public static List JavaDoc getAvailableSites(CmsObject cms, boolean workplaceMode) {
122
123         Map JavaDoc sites = OpenCms.getSiteManager().getSites();
124         List JavaDoc siteroots = new ArrayList JavaDoc(sites.size() + 1);
125         Map JavaDoc siteServers = new HashMap JavaDoc(sites.size() + 1);
126         List JavaDoc result = new ArrayList JavaDoc(sites.size() + 1);
127
128         Iterator JavaDoc i;
129         // add site list
130
i = sites.keySet().iterator();
131         while (i.hasNext()) {
132             CmsSite site = (CmsSite)sites.get(i.next());
133             String JavaDoc folder = site.getSiteRoot() + "/";
134             if (!siteroots.contains(folder)) {
135                 siteroots.add(folder);
136                 siteServers.put(folder, site.getSiteMatcher());
137             }
138         }
139         // add default site
140
if (workplaceMode && OpenCms.getSiteManager().getDefaultSite() != null) {
141             String JavaDoc folder = OpenCms.getSiteManager().getDefaultSite().getSiteRoot() + "/";
142             if (!siteroots.contains(folder)) {
143                 siteroots.add(folder);
144             }
145         }
146
147         String JavaDoc currentRoot = cms.getRequestContext().getSiteRoot();
148         cms.getRequestContext().saveSiteRoot();
149         try {
150             // for all operations here we need no context
151
cms.getRequestContext().setSiteRoot("/");
152             if (workplaceMode && cms.hasRole(CmsRole.ROOT_FOLDER_ACCESS)) {
153                 if (!siteroots.contains("/")) {
154                     siteroots.add("/");
155                 }
156                 if (!siteroots.contains(currentRoot + "/")) {
157                     siteroots.add(currentRoot + "/");
158                 }
159             }
160             Collections.sort(siteroots);
161             i = siteroots.iterator();
162             while (i.hasNext()) {
163                 String JavaDoc folder = (String JavaDoc)i.next();
164                 try {
165                     CmsResource res = cms.readResource(folder);
166                     if (!workplaceMode || cms.hasPermissions(res, CmsPermissionSet.ACCESS_VIEW)) {
167                         String JavaDoc title = cms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue();
168                         if (title == null) {
169                             title = folder;
170                         }
171                         result.add(new CmsSite(
172                             folder,
173                             res.getStructureId(),
174                             title,
175                             (CmsSiteMatcher)siteServers.get(folder)));
176                     }
177
178                 } catch (CmsException e) {
179                     // user probably has no read access to the folder, ignore and continue iterating
180
}
181             }
182         } catch (Throwable JavaDoc t) {
183             LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_SITE_PROP_FAILED_0), t);
184         } finally {
185             // restore the user's current context
186
cms.getRequestContext().restoreSiteRoot();
187         }
188         return result;
189     }
190
191     /**
192      * Returns the current site for the provided cms context object.<p>
193      *
194      * @param cms the cms context object to check for the site
195      * @return the current site for the provided cms context object
196      */

197     public static CmsSite getCurrentSite(CmsObject cms) {
198
199         String JavaDoc siteRoot = cms.getRequestContext().getSiteRoot();
200         CmsSite site = getSite(siteRoot);
201         if (site == null) {
202             return OpenCms.getSiteManager().getDefaultSite();
203         } else {
204             return site;
205         }
206     }
207
208     /**
209      * Returns the site with has the provided site root path,
210      * or null if no configured site has that root path.<p>
211      *
212      * @param siteRoot the root path to look up the site for
213      * @return the site with has the provided site root path,
214      * or null if no configured site has that root path
215      */

216     public static CmsSite getSite(String JavaDoc siteRoot) {
217
218         Map JavaDoc sites = OpenCms.getSiteManager().getSites();
219         Iterator JavaDoc i = sites.keySet().iterator();
220         while (i.hasNext()) {
221             CmsSite site = (CmsSite)sites.get(i.next());
222             if (siteRoot.equals(site.getSiteRoot())) {
223                 return site;
224             }
225         }
226         return null;
227     }
228
229     /**
230      * Returns the site root part of the resources root path,
231      * or null if the path does not match any site root.<p>
232      *
233      * @param path the root path of a resource
234      * @return the site root part of the resources root path, or null if the path does not match any site root
235      */

236     public static String JavaDoc getSiteRoot(String JavaDoc path) {
237
238         Set roots = OpenCms.getSiteManager().getSiteRoots();
239         // most sites will be subfolders of the "/sites/" folder,
240
int pos = path.indexOf('/', 7);
241         if (pos > 0) {
242             String JavaDoc candidate = path.substring(0, pos);
243             if (roots.contains(candidate)) {
244                 return candidate;
245             }
246         }
247         // site root not found as subfolder of "/sites/"
248
Iterator JavaDoc i = roots.iterator();
249         while (i.hasNext()) {
250             String JavaDoc siteRoot = (String JavaDoc)i.next();
251             if (path.startsWith(siteRoot)) {
252                 return siteRoot;
253             }
254         }
255         return null;
256     }
257
258     /**
259      * Adds an alias to the currently configured site.
260      *
261      * @param alias the url of the alias server
262      */

263     public void addAliasToConfigSite(String JavaDoc alias) {
264
265         CmsSiteMatcher siteMatcher = new CmsSiteMatcher(alias);
266         m_aliases.add(siteMatcher);
267     }
268
269     /**
270      * Adds a new CmsSite to the list of configured sites,
271      * this is only allowed during configuration.<p>
272      *
273      * If this method is called after the configuration is finished,
274      * a <code>RuntimeException</code> is thrown.<p>
275      *
276      * @param server the Server
277      * @param uri the vfs path
278      * @param secureServer a secure server, can be <code>null</code>
279      * @param exclusive if set to <code>true</code>, secure resources will only be available using the configured secure url
280      * @param error if exclusive, and set to <code>true</code> will generate a 404 error,
281      * if set to <code>false</code> will redirect to secure url
282      *
283      * @throws CmsConfigurationException if the site contains a servername, that is already assigned
284      */

285     public void addSite(String JavaDoc server, String JavaDoc uri, String JavaDoc secureServer, String JavaDoc exclusive, String JavaDoc error)
286     throws CmsConfigurationException {
287
288         if (m_frozen) {
289             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
290         }
291         CmsSiteMatcher matcher = new CmsSiteMatcher(server);
292         CmsSite site = new CmsSite(uri, matcher);
293         addServer(matcher, site);
294         if (CmsStringUtil.isNotEmpty(secureServer)) {
295             matcher = new CmsSiteMatcher(secureServer);
296             site.setSecureServer(matcher);
297             site.setExclusiveUrl(Boolean.valueOf(exclusive).booleanValue());
298             site.setExclusiveError(Boolean.valueOf(error).booleanValue());
299             addServer(matcher, site);
300         }
301
302         // Note that Digester first calls the addAliasToConfigSite method.
303
// Therefore, the aliases are already
304
site.setAliases(m_aliases);
305         Iterator JavaDoc i = m_aliases.iterator();
306         while (i.hasNext()) {
307             matcher = (CmsSiteMatcher)i.next();
308             addServer(matcher, site);
309         }
310         m_aliases = new ArrayList JavaDoc();
311         m_siteRoots.add(site.getSiteRoot());
312         if (CmsLog.INIT.isInfoEnabled()) {
313             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SITE_ROOT_ADDED_1, site.toString()));
314         }
315     }
316
317     /**
318      * Returns the default site.<p>
319      *
320      * @return the default site
321      */

322     public CmsSite getDefaultSite() {
323
324         return m_defaultSite;
325     }
326
327     /**
328      * Returns the defaultUri.<p>
329      *
330      * @return the defaultUri
331      */

332     public String JavaDoc getDefaultUri() {
333
334         return m_defaultUri;
335     }
336
337     /**
338      * Returns an unmodifiable set of all configured site roots (Strings).<p>
339      *
340      * @return an unmodifiable set of all configured site roots (Strings)
341      */

342     public Set getSiteRoots() {
343
344         return m_siteRoots;
345     }
346
347     /**
348      * Returns a map of configured sites.<p>
349      *
350      * The map uses CmsSiteMatcher objects as key and CmsSite as value.<p>
351      *
352      * @return a map of configured sites
353      */

354     public Map JavaDoc getSites() {
355
356         return m_sites;
357     }
358
359     /**
360      * Returns the workplace server.<p>
361      *
362      * @return the workplace server
363      */

364     public String JavaDoc getWorkplaceServer() {
365
366         return m_workplaceServer;
367     }
368
369     /**
370      * Returns the site matcher that matches the workplace site.<p>
371      *
372      * @return the site matcher that matches the workplace site
373      */

374     public CmsSiteMatcher getWorkplaceSiteMatcher() {
375
376         return m_workplaceSiteMatcher;
377     }
378
379     /**
380      * Initializes the site manager with the OpenCms system configuration.<p>
381      *
382      * @param cms an OpenCms context object that must have been initialized with "Admin" permissions
383      */

384     public void initialize(CmsObject cms) {
385
386         if (CmsLog.INIT.isInfoEnabled()) {
387             CmsLog.INIT.info(Messages.get().getBundle().key(
388                 Messages.INIT_NUM_SITE_ROOTS_CONFIGURED_1,
389                 new Integer JavaDoc((m_sites.size() + ((m_defaultUri != null) ? 1 : 0)))));
390         }
391
392         // check the presence of sites in VFS
393
Iterator JavaDoc i = m_sites.values().iterator();
394         while (i.hasNext()) {
395             CmsSite site = (CmsSite)i.next();
396             if (site != null) {
397                 try {
398                     cms.readResource(site.getSiteRoot());
399                 } catch (Throwable JavaDoc t) {
400                     if (CmsLog.INIT.isWarnEnabled()) {
401                         CmsLog.INIT.warn(Messages.get().getBundle().key(Messages.INIT_NO_ROOT_FOLDER_1, site));
402                     }
403                 }
404             }
405         }
406
407         // check the presence of the default site in VFS
408
if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_defaultUri)) {
409             m_defaultSite = null;
410         } else {
411             m_defaultSite = new CmsSite(m_defaultUri, CmsSiteMatcher.DEFAULT_MATCHER);
412             try {
413                 cms.readResource(m_defaultSite.getSiteRoot());
414             } catch (Throwable JavaDoc t) {
415                 if (CmsLog.INIT.isWarnEnabled()) {
416                     CmsLog.INIT.warn(Messages.get().getBundle().key(
417                         Messages.INIT_NO_ROOT_FOLDER_DEFAULT_SITE_1,
418                         m_defaultSite));
419                 }
420             }
421         }
422         if (m_defaultSite == null) {
423             m_defaultSite = new CmsSite("/", CmsSiteMatcher.DEFAULT_MATCHER);
424         }
425         if (CmsLog.INIT.isInfoEnabled()) {
426             if (m_defaultSite != null) {
427                 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_DEFAULT_SITE_ROOT_1, m_defaultSite));
428             } else {
429                 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_DEFAULT_SITE_ROOT_0));
430             }
431         }
432         m_workplaceSiteMatcher = new CmsSiteMatcher(m_workplaceServer);
433         if (CmsLog.INIT.isInfoEnabled()) {
434             if (m_workplaceSiteMatcher != null) {
435                 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WORKPLACE_SITE_1, m_workplaceSiteMatcher));
436             } else {
437                 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WORKPLACE_SITE_0));
438             }
439         }
440
441         // set site lists to unmodifiable
442
m_sites = Collections.unmodifiableMap(m_sites);
443         m_siteRoots = Collections.unmodifiableSet(m_siteRoots);
444
445         // initialization is done, set the frozen flag to true
446
m_frozen = true;
447     }
448
449     /**
450      * Returns true if the given site matcher matches a site.<p>
451      *
452      * @param matcher the site matcher to match the site with
453      * @return true if the matcher matches a site
454      */

455     public boolean isMatching(CmsSiteMatcher matcher) {
456
457         return m_sites.get(matcher) != null;
458     }
459
460     /**
461      * Returns if the given site matcher matches the current site.<p>
462      *
463      * @param cms the cms object
464      * @param matcher the site matcher to match the site with
465      * @return true if the matcher matches the current site
466      */

467     public boolean isMatchingCurrentSite(CmsObject cms, CmsSiteMatcher matcher) {
468
469         return m_sites.get(matcher) == getCurrentSite(cms);
470     }
471
472     /**
473      * Returns <code>true</code> if the given request is against the configured OpenCms workplace.<p>
474      *
475      * @param req the request to match
476      * @return <code>true</code> if the given request is against the configured OpenCms workplace
477      */

478     public boolean isWorkplaceRequest(HttpServletRequest JavaDoc req) {
479
480         if (req == null) {
481             // this may be true inside a static export test case scenario
482
return false;
483         }
484         CmsSiteMatcher matcher = new CmsSiteMatcher(req.getScheme(), req.getServerName(), req.getServerPort());
485         return m_workplaceSiteMatcher.equals(matcher);
486     }
487
488     /**
489      * Matches the given request against all configures sites and returns
490      * the matching site, or the default site if no sites matches.<p>
491      *
492      * @param req the request to match
493      * @return the matching site, or the default site if no sites matches
494      */

495     public CmsSite matchRequest(HttpServletRequest JavaDoc req) {
496
497         CmsSiteMatcher matcher = new CmsSiteMatcher(req.getScheme(), req.getServerName(), req.getServerPort());
498         CmsSite site = matchSite(matcher);
499         if (LOG.isDebugEnabled()) {
500             String JavaDoc requestServer = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
501             LOG.debug(Messages.get().getBundle().key(
502                 Messages.LOG_MATCHING_REQUEST_TO_SITE_2,
503                 requestServer,
504                 site.toString()));
505         }
506         return site;
507     }
508
509     /**
510      * Return the site that matches the given site matcher,
511      * or the default site if no sites matches.<p>
512      *
513      * @param matcher the site matcher to match the site with
514      * @return the matching site, or the defaule site if no sites matches
515      */

516     public CmsSite matchSite(CmsSiteMatcher matcher) {
517
518         CmsSite site = (CmsSite)m_sites.get(matcher);
519         if (site == null) {
520             // return the default site (might be null as well)
521
site = m_defaultSite;
522         }
523         return site;
524     }
525
526     /**
527      * Sets the default uri, this is only allowed during configuration.<p>
528      *
529      * If this method is called after the configuration is finished,
530      * a <code>RuntimeException</code> is thrown.<p>
531      *
532      * @param defaultUri the defaultUri to set
533      */

534     public void setDefaultUri(String JavaDoc defaultUri) {
535
536         if (m_frozen) {
537             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
538         }
539         m_defaultUri = defaultUri;
540     }
541
542     /**
543      * Sets the workplace server, this is only allowed during configuration.<p>
544      *
545      * If this method is called after the configuration is finished,
546      * a <code>RuntimeException</code> is thrown.<p>
547      *
548      * @param workplaceServer the workplace server to set
549      */

550     public void setWorkplaceServer(String JavaDoc workplaceServer) {
551
552         if (m_frozen) {
553             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
554         }
555         m_workplaceServer = workplaceServer;
556     }
557
558     /**
559      * Adds a new Site matcher object to the map of server names.
560      *
561      * @param server the SiteMatcher of the server
562      * @param site the site to add
563      *
564      * @throws CmsConfigurationException if the site contains a servername, that is already assigned
565      */

566     private void addServer(CmsSiteMatcher server, CmsSite site) throws CmsConfigurationException {
567
568         if (m_sites.containsKey(server)) {
569             throw new CmsConfigurationException(Messages.get().container(
570                 Messages.ERR_DUPLICATE_SERVER_NAME_1,
571                 server.getUrl()));
572         }
573         m_sites.put(server, site);
574     }
575 }
Popular Tags