KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > parentcm > ParentComponentManager


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.samples.parentcm;
17
18 import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
19 import org.apache.avalon.excalibur.naming.memory.MemoryInitialContextFactory;
20 import org.apache.avalon.framework.activity.Initializable;
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.component.ComponentException;
23 import org.apache.avalon.framework.component.ComponentManager;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.context.DefaultContext;
26 import org.apache.avalon.framework.logger.LogEnabled;
27 import org.apache.avalon.framework.logger.Logger;
28
29 import javax.naming.Context JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 /**
33  * A sample parent component manager. This manager will lookup the configuration object
34  * given by the initialization parameter in JNDI, use it to configure an ExcaliburComponentManager
35  * and delegate any requests to it.
36  *
37  * @author <a HREF="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
38  * @version CVS $Id: ParentComponentManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class ParentComponentManager implements ComponentManager, LogEnabled, Initializable {
41
42     /**
43      * Our logger.
44      */

45     private Logger logger;
46
47     /**
48      * The JNDI name where the component manager configuration can be found.
49      */

50     private final String JavaDoc jndiName;
51
52     /**
53      * The delegate that will be configured and provide the
54      * functionality for this component manager.
55      */

56     private final ExcaliburComponentManager delegate;
57
58     public ParentComponentManager(final String JavaDoc jndiName) {
59         this.jndiName = jndiName;
60
61         // Initialize it here so we can let it be final.
62
this.delegate = new ExcaliburComponentManager();
63     }
64
65     public boolean hasComponent(final String JavaDoc role) {
66         return delegate.hasComponent(role);
67     }
68
69     /**
70      * Initializes the CM by looking up the configuration object and using it to
71      * configure the delegate.
72      */

73     public void initialize() throws Exception JavaDoc {
74         this.logger.debug("Looking up component manager configuration at : " + this.jndiName);
75
76         Hashtable JavaDoc environment = new Hashtable JavaDoc();
77         environment.put(Context.INITIAL_CONTEXT_FACTORY, MemoryInitialContextFactory.class.getName());
78
79         //
80
// Yes, this is cheating, but the Excalibur in-memory naming provider
81
// is transient. That is, it doesn't store objects persistently and
82
// is more like a HashMap.
83
//
84
// Should be:
85
// Context initialContext = new InitialContext(environment);
86
//
87
Context initialContext = Configurator.initialContext;
88
89         Configuration config = (Configuration) initialContext.lookup(this.jndiName);
90
91         // We ignore the setRoleManager call, as ExcaliburComponentManager handles that
92
// in configure().
93
this.delegate.enableLogging(logger);
94         this.delegate.contextualize(new DefaultContext());
95         this.delegate.configure(config);
96         this.delegate.initialize();
97
98         this.logger.debug("Component manager successfully initialized.");
99     }
100
101     public Component lookup(final String JavaDoc role) throws ComponentException {
102         return this.delegate.lookup(role);
103     }
104
105     public void release(final Component component) {
106         this.delegate.release(component);
107     }
108
109     /**
110      * Provide component with a logger.
111      *
112      * @param logger the logger
113      */

114     public void enableLogging(Logger logger) {
115         this.logger = logger;
116     }
117 }
118
119
Popular Tags