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.slide.impl;
019:
020: import java.util.Hashtable;
021:
022: import org.apache.avalon.framework.activity.Disposable;
023: import org.apache.avalon.framework.activity.Initializable;
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.configuration.SAXConfigurationHandler;
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.service.ServiceException;
032: import org.apache.avalon.framework.service.ServiceManager;
033: import org.apache.avalon.framework.service.Serviceable;
034: import org.apache.avalon.framework.thread.ThreadSafe;
035: import org.apache.cocoon.Constants;
036: import org.apache.cocoon.components.slide.SlideRepository;
037: import org.apache.cocoon.environment.Context;
038: import org.apache.excalibur.source.Source;
039: import org.apache.excalibur.source.SourceResolver;
040: import org.apache.excalibur.xml.sax.SAXParser;
041: import org.apache.slide.common.Domain;
042: import org.apache.slide.common.EmbeddedDomain;
043: import org.apache.slide.common.NamespaceAccessToken;
044: import org.xml.sax.InputSource;
045:
046: /**
047: * The class represent a manger for slide repositories
048: *
049: * @version CVS $Id: SlideRepositoryImpl.java 433543 2006-08-22 06:22:54Z crossley $
050: */
051: public class SlideRepositoryImpl extends AbstractLogEnabled implements
052: SlideRepository, Contextualizable, Serviceable, Configurable,
053: Initializable, Disposable, ThreadSafe {
054:
055: private ServiceManager manager;
056:
057: /**
058: * The SlideRepository will handle the domain lifecycle only,
059: * if it is not already initialzed.
060: */
061: private EmbeddedDomain domain = null;
062:
063: private String file;
064: private String contextpath;
065: private String workdir;
066:
067: public SlideRepositoryImpl() {
068: }
069:
070: public void service(ServiceManager manager) throws ServiceException {
071: this .manager = manager;
072: }
073:
074: public void contextualize(
075: org.apache.avalon.framework.context.Context context)
076: throws ContextException {
077: Context ctx = ((Context) context
078: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT));
079: this .contextpath = ctx.getRealPath("/");
080: this .workdir = context.get(Constants.CONTEXT_WORK_DIR)
081: .toString();
082: }
083:
084: public void configure(Configuration configuration)
085: throws ConfigurationException {
086:
087: this .file = configuration.getAttribute("file",
088: "WEB-INF/slide.xconf");
089: }
090:
091: public void initialize() throws Exception {
092:
093: if (Domain.isInitialized()) {
094: return;
095: }
096:
097: getLogger().info("Initializing domain.");
098:
099: this .domain = new EmbeddedDomain();
100: // FIXME Could not remove deprecated method, because some important
101: // messages were thrown over the domain logger
102: domain.setLogger(new SlideLoggerAdapter(getLogger()));
103:
104: SourceResolver resolver = null;
105: SAXParser parser = null;
106: Source source = null;
107: Configuration configuration = null;
108: try {
109: resolver = (SourceResolver) this .manager
110: .lookup(SourceResolver.ROLE);
111:
112: parser = (SAXParser) this .manager.lookup(SAXParser.ROLE);
113: SAXConfigurationHandler confighandler = new SAXConfigurationHandler();
114:
115: source = resolver.resolveURI(this .file);
116: parser.parse(new InputSource(source.getInputStream()),
117: confighandler);
118: configuration = confighandler.getConfiguration();
119:
120: } finally {
121: if (source != null) {
122: resolver.release(source);
123: }
124: if (parser != null) {
125: this .manager.release(parser);
126: }
127: if (resolver != null) {
128: this .manager.release(resolver);
129: }
130: }
131:
132: Configuration[] parameters = configuration
133: .getChildren("parameter");
134: Hashtable table = new Hashtable();
135: for (int i = 0; i < parameters.length; i++) {
136: String name = parameters[i].getAttribute("name");
137: table.put(name, parameters[i].getValue(""));
138: }
139: table.put("contextpath", this .contextpath);
140: table.put("workdir", this .workdir);
141: this .domain.setParameters(table);
142:
143: domain.setDefaultNamespace(configuration.getAttribute(
144: "default", "cocoon"));
145: Configuration[] namespace = configuration
146: .getChildren("namespace");
147:
148: for (int i = 0; i < namespace.length; i++) {
149: String name = namespace[i].getAttribute("name");
150: Configuration definition = namespace[i]
151: .getChild("definition");
152: Configuration config = namespace[i]
153: .getChild("configuration");
154: Configuration data = namespace[i].getChild("data");
155:
156: getLogger().info("Initializing namespace: " + name);
157:
158: domain.addNamespace(name, new SlideLoggerAdapter(
159: getLogger().getChildLogger(name)),
160: new SlideConfigurationAdapter(definition),
161: new SlideConfigurationAdapter(config),
162: new SlideConfigurationAdapter(data));
163:
164: }
165:
166: domain.start();
167: Domain.setInitialized(true);
168: }
169:
170: public void dispose() {
171: try {
172: domain.stop();
173: } catch (Exception e) {
174: getLogger().error("Could not stop domain", e);
175: }
176: }
177:
178: /**
179: * Returns a token for the access of the default namespace.
180: *
181: * @return NamespaceAccessToken Access token to the namespace
182: */
183: public NamespaceAccessToken getDefaultNamespaceToken() {
184:
185: if (domain != null) {
186: return this .domain.getNamespaceToken(this .domain
187: .getDefaultNamespace());
188: }
189:
190: return Domain.accessNamespace(null, Domain
191: .getDefaultNamespace());
192: }
193:
194: /**
195: * Returns a token for the access of a namespace.
196: *
197: * @param namespaceName Name of the namespace on which access is requested
198: * @return NamespaceAccessToken Access token to the namespace
199: */
200: public NamespaceAccessToken getNamespaceToken(String namespaceName) {
201:
202: if (namespaceName == null) {
203: return getDefaultNamespaceToken();
204: }
205:
206: if (domain != null) {
207: return this.domain.getNamespaceToken(namespaceName);
208: }
209:
210: return Domain.accessNamespace(null, namespaceName);
211: }
212: }
|