001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.xbean.server.spring.reference; 018 019 import java.io.Serializable; 020 021 import org.apache.xbean.kernel.ServiceContext; 022 import org.apache.xbean.kernel.ServiceContextThreadLocal; 023 import org.apache.xbean.kernel.ServiceName; 024 import org.springframework.beans.factory.FactoryBean; 025 import org.springframework.beans.factory.config.BeanDefinitionHolder; 026 import org.springframework.beans.factory.support.RootBeanDefinition; 027 028 /** 029 * ServiceNameReference is a the ServiceName in the ServiceContextThreadLocal. 030 * @author Dain Sundstrom 031 * @version $Id$ 032 * @since 2.0 033 */ 034 public class ServiceNameReference implements FactoryBean, Serializable { 035 /** 036 * Creates a bean definition for ServiceNameReference. 037 * @return a bean definition for ServiceNameReference 038 */ 039 public static BeanDefinitionHolder createBeanDefinition() { 040 RootBeanDefinition beanDefinition = new RootBeanDefinition(ServiceNameReference.class, 0); 041 return new BeanDefinitionHolder(beanDefinition, ServiceNameReference.class.getName()); 042 } 043 044 public final Class getObjectType() { 045 return ServiceName.class; 046 } 047 048 public synchronized final Object getObject() { 049 ServiceContext serviceContext = ServiceContextThreadLocal.get(); 050 if (serviceContext == null) { 051 throw new IllegalStateException("Service context has not been set"); 052 } 053 return serviceContext.getServiceName(); 054 } 055 056 public boolean isSingleton() { 057 return true; 058 } 059 }