001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.engine.state;
016
017import java.util.HashMap;
018import java.util.Iterator;
019import java.util.Map;
020
021import org.apache.hivemind.ApplicationRuntimeException;
022import org.apache.hivemind.ErrorLog;
023
024/**
025 * @author Howard M. Lewis Ship
026 * @since 4.0
027 */
028public class SOMRegistryImpl implements StateObjectManagerRegistry
029{
030    private ErrorLog _errorLog;
031
032    private Map _factoryContributions;
033
034    private Map _applicationContributions;
035
036    private Map _persistenceManagers;
037
038    private Map _objectManagers = new HashMap();
039
040    public void initializeService()
041    {
042        Map contributions = new HashMap();
043        contributions.putAll(_factoryContributions);
044        // Overwrite any duplicates with the application contributions
045        contributions.putAll(_applicationContributions);
046
047        Iterator i = contributions.values().iterator();
048        while (i.hasNext())
049        {
050            StateObjectContribution c = (StateObjectContribution) i.next();
051
052            String objectName = c.getName();
053            String scope = c.getScope();
054
055            StateObjectPersistenceManager pm = (StateObjectPersistenceManager) _persistenceManagers
056                    .get(scope);
057
058            if (pm == null)
059            {
060                _errorLog.error(
061                        StateMessages.unknownScope(objectName, scope),
062                        c.getLocation(),
063                        null);
064                continue;
065            }
066
067            StateObjectManager manager = new StateObjectManagerImpl(objectName, c.getFactory(), pm);
068
069            _objectManagers.put(objectName, manager);
070
071        }
072    }
073
074    public StateObjectManager get(String objectName)
075    {
076        StateObjectManager manager = (StateObjectManager) _objectManagers.get(objectName);
077
078        if (manager == null)
079            throw new ApplicationRuntimeException(StateMessages.unknownStateObjectName(objectName));
080
081        return manager;
082    }
083
084    public void setApplicationContributions(Map applicationContributions)
085    {
086        _applicationContributions = applicationContributions;
087    }
088
089    public void setErrorLog(ErrorLog errorLog)
090    {
091        _errorLog = errorLog;
092    }
093
094    public void setFactoryContributions(Map factoryContributions)
095    {
096        _factoryContributions = factoryContributions;
097    }
098
099    public void setPersistenceManagers(Map persistenceManagers)
100    {
101        _persistenceManagers = persistenceManagers;
102    }
103}