KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.util.Map JavaDoc;
21
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.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.cocoon.components.source.SourceDescriptor;
31 import org.apache.excalibur.source.ModifiableTraversableSource;
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.SourceResolver;
36
37 /**
38  * Creates RepositorySources.
39  */

40 public class RepositorySourceFactory extends AbstractLogEnabled
41 implements SourceFactory, Serviceable, Configurable, ThreadSafe {
42     
43     private ServiceManager m_manager;
44     private SourceResolver m_resolver;
45     private SourceDescriptor m_descriptor;
46     private String JavaDoc m_name;
47     private boolean m_isInitialized;
48     
49     public RepositorySourceFactory() {
50     }
51     
52     private synchronized void lazyInitialize() throws IOException JavaDoc {
53         if (m_isInitialized) {
54             return;
55         }
56         if (m_resolver == null) {
57             try {
58                 m_resolver = (SourceResolver) m_manager.lookup(SourceResolver.ROLE);
59             }
60             catch (ServiceException e) {
61                 throw new IOException JavaDoc("Resolver service is not available: " + e.toString());
62             }
63         }
64         if (m_manager.hasService(SourceDescriptor.ROLE)) {
65             try {
66                 m_descriptor = (SourceDescriptor) m_manager.lookup(SourceDescriptor.ROLE);
67             }
68             catch (ServiceException e) {
69                 // impossible
70
}
71         }
72         else {
73             m_descriptor = null;
74             if (getLogger().isInfoEnabled()) {
75                 final String JavaDoc message =
76                     "SourceDescriptor is not available. " +
77                     "RepositorySource will not support " +
78                     "source properties.";
79                 getLogger().info(message);
80             }
81         }
82     }
83     
84     /**
85      * Read the <code>name</code> attribute.
86      */

87     public void configure(final Configuration configuration) throws ConfigurationException {
88         m_name = configuration.getAttribute("name");
89     }
90     
91     /**
92      * Lookup the SourceDescriptorManager service.
93      */

94     public void service(final ServiceManager manager) {
95         m_manager = manager;
96     }
97     
98     public Source getSource(String JavaDoc location, Map JavaDoc parameters)
99         throws IOException JavaDoc, MalformedURLException JavaDoc {
100         
101         if (getLogger().isDebugEnabled()) {
102             getLogger().debug("Creating RepositorySource for " + location);
103         }
104         
105         // lazy initialization due to circular dependency
106
if (!m_isInitialized) {
107             lazyInitialize();
108         }
109         
110         // assert location.startsWith(m_name)
111
location = location.substring(m_name.length()+1);
112         Source source = m_resolver.resolveURI(location);
113         if (!(source instanceof ModifiableTraversableSource)) {
114             final String JavaDoc message = "Delegate should be a ModifiableTraversableSource";
115             throw new SourceException(message);
116         }
117         
118         return new RepositorySource(
119             m_name,
120             (ModifiableTraversableSource) source,
121             m_descriptor,
122             getLogger()
123         );
124     }
125     
126     public void release(final Source source) {
127         if (source instanceof RepositorySource) {
128             m_resolver.release(((RepositorySource) source).m_delegate);
129         }
130     }
131
132 }
133
Popular Tags