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.cocoon.components;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.component.Component;
022: import org.apache.avalon.framework.component.ComponentException;
023: import org.apache.avalon.framework.component.ComponentManager;
024: import org.apache.avalon.framework.component.ComponentSelector;
025: import org.apache.avalon.framework.context.Context;
026: import org.apache.avalon.framework.context.ContextException;
027: import org.apache.avalon.framework.context.DefaultContext;
028: import org.apache.avalon.framework.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.apache.avalon.framework.service.ServiceSelector;
031: import org.apache.cocoon.environment.Environment;
032:
033: /**
034: * This is the {@link Context} implementation for Cocoon components.
035: * It extends the {@link DefaultContext} by a special handling for
036: * getting objects from the object model and other application information.
037: *
038: * @see org.apache.cocoon.components.ContextHelper
039: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
040: * @version CVS $Id: ComponentContext.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042:
043: public class ComponentContext extends DefaultContext {
044:
045: protected static final String OBJECT_MODEL_KEY_PREFIX = ContextHelper.CONTEXT_OBJECT_MODEL + '.';
046:
047: /**
048: * Create a Context with specified data and parent.
049: *
050: * @param contextData the context data
051: * @param parent the parent Context (may be null)
052: */
053: public ComponentContext(final Map contextData, final Context parent) {
054: super (contextData, parent);
055: }
056:
057: /**
058: * Create a Context with specified data.
059: *
060: * @param contextData the context data
061: */
062: public ComponentContext(final Map contextData) {
063: super (contextData);
064: }
065:
066: /**
067: * Create a Context with specified parent.
068: *
069: * @param parent the parent Context (may be null)
070: */
071: public ComponentContext(final Context parent) {
072: super (parent);
073: }
074:
075: /**
076: * Create a Context with no parent.
077: *
078: */
079: public ComponentContext() {
080: super ();
081: }
082:
083: /**
084: * Retrieve an item from the Context.
085: *
086: * @param key the key of item
087: * @return the item stored in context
088: * @throws ContextException if item not present
089: */
090: public Object get(final Object key) throws ContextException {
091: if (ContextHelper.CONTEXT_OBJECT_MODEL.equals(key)) {
092: final Environment env = CocoonComponentManager
093: .getCurrentEnvironment();
094: if (env == null) {
095: throw new ContextException("Unable to locate " + key
096: + " (No environment available)");
097: }
098: return env.getObjectModel();
099: } else if (ContextHelper.CONTEXT_SITEMAP_SERVICE_MANAGER
100: .equals(key)) {
101: final ComponentManager manager = CocoonComponentManager
102: .getSitemapComponentManager();
103: if (manager == null) {
104: throw new ContextException("Unable to locate " + key
105: + " (No environment available)");
106: }
107: return new ComponentManagerWrapper(manager);
108: }
109: if (key instanceof String) {
110: String stringKey = (String) key;
111: if (stringKey.startsWith(OBJECT_MODEL_KEY_PREFIX)) {
112: final Environment env = CocoonComponentManager
113: .getCurrentEnvironment();
114: if (env == null) {
115: throw new ContextException("Unable to locate "
116: + key + " (No environment available)");
117: }
118: final Map objectModel = env.getObjectModel();
119: String objectKey = stringKey
120: .substring(OBJECT_MODEL_KEY_PREFIX.length());
121:
122: Object o = objectModel.get(objectKey);
123: if (o == null) {
124: final String message = "Unable to locate " + key;
125: throw new ContextException(message);
126: }
127: return o;
128: }
129: }
130: return super .get(key);
131: }
132:
133: public static final class ComponentManagerWrapper implements
134: ServiceManager {
135:
136: protected final ComponentManager manager;
137:
138: public ComponentManagerWrapper(ComponentManager m) {
139: this .manager = m;
140: }
141:
142: /* (non-Javadoc)
143: * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
144: */
145: public boolean hasService(String role) {
146: return this .manager.hasComponent(role);
147: }
148:
149: /* (non-Javadoc)
150: * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
151: */
152: public Object lookup(String role) throws ServiceException {
153: try {
154: Object o = this .manager.lookup(role);
155: if (o instanceof ComponentSelector) {
156: o = new ComponentSelectorWrapper(
157: (ComponentSelector) o);
158: }
159: return o;
160: } catch (ComponentException ce) {
161: throw new ServiceException("ComponentManagerWrapper",
162: "Unable to lookup component: " + role, ce);
163: }
164: }
165:
166: /* (non-Javadoc)
167: * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
168: */
169: public void release(Object c) {
170: if (c instanceof ComponentSelectorWrapper) {
171: c = ((ComponentSelectorWrapper) c).getComponent();
172: }
173: this .manager.release((Component) c);
174: }
175: }
176:
177: public static final class ComponentSelectorWrapper implements
178: ServiceSelector {
179:
180: protected final ComponentSelector selector;
181:
182: public ComponentSelectorWrapper(ComponentSelector s) {
183: this .selector = s;
184: }
185:
186: /* (non-Javadoc)
187: * @see org.apache.avalon.framework.service.ServiceSelector#isSelectable(java.lang.Object)
188: */
189: public boolean isSelectable(Object role) {
190: return this .selector.hasComponent(role);
191: }
192:
193: /* (non-Javadoc)
194: * @see org.apache.avalon.framework.service.ServiceSelector#release(java.lang.Object)
195: */
196: public void release(Object role) {
197: this .selector.release((Component) role);
198: }
199:
200: /* (non-Javadoc)
201: * @see org.apache.avalon.framework.service.ServiceSelector#select(java.lang.Object)
202: */
203: public Object select(Object role) throws ServiceException {
204: try {
205: Object o = this .selector.select(role);
206: if (o instanceof ComponentSelector) {
207: o = new ComponentSelectorWrapper(
208: (ComponentSelector) o);
209: }
210: return o;
211: } catch (ComponentException ce) {
212: throw new ServiceException("ComponentServiceWrapper",
213: "Unable to lookup component: " + role, ce);
214: }
215: }
216:
217: public ComponentSelector getComponent() {
218: return this.selector;
219: }
220:
221: }
222: }
|