001 package org.apache.fulcrum.spring; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.util.Map; 023 024 import org.apache.avalon.framework.activity.Disposable; 025 import org.apache.avalon.framework.activity.Initializable; 026 import org.apache.avalon.framework.logger.Logger; 027 import org.apache.avalon.framework.logger.CommonsLogger; 028 import org.apache.avalon.framework.service.ServiceManager; 029 import org.apache.avalon.framework.service.ServiceException; 030 import org.apache.commons.logging.LogFactory; 031 import org.apache.commons.logging.Log; 032 import org.springframework.beans.factory.BeanFactoryAware; 033 import org.springframework.beans.factory.BeanFactory; 034 import org.springframework.beans.factory.BeanNameAware; 035 import org.springframework.beans.BeansException; 036 037 /** 038 * Base class to create an Avalon container as Spring bean. 039 * 040 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> 041 */ 042 public abstract class AvalonContainerBean implements BeanNameAware, BeanFactoryAware, ServiceManager, Initializable, Disposable 043 { 044 /** The service manager used for service lookups */ 045 private ServiceManager serviceManager; 046 047 /** The logger being used */ 048 private Logger logger; 049 050 /** the name of the bean */ 051 private String beanName; 052 053 /** the Spring bean factory creating this instance */ 054 private BeanFactory beanFactory; 055 056 /** the Avalon default context passed to the container */ 057 private Map defaultContext; 058 059 /** 060 * Constructor 061 */ 062 public AvalonContainerBean() 063 { 064 } 065 066 ///////////////////////////////////////////////////////////////////////// 067 // Service Interface Implementation 068 ///////////////////////////////////////////////////////////////////////// 069 070 /** 071 * Initialize the instance. This method must be configured using 072 * the 'init-method' attribute. 073 * 074 * @see org.apache.avalon.framework.activity.Initializable#initialize() 075 * @throws Exception the initialization failed 076 */ 077 public abstract void initialize() throws Exception; 078 079 /** 080 * Dispose the YAAFI container. This method must be configured using 081 * the 'destroy-method' attribute. 082 * 083 * @see org.apache.avalon.framework.activity.Disposable#dispose() 084 */ 085 public abstract void dispose(); 086 087 /** 088 * @see org.apache.avalon.framework.service.ServiceManager#lookup(String) 089 */ 090 public Object lookup(String s) throws ServiceException 091 { 092 return this.getServiceManager().lookup(s); 093 } 094 095 /** 096 * @see org.apache.avalon.framework.service.ServiceManager#hasService(String) 097 */ 098 public boolean hasService(String s) 099 { 100 return this.getServiceManager().hasService(s); 101 } 102 103 /** 104 * @see org.apache.avalon.framework.service.ServiceManager#release(Object) 105 */ 106 public void release(Object o) 107 { 108 this.getServiceManager().release(o); 109 } 110 111 /** 112 * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory) 113 */ 114 public void setBeanFactory(BeanFactory beanFactory) throws BeansException 115 { 116 this.beanFactory = beanFactory; 117 } 118 119 /** 120 * @see org.springframework.beans.factory.BeanNameAware#setBeanName(String) 121 */ 122 public void setBeanName(String name) 123 { 124 this.beanName = name; 125 } 126 127 ///////////////////////////////////////////////////////////////////////// 128 // Generated getters & setters 129 ///////////////////////////////////////////////////////////////////////// 130 131 /** 132 * @return Returns the logger. 133 */ 134 public Logger getLogger() 135 { 136 if(this.logger == null) 137 { 138 this.logger = this.createLogger(); 139 } 140 141 return this.logger; 142 } 143 144 /** 145 * @param logger The logger to set. 146 */ 147 public void setLogger(Logger logger) 148 { 149 this.logger = logger; 150 } 151 152 153 /** 154 * Get the values for the custom Avalon context 155 * 156 * @return the Avalon default context 157 */ 158 public Map getDefaultContext() { 159 return defaultContext; 160 } 161 162 /** 163 * Allows setting a custom Avalon context. 164 * 165 * @param defaultContext The Avalon default context to set 166 */ 167 public void setDefaultContext(Map defaultContext) { 168 this.defaultContext = defaultContext; 169 } 170 171 /** 172 * @return the Spring bean name 173 */ 174 public String getBeanName() { 175 return beanName; 176 } 177 178 /** 179 * @return the Spring bean factory 180 */ 181 public BeanFactory getBeanFactory() { 182 return beanFactory; 183 } 184 185 ///////////////////////////////////////////////////////////////////////// 186 // Implementation 187 ///////////////////////////////////////////////////////////////////////// 188 189 /** 190 * Create the Avalon Logger to be used for the Avalon container. This 191 * method can be overridden if you don't want a CommonsLogger. 192 * 193 * @return avalon loggger 194 */ 195 protected Logger createLogger() 196 { 197 Log log = LogFactory.getLog(this.getBeanName()); 198 return new CommonsLogger(log, this.getBeanName()); 199 } 200 201 protected ServiceManager getServiceManager() 202 { 203 return this.serviceManager; 204 } 205 206 protected void setServiceManager(ServiceManager serviceManager) { 207 this.serviceManager = serviceManager; 208 } 209 }