1 package org.apache.fulcrum.spring; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.Map; 23 24 import org.apache.avalon.framework.activity.Disposable; 25 import org.apache.avalon.framework.activity.Initializable; 26 import org.apache.avalon.framework.logger.Logger; 27 import org.apache.avalon.framework.logger.CommonsLogger; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.commons.logging.Log; 32 import org.springframework.beans.factory.BeanFactoryAware; 33 import org.springframework.beans.factory.BeanFactory; 34 import org.springframework.beans.factory.BeanNameAware; 35 import org.springframework.beans.BeansException; 36 37 /** 38 * Base class to create an Avalon container as Spring bean. 39 * 40 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> 41 */ 42 public abstract class AvalonContainerBean implements BeanNameAware, BeanFactoryAware, ServiceManager, Initializable, Disposable 43 { 44 /** The service manager used for service lookups */ 45 private ServiceManager serviceManager; 46 47 /** The logger being used */ 48 private Logger logger; 49 50 /** the name of the bean */ 51 private String beanName; 52 53 /** the Spring bean factory creating this instance */ 54 private BeanFactory beanFactory; 55 56 /** the Avalon default context passed to the container */ 57 private Map defaultContext; 58 59 /** 60 * Constructor 61 */ 62 public AvalonContainerBean() 63 { 64 } 65 66 ///////////////////////////////////////////////////////////////////////// 67 // Service Interface Implementation 68 ///////////////////////////////////////////////////////////////////////// 69 70 /** 71 * Initialize the instance. This method must be configured using 72 * the 'init-method' attribute. 73 * 74 * @see org.apache.avalon.framework.activity.Initializable#initialize() 75 * @throws Exception the initialization failed 76 */ 77 public abstract void initialize() throws Exception; 78 79 /** 80 * Dispose the YAAFI container. This method must be configured using 81 * the 'destroy-method' attribute. 82 * 83 * @see org.apache.avalon.framework.activity.Disposable#dispose() 84 */ 85 public abstract void dispose(); 86 87 /** 88 * @see org.apache.avalon.framework.service.ServiceManager#lookup(String) 89 */ 90 public Object lookup(String s) throws ServiceException 91 { 92 return this.getServiceManager().lookup(s); 93 } 94 95 /** 96 * @see org.apache.avalon.framework.service.ServiceManager#hasService(String) 97 */ 98 public boolean hasService(String s) 99 { 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 }