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.lenya.cms.usecase.impl;
019:
020: import java.util.SortedSet;
021: import java.util.TreeSet;
022:
023: import org.apache.avalon.framework.activity.Disposable;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.service.ServiceSelector;
028: import org.apache.avalon.framework.service.Serviceable;
029: import org.apache.avalon.framework.thread.ThreadSafe;
030: import org.apache.cocoon.environment.Request;
031: import org.apache.lenya.cms.cocoon.components.context.ContextUtility;
032: import org.apache.lenya.cms.publication.DocumentFactory;
033: import org.apache.lenya.cms.publication.DocumentUtil;
034: import org.apache.lenya.cms.publication.Publication;
035: import org.apache.lenya.cms.publication.URLInformation;
036: import org.apache.lenya.cms.publication.templating.PublicationTemplateManager;
037: import org.apache.lenya.cms.usecase.Usecase;
038: import org.apache.lenya.cms.usecase.UsecaseResolver;
039:
040: /**
041: * Usecase resolver implementation.
042: *
043: * @version $Id: UsecaseResolverImpl.java 568041 2007-08-21 09:47:23Z andreas $
044: */
045: public class UsecaseResolverImpl extends AbstractLogEnabled implements
046: UsecaseResolver, Serviceable, Disposable, ThreadSafe {
047:
048: /**
049: * Ctor.
050: */
051: public UsecaseResolverImpl() {
052: // do nothing
053: }
054:
055: private ServiceSelector selector;
056:
057: private ServiceManager manager;
058:
059: /**
060: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
061: */
062: public void service(ServiceManager _manager)
063: throws ServiceException {
064: this .manager = _manager;
065: }
066:
067: protected ServiceSelector getSelector() throws ServiceException {
068: if (this .selector == null) {
069: this .selector = (ServiceSelector) this .manager
070: .lookup(Usecase.ROLE + "Selector");
071: }
072: return this .selector;
073: }
074:
075: /**
076: * @see org.apache.lenya.cms.usecase.UsecaseResolver#release(org.apache.lenya.cms.usecase.Usecase)
077: */
078: public void release(Usecase usecase) throws ServiceException {
079: if (usecase == null) {
080: throw new IllegalArgumentException(
081: "The usecase to release must not be null.");
082: }
083: getSelector().release(usecase);
084:
085: }
086:
087: /**
088: * @see org.apache.avalon.framework.activity.Disposable#dispose()
089: */
090: public void dispose() {
091: if (this .selector != null) {
092: this .manager.release(this .selector);
093: }
094: }
095:
096: /**
097: * Returns the name of the publication-overridden usecase to be resolved.
098: * @param webappUrl The web application URL.
099: * @param name The plain usecase name.
100: * @return A string.
101: * @throws ServiceException if an error occurs.
102: */
103: protected String getUsecaseName(String webappUrl, final String name)
104: throws ServiceException {
105: String newName = null;
106:
107: Publication publication = getPublication(webappUrl);
108: if (publication != null) {
109: PublicationTemplateManager templateManager = null;
110: try {
111: templateManager = (PublicationTemplateManager) this .manager
112: .lookup(PublicationTemplateManager.ROLE);
113: newName = (String) templateManager.getSelectableHint(
114: publication, getSelector(), name);
115: } finally {
116: if (templateManager != null) {
117: this .manager.release(templateManager);
118: }
119: }
120: } else {
121: newName = name;
122: }
123:
124: return newName;
125: }
126:
127: /**
128: * Returns the publication the usecase was invoked in.
129: * @param webappUrl The web application URL.
130: * @return A publication.
131: */
132: protected Publication getPublication(String webappUrl) {
133: Publication publication = null;
134: ContextUtility util = null;
135: try {
136: util = (ContextUtility) this .manager
137: .lookup(ContextUtility.ROLE);
138: Request request = util.getRequest();
139: DocumentFactory factory = DocumentUtil.getDocumentFactory(
140: this .manager, request);
141:
142: URLInformation info = new URLInformation(webappUrl);
143: String pubId = info.getPublicationId();
144:
145: if (pubId != null && factory.existsPublication(pubId)) {
146: publication = factory.getPublication(pubId);
147: }
148: } catch (Exception e) {
149: throw new RuntimeException(e);
150: } finally {
151: if (util != null) {
152: this .manager.release(util);
153: }
154: }
155: return publication;
156: }
157:
158: /**
159: * @see org.apache.lenya.cms.usecase.UsecaseResolver#resolve(java.lang.String,
160: * java.lang.String)
161: */
162: public Usecase resolve(String webappUrl, String name)
163: throws ServiceException {
164: Object usecaseName = getUsecaseName(webappUrl, name);
165: Usecase usecase = (Usecase) getSelector().select(usecaseName);
166: usecase.setName(name);
167: usecase.setSourceURL(webappUrl);
168: return usecase;
169: }
170:
171: /**
172: * @see org.apache.lenya.cms.usecase.UsecaseResolver#isRegistered(java.lang.String,
173: * java.lang.String)
174: */
175: public boolean isRegistered(String webappUrl, String name)
176: throws ServiceException {
177: String usecaseName = getUsecaseName(webappUrl, name);
178: return getSelector().isSelectable(usecaseName);
179: }
180:
181: /**
182: * @return The names of all registered usecases in alphabetical order.
183: */
184: public String[] getUsecaseNames() {
185: if (this .usecaseNames == null) {
186: throw new IllegalStateException("No usecase registered!");
187: }
188: return (String[]) this .usecaseNames
189: .toArray(new String[this .usecaseNames.size()]);
190: }
191:
192: private SortedSet usecaseNames;
193:
194: public void register(String usecaseName) {
195: if (this .usecaseNames == null) {
196: this .usecaseNames = new TreeSet();
197: }
198: this.usecaseNames.add(usecaseName);
199: }
200:
201: }
|