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 org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.internal.Module;
020    import org.apache.hivemind.service.ObjectProvider;
021    import org.apache.hivemind.util.PropertyUtils;
022    
023    /**
024     * {@link org.apache.hivemind.service.ObjectProvider} implementation
025     * that obtains a named property from a service.  This provider is
026     * mapped to the prefix "service-property", the service id is seperated
027     * from the property name by a colon.  Example:
028     * <code>service-property:MyService:myProperty</code>
029     *
030     * @author Howard Lewis Ship
031     */
032    public class ServicePropertyObjectProvider implements ObjectProvider
033    {
034    
035        public Object provideObject(
036            Module contributingModule,
037            Class propertyType,
038            String locator,
039            Location location)
040        {
041            int commax = locator.indexOf(':');
042    
043            if (commax < 2)
044            {
045                throw new ApplicationRuntimeException(
046                    ServiceMessages.invalidServicePropertyLocator(locator),
047                    location,
048                    null);
049            }
050    
051            String serviceId = locator.substring(0, commax);
052            String propertyName = locator.substring(commax + 1);
053    
054            Object service = contributingModule.getService(serviceId, Object.class);
055    
056            return PropertyUtils.read(service, propertyName);
057        }
058    
059    }