001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.site;
019:
020: import java.util.HashSet;
021: import java.util.Set;
022:
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.avalon.framework.service.ServiceSelector;
025: import org.apache.lenya.cms.publication.DocumentFactory;
026: import org.apache.lenya.cms.publication.DocumentLocator;
027: import org.apache.lenya.cms.publication.Publication;
028:
029: /**
030: * Utility to handle site structures.
031: *
032: * @version $Id: SiteUtil.java 566250 2007-08-15 16:34:09Z andreas $
033: */
034: public class SiteUtil {
035:
036: private SiteUtil() {
037: }
038:
039: /**
040: * Returns a sub-site starting with a certain node, which includes the node
041: * itself and all nodes which require this node, in preorder.
042: *
043: * @param manager The service manager.
044: * @param node The top-level document.
045: * @return A document set.
046: * @throws SiteException if an error occurs.
047: */
048: public static NodeSet getSubSite(ServiceManager manager,
049: SiteNode node) throws SiteException {
050: ServiceSelector selector = null;
051: SiteManager siteManager = null;
052: SiteNode[] subsite;
053: try {
054: selector = (ServiceSelector) manager
055: .lookup(SiteManager.ROLE + "Selector");
056: siteManager = (SiteManager) selector.select(node
057: .getStructure().getPublication()
058: .getSiteManagerHint());
059:
060: DocumentFactory map = node.getStructure().getPublication()
061: .getFactory();
062: Set nodes = new HashSet();
063: nodes.add(node);
064:
065: SiteNode[] requiringNodes = siteManager
066: .getRequiringResources(map, node);
067: for (int i = 0; i < requiringNodes.length; i++) {
068: nodes.add(requiringNodes[i]);
069: }
070:
071: subsite = (SiteNode[]) nodes.toArray(new SiteNode[nodes
072: .size()]);
073: } catch (Exception e) {
074: throw new SiteException(e);
075: } finally {
076: if (selector != null) {
077: if (siteManager != null) {
078: selector.release(siteManager);
079: }
080: manager.release(selector);
081: }
082: }
083: return new NodeSet(manager, subsite);
084: }
085:
086: /**
087: * @see org.apache.lenya.cms.site.SiteManager#getAvailableLocator(DocumentFactory,
088: * DocumentLocator)
089: * @param manager The service manager.
090: * @param factory The factory.
091: * @param locator The locator.
092: * @return A document.
093: * @throws SiteException if an error occurs.
094: */
095: public static DocumentLocator getAvailableLocator(
096: ServiceManager manager, DocumentFactory factory,
097: DocumentLocator locator) throws SiteException {
098: ServiceSelector selector = null;
099: SiteManager siteManager = null;
100: try {
101: selector = (ServiceSelector) manager
102: .lookup(SiteManager.ROLE + "Selector");
103: Publication pub = factory.getPublication(locator
104: .getPublicationId());
105: siteManager = (SiteManager) selector.select(pub
106: .getSiteManagerHint());
107: return siteManager.getAvailableLocator(factory, locator);
108: } catch (Exception e) {
109: throw new SiteException(e);
110: } finally {
111: if (selector != null) {
112: if (siteManager != null) {
113: selector.release(siteManager);
114: }
115: manager.release(selector);
116: }
117: }
118: }
119:
120: }
|