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.lib.factory;
016
017 import org.apache.hivemind.ApplicationRuntimeException;
018 import org.apache.hivemind.HiveMind;
019 import org.apache.hivemind.Location;
020 import org.apache.hivemind.internal.Module;
021 import org.apache.hivemind.lib.BeanFactory;
022 import org.apache.hivemind.service.ObjectProvider;
023
024 /**
025 * An {@link org.apache.hivemind.service.ObjectProvider}
026 * that references a named (or named and initialized) bean from a
027 * {@link org.apache.hivemind.lib.BeanFactory}. The translator string is of the form:
028 * <code>service-id:name[,initializer]</code>. That is, the text after the colon
029 * is an initializer passed to {@link org.apache.hivemind.lib.BeanFactory#get(String)}.
030 *
031 * @author Howard Lewis Ship
032 */
033 public class BeanFactoryObjectProvider implements ObjectProvider
034 {
035 public Object provideObject(
036 Module contributingModule,
037 Class propertyType,
038 String inputValue,
039 Location location)
040 {
041 if (HiveMind.isBlank(inputValue))
042 return null;
043
044 int colonx = inputValue.indexOf(':');
045
046 if (colonx < 0)
047 throw new ApplicationRuntimeException(
048 FactoryMessages.invalidBeanTranslatorFormat(inputValue),
049 location,
050 null);
051
052 String serviceId = inputValue.substring(0, colonx);
053
054 if (serviceId.length() == 0)
055 throw new ApplicationRuntimeException(
056 FactoryMessages.invalidBeanTranslatorFormat(inputValue),
057 location,
058 null);
059
060 String locator = inputValue.substring(colonx + 1);
061
062 if (locator.length() == 0)
063 throw new ApplicationRuntimeException(
064 FactoryMessages.invalidBeanTranslatorFormat(inputValue),
065 location,
066 null);
067
068 BeanFactory f = (BeanFactory) contributingModule.getService(serviceId, BeanFactory.class);
069
070 return f.get(locator);
071 }
072
073 }