KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > PublicationFactory


1 /*
2  * Copyright 1999-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
18 /* $Id: PublicationFactory.java 43091 2004-06-28 08:32:13Z andreas $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cocoon.environment.Context;
28 import org.apache.cocoon.environment.ObjectModelHelper;
29 import org.apache.cocoon.environment.Request;
30 import org.apache.excalibur.source.Source;
31 import org.apache.excalibur.source.SourceResolver;
32 import org.apache.excalibur.source.SourceUtil;
33 import org.apache.lenya.cms.publication.file.FilePublication;
34 import org.apache.lenya.util.ServletHelper;
35 import org.apache.log4j.Category;
36
37 /**
38  * Factory for creating publication objects.
39  */

40 public final class PublicationFactory {
41
42     private static Category log = Category.getInstance(PublicationFactory.class);
43
44     /**
45      * Create a new <code>PublicationFactory</code>.
46      */

47     private PublicationFactory() {
48     }
49
50     private static Map JavaDoc keyToPublication = new HashMap JavaDoc();
51
52     /**
53      * Creates a new publication.
54      * The publication ID is resolved from the request URI.
55      * The servlet context path is resolved from the context object.
56     
57      * @param objectModel The object model of the Cocoon component.
58      *
59      * @return a <code>Publication</code>
60      *
61      * @throws PublicationException if there was a problem creating the publication.
62      */

63     public static Publication getPublication(Map JavaDoc objectModel) throws PublicationException {
64
65         assert objectModel != null;
66         Request request = ObjectModelHelper.getRequest(objectModel);
67         Context context = ObjectModelHelper.getContext(objectModel);
68         return getPublication(request, context);
69     }
70
71     /**
72      * Create a new publication with the given publication-id and servlet context path.
73      * These publications are cached and reused for similar requests.
74      *
75      * @param id the publication id
76      * @param servletContextPath the servlet context path of the publication
77      *
78      * @return a <code>Publication</code>
79      *
80      * @throws PublicationException if there was a problem creating the publication.
81      */

82     public static Publication getPublication(String JavaDoc id, String JavaDoc servletContextPath)
83         throws PublicationException {
84
85         assert id != null;
86         assert servletContextPath != null;
87
88         String JavaDoc key = generateKey(id, servletContextPath);
89         Publication publication = null;
90
91         if (keyToPublication.containsKey(key)) {
92             publication = (Publication) keyToPublication.get(key);
93         } else {
94             if (PublicationFactory.existsPublication(id, servletContextPath)) {
95                 publication = new FilePublication(id, servletContextPath);
96                 keyToPublication.put(key, publication);
97             }
98         }
99
100         if (publication == null) {
101             throw new PublicationException("The publication for ID [" + id + "] could not be created.");
102         }
103         return publication;
104     }
105
106     /**
107      * Generates a key to cache a publication.
108      * The cache key is constructed using the canonical servlet context path
109      * and the publication ID.
110      *
111      * @param publicationId The publication ID.
112      * @param servletContextPath The servlet context path.
113      * @return A cache key.
114      * @throws PublicationException when the servlet context does not exist.
115      */

116     protected static String JavaDoc generateKey(String JavaDoc publicationId, String JavaDoc servletContextPath)
117         throws PublicationException {
118         String JavaDoc key;
119         File JavaDoc servletContext = new File JavaDoc(servletContextPath);
120         String JavaDoc canonicalPath;
121         try {
122             canonicalPath = servletContext.getCanonicalPath();
123         } catch (IOException JavaDoc e) {
124             throw new PublicationException(e);
125         }
126         key = canonicalPath + "_" + publicationId;
127         return key;
128     }
129
130     /**
131      * Creates a new publication based on a request and a context.
132      *
133      * @param request A request.
134      * @param context A context.
135      *
136      * @return A publication.
137      *
138      * @throws PublicationException if there was a problem creating the publication.
139      */

140     public static Publication getPublication(Request request, Context context)
141         throws PublicationException {
142
143         log.debug("Creating publication from Cocoon object model");
144         String JavaDoc webappUrl = ServletHelper.getWebappURI(request);
145         String JavaDoc servletContextPath = context.getRealPath("");
146         return getPublication(webappUrl, new File JavaDoc(servletContextPath));
147     }
148
149     /**
150      * Creates a publication from a webapp URL and a servlet context directory.
151      * @param webappUrl The URL within the web application (without context prefix)
152      * @param servletContext The Lenya servlet context directory
153      * @return A publication
154      * @throws PublicationException when something went wrong
155      */

156     public static Publication getPublication(String JavaDoc webappUrl, File JavaDoc servletContext)
157         throws PublicationException {
158         log.debug("Creating publication from webapp URL and servlet context");
159
160         log.debug(" Webapp URL: [" + webappUrl + "]");
161         String JavaDoc publicationId = new URLInformation(webappUrl).getPublicationId();
162         Publication publication = getPublication(publicationId, servletContext.getAbsolutePath());
163         return publication;
164     }
165
166     /**
167      * Checks if a publication with a certain ID exists in a certain context.
168      * @param id The publication ID.
169      * @param servletContextPath The webapp context path.
170      * @return <code>true</code> if the publication exists, <code>false</code> otherwise.
171      */

172     public static boolean existsPublication(String JavaDoc id, String JavaDoc servletContextPath) {
173
174         if (servletContextPath.endsWith("/")) {
175             servletContextPath = servletContextPath.substring(0, servletContextPath.length() - 1);
176         }
177
178         File JavaDoc publicationDirectory =
179             new File JavaDoc(
180                 servletContextPath
181                     + File.separator
182                     + Publication.PUBLICATION_PREFIX
183                     + File.separator
184                     + id);
185
186         boolean exists = true;
187         exists = exists && publicationDirectory.isDirectory();
188         exists = exists && new File JavaDoc(publicationDirectory, Publication.CONFIGURATION_FILE).exists();
189
190         return exists;
191     }
192
193     /**
194      * Creates a publication using a source resolver and a request.
195      * @param resolver The source resolver.
196      * @param request The request.
197      * @return A publication.
198      * @throws PublicationException when something went wrong.
199      */

200     public static Publication getPublication(SourceResolver resolver, Request request)
201         throws PublicationException {
202         log.debug("Creating publication from resolver and request");
203         Publication publication;
204         String JavaDoc webappUri = ServletHelper.getWebappURI(request);
205         Source source = null;
206         try {
207             source = resolver.resolveURI("context:///");
208             File JavaDoc servletContext = SourceUtil.getFile(source);
209             publication = PublicationFactory.getPublication(webappUri, servletContext);
210         } catch (Exception JavaDoc e) {
211             throw new PublicationException(e);
212         } finally {
213             if (source != null) {
214                 resolver.release(source);
215             }
216         }
217         return publication;
218     }
219     
220 }
221
Popular Tags