1 package nl.hippo.componentcontainers; 2 3 import org.apache.avalon.framework.context.Context; 4 import org.apache.avalon.framework.context.ContextException; 5 import org.apache.avalon.framework.context.Contextualizable; 6 import org.apache.avalon.framework.logger.AbstractLogEnabled; 7 import org.apache.avalon.framework.logger.LogEnabled; 8 import org.apache.avalon.framework.service.ServiceException; 9 import org.apache.avalon.framework.service.ServiceManager; 10 import org.apache.avalon.framework.service.Serviceable; 11 import org.apache.avalon.framework.thread.ThreadSafe; 12 import org.apache.cocoon.Constants; 13 import org.springframework.web.context.WebApplicationContext; 14 15 public class AvalonSpringBridgeImpl 16 extends AbstractLogEnabled 17 implements AvalonSpringBridge, Serviceable, Contextualizable, ThreadSafe 18 { 19 20 private Context m_context; 21 22 private WebApplicationContext m_webApplicationContext; 23 24 public AvalonSpringBridgeImpl() 25 { 26 super(); 27 } 28 29 public void service(ServiceManager serviceManager) throws ServiceException 30 { 31 if (m_context != null) 32 { 33 try 34 { 35 Object environmentContextAsObject = m_context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 36 if (environmentContextAsObject != null 37 && environmentContextAsObject instanceof org.apache.cocoon.environment.Context) 38 { 39 org.apache.cocoon.environment.Context environmentContext = (org.apache.cocoon.environment.Context) environmentContextAsObject; 40 Object wacAsObject = environmentContext 41 .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 42 if (wacAsObject != null && wacAsObject instanceof WebApplicationContext) 43 { 44 m_webApplicationContext = (WebApplicationContext) wacAsObject; 45 Object serviceManagerSourceAsObject = m_webApplicationContext 46 .getBean("serviceManager"); 47 if (serviceManagerSourceAsObject != null) 48 { 49 if (serviceManagerSourceAsObject instanceof ServiceManagerProxy) 50 { 51 ServiceManagerProxy serviceManagerSource = (ServiceManagerProxy) serviceManagerSourceAsObject; 52 serviceManagerSource.setServiceManager(serviceManager); 53 } 54 if (serviceManagerSourceAsObject instanceof LogEnabled) 55 { 56 LogEnabled logEnabled = (LogEnabled) serviceManagerSourceAsObject; 57 logEnabled.enableLogging(getLogger()); 58 } 59 } 60 } 61 } 62 } 63 catch (ContextException e) 64 { 65 throw new ServiceException(null, "Unable to obtain context", e); 66 } 67 } 68 } 69 70 public void contextualize(Context context) throws ContextException 71 { 72 m_context = context; 73 } 74 75 public Object getBean(String id) 76 { 77 Object result = null; 78 if (m_webApplicationContext != null) 79 { 80 result = m_webApplicationContext.getBean(id); 81 } 82 return result; 83 } 84 85 } 86 | Popular Tags |