1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 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 40 public final class PublicationFactory { 41 42 private static Category log = Category.getInstance(PublicationFactory.class); 43 44 47 private PublicationFactory() { 48 } 49 50 private static Map keyToPublication = new HashMap (); 51 52 63 public static Publication getPublication(Map 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 82 public static Publication getPublication(String id, String servletContextPath) 83 throws PublicationException { 84 85 assert id != null; 86 assert servletContextPath != null; 87 88 String 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 116 protected static String generateKey(String publicationId, String servletContextPath) 117 throws PublicationException { 118 String key; 119 File servletContext = new File (servletContextPath); 120 String canonicalPath; 121 try { 122 canonicalPath = servletContext.getCanonicalPath(); 123 } catch (IOException e) { 124 throw new PublicationException(e); 125 } 126 key = canonicalPath + "_" + publicationId; 127 return key; 128 } 129 130 140 public static Publication getPublication(Request request, Context context) 141 throws PublicationException { 142 143 log.debug("Creating publication from Cocoon object model"); 144 String webappUrl = ServletHelper.getWebappURI(request); 145 String servletContextPath = context.getRealPath(""); 146 return getPublication(webappUrl, new File (servletContextPath)); 147 } 148 149 156 public static Publication getPublication(String webappUrl, File servletContext) 157 throws PublicationException { 158 log.debug("Creating publication from webapp URL and servlet context"); 159 160 log.debug(" Webapp URL: [" + webappUrl + "]"); 161 String publicationId = new URLInformation(webappUrl).getPublicationId(); 162 Publication publication = getPublication(publicationId, servletContext.getAbsolutePath()); 163 return publication; 164 } 165 166 172 public static boolean existsPublication(String id, String servletContextPath) { 173 174 if (servletContextPath.endsWith("/")) { 175 servletContextPath = servletContextPath.substring(0, servletContextPath.length() - 1); 176 } 177 178 File publicationDirectory = 179 new File ( 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 (publicationDirectory, Publication.CONFIGURATION_FILE).exists(); 189 190 return exists; 191 } 192 193 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 webappUri = ServletHelper.getWebappURI(request); 205 Source source = null; 206 try { 207 source = resolver.resolveURI("context:///"); 208 File servletContext = SourceUtil.getFile(source); 209 publication = PublicationFactory.getPublication(webappUri, servletContext); 210 } catch (Exception 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 |