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.source.impl;
018:
019: import org.apache.avalon.excalibur.pool.Recyclable;
020: import org.apache.avalon.framework.activity.Disposable;
021: import org.apache.avalon.framework.component.ComponentException;
022: import org.apache.avalon.framework.component.ComponentManager;
023: import org.apache.avalon.framework.component.Composable;
024: import org.apache.avalon.framework.configuration.Configurable;
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.context.Context;
028: import org.apache.avalon.framework.context.ContextException;
029: import org.apache.avalon.framework.context.Contextualizable;
030: import org.apache.avalon.framework.logger.AbstractLogEnabled;
031: import org.apache.avalon.framework.logger.LogEnabled;
032: import org.apache.avalon.framework.thread.ThreadSafe;
033: import org.apache.cocoon.CascadingIOException;
034: import org.apache.cocoon.ProcessingException;
035: import org.apache.cocoon.components.CocoonComponentManager;
036: import org.apache.cocoon.environment.Environment;
037: import org.apache.cocoon.util.ClassUtils;
038: import org.apache.excalibur.source.Source;
039: import org.apache.excalibur.source.SourceFactory;
040:
041: import java.io.IOException;
042: import java.net.MalformedURLException;
043: import java.util.Map;
044:
045: /**
046: * This class wraps a Cocoon SourceFactory and makes it
047: * usable within the Avalon Excalibur source resolving architecure.
048: * The main purpose is to avoid recoding existing factories.
049: *
050: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
051: * @version CVS $Id: SourceFactoryWrapper.java 433543 2006-08-22 06:22:54Z crossley $
052: */
053: public final class SourceFactoryWrapper extends AbstractLogEnabled
054: implements SourceFactory, ThreadSafe, Configurable, Disposable,
055: Composable, Contextualizable {
056: /** The <code>ComponentManager</code> */
057: private ComponentManager manager;
058:
059: /** The special Source factories */
060: private org.apache.cocoon.components.source.SourceFactory sourceFactory;
061:
062: /** The context */
063: private Context context;
064:
065: /**
066: * Configure the SourceFactories
067: */
068: public void configure(final Configuration conf)
069: throws ConfigurationException {
070:
071: try {
072: final Configuration factoryConf = conf
073: .getChild("source-factory");
074: final String className = factoryConf.getAttribute("class");
075: if (this .getLogger().isDebugEnabled()) {
076: this .getLogger().debug(
077: "Getting the SourceFactory " + className);
078: }
079: this .sourceFactory = (org.apache.cocoon.components.source.SourceFactory) ClassUtils
080: .newInstance(className);
081: this .init(this .sourceFactory, factoryConf);
082: } catch (ConfigurationException e) {
083: throw e;
084: } catch (Exception e) {
085: throw new ConfigurationException(
086: "Could not get parameters because: "
087: + e.getMessage(), e);
088: }
089: }
090:
091: /**
092: * Get the context
093: */
094: public void contextualize(Context context) throws ContextException {
095: this .context = context;
096: }
097:
098: /**
099: * Set the current <code>ComponentManager</code> instance used by this
100: * <code>Composable</code>.
101: */
102: public void compose(ComponentManager manager)
103: throws ComponentException {
104: this .manager = manager;
105: }
106:
107: /**
108: * Dispose
109: */
110: public void dispose() {
111: if (this .sourceFactory != null) {
112: this .deinit(this .sourceFactory);
113: }
114: this .sourceFactory = null;
115: }
116:
117: /**
118: * Get a <code>Source</code> object.
119: * @param parameters This is optional.
120: */
121: public Source getSource(String location, Map parameters)
122: throws MalformedURLException, IOException {
123: if (getLogger().isDebugEnabled()) {
124: getLogger().debug("Creating source object for " + location);
125: }
126:
127: final Environment currentEnv = CocoonComponentManager
128: .getCurrentEnvironment();
129: org.apache.cocoon.environment.Source source;
130: try {
131: source = this .sourceFactory.getSource(currentEnv, location);
132: } catch (ProcessingException pe) {
133: throw new CascadingIOException("ProcessingException: "
134: + pe.getMessage(), pe);
135: }
136: return new CocoonToAvalonSource(location, source);
137: }
138:
139: /**
140: * Init a source factory
141: */
142: private void init(
143: org.apache.cocoon.components.source.SourceFactory factory,
144: Configuration config) throws ContextException,
145: ComponentException, ConfigurationException {
146: if (factory instanceof LogEnabled) {
147: ((LogEnabled) factory).enableLogging(getLogger());
148: }
149: if (factory instanceof Contextualizable) {
150: ((Contextualizable) factory).contextualize(this .context);
151: }
152: if (factory instanceof Composable) {
153: ((Composable) factory).compose(this .manager);
154: }
155: if (config != null && factory instanceof Configurable) {
156: ((Configurable) factory).configure(config);
157: }
158: }
159:
160: /**
161: * Deinit a source factory
162: */
163: private void deinit(
164: org.apache.cocoon.components.source.SourceFactory factory) {
165: if (factory instanceof Disposable) {
166: ((Disposable) factory).dispose();
167: }
168: }
169:
170: /**
171: * Release a {@link Source} object.
172: */
173: public void release(Source source) {
174: if (null != source) {
175: if (this .getLogger().isDebugEnabled()) {
176: this .getLogger().debug(
177: "Releasing source " + source.getURI());
178: }
179: ((Recyclable) source).recycle();
180: }
181: }
182:
183: }
|