1 16 package org.apache.cocoon.components; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.component.Component; 21 import org.apache.avalon.framework.component.ComponentException; 22 import org.apache.avalon.framework.component.ComponentManager; 23 import org.apache.avalon.framework.component.ComponentSelector; 24 import org.apache.avalon.framework.context.Context; 25 import org.apache.avalon.framework.context.ContextException; 26 import org.apache.avalon.framework.context.DefaultContext; 27 import org.apache.avalon.framework.service.ServiceException; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.ServiceSelector; 30 import org.apache.cocoon.environment.Environment; 31 32 41 42 public class ComponentContext 43 extends DefaultContext { 44 45 protected static final String OBJECT_MODEL_KEY_PREFIX = ContextHelper.CONTEXT_OBJECT_MODEL + '.'; 46 47 53 public ComponentContext(final Map contextData, final Context parent) { 54 super( contextData, parent ); 55 } 56 57 62 public ComponentContext(final Map contextData) { 63 super( contextData ); 64 } 65 66 71 public ComponentContext(final Context parent) { 72 super( parent ); 73 } 74 75 79 public ComponentContext() { 80 super(); 81 } 82 83 90 public Object get( final Object key ) 91 throws ContextException { 92 if ( ContextHelper.CONTEXT_OBJECT_MODEL.equals(key)) { 93 final Environment env = CocoonComponentManager.getCurrentEnvironment(); 94 if ( env == null ) { 95 throw new ContextException("Unable to locate " + key + " (No environment available)"); 96 } 97 return env.getObjectModel(); 98 } else if ( ContextHelper.CONTEXT_SITEMAP_SERVICE_MANAGER.equals(key)) { 99 final ComponentManager manager = CocoonComponentManager.getSitemapComponentManager(); 100 if ( manager == null ) { 101 throw new ContextException("Unable to locate " + key + " (No environment available)"); 102 } 103 return new ComponentManagerWrapper(manager); 104 } 105 if ( key instanceof String ) { 106 String stringKey = (String )key; 107 if ( stringKey.startsWith(OBJECT_MODEL_KEY_PREFIX) ) { 108 final Environment env = CocoonComponentManager.getCurrentEnvironment(); 109 if ( env == null ) { 110 throw new ContextException("Unable to locate " + key + " (No environment available)"); 111 } 112 final Map objectModel = env.getObjectModel(); 113 String objectKey = stringKey.substring(OBJECT_MODEL_KEY_PREFIX.length()); 114 115 Object o = objectModel.get( objectKey ); 116 if ( o == null ) { 117 final String message = "Unable to locate " + key; 118 throw new ContextException( message ); 119 } 120 return o; 121 } 122 } 123 return super.get( key ); 124 } 125 126 public static final class ComponentManagerWrapper implements ServiceManager { 127 128 protected final ComponentManager manager; 129 130 public ComponentManagerWrapper(ComponentManager m) { 131 this.manager = m; 132 } 133 134 137 public boolean hasService(String role) { 138 return this.manager.hasComponent(role); 139 } 140 141 144 public Object lookup(String role) throws ServiceException { 145 try { 146 Object o = this.manager.lookup(role); 147 if ( o instanceof ComponentSelector ) { 148 o = new ComponentSelectorWrapper((ComponentSelector)o); 149 } 150 return o; 151 } catch (ComponentException ce) { 152 throw new ServiceException("ComponentManagerWrapper", "Unable to lookup component: " + role, ce); 153 } 154 } 155 156 159 public void release(Object c) { 160 if ( c instanceof ComponentSelectorWrapper ) { 161 c = ((ComponentSelectorWrapper)c).getComponent(); 162 } 163 this.manager.release((Component)c); 164 } 165 } 166 167 public static final class ComponentSelectorWrapper implements ServiceSelector { 168 169 protected final ComponentSelector selector; 170 171 public ComponentSelectorWrapper(ComponentSelector s) { 172 this.selector = s; 173 } 174 175 178 public boolean isSelectable(Object role) { 179 return this.selector.hasComponent(role); 180 } 181 182 185 public void release(Object role) { 186 this.selector.release((Component)role); 187 } 188 189 192 public Object select(Object role) throws ServiceException { 193 try { 194 Object o = this.selector.select(role); 195 if ( o instanceof ComponentSelector ) { 196 o = new ComponentSelectorWrapper((ComponentSelector)o); 197 } 198 return o; 199 } catch (ComponentException ce) { 200 throw new ServiceException("ComponentServiceWrapper", "Unable to lookup component: " + role, ce); 201 } 202 } 203 204 public ComponentSelector getComponent() { 205 return this.selector; 206 } 207 208 } 209 } 210 | Popular Tags |