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:
018: package org.apache.cocoon.components.source.impl;
019:
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.avalon.framework.context.ContextException;
026: import org.apache.avalon.framework.context.Contextualizable;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.avalon.framework.thread.ThreadSafe;
032: import org.apache.cocoon.components.slide.SlideRepository;
033: import org.apache.excalibur.source.Source;
034: import org.apache.excalibur.source.SourceException;
035: import org.apache.excalibur.source.SourceFactory;
036: import org.apache.excalibur.source.SourceParameters;
037: import org.apache.excalibur.source.SourceUtil;
038: import org.apache.slide.common.NamespaceAccessToken;
039:
040: /**
041: * A factory for sources from a Jakarta Slide repository.
042: *
043: * @version CVS $Id: SlideSourceFactory.java 433543 2006-08-22 06:22:54Z crossley $
044: */
045: public class SlideSourceFactory extends AbstractLogEnabled implements
046: SourceFactory, Contextualizable, Serviceable, ThreadSafe {
047:
048: private ServiceManager m_manager;
049: private SlideRepository m_repository;
050: private Context m_context;
051:
052: public SlideSourceFactory() {
053: }
054:
055: /**
056: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
057: *
058: * @param context The context.
059: */
060: public void contextualize(Context context) throws ContextException {
061: m_context = context;
062: }
063:
064: /**
065: * Lookup the SlideRepository.
066: *
067: * @param manager ServiceManager.
068: */
069: public void service(ServiceManager manager) throws ServiceException {
070: m_manager = manager;
071: }
072:
073: /**
074: * Get a <code>Source</code> object.
075: *
076: * @param location URI of the source.
077: * @param parameters This is optional.
078: *
079: * @return A new source object.
080: */
081: public Source getSource(String location, Map parameters)
082: throws MalformedURLException, IOException, SourceException {
083:
084: if (m_repository == null) {
085: try {
086: m_repository = (SlideRepository) m_manager
087: .lookup(SlideRepository.ROLE);
088: } catch (ServiceException se) {
089: throw new SourceException(
090: "Unable to lookup repository.", se);
091: }
092: }
093:
094: if (getLogger().isDebugEnabled()) {
095: getLogger().debug("Creating source object for " + location);
096: }
097:
098: final String[] parts = SourceUtil.parseUrl(location);
099: final String scheme = parts[SourceUtil.SCHEME];
100: final String authority = parts[SourceUtil.AUTHORITY];
101: final String query = parts[SourceUtil.QUERY];
102: String path = parts[SourceUtil.PATH];
103:
104: String principal;
105: String namespace;
106:
107: // parse the authority string for [usr][:pwd]@ns
108: int index = authority.indexOf('@');
109: if (index == -1) {
110: principal = "guest";
111: namespace = authority;
112: } else {
113: principal = authority.substring(0, index);
114: namespace = authority.substring(index + 1);
115: }
116:
117: if (path == null || path.length() == 0) {
118: path = "/";
119: }
120:
121: NamespaceAccessToken nat = m_repository
122: .getNamespaceToken(namespace);
123: if (nat == null) {
124: throw new SourceException("No such namespace: " + namespace);
125: }
126:
127: SourceParameters queryParameters = null;
128:
129: if (query == null || query.length() == 0) {
130: queryParameters = new SourceParameters();
131: } else {
132: queryParameters = new SourceParameters(query);
133: }
134:
135: String version = queryParameters.getParameter("version", null);
136: String scope = queryParameters.getParameter("scope", nat
137: .getNamespaceConfig().getFilesPath());
138:
139: if (getLogger().isDebugEnabled()) {
140: getLogger().debug("scheme: " + scheme);
141: getLogger().debug("principal: " + principal);
142: getLogger().debug("namespace: " + namespace);
143: getLogger().debug("path: " + path);
144: getLogger().debug("version: " + version);
145: getLogger().debug("scope: " + scope);
146: }
147:
148: SlideSource source = new SlideSource(nat, scheme, scope, path,
149: principal, version);
150:
151: source.enableLogging(getLogger());
152: source.contextualize(m_context);
153: source.service(m_manager);
154: source.initialize();
155:
156: return source;
157: }
158:
159: /**
160: * Release a {@link Source} object.
161: *
162: * @param source Source, which should be released.
163: */
164: public void release(Source source) {
165: if (null != source) {
166: if (getLogger().isDebugEnabled()) {
167: getLogger()
168: .debug("Releasing source " + source.getURI());
169: }
170: }
171: }
172:
173: }
|