KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > slide > impl > SlideRepositoryImpl


1 /*
2  * Copyright 1999-2002,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.slide.impl;
18
19 import java.util.Hashtable JavaDoc;
20
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.activity.Initializable;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.framework.logger.AbstractLogEnabled;
30 import org.apache.avalon.framework.service.ServiceException;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34 import org.apache.cocoon.Constants;
35 import org.apache.cocoon.components.slide.SlideRepository;
36 import org.apache.cocoon.environment.Context;
37 import org.apache.excalibur.source.Source;
38 import org.apache.excalibur.source.SourceResolver;
39 import org.apache.excalibur.xml.sax.SAXParser;
40 import org.apache.slide.common.Domain;
41 import org.apache.slide.common.EmbeddedDomain;
42 import org.apache.slide.common.NamespaceAccessToken;
43 import org.xml.sax.InputSource JavaDoc;
44
45 /**
46  * The class represent a manger for slide repositories
47  *
48  * @version CVS $Id: SlideRepositoryImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
49  */

50 public class SlideRepositoryImpl extends AbstractLogEnabled
51 implements SlideRepository, Contextualizable, Serviceable, Configurable,
52 Initializable, Disposable, ThreadSafe {
53
54     private ServiceManager manager;
55
56     /**
57      * The SlideRepository will handle the domain lifecycle only,
58      * if it is not already initialzed.
59      */

60     private EmbeddedDomain domain = null;
61
62     private String JavaDoc file;
63     private String JavaDoc contextpath;
64     private String JavaDoc workdir;
65
66
67     public SlideRepositoryImpl() {
68     }
69
70     public void service(ServiceManager manager) throws ServiceException {
71         this.manager = manager;
72     }
73
74     public void contextualize(org.apache.avalon.framework.context.Context context)
75       throws ContextException {
76         Context ctx = ((Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT));
77         this.contextpath = ctx.getRealPath("/");
78         this.workdir = context.get(Constants.CONTEXT_WORK_DIR).toString();
79     }
80
81     public void configure(Configuration configuration)
82       throws ConfigurationException {
83
84         this.file = configuration.getAttribute("file", "WEB-INF/slide.xconf");
85     }
86
87     public void initialize() throws Exception JavaDoc {
88
89         if (Domain.isInitialized()) {
90             return;
91         }
92
93         getLogger().info("Initializing domain.");
94
95         this.domain = new EmbeddedDomain();
96         // FIXME Could not remove deprecated method, because some important
97
// messages were thrown over the domain logger
98
domain.setLogger(new SlideLoggerAdapter(getLogger()));
99
100         SourceResolver resolver = null;
101         SAXParser parser = null;
102         Source source = null;
103         Configuration configuration = null;
104         try {
105             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
106
107             parser = (SAXParser) this.manager.lookup(SAXParser.ROLE);
108             SAXConfigurationHandler confighandler = new SAXConfigurationHandler();
109
110             source = resolver.resolveURI(this.file);
111             parser.parse(new InputSource JavaDoc(source.getInputStream()),confighandler);
112             configuration = confighandler.getConfiguration();
113
114         } finally {
115             if (source != null) {
116                 resolver.release(source);
117             }
118             if (parser != null) {
119                 this.manager.release(parser);
120             }
121             if (resolver != null) {
122                 this.manager.release(resolver);
123             }
124         }
125         
126         Configuration[] parameters = configuration.getChildren("parameter");
127         Hashtable JavaDoc table = new Hashtable JavaDoc();
128         for (int i = 0; i < parameters.length; i++) {
129             String JavaDoc name = parameters[i].getAttribute("name");
130             table.put(name, parameters[i].getValue(""));
131         }
132         table.put("contextpath", this.contextpath);
133         table.put("workdir", this.workdir);
134         this.domain.setParameters(table);
135         
136         domain.setDefaultNamespace(configuration.getAttribute("default","cocoon"));
137         Configuration[] namespace = configuration.getChildren("namespace");
138
139         for (int i = 0; i< namespace.length; i++) {
140             String JavaDoc name = namespace[i].getAttribute("name");
141             Configuration definition = namespace[i].getChild("definition");
142             Configuration config = namespace[i].getChild("configuration");
143             Configuration data = namespace[i].getChild("data");
144             
145             getLogger().info("Initializing namespace: " + name);
146             
147             domain.addNamespace(name,
148                                 new SlideLoggerAdapter(getLogger().getChildLogger(name)),
149                                 new SlideConfigurationAdapter(definition),
150                                 new SlideConfigurationAdapter(config),
151                                 new SlideConfigurationAdapter(data));
152
153         }
154         
155         domain.start();
156         Domain.setInitialized(true);
157     }
158
159     public void dispose() {
160         try {
161             domain.stop();
162         } catch (Exception JavaDoc e) {
163             getLogger().error("Could not stop domain", e);
164         }
165     }
166
167     /**
168      * Returns a token for the access of the default namespace.
169      *
170      * @return NamespaceAccessToken Access token to the namespace
171      */

172     public NamespaceAccessToken getDefaultNamespaceToken() {
173
174         if (domain != null) {
175             return this.domain.getNamespaceToken(this.domain.getDefaultNamespace());
176         }
177
178         return Domain.accessNamespace(null, Domain.getDefaultNamespace());
179     }
180
181     /**
182      * Returns a token for the access of a namespace.
183      *
184      * @param namespaceName Name of the namespace on which access is requested
185      * @return NamespaceAccessToken Access token to the namespace
186      */

187     public NamespaceAccessToken getNamespaceToken(String JavaDoc namespaceName) {
188         
189         if (namespaceName == null) {
190             return getDefaultNamespaceToken();
191         }
192
193         if (domain != null) {
194             return this.domain.getNamespaceToken(namespaceName);
195         }
196
197         return Domain.accessNamespace(null, namespaceName);
198     }
199 }
200
Popular Tags