KickJava   Java API By Example, From Geeks To Geeks.

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


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

50
51 public final class URLFactoryWrapper
52     extends AbstractLogEnabled
53     implements SourceFactory,
54                ThreadSafe,
55                Configurable,
56                Disposable,
57                Composable,
58                Contextualizable
59 {
60     /** The <code>ComponentManager</code> */
61     private ComponentManager manager;
62
63     /** The special Source factories */
64     private URLFactory urlFactory;
65
66     /** The context */
67     private Context context;
68
69     /**
70      * Configure the SourceFactories
71      */

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

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

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

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

123     public Source getSource( String JavaDoc location, Map JavaDoc parameters )
124         throws MalformedURLException JavaDoc, IOException JavaDoc
125     {
126         if( this.getLogger().isDebugEnabled() ) {
127             this.getLogger().debug( "Creating source object for " + location );
128         }
129
130         final int protocolPos = location.indexOf("://");
131         final URL JavaDoc url = this.urlFactory.getURL(location.substring(protocolPos+3));
132         final URLSource source = new org.apache.excalibur.source.impl.URLSource();
133         source.init(url, parameters);
134         return source;
135     }
136
137     /**
138      * Init a url factory
139      */

140     private void init(URLFactory factory,
141                       Configuration config)
142     throws ContextException, ComponentException, ConfigurationException {
143         if (factory instanceof LogEnabled) {
144             ((LogEnabled) factory).enableLogging(getLogger());
145         }
146         if (factory instanceof Contextualizable) {
147             ((Contextualizable) factory).contextualize (this.context);
148         }
149         if (factory instanceof Composable) {
150             ((Composable) factory).compose(this.manager);
151         }
152         if (config != null && factory instanceof Configurable) {
153             ((Configurable) factory).configure(config);
154         }
155     }
156
157     /**
158      * Deinit a url factory
159      */

160     private void deinit(URLFactory factory) {
161         if (factory instanceof Disposable) {
162             ((Disposable) factory).dispose();
163         }
164     }
165
166     /**
167      * Release a {@link Source} object.
168      */

169     public void release( Source source ) {
170         if ( null != source ) {
171             if ( this.getLogger().isDebugEnabled() ) {
172                 this.getLogger().debug("Releasing source " + source.getURI());
173             }
174             // do simply nothing
175
}
176     }
177
178 }
179
Popular Tags