/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.lenya.cms.publication;

import java.util.ArrayList;
import java.util.List;

import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceSelector;
import org.apache.lenya.cms.repository.Node;
import org.apache.lenya.cms.repository.RepositoryException;
import org.apache.lenya.cms.site.SiteException;
import org.apache.lenya.cms.site.SiteManager;
import org.apache.lenya.cms.site.SiteNode;
import org.apache.lenya.cms.site.SiteStructure;

/**
 * Area implementation.
 */
public class AreaImpl implements Area {

    private String name;
    private Publication pub;
    private DocumentFactory factory;
    private ServiceManager manager;

    /**
     * @param manager The service manager.
     * @param factory The factory.
     * @param pub The publication.
     * @param name The area name.
     */
    public AreaImpl(ServiceManager manager, DocumentFactory factory, Publication pub, String name) {
        this.manager = manager;
        this.factory = factory;
        this.pub = pub;
        this.name = name;
    }

    public boolean contains(String uuid, String language) {
        String sourceUri = DocumentImpl.getSourceURI(pub, name, uuid, language);
        Node node = DocumentImpl.getRepositoryNode(this.manager, this.factory, sourceUri);
        try {
            return node.exists();
        } catch (RepositoryException e) {
            throw new RuntimeException(e);
        }
    }

    public Document getDocument(String uuid, String language) throws PublicationException {
        if (!contains(uuid, language)) {
            throw new PublicationException("The area [" + this + "] doesn't contain the document ["
                    + uuid + ":" + language + "].");
        }
        return this.factory.get(getPublication(), getName(), uuid, language);
    }

    public String getName() {
        return this.name;
    }

    public Publication getPublication() {
        return this.pub;
    }

    private SiteStructure site;

    public SiteStructure getSite() {
        if (this.site == null) {
            SiteManager siteManager = null;
            ServiceSelector selector = null;
            try {
                selector = (ServiceSelector) this.manager.lookup(SiteManager.ROLE + "Selector");
                siteManager = (SiteManager) selector.select(getPublication().getSiteManagerHint());
                this.site = siteManager.getSiteStructure(this.factory, getPublication(), getName());
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                if (selector != null) {
                    if (siteManager != null) {
                        selector.release(siteManager);
                    }
                    this.manager.release(selector);
                }
            }
        }
        return this.site;
    }

    public String toString() {
        return getPublication().getId() + ":" + getName();
    }

    public Document[] getDocuments() {
        SiteNode[] nodes = getSite().getNodes();
        List docs = new ArrayList();
        for (int i = 0; i < nodes.length; i++) {
            String[] langs = nodes[i].getLanguages();
            for (int l = 0; l < langs.length; l++) {
                try {
                    docs.add(nodes[i].getLink(langs[l]).getDocument());
                } catch (SiteException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return (Document[]) docs.toArray(new Document[docs.size()]);
    }

}

@@ -2805,22 +2805,21 @@
 %7D%0A%0A    p
-rivate
+ublic
  SiteStr
@@ -2829,31 +2829,41 @@
 ure 
-site;%0A%0A    public SiteS
+getSite() %7B%0A        // The site s
 truc
@@ -2871,57 +2871,154 @@
 ure 
-getSite() %7B%0A        if (this.site == null) %7B%0A    
+has to be loaded every time because the publication factory is sharable%0A        // and therefore the danger of stale site structures could occur.%0A
     
@@ -3045,36 +3045,32 @@
 Manager = null;%0A
-    
         ServiceS
@@ -3098,38 +3098,30 @@
 ll;%0A        
-    
 try %7B%0A
-    
             
@@ -3213,20 +3213,16 @@
         
-    
 siteMana
@@ -3313,23 +3313,14 @@
     
-    this.site =
+return
  sit
@@ -3389,36 +3389,32 @@
 ame());%0A        
-    
 %7D catch (Excepti
@@ -3425,36 +3425,32 @@
 ) %7B%0A            
-    
 throw new Runtim
@@ -3472,20 +3472,16 @@
         
-    
 %7D finall
@@ -3492,28 +3492,24 @@
             
-    
 if (selector
@@ -3512,36 +3512,32 @@
 ctor != null) %7B%0A
-    
                 
@@ -3575,36 +3575,32 @@
                 
-    
 selector.release
@@ -3626,34 +3626,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -3684,36 +3684,32 @@
 r);%0A            
-    
 %7D%0A            %7D%0A
@@ -3706,49 +3706,9 @@
     
-    %7D%0A        %7D%0A        return this.site;
+%7D
 %0A   

Load site structure for area every time it is requested to avoid stale site objects