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:
019: package org.apache.lenya.cms.publication;
020:
021: import java.io.File;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.avalon.framework.logger.AbstractLogEnabled;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.ServiceSelector;
032: import org.apache.lenya.cms.cocoon.components.context.ContextUtility;
033: import org.apache.lenya.cms.repository.Session;
034:
035: /**
036: * A publication.
037: * @version $Id: PublicationImpl.java 565766 2007-08-14 14:09:21Z andreas $
038: */
039: public class PublicationImpl extends AbstractLogEnabled implements
040: Publication {
041:
042: private PublicationConfiguration delegate;
043: protected ServiceManager manager;
044: private DocumentFactory factory;
045:
046: protected PublicationImpl(ServiceManager manager,
047: DocumentFactory factory, PublicationConfiguration delegate) {
048: this .delegate = delegate;
049: this .manager = manager;
050: this .factory = factory;
051: }
052:
053: public boolean exists() {
054: return delegate.exists();
055: }
056:
057: public String getBreadcrumbPrefix() {
058: return delegate.getBreadcrumbPrefix();
059: }
060:
061: public String getContentDir() {
062: return delegate.getContentDir();
063: }
064:
065: public File getContentDirectory(String area) {
066: return delegate.getContentDirectory(area);
067: }
068:
069: public String getContentURI(String area) {
070: return delegate.getContentURI(area);
071: }
072:
073: public String getDefaultLanguage() {
074: return delegate.getDefaultLanguage();
075: }
076:
077: public File getDirectory() {
078: return delegate.getDirectory();
079: }
080:
081: private DocumentBuilder documentBuilder;
082:
083: public DocumentBuilder getDocumentBuilder() {
084: if (this .documentBuilder == null) {
085: ServiceSelector selector = null;
086: try {
087: selector = (ServiceSelector) this .manager
088: .lookup(DocumentBuilder.ROLE + "Selector");
089: this .documentBuilder = (DocumentBuilder) selector
090: .select(delegate.getDocumentBuilderHint());
091: } catch (Exception e) {
092: throw new RuntimeException(e);
093: }
094: }
095: return this .documentBuilder;
096: }
097:
098: public String getId() {
099: return delegate.getId();
100: }
101:
102: public String getInstantiatorHint() {
103: return delegate.getInstantiatorHint();
104: }
105:
106: public String[] getLanguages() {
107: return delegate.getLanguages();
108: }
109:
110: public DocumentIdToPathMapper getPathMapper() {
111: return delegate.getPathMapper();
112: }
113:
114: public Proxy getProxy(Document document, boolean isSslProtected) {
115: return getProxy(document.getArea(), isSslProtected);
116: }
117:
118: public Proxy getProxy(String area, boolean isSslProtected) {
119: Proxy proxy = delegate.getProxy(area, isSslProtected);
120: if (proxy == null) {
121: String url = getDefaultProxyUrl(area);
122: proxy = new Proxy();
123: proxy.setUrl(url);
124: this .delegate.setProxy(area, isSslProtected, proxy);
125: }
126: return proxy;
127: }
128:
129: private String contextPath;
130:
131: protected String getContextPath() {
132: if (this .contextPath == null) {
133: ContextUtility context = null;
134: try {
135: context = (ContextUtility) this .manager
136: .lookup(ContextUtility.ROLE);
137: this .contextPath = context.getRequest()
138: .getContextPath();
139: } catch (ServiceException e) {
140: throw new RuntimeException(e);
141: } finally {
142: if (context != null) {
143: this .manager.release(context);
144: }
145: }
146: }
147: return this .contextPath;
148: }
149:
150: /**
151: * @param area The area.
152: * @return The proxy URL if no proxy is declared in {@link PublicationConfiguration#CONFIGURATION_FILE}.
153: */
154: protected String getDefaultProxyUrl(String area) {
155: return getContextPath() + "/" + getId() + "/" + area;
156: }
157:
158: private List allResourceTypes;
159:
160: protected String getFirstTemplateId() {
161: String[] templateIds = getTemplateIds();
162: if (templateIds.length > 0) {
163: return templateIds[0];
164: } else {
165: return null;
166: }
167: }
168:
169: public String[] getResourceTypeNames() {
170: if (this .allResourceTypes == null) {
171: this .allResourceTypes = new ArrayList();
172: this .allResourceTypes.addAll(Arrays.asList(this .delegate
173: .getResourceTypeNames()));
174: String templateId = getFirstTemplateId();
175: if (templateId != null) {
176: try {
177: Publication template = getFactory().getPublication(
178: templateId);
179: String[] templateTypes = template
180: .getResourceTypeNames();
181: this .allResourceTypes.addAll(Arrays
182: .asList(templateTypes));
183: } catch (PublicationException e) {
184: throw new RuntimeException(e);
185: }
186: }
187: }
188: return (String[]) this .allResourceTypes
189: .toArray(new String[this .allResourceTypes.size()]);
190: }
191:
192: public File getServletContext() {
193: return delegate.getServletContext();
194: }
195:
196: public String getSiteManagerHint() {
197: return delegate.getSiteManagerHint();
198: }
199:
200: public String getSourceURI() {
201: return delegate.getSourceURI();
202: }
203:
204: public String[] getTemplateIds() {
205: return delegate.getTemplateIds();
206: }
207:
208: public String getWorkflowSchema(ResourceType resourceType) {
209: String schema = this .delegate.getWorkflowSchema(resourceType);
210: if (schema == null && getTemplateIds().length > 0) {
211: String templateId = getFirstTemplateId();
212: try {
213: Publication template = getFactory().getPublication(
214: templateId);
215: schema = template.getWorkflowSchema(resourceType);
216: } catch (PublicationException e) {
217: throw new RuntimeException(e);
218: }
219: }
220: return schema;
221: }
222:
223: public void setDefaultLanguage(String language) {
224: delegate.setDefaultLanguage(language);
225: }
226:
227: public void setPathMapper(DefaultDocumentIdToPathMapper mapper) {
228: delegate.setPathMapper(mapper);
229: }
230:
231: private Map areas = new HashMap();
232:
233: public Area getArea(String name) throws PublicationException {
234: if (!this .areas.containsKey(name)) {
235: Area area = new AreaImpl(this .manager, this .factory, this ,
236: name);
237: this .areas.put(name, area);
238: }
239: return (Area) this .areas.get(name);
240: }
241:
242: public String[] getAreaNames() {
243: return delegate.getAreaNames();
244: }
245:
246: public DocumentFactory getFactory() {
247: return this .factory;
248: }
249:
250: public boolean equals(Object obj) {
251: if (!getClass().isInstance(obj)) {
252: return false;
253: }
254: return ((Publication) obj).getId().equals(getId());
255: }
256:
257: public int hashCode() {
258: return getId().hashCode();
259: }
260:
261: public String toString() {
262: return getId();
263: }
264:
265: public String getName() {
266: return delegate.getName();
267: }
268:
269: public Session getSession() {
270: return getFactory().getSession();
271: }
272:
273: }
|