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: /* $Id: PublicationFactory.java 177927 2005-05-23 05:32:20Z gregor $ */
020:
021: package org.apache.lenya.cms.publication;
022:
023: import java.io.File;
024: import java.io.FileFilter;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import org.apache.avalon.framework.activity.Initializable;
033: import org.apache.avalon.framework.container.ContainerUtil;
034: import org.apache.avalon.framework.logger.AbstractLogEnabled;
035: import org.apache.avalon.framework.service.ServiceException;
036: import org.apache.avalon.framework.service.ServiceManager;
037: import org.apache.avalon.framework.service.Serviceable;
038: import org.apache.avalon.framework.thread.ThreadSafe;
039: import org.apache.excalibur.source.Source;
040: import org.apache.excalibur.source.SourceResolver;
041: import org.apache.excalibur.source.SourceUtil;
042: import org.apache.lenya.cms.repository.RepositoryException;
043: import org.apache.lenya.util.Assert;
044:
045: /**
046: * Factory for creating publication objects.
047: */
048: public final class PublicationManagerImpl extends AbstractLogEnabled
049: implements PublicationManager, Serviceable, Initializable,
050: ThreadSafe {
051:
052: /**
053: * Create a new <code>PublicationFactory</code>.
054: */
055: public PublicationManagerImpl() {
056: }
057:
058: private Map id2config;
059:
060: protected synchronized Map getId2config()
061: throws PublicationException {
062: if (this .id2config == null) {
063: this .id2config = new HashMap();
064: File servletContext = new File(this .servletContextPath);
065: File publicationsDirectory = new File(servletContext,
066: Publication.PUBLICATION_PREFIX);
067: File[] publicationDirectories = publicationsDirectory
068: .listFiles(new FileFilter() {
069: public boolean accept(File file) {
070: File configFile = new File(
071: file,
072: PublicationConfiguration.CONFIGURATION_FILE);
073: return configFile.exists();
074: }
075: });
076: for (int i = 0; i < publicationDirectories.length; i++) {
077: String id = publicationDirectories[i].getName();
078: addPublication(id);
079: }
080: }
081: return this .id2config;
082: }
083:
084: public Publication getPublication(DocumentFactory factory, String id)
085: throws PublicationException {
086:
087: Assert.notNull("publication ID", id);
088: Map id2config = getId2config();
089: if (!id2config.containsKey(id)) {
090: throw new PublicationException("The publication [" + id
091: + "] does not exist.");
092: }
093:
094: PublicationConfiguration config = (PublicationConfiguration) id2config
095: .get(id);
096: PublicationFactory pubFactory = new PublicationFactory(
097: this .manager, config);
098: try {
099: return (Publication) factory.getSession()
100: .getRepositoryItem(pubFactory, id);
101: } catch (RepositoryException e) {
102: throw new PublicationException(e);
103: }
104: }
105:
106: public Publication[] getPublications(DocumentFactory factory) {
107: List publications = new ArrayList();
108:
109: try {
110: Map id2config = getId2config();
111: for (Iterator i = id2config.keySet().iterator(); i
112: .hasNext();) {
113: String publicationId = (String) i.next();
114: Publication publication = getPublication(factory,
115: publicationId);
116: publications.add(publication);
117: }
118: } catch (RuntimeException e) {
119: throw e;
120: } catch (Exception e) {
121: throw new RuntimeException(e);
122: }
123:
124: return (Publication[]) publications
125: .toArray(new Publication[publications.size()]);
126: }
127:
128: public String[] getPublicationIds() {
129: Set ids;
130: try {
131: ids = getId2config().keySet();
132: } catch (PublicationException e) {
133: throw new RuntimeException(e);
134: }
135: return (String[]) ids.toArray(new String[ids.size()]);
136: }
137:
138: private String servletContextPath;
139:
140: private ServiceManager manager;
141:
142: public void service(ServiceManager manager) throws ServiceException {
143: this .manager = manager;
144: }
145:
146: public void initialize() throws Exception {
147: SourceResolver resolver = null;
148: Source source = null;
149: try {
150: resolver = (SourceResolver) this .manager
151: .lookup(SourceResolver.ROLE);
152: source = resolver.resolveURI("context:///");
153: this .servletContextPath = SourceUtil.getFile(source)
154: .getCanonicalPath();
155: } finally {
156: if (resolver != null) {
157: if (source != null) {
158: resolver.release(source);
159: }
160: this .manager.release(resolver);
161: }
162: }
163: }
164:
165: public void addPublication(String pubId)
166: throws PublicationException {
167: Map id2config = getId2config();
168: if (id2config.containsKey(pubId)) {
169: throw new PublicationException("The publication [" + pubId
170: + "] already exists.");
171: }
172: PublicationConfiguration config = new PublicationConfiguration(
173: pubId, this .servletContextPath);
174: ContainerUtil.enableLogging(config, getLogger());
175: id2config.put(pubId, config);
176: }
177:
178: protected String getServletContextPath() {
179: return this.servletContextPath;
180: }
181:
182: }
|