KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.cocoon.components.source.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.Contextualizable;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.cocoon.components.slide.SlideRepository;
32 import org.apache.excalibur.source.Source;
33 import org.apache.excalibur.source.SourceException;
34 import org.apache.excalibur.source.SourceFactory;
35 import org.apache.excalibur.source.SourceParameters;
36 import org.apache.excalibur.source.SourceUtil;
37 import org.apache.slide.common.NamespaceAccessToken;
38
39 /**
40  * A factory for sources from a Jakarta Slide repository.
41  *
42  * @version CVS $Id: SlideSourceFactory.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  */

44 public class SlideSourceFactory extends AbstractLogEnabled
45 implements SourceFactory, Contextualizable, Serviceable, ThreadSafe {
46
47     private ServiceManager m_manager;
48     private SlideRepository m_repository;
49     private Context m_context;
50
51     public SlideSourceFactory() {
52     }
53     
54     /**
55      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
56      *
57      * @param context The context.
58      */

59     public void contextualize(Context context) throws ContextException {
60         m_context = context;
61     }
62     
63     /**
64      * Lookup the SlideRepository.
65      *
66      * @param manager ServiceManager.
67      */

68     public void service(ServiceManager manager) throws ServiceException {
69         m_manager = manager;
70     }
71
72     
73     /**
74      * Get a <code>Source</code> object.
75      *
76      * @param location URI of the source.
77      * @param parameters This is optional.
78      *
79      * @return A new source object.
80      */

81     public Source getSource(String JavaDoc location, Map JavaDoc parameters)
82     throws MalformedURLException JavaDoc, IOException JavaDoc, SourceException {
83
84         if ( m_repository == null ) {
85             try {
86                 m_repository = (SlideRepository) m_manager.lookup(SlideRepository.ROLE);
87             } catch (ServiceException se) {
88                 throw new SourceException("Unable to lookup repository.", se);
89             }
90         }
91         
92         if (getLogger().isDebugEnabled()) {
93             getLogger().debug("Creating source object for " + location);
94         }
95         
96         final String JavaDoc[] parts = SourceUtil.parseUrl(location);
97         final String JavaDoc scheme = parts[SourceUtil.SCHEME];
98         final String JavaDoc authority = parts[SourceUtil.AUTHORITY];
99         final String JavaDoc query = parts[SourceUtil.QUERY];
100         String JavaDoc path = parts[SourceUtil.PATH];
101         
102         String JavaDoc principal;
103         String JavaDoc namespace;
104         
105         // parse the authority string for [usr][:pwd]@ns
106
int index = authority.indexOf('@');
107         if (index == -1) {
108             principal = "guest";
109             namespace = authority;
110         }
111         else {
112             principal = authority.substring(0,index);
113             namespace = authority.substring(index+1);
114         }
115         
116         if (path == null || path.length() == 0) {
117             path = "/";
118         }
119         
120         NamespaceAccessToken nat = m_repository.getNamespaceToken(namespace);
121         if (nat == null) {
122             throw new SourceException("No such namespace: " + namespace);
123         }
124
125         SourceParameters queryParameters = null;
126
127         if (query == null || query.length() == 0) {
128             queryParameters = new SourceParameters();
129         } else {
130             queryParameters = new SourceParameters(query);
131         }
132
133         String JavaDoc version = queryParameters.getParameter("version",null);
134         String JavaDoc scope = queryParameters.getParameter("scope",
135             nat.getNamespaceConfig().getFilesPath());
136         
137         if (getLogger().isDebugEnabled()) {
138             getLogger().debug("scheme: " + scheme);
139             getLogger().debug("principal: " + principal);
140             getLogger().debug("namespace: " + namespace);
141             getLogger().debug("path: " + path);
142             getLogger().debug("version: " + version);
143             getLogger().debug("scope: " + scope);
144         }
145
146         SlideSource source = new SlideSource(nat,scheme,scope,path,principal,version);
147
148         source.enableLogging(getLogger());
149         source.contextualize(m_context);
150         source.service(m_manager);
151         source.initialize();
152
153         return source;
154     }
155
156     /**
157      * Release a {@link Source} object.
158      *
159      * @param source Source, which should be released.
160      */

161     public void release(Source source) {
162         if (null!=source) {
163             if (getLogger().isDebugEnabled()) {
164                 getLogger().debug("Releasing source "+source.getURI());
165             }
166         }
167     }
168
169 }
170
171
Popular Tags