KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > ComponentContext


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components;
17
18 import java.util.Map JavaDoc;
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 /**
33  * This is the {@link Context} implementation for Cocoon components.
34  * It extends the {@link DefaultContext} by a special handling for
35  * getting objects from the object model and other application information.
36  *
37  * @see org.apache.cocoon.components.ContextHelper
38  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
39  * @version CVS $Id: ComponentContext.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

41
42 public class ComponentContext
43     extends DefaultContext {
44
45     protected static final String JavaDoc OBJECT_MODEL_KEY_PREFIX = ContextHelper.CONTEXT_OBJECT_MODEL + '.';
46     
47     /**
48      * Create a Context with specified data and parent.
49      *
50      * @param contextData the context data
51      * @param parent the parent Context (may be null)
52      */

53     public ComponentContext(final Map JavaDoc contextData, final Context parent) {
54         super( contextData, parent );
55     }
56
57     /**
58      * Create a Context with specified data.
59      *
60      * @param contextData the context data
61      */

62     public ComponentContext(final Map JavaDoc contextData) {
63         super( contextData );
64     }
65
66     /**
67      * Create a Context with specified parent.
68      *
69      * @param parent the parent Context (may be null)
70      */

71     public ComponentContext(final Context parent) {
72         super( parent );
73     }
74
75     /**
76      * Create a Context with no parent.
77      *
78      */

79     public ComponentContext() {
80         super();
81     }
82
83     /**
84      * Retrieve an item from the Context.
85      *
86      * @param key the key of item
87      * @return the item stored in context
88      * @throws ContextException if item not present
89      */

90     public Object JavaDoc get( final Object JavaDoc 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 JavaDoc ) {
106             String JavaDoc stringKey = (String JavaDoc)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 JavaDoc objectModel = env.getObjectModel();
113                 String JavaDoc objectKey = stringKey.substring(OBJECT_MODEL_KEY_PREFIX.length());
114                 
115                 Object JavaDoc o = objectModel.get( objectKey );
116                 if ( o == null ) {
117                     final String JavaDoc 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         /* (non-Javadoc)
135          * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
136          */

137         public boolean hasService(String JavaDoc role) {
138             return this.manager.hasComponent(role);
139         }
140
141         /* (non-Javadoc)
142          * @see org.apache.avalon.framework.service.ServiceManager#lookup(java.lang.String)
143          */

144         public Object JavaDoc lookup(String JavaDoc role) throws ServiceException {
145             try {
146                 Object JavaDoc 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         /* (non-Javadoc)
157          * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object)
158          */

159         public void release(Object JavaDoc 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         /* (non-Javadoc)
176          * @see org.apache.avalon.framework.service.ServiceSelector#isSelectable(java.lang.Object)
177          */

178         public boolean isSelectable(Object JavaDoc role) {
179             return this.selector.hasComponent(role);
180         }
181         
182         /* (non-Javadoc)
183          * @see org.apache.avalon.framework.service.ServiceSelector#release(java.lang.Object)
184          */

185         public void release(Object JavaDoc role) {
186             this.selector.release((Component)role);
187         }
188         
189         /* (non-Javadoc)
190          * @see org.apache.avalon.framework.service.ServiceSelector#select(java.lang.Object)
191          */

192         public Object JavaDoc select(Object JavaDoc role) throws ServiceException {
193             try {
194                 Object JavaDoc 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