001    // Copyright 2004, 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.hivemind.service.impl;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.hivemind.ApplicationRuntimeException;
021    import org.apache.hivemind.ErrorLog;
022    import org.apache.hivemind.HiveMind;
023    import org.apache.hivemind.Location;
024    import org.apache.hivemind.internal.Module;
025    import org.apache.hivemind.schema.Translator;
026    import org.apache.hivemind.service.ObjectProvider;
027    
028    /**
029     * Implementation of the indirect translator. This translator allows the contributor, not the
030     * schema, to define where object values come from, and is fully extensible. Perhaps I'll have an
031     * inspiration and find a better name than "indirect".
032     * 
033     * @author Howard Lewis Ship
034     */
035    public class ObjectTranslator implements Translator
036    {
037        /** @since 1.1 */
038        private ErrorLog _errorLog;
039    
040        /**
041         * Keyed on prefix, value is an {@link org.apache.hivemind.service.ObjectProvider}.
042         */
043        private Map _providers = new HashMap();
044    
045        public Object translate(Module contributingModule, Class propertyType, String inputValue,
046                Location location)
047        {
048            if (HiveMind.isBlank(inputValue))
049                return null;
050    
051            int colonx = inputValue.indexOf(':');
052    
053            if (colonx < 1)
054            {
055                _errorLog.error(ServiceMessages.invalidProviderSelector(inputValue), null, null);
056    
057                return null;
058            }
059    
060            String prefix = inputValue.substring(0, colonx);
061    
062            ObjectProvider provider = (ObjectProvider) _providers.get(prefix);
063    
064            if (provider == null)
065            {
066                _errorLog.error(ServiceMessages.unknownProviderPrefix(prefix), location, null);
067    
068                return null;
069            }
070    
071            String locator = inputValue.substring(colonx + 1);
072    
073            try
074            {
075                return provider.provideObject(contributingModule, propertyType, locator, location);
076            }
077            catch (Exception ex)
078            {
079                throw new ApplicationRuntimeException(ex.getMessage(), location, ex);
080            }
081    
082        }
083    
084        public void setContributions(Map map)
085        {
086            _providers = map;
087        }
088    
089        /** @since 1.1 */
090        public void setErrorLog(ErrorLog errorLog)
091        {
092            _errorLog = errorLog;
093        }
094    }