Source Code Cross Referenced for CmsSiteManagerImpl.java in  » Content-Management-System » opencms » org » opencms » site » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » opencms » org.opencms.site 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/site/CmsSiteManagerImpl.java,v $
003:         * Date   : $Date: 2008-02-27 12:05:50 $
004:         * Version: $Revision: 1.7 $
005:         *
006:         * This library is part of OpenCms -
007:         * the Open Source Content Management System
008:         *
009:         * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010:         *
011:         * This library is free software; you can redistribute it and/or
012:         * modify it under the terms of the GNU Lesser General Public
013:         * License as published by the Free Software Foundation; either
014:         * version 2.1 of the License, or (at your option) any later version.
015:         *
016:         * This library is distributed in the hope that it will be useful,
017:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
018:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019:         * Lesser General Public License for more details.
020:         *
021:         * For further information about Alkacon Software GmbH, please see the
022:         * company website: http://www.alkacon.com
023:         *
024:         * For further information about OpenCms, please see the
025:         * project website: http://www.opencms.org
026:         * 
027:         * You should have received a copy of the GNU Lesser General Public
028:         * License along with this library; if not, write to the Free Software
029:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030:         */
031:
032:        package org.opencms.site;
033:
034:        import org.opencms.configuration.CmsConfigurationException;
035:        import org.opencms.file.CmsObject;
036:        import org.opencms.file.CmsPropertyDefinition;
037:        import org.opencms.file.CmsResource;
038:        import org.opencms.file.CmsResourceFilter;
039:        import org.opencms.main.CmsException;
040:        import org.opencms.main.CmsLog;
041:        import org.opencms.main.CmsRuntimeException;
042:        import org.opencms.main.OpenCms;
043:        import org.opencms.security.CmsPermissionSet;
044:        import org.opencms.security.CmsRole;
045:        import org.opencms.util.CmsStringUtil;
046:
047:        import java.util.ArrayList;
048:        import java.util.Collections;
049:        import java.util.HashMap;
050:        import java.util.Iterator;
051:        import java.util.List;
052:        import java.util.Map;
053:        import java.util.Set;
054:
055:        import javax.servlet.http.HttpServletRequest;
056:
057:        import org.apache.commons.logging.Log;
058:
059:        /**
060:         * Manages all configured sites in OpenCms.<p>
061:         *
062:         * To obtain the configured site manager instance, use {@link OpenCms#getSiteManager()}.<p>
063:         *
064:         * @author  Alexander Kandzior 
065:         *
066:         * @version $Revision: 1.7 $ 
067:         * 
068:         * @since 7.0.2
069:         */
070:        public final class CmsSiteManagerImpl {
071:
072:            /** The static log object for this class. */
073:            private static final Log LOG = CmsLog
074:                    .getLog(CmsSiteManagerImpl.class);
075:
076:            /** The path to the "/sites/" folder. */
077:            private static final String SITES_FOLDER = "/sites/";
078:
079:            /** The length of the "/sites/" folder plus 1. */
080:            private static final int SITES_FOLDER_POS = SITES_FOLDER.length() + 1;
081:
082:            /** A list of additional site roots, that is site roots that are not belows the "/sites/" folder. */
083:            private List m_additionalSiteRoots;
084:
085:            /** 
086:             * The list of aliases for the site that is configured at the moment, 
087:             * needed for the sites added during configuration. */
088:            private List m_aliases;
089:
090:            /** The default site root. */
091:            private CmsSite m_defaultSite;
092:
093:            /** The default URI. */
094:            private String m_defaultUri;
095:
096:            /** Indicates if the configuration is finalized (frozen). */
097:            private boolean m_frozen;
098:
099:            /** Maps site matchers to sites. */
100:            private Map m_siteMatcherSites;
101:
102:            /** The set of all configured site root paths (as String). */
103:            private Set m_siteRoots;
104:
105:            /** Maps site roots to sites. */
106:            private Map m_siteRootSites;
107:
108:            /** The workplace server. */
109:            private String m_workplaceServer;
110:
111:            /** The site matcher that matches the workplace site. */
112:            private CmsSiteMatcher m_workplaceSiteMatcher;
113:
114:            /**
115:             * Creates a new CmsSiteManager.<p>
116:             *
117:             */
118:            public CmsSiteManagerImpl() {
119:
120:                m_siteMatcherSites = new HashMap();
121:                m_siteRootSites = new HashMap();
122:                m_aliases = new ArrayList();
123:                m_additionalSiteRoots = new ArrayList();
124:
125:                if (CmsLog.INIT.isInfoEnabled()) {
126:                    CmsLog.INIT.info(Messages.get().getBundle().key(
127:                            Messages.INIT_START_SITE_CONFIG_0));
128:                }
129:            }
130:
131:            /**
132:             * Adds an alias to the currently configured site.
133:             * 
134:             * @param alias the URL of the alias server
135:             */
136:            public void addAliasToConfigSite(String alias) {
137:
138:                CmsSiteMatcher siteMatcher = new CmsSiteMatcher(alias);
139:                m_aliases.add(siteMatcher);
140:            }
141:
142:            /**
143:             * Adds a new CmsSite to the list of configured sites, 
144:             * this is only allowed during configuration.<p>
145:             * 
146:             * If this method is called after the configuration is finished, 
147:             * a <code>RuntimeException</code> is thrown.<p>
148:             * 
149:             * @param server the Server
150:             * @param uri the VFS path
151:             * @param secureServer a secure server, can be <code>null</code>
152:             * @param exclusive if set to <code>true</code>, secure resources will only be available using the configured secure url
153:             * @param error if exclusive, and set to <code>true</code> will generate a 404 error, 
154:             *                             if set to <code>false</code> will redirect to secure URL
155:             * 
156:             * @throws CmsConfigurationException if the site contains a server name, that is already assigned
157:             */
158:            public void addSite(String server, String uri, String secureServer,
159:                    String exclusive, String error)
160:                    throws CmsConfigurationException {
161:
162:                if (m_frozen) {
163:                    throw new CmsRuntimeException(Messages.get().container(
164:                            Messages.ERR_CONFIG_FROZEN_0));
165:                }
166:                CmsSiteMatcher matcher = new CmsSiteMatcher(server);
167:                CmsSite site = new CmsSite(uri, matcher);
168:                addServer(matcher, site);
169:                if (CmsStringUtil.isNotEmpty(secureServer)) {
170:                    matcher = new CmsSiteMatcher(secureServer);
171:                    site.setSecureServer(matcher);
172:                    site.setExclusiveUrl(Boolean.valueOf(exclusive)
173:                            .booleanValue());
174:                    site.setExclusiveError(Boolean.valueOf(error)
175:                            .booleanValue());
176:                    addServer(matcher, site);
177:                }
178:
179:                // note that Digester first calls the addAliasToConfigSite method.
180:                // therefore, the aliases are already set
181:                site.setAliases(m_aliases);
182:                Iterator i = m_aliases.iterator();
183:                while (i.hasNext()) {
184:                    matcher = (CmsSiteMatcher) i.next();
185:                    addServer(matcher, site);
186:                }
187:                m_aliases = new ArrayList();
188:                m_siteRootSites.put(site.getSiteRoot(), site);
189:                if (CmsLog.INIT.isInfoEnabled()) {
190:                    CmsLog.INIT.info(Messages.get().getBundle().key(
191:                            Messages.INIT_SITE_ROOT_ADDED_1, site.toString()));
192:                }
193:            }
194:
195:            /**
196:             * Returns a list of all sites available for the current user.<p>
197:             * 
198:             * @param cms the current OpenCms user context 
199:             * @param workplaceMode if true, the root and current site is included for the admin user
200:             *                      and the view permission is required to see the site root
201:             * 
202:             * @return a list of all site available for the current user
203:             */
204:            public List getAvailableSites(CmsObject cms, boolean workplaceMode) {
205:
206:                return getAvailableSites(cms, workplaceMode, cms
207:                        .getRequestContext().getOuFqn());
208:            }
209:
210:            /**
211:             * Returns a list of all {@link CmsSite} instances that are compatible to the given organizational unit.<p>
212:             * 
213:             * @param cms the current OpenCms user context 
214:             * @param workplaceMode if true, the root and current site is included for the admin user
215:             *                      and the view permission is required to see the site root
216:             * @param ouFqn the organizational unit
217:             * 
218:             * @return a list of all site available for the current user
219:             */
220:            public List getAvailableSites(CmsObject cms, boolean workplaceMode,
221:                    String ouFqn) {
222:
223:                List siteroots = new ArrayList(m_siteMatcherSites.size() + 1);
224:                Map siteServers = new HashMap(m_siteMatcherSites.size() + 1);
225:                List result = new ArrayList(m_siteMatcherSites.size() + 1);
226:
227:                Iterator i;
228:                // add site list
229:                i = m_siteMatcherSites.keySet().iterator();
230:                while (i.hasNext()) {
231:                    CmsSite site = (CmsSite) m_siteMatcherSites.get(i.next());
232:                    String folder = site.getSiteRoot() + "/";
233:                    if (!siteroots.contains(folder)) {
234:                        siteroots.add(folder);
235:                        siteServers.put(folder, site.getSiteMatcher());
236:                    }
237:                }
238:                // add default site
239:                if (workplaceMode && (m_defaultSite != null)) {
240:                    String folder = m_defaultSite.getSiteRoot() + "/";
241:                    if (!siteroots.contains(folder)) {
242:                        siteroots.add(folder);
243:                    }
244:                }
245:
246:                String storedSiteRoot = cms.getRequestContext().getSiteRoot();
247:                try {
248:                    // for all operations here we need no context
249:                    cms.getRequestContext().setSiteRoot("/");
250:                    if (workplaceMode
251:                            && OpenCms.getRoleManager().hasRole(cms,
252:                                    CmsRole.DEVELOPER)) {
253:                        if (!siteroots.contains("/")) {
254:                            siteroots.add("/");
255:                        }
256:                        if (!siteroots.contains(storedSiteRoot + "/")) {
257:                            siteroots.add(storedSiteRoot + "/");
258:                        }
259:                    }
260:                    List resources;
261:                    try {
262:                        resources = OpenCms.getOrgUnitManager()
263:                                .getResourcesForOrganizationalUnit(cms, ouFqn);
264:                    } catch (CmsException e) {
265:                        return Collections.EMPTY_LIST;
266:                    }
267:
268:                    Collections.sort(siteroots); // sort by resource name
269:                    i = siteroots.iterator();
270:                    while (i.hasNext()) {
271:                        String folder = (String) i.next();
272:                        boolean compatible = false;
273:                        Iterator itResources = resources.iterator();
274:                        while (itResources.hasNext()) {
275:                            CmsResource resource = (CmsResource) itResources
276:                                    .next();
277:                            if (resource.getRootPath().startsWith(folder)
278:                                    || folder
279:                                            .startsWith(resource.getRootPath())) {
280:                                compatible = true;
281:                                break;
282:                            }
283:                        }
284:                        // select only sites compatibles to the given organizational unit 
285:                        if (compatible) {
286:                            try {
287:                                CmsResource res = cms.readResource(folder);
288:                                if (!workplaceMode
289:                                        || cms.hasPermissions(res,
290:                                                CmsPermissionSet.ACCESS_VIEW,
291:                                                false,
292:                                                CmsResourceFilter.ONLY_VISIBLE)) {
293:                                    String title = cms
294:                                            .readPropertyObject(
295:                                                    res,
296:                                                    CmsPropertyDefinition.PROPERTY_TITLE,
297:                                                    false).getValue();
298:                                    if (title == null) {
299:                                        title = folder;
300:                                    }
301:                                    String position = cms
302:                                            .readPropertyObject(
303:                                                    res,
304:                                                    CmsPropertyDefinition.PROPERTY_NAVPOS,
305:                                                    false).getValue();
306:                                    result.add(new CmsSite(folder, res
307:                                            .getStructureId(), title,
308:                                            (CmsSiteMatcher) siteServers
309:                                                    .get(folder), position));
310:                                }
311:                            } catch (CmsException e) {
312:                                // user probably has no read access to the folder, ignore and continue iterating            
313:                            }
314:                        }
315:                    }
316:
317:                    Collections.sort(result);
318:                } catch (Throwable t) {
319:                    LOG.error(Messages.get().getBundle().key(
320:                            Messages.LOG_READ_SITE_PROP_FAILED_0), t);
321:                } finally {
322:                    // restore the user's current context 
323:                    cms.getRequestContext().setSiteRoot(storedSiteRoot);
324:                }
325:                return result;
326:            }
327:
328:            /**
329:             * Returns the current site for the provided OpenCms user context object.<p>
330:             * 
331:             * In the unlikely case that no site matches with the provided OpenCms user context,
332:             * the default site is returned.<p>
333:             * 
334:             * @param cms the OpenCms user context object to check for the site
335:             * 
336:             * @return the current site for the provided OpenCms user context object
337:             */
338:            public CmsSite getCurrentSite(CmsObject cms) {
339:
340:                CmsSite site = getSiteForSiteRoot(cms.getRequestContext()
341:                        .getSiteRoot());
342:                return (site == null) ? m_defaultSite : site;
343:            }
344:
345:            /**
346:             * Returns the default site.<p>
347:             * 
348:             * @return the default site
349:             */
350:            public CmsSite getDefaultSite() {
351:
352:                return m_defaultSite;
353:            }
354:
355:            /**
356:             * Returns the defaultUri.<p>
357:             *
358:             * @return the defaultUri
359:             */
360:            public String getDefaultUri() {
361:
362:                return m_defaultUri;
363:            }
364:
365:            /**
366:             * Returns the site with has the provided site root, 
367:             * or <code>null</code> if no configured site has that site root.<p>
368:             * 
369:             * The site root must have the form:
370:             * <code>/sites/default</code>.<br>
371:             * That means there must be a leading, but no trailing slash.<p>
372:             * 
373:             * @param siteRoot the site root to look up the site for
374:             * 
375:             * @return the site with has the provided site root, 
376:             *      or <code>null</code> if no configured site has that site root
377:             *      
378:             * @deprecated use {@link #getSiteForSiteRoot(String)} instead
379:             * 
380:             * @see #getSiteForSiteRoot(String)
381:             */
382:            public CmsSite getSite(String siteRoot) {
383:
384:                return (CmsSite) m_siteRootSites.get(siteRoot);
385:            }
386:
387:            /**
388:             * Returns the site for the given resource path, using the fall back site root
389:             * in case the resource path is no root path.<p>
390:             * 
391:             * In case neither the given resource path, nor the given fall back site root 
392:             * matches any configured site, the default site is returned.<p>
393:             * 
394:             * Usually the fall back site root should be taken from {@link org.opencms.file.CmsRequestContext#getSiteRoot()},
395:             * in which case a site for the site root should always exist.<p>
396:             * 
397:             * This is the same as first calling {@link #getSiteForRootPath(String)} with the 
398:             * <code>resourcePath</code> parameter, and if this fails calling 
399:             * {@link #getSiteForSiteRoot(String)} with the <code>fallbackSiteRoot</code> parameter,
400:             * and if this fails calling {@link #getDefaultSite()}.<p>
401:             * 
402:             * @param resourcePath the resource path to get the site for
403:             * @param fallbackSiteRoot site root to use in case the resource path is no root path
404:             * 
405:             * @return the site for the given resource path, using the fall back site root
406:             *      in case the resource path is no root path
407:             *      
408:             * @see #getSiteForRootPath(String)
409:             */
410:            public CmsSite getSite(String resourcePath, String fallbackSiteRoot) {
411:
412:                CmsSite result = getSiteForRootPath(resourcePath);
413:                if (result == null) {
414:                    result = getSiteForSiteRoot(fallbackSiteRoot);
415:                    if (result == null) {
416:                        result = getDefaultSite();
417:                    }
418:                }
419:                return result;
420:            }
421:
422:            /**
423:             * Returns the site for the given resources root path, 
424:             * or <code>null</code> if the resources root path does not match any site.<p>
425:             * 
426:             * @param rootPath the root path of a resource
427:             * 
428:             * @return the site for the given resources root path, 
429:             *      or <code>null</code> if the resources root path does not match any site
430:             *      
431:             * @see #getSiteForSiteRoot(String)
432:             * @see #getSiteRoot(String)
433:             */
434:            public CmsSite getSiteForRootPath(String rootPath) {
435:
436:                // most sites will be below the "/sites/" folder, 
437:                CmsSite result = lookupSitesFolder(rootPath);
438:                if (result != null) {
439:                    return result;
440:                }
441:                // look through all folders that are not below "/sites/"
442:                String siteRoot = lookupAdditionalSite(rootPath);
443:                return (siteRoot != null) ? getSiteForSiteRoot(siteRoot) : null;
444:            }
445:
446:            /**
447:             * Returns the site with has the provided site root, 
448:             * or <code>null</code> if no configured site has that site root.<p>
449:             * 
450:             * The site root must have the form:
451:             * <code>/sites/default</code>.<br>
452:             * That means there must be a leading, but no trailing slash.<p>
453:             * 
454:             * @param siteRoot the site root to look up the site for
455:             * 
456:             * @return the site with has the provided site root, 
457:             *      or <code>null</code> if no configured site has that site root
458:             *      
459:             * @see #getSiteForRootPath(String)
460:             */
461:            public CmsSite getSiteForSiteRoot(String siteRoot) {
462:
463:                return (CmsSite) m_siteRootSites.get(siteRoot);
464:            }
465:
466:            /**
467:             * Returns the site root part for the given resources root path, 
468:             * or <code>null</code> if the given resources root path does not match any site root.<p>
469:             * 
470:             * The site root returned will have the form:
471:             * <code>/sites/default</code>.<br>
472:             * That means there will a leading, but no trailing slash.<p>
473:             * 
474:             * @param rootPath the root path of a resource
475:             * 
476:             * @return the site root part of the resources root path, 
477:             *      or <code>null</code> if the path does not match any site root
478:             *      
479:             * @see #getSiteForRootPath(String)
480:             */
481:            public String getSiteRoot(String rootPath) {
482:
483:                // most sites will be below the "/sites/" folder, 
484:                CmsSite site = lookupSitesFolder(rootPath);
485:                if (site != null) {
486:                    return site.getSiteRoot();
487:                }
488:                // look through all folders that are not below "/sites/"
489:                return lookupAdditionalSite(rootPath);
490:            }
491:
492:            /**
493:             * Returns an unmodifiable set of all configured site roots (Strings).<p>
494:             *  
495:             * @return an unmodifiable set of all configured site roots (Strings)
496:             */
497:            public Set getSiteRoots() {
498:
499:                return m_siteRoots;
500:            }
501:
502:            /**
503:             * Returns the map of configured sites, using 
504:             * {@link CmsSiteMatcher} objects as keys and {@link CmsSite} objects as values.<p>
505:             * 
506:             * @return the map of configured sites, using {@link CmsSiteMatcher} 
507:             *      objects as keys and {@link CmsSite} objects as values
508:             */
509:            public Map getSites() {
510:
511:                return m_siteMatcherSites;
512:            }
513:
514:            /**
515:             * Returns the workplace server.<p>
516:             *
517:             * @return the workplace server
518:             */
519:            public String getWorkplaceServer() {
520:
521:                return m_workplaceServer;
522:            }
523:
524:            /**
525:             * Returns the site matcher that matches the workplace site.<p>
526:             * 
527:             * @return the site matcher that matches the workplace site
528:             */
529:            public CmsSiteMatcher getWorkplaceSiteMatcher() {
530:
531:                return m_workplaceSiteMatcher;
532:            }
533:
534:            /**
535:             * Initializes the site manager with the OpenCms system configuration.<p>
536:             * 
537:             * @param cms an OpenCms context object that must have been initialized with "Admin" permissions
538:             */
539:            public void initialize(CmsObject cms) {
540:
541:                if (CmsLog.INIT.isInfoEnabled()) {
542:                    CmsLog.INIT
543:                            .info(Messages
544:                                    .get()
545:                                    .getBundle()
546:                                    .key(
547:                                            Messages.INIT_NUM_SITE_ROOTS_CONFIGURED_1,
548:                                            new Integer(
549:                                                    (m_siteMatcherSites.size() + ((m_defaultUri != null) ? 1
550:                                                            : 0)))));
551:                }
552:
553:                // check the presence of sites in VFS
554:                Iterator i = m_siteMatcherSites.values().iterator();
555:                while (i.hasNext()) {
556:                    CmsSite site = (CmsSite) i.next();
557:                    if (site != null) {
558:                        try {
559:                            cms.readResource(site.getSiteRoot());
560:                        } catch (Throwable t) {
561:                            if (CmsLog.INIT.isWarnEnabled()) {
562:                                CmsLog.INIT.warn(Messages.get().getBundle()
563:                                        .key(Messages.INIT_NO_ROOT_FOLDER_1,
564:                                                site));
565:                            }
566:                        }
567:                    }
568:                }
569:
570:                // check the presence of the default site in VFS
571:                if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_defaultUri)) {
572:                    m_defaultSite = null;
573:                } else {
574:                    m_defaultSite = new CmsSite(m_defaultUri,
575:                            CmsSiteMatcher.DEFAULT_MATCHER);
576:                    try {
577:                        cms.readResource(m_defaultSite.getSiteRoot());
578:                    } catch (Throwable t) {
579:                        if (CmsLog.INIT.isWarnEnabled()) {
580:                            CmsLog.INIT
581:                                    .warn(Messages
582:                                            .get()
583:                                            .getBundle()
584:                                            .key(
585:                                                    Messages.INIT_NO_ROOT_FOLDER_DEFAULT_SITE_1,
586:                                                    m_defaultSite));
587:                        }
588:                    }
589:                }
590:                if (m_defaultSite == null) {
591:                    m_defaultSite = new CmsSite("/",
592:                            CmsSiteMatcher.DEFAULT_MATCHER);
593:                }
594:                if (CmsLog.INIT.isInfoEnabled()) {
595:                    if (m_defaultSite != null) {
596:                        CmsLog.INIT.info(Messages.get().getBundle().key(
597:                                Messages.INIT_DEFAULT_SITE_ROOT_1,
598:                                m_defaultSite));
599:                    } else {
600:                        CmsLog.INIT.info(Messages.get().getBundle().key(
601:                                Messages.INIT_DEFAULT_SITE_ROOT_0));
602:                    }
603:                }
604:                m_workplaceSiteMatcher = new CmsSiteMatcher(m_workplaceServer);
605:                if (CmsLog.INIT.isInfoEnabled()) {
606:                    if (m_workplaceSiteMatcher != null) {
607:                        CmsLog.INIT.info(Messages.get().getBundle().key(
608:                                Messages.INIT_WORKPLACE_SITE_1,
609:                                m_workplaceSiteMatcher));
610:                    } else {
611:                        CmsLog.INIT.info(Messages.get().getBundle().key(
612:                                Messages.INIT_WORKPLACE_SITE_0));
613:                    }
614:                }
615:
616:                // set site lists to unmodifiable 
617:                m_siteMatcherSites = Collections
618:                        .unmodifiableMap(m_siteMatcherSites);
619:                m_siteRoots = Collections.unmodifiableSet(m_siteRootSites
620:                        .keySet());
621:
622:                // store additional site roots to optimize lookups later
623:                Iterator j = m_siteRoots.iterator();
624:                while (j.hasNext()) {
625:                    String root = (String) j.next();
626:                    if (!root.startsWith(SITES_FOLDER)) {
627:                        m_additionalSiteRoots.add(root);
628:                    }
629:                }
630:
631:                // initialization is done, set the frozen flag to true 
632:                m_frozen = true;
633:            }
634:
635:            /**
636:             * Returns <code>true</code> if the given site matcher matches any configured site,
637:             * which includes the workplace site.<p>
638:             * 
639:             * @param matcher the site matcher to match the site with
640:             * 
641:             * @return <code>true</code> if the matcher matches a site
642:             */
643:            public boolean isMatching(CmsSiteMatcher matcher) {
644:
645:                boolean result = m_siteMatcherSites.get(matcher) != null;
646:                if (!result) {
647:                    // try to match the workplace site
648:                    result = (m_workplaceSiteMatcher != null)
649:                            && m_workplaceSiteMatcher.equals(matcher);
650:                }
651:                return result;
652:            }
653:
654:            /**
655:             * Returns <code>true</code> if the given site matcher matches the current site.<p>
656:             * 
657:             * @param cms the current OpenCms user context 
658:             * @param matcher the site matcher to match the site with
659:             * 
660:             * @return <code>true</code> if the matcher matches the current site
661:             */
662:            public boolean isMatchingCurrentSite(CmsObject cms,
663:                    CmsSiteMatcher matcher) {
664:
665:                return m_siteMatcherSites.get(matcher) == getCurrentSite(cms);
666:            }
667:
668:            /**
669:             * Returns <code>true</code> if the given site matcher matches the configured OpenCms workplace.<p> 
670:             * 
671:             * @param matcher the site matcher to match the site with
672:             * 
673:             * @return <code>true</code> if the given site matcher matches the configured OpenCms workplace
674:             */
675:            public boolean isWorkplaceRequest(CmsSiteMatcher matcher) {
676:
677:                return (m_workplaceSiteMatcher != null)
678:                        && m_workplaceSiteMatcher.equals(matcher);
679:            }
680:
681:            /**
682:             * Returns <code>true</code> if the given request is against the configured OpenCms workplace.<p> 
683:             * 
684:             * @param req the request to match 
685:             * 
686:             * @return <code>true</code> if the given request is against the configured OpenCms workplace
687:             */
688:            public boolean isWorkplaceRequest(HttpServletRequest req) {
689:
690:                if (req == null) {
691:                    // this may be true inside a static export test case scenario
692:                    return false;
693:                }
694:                return isWorkplaceRequest(new CmsSiteMatcher(req.getScheme(),
695:                        req.getServerName(), req.getServerPort()));
696:            }
697:
698:            /**
699:             * Matches the given request against all configures sites and returns 
700:             * the matching site, or the default site if no sites matches.<p>
701:             * 
702:             * @param req the request to match 
703:             * 
704:             * @return the matching site, or the default site if no sites matches
705:             */
706:            public CmsSite matchRequest(HttpServletRequest req) {
707:
708:                CmsSiteMatcher matcher = new CmsSiteMatcher(req.getScheme(),
709:                        req.getServerName(), req.getServerPort());
710:                CmsSite site = matchSite(matcher);
711:
712:                if (LOG.isDebugEnabled()) {
713:                    String requestServer = req.getScheme() + "://"
714:                            + req.getServerName() + ":" + req.getServerPort();
715:                    LOG.debug(Messages.get().getBundle().key(
716:                            Messages.LOG_MATCHING_REQUEST_TO_SITE_2,
717:                            requestServer, site.toString()));
718:                }
719:                return site;
720:            }
721:
722:            /**
723:             * Return the configured site that matches the given site matcher,
724:             * or the default site if no sites matches.<p>
725:             * 
726:             * @param matcher the site matcher to match the site with
727:             * @return the matching site, or the default site if no sites matches
728:             */
729:            public CmsSite matchSite(CmsSiteMatcher matcher) {
730:
731:                CmsSite site = (CmsSite) m_siteMatcherSites.get(matcher);
732:                if (site == null) {
733:                    // return the default site (might be null as well)
734:                    site = m_defaultSite;
735:                }
736:                return site;
737:            }
738:
739:            /**
740:             * Sets the default URI, this is only allowed during configuration.<p>
741:             * 
742:             * If this method is called after the configuration is finished, 
743:             * a <code>RuntimeException</code> is thrown.<p>
744:             * 
745:             * @param defaultUri the defaultUri to set
746:             */
747:            public void setDefaultUri(String defaultUri) {
748:
749:                if (m_frozen) {
750:                    throw new CmsRuntimeException(Messages.get().container(
751:                            Messages.ERR_CONFIG_FROZEN_0));
752:                }
753:                m_defaultUri = defaultUri;
754:            }
755:
756:            /**
757:             * Sets the workplace server, this is only allowed during configuration.<p>
758:             * 
759:             * If this method is called after the configuration is finished, 
760:             * a <code>RuntimeException</code> is thrown.<p>
761:             * 
762:             * @param workplaceServer the workplace server to set
763:             */
764:            public void setWorkplaceServer(String workplaceServer) {
765:
766:                if (m_frozen) {
767:                    throw new CmsRuntimeException(Messages.get().container(
768:                            Messages.ERR_CONFIG_FROZEN_0));
769:                }
770:                m_workplaceServer = workplaceServer;
771:            }
772:
773:            /**
774:             * Adds a new Site matcher object to the map of server names.
775:             * 
776:             * @param matcher the SiteMatcher of the server
777:             * @param site the site to add
778:             * 
779:             * @throws CmsConfigurationException if the site contains a server name, that is already assigned
780:             */
781:            private void addServer(CmsSiteMatcher matcher, CmsSite site)
782:                    throws CmsConfigurationException {
783:
784:                if (m_siteMatcherSites.containsKey(matcher)) {
785:                    throw new CmsConfigurationException(Messages.get()
786:                            .container(Messages.ERR_DUPLICATE_SERVER_NAME_1,
787:                                    matcher.getUrl()));
788:                }
789:                m_siteMatcherSites.put(matcher, site);
790:            }
791:
792:            /**
793:             * Returns <code>true</code> if the given root path matches any of the stored additional sites.<p> 
794:             * 
795:             * @param rootPath the root path to check
796:             * 
797:             * @return <code>true</code> if the given root path matches any of the stored additional sites
798:             */
799:            private String lookupAdditionalSite(String rootPath) {
800:
801:                for (int i = 0, size = m_additionalSiteRoots.size(); i < size; i++) {
802:                    String siteRoot = (String) m_additionalSiteRoots.get(i);
803:                    if (rootPath.startsWith(siteRoot)) {
804:                        return siteRoot;
805:                    }
806:                }
807:                return null;
808:            }
809:
810:            /**
811:             * Returns the configured site if the given root path matches site in the "/sites/" folder, 
812:             * or <code>null</code> otherwise.<p> 
813:             * 
814:             * @param rootPath the root path to check
815:             * 
816:             * @return the configured site if the given root path matches site in the "/sites/" folder, 
817:             *      or <code>null</code> otherwise
818:             */
819:            private CmsSite lookupSitesFolder(String rootPath) {
820:
821:                int pos = rootPath.indexOf('/', SITES_FOLDER_POS);
822:                if (pos > 0) {
823:                    // this assumes that the root path may likely start with something like "/sites/default/" 
824:                    // just cut the first 2 directories from the root path and do a direct lookup in the internal map
825:                    return (CmsSite) m_siteRootSites.get(rootPath.substring(0,
826:                            pos));
827:                }
828:                return null;
829:            }
830:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.