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    
015    package org.apache.tapestry.engine.state;
016    
017    import java.util.HashMap;
018    import java.util.Iterator;
019    import java.util.Map;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.ErrorLog;
023    
024    /**
025     * @author Howard M. Lewis Ship
026     * @since 4.0
027     */
028    public class SOMRegistryImpl implements StateObjectManagerRegistry
029    {
030    
031        private ErrorLog _errorLog;
032    
033        private Map _factoryContributions;
034    
035        private Map _applicationContributions;
036    
037        private Map _persistenceManagers;
038    
039        private Map _objectManagers = new HashMap();
040    
041        public void initializeService()
042        {
043            Map contributions = new HashMap();
044            contributions.putAll(_factoryContributions);
045            // Overwrite any duplicates with the application contributions
046            contributions.putAll(_applicationContributions);
047    
048            Iterator i = contributions.values().iterator();
049            while(i.hasNext())
050            {
051                StateObjectContribution c = (StateObjectContribution) i.next();
052    
053                String objectName = c.getName();
054                String scope = c.getScope();
055    
056                StateObjectPersistenceManager pm = (StateObjectPersistenceManager) _persistenceManagers
057                        .get(scope);
058    
059                if (pm == null)
060                {
061                    _errorLog.error(StateMessages.unknownScope(objectName, scope),
062                            c.getLocation(), null);
063                    continue;
064                }
065    
066                StateObjectManager manager = new StateObjectManagerImpl(objectName,
067                        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
077                    .get(objectName);
078    
079            if (manager == null)
080                throw new ApplicationRuntimeException(StateMessages
081                        .unknownStateObjectName(objectName));
082    
083            return manager;
084        }
085    
086        public void setApplicationContributions(Map applicationContributions)
087        {
088            _applicationContributions = applicationContributions;
089        }
090    
091        public void setErrorLog(ErrorLog errorLog)
092        {
093            _errorLog = errorLog;
094        }
095    
096        public void setFactoryContributions(Map factoryContributions)
097        {
098            _factoryContributions = factoryContributions;
099        }
100    
101        public void setPersistenceManagers(Map persistenceManagers)
102        {
103            _persistenceManagers = persistenceManagers;
104        }
105    }