KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > SourceFactoryWrapper


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.source.impl;
17
18 import org.apache.avalon.excalibur.pool.Recyclable;
19 import org.apache.avalon.framework.activity.Disposable;
20 import org.apache.avalon.framework.component.ComponentException;
21 import org.apache.avalon.framework.component.ComponentManager;
22 import org.apache.avalon.framework.component.Composable;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.framework.logger.AbstractLogEnabled;
30 import org.apache.avalon.framework.logger.LogEnabled;
31 import org.apache.avalon.framework.thread.ThreadSafe;
32 import org.apache.cocoon.CascadingIOException;
33 import org.apache.cocoon.ProcessingException;
34 import org.apache.cocoon.components.CocoonComponentManager;
35 import org.apache.cocoon.environment.Environment;
36 import org.apache.cocoon.util.ClassUtils;
37 import org.apache.excalibur.source.Source;
38 import org.apache.excalibur.source.SourceFactory;
39
40 import java.io.IOException JavaDoc;
41 import java.net.MalformedURLException JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * This class wraps a Cocoon SourceFactory and makes it
46  * usable within the Avalon Excalibur source resolving architecure.
47  * The main purpose is to avoid recoding existing factories.
48  *
49  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
50  * @version CVS $Id: SourceFactoryWrapper.java 30932 2004-07-29 17:35:38Z vgritsenko $
51  */

52 public final class SourceFactoryWrapper
53     extends AbstractLogEnabled
54     implements SourceFactory,
55                ThreadSafe,
56                Configurable,
57                Disposable,
58                Composable,
59                Contextualizable
60 {
61     /** The <code>ComponentManager</code> */
62     private ComponentManager manager;
63
64     /** The special Source factories */
65     private org.apache.cocoon.components.source.SourceFactory sourceFactory;
66
67     /** The context */
68     private Context context;
69
70     /**
71      * Configure the SourceFactories
72      */

73     public void configure(final Configuration conf)
74     throws ConfigurationException {
75
76         try {
77             final Configuration factoryConf = conf.getChild("source-factory");
78             final String JavaDoc className = factoryConf.getAttribute("class");
79             if (this.getLogger().isDebugEnabled()) {
80                 this.getLogger().debug("Getting the SourceFactory " + className);
81             }
82             this.sourceFactory = (org.apache.cocoon.components.source.SourceFactory)
83                          ClassUtils.newInstance(className);
84             this.init(this.sourceFactory, factoryConf);
85         } catch (ConfigurationException e) {
86             throw e;
87         } catch (Exception JavaDoc e) {
88             throw new ConfigurationException("Could not get parameters because: " +
89                                            e.getMessage(), e);
90         }
91     }
92
93     /**
94      * Get the context
95      */

96     public void contextualize(Context context)
97     throws ContextException {
98         this.context = context;
99     }
100
101     /**
102      * Set the current <code>ComponentManager</code> instance used by this
103      * <code>Composable</code>.
104      */

105     public void compose(ComponentManager manager)
106     throws ComponentException {
107         this.manager = manager;
108     }
109
110     /**
111      * Dispose
112      */

113     public void dispose() {
114         if (this.sourceFactory != null) {
115             this.deinit(this.sourceFactory);
116         }
117         this.sourceFactory = null;
118     }
119
120
121     /**
122      * Get a <code>Source</code> object.
123      * @param parameters This is optional.
124      */

125     public Source getSource( String JavaDoc location, Map JavaDoc parameters )
126         throws MalformedURLException JavaDoc, IOException
127     {
128         if( getLogger().isDebugEnabled() )
129         {
130             getLogger().debug( "Creating source object for " + location );
131         }
132
133         final Environment currentEnv = CocoonComponentManager.getCurrentEnvironment();
134         org.apache.cocoon.environment.Source source;
135         try {
136             source = this.sourceFactory.getSource(currentEnv, location);
137         } catch (ProcessingException pe) {
138             throw new CascadingIOException("ProcessingException: " + pe.getMessage(), pe);
139         }
140         return new CocoonToAvalonSource( location, source );
141     }
142
143     /**
144      * Init a source factory
145      */

146     private void init(org.apache.cocoon.components.source.SourceFactory factory,
147                       Configuration config)
148     throws ContextException, ComponentException, ConfigurationException {
149         if (factory instanceof LogEnabled) {
150             ((LogEnabled) factory).enableLogging(getLogger());
151         }
152         if (factory instanceof Contextualizable) {
153             ((Contextualizable) factory).contextualize (this.context);
154         }
155         if (factory instanceof Composable) {
156             ((Composable) factory).compose(this.manager);
157         }
158         if (config != null && factory instanceof Configurable) {
159             ((Configurable) factory).configure(config);
160         }
161     }
162
163     /**
164      * Deinit a source factory
165      */

166     private void deinit(org.apache.cocoon.components.source.SourceFactory factory) {
167         if (factory instanceof Disposable) {
168             ((Disposable) factory).dispose();
169         }
170     }
171     
172     /**
173      * Release a {@link Source} object.
174      */

175     public void release( Source source ) {
176         if ( null != source ) {
177             if ( this.getLogger().isDebugEnabled() ) {
178                 this.getLogger().debug("Releasing source " + source.getURI());
179             }
180             ((Recyclable)source).recycle();
181         }
182     }
183
184
185 }
186
Popular Tags