1 17 package org.alfresco.repo.template; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.io.Reader ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 26 import org.alfresco.model.ContentModel; 27 import org.alfresco.service.ServiceRegistry; 28 import org.alfresco.service.cmr.repository.ContentReader; 29 import org.alfresco.service.cmr.repository.ContentService; 30 import org.alfresco.service.cmr.repository.NodeRef; 31 import org.alfresco.service.cmr.repository.NodeService; 32 import org.alfresco.service.cmr.repository.StoreRef; 33 import org.alfresco.util.ApplicationContextHelper; 34 35 import freemarker.cache.TemplateLoader; 36 37 45 public class ClassPathRepoTemplateLoader implements TemplateLoader 46 { 47 private NodeService nodeService; 48 private ContentService contentService; 49 50 public ClassPathRepoTemplateLoader(NodeService nodeService, ContentService contentService) 51 { 52 if (nodeService == null) 53 { 54 throw new IllegalArgumentException ("NodeService is mandatory."); 55 } 56 if (contentService == null) 57 { 58 throw new IllegalArgumentException ("ContentService is mandatory."); 59 } 60 this.nodeService = nodeService; 61 this.contentService = contentService; 62 } 63 64 67 public Object findTemplateSource(String name) 68 throws IOException 69 { 70 if (name.indexOf(StoreRef.URI_FILLER) != -1) 71 { 72 NodeRef ref = new NodeRef(name); 73 if (this.nodeService.exists(ref) == true) 74 { 75 return new RepoTemplateSource(ref); 76 } 77 else 78 { 79 return null; 80 } 81 } 82 else 83 { 84 URL url = this.getClass().getClassLoader().getResource(name); 85 return url == null ? null : new ClassPathTemplateSource(url); 86 } 87 } 88 89 public long getLastModified(Object templateSource) 90 { 91 return ((BaseTemplateSource)templateSource).lastModified(); 92 } 93 94 public Reader getReader(Object templateSource, String encoding) throws IOException 95 { 96 return ((BaseTemplateSource)templateSource).getReader(); 97 } 98 99 public void closeTemplateSource(Object templateSource) throws IOException 100 { 101 ((BaseTemplateSource)templateSource).close(); 102 } 103 104 105 108 abstract class BaseTemplateSource 109 { 110 public abstract Reader getReader() throws IOException ; 111 112 public abstract void close() throws IOException ; 113 114 public abstract long lastModified(); 115 } 116 117 118 121 class ClassPathTemplateSource extends BaseTemplateSource 122 { 123 private final URL url; 124 private URLConnection conn; 125 private InputStream inputStream; 126 127 ClassPathTemplateSource(URL url) throws IOException 128 { 129 this.url = url; 130 this.conn = url.openConnection(); 131 } 132 133 public boolean equals(Object o) 134 { 135 if (o instanceof ClassPathTemplateSource) 136 { 137 return url.equals(((ClassPathTemplateSource)o).url); 138 } 139 else 140 { 141 return false; 142 } 143 } 144 145 public int hashCode() 146 { 147 return url.hashCode(); 148 } 149 150 public String toString() 151 { 152 return url.toString(); 153 } 154 155 public long lastModified() 156 { 157 return conn.getLastModified(); 158 } 159 160 public Reader getReader() throws IOException 161 { 162 inputStream = conn.getInputStream(); 163 return new InputStreamReader (inputStream); 164 } 165 166 public void close() throws IOException 167 { 168 try 169 { 170 if (inputStream != null) 171 { 172 inputStream.close(); 173 } 174 } 175 finally 176 { 177 inputStream = null; 178 conn = null; 179 } 180 } 181 } 182 183 186 class RepoTemplateSource extends BaseTemplateSource 187 { 188 private final NodeRef nodeRef; 189 private InputStream inputStream; 190 private ContentReader conn; 191 192 RepoTemplateSource(NodeRef ref) throws IOException 193 { 194 this.nodeRef = ref; 195 this.conn = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); 196 } 197 198 public boolean equals(Object o) 199 { 200 if (o instanceof RepoTemplateSource) 201 { 202 return nodeRef.equals(((RepoTemplateSource)o).nodeRef); 203 } 204 else 205 { 206 return false; 207 } 208 } 209 210 public int hashCode() 211 { 212 return nodeRef.hashCode(); 213 } 214 215 public String toString() 216 { 217 return nodeRef.toString(); 218 } 219 220 public long lastModified() 221 { 222 return conn.getLastModified(); 223 } 224 225 public Reader getReader() throws IOException 226 { 227 inputStream = conn.getContentInputStream(); 228 return new InputStreamReader (inputStream); 229 } 230 231 public void close() throws IOException 232 { 233 try 234 { 235 if (inputStream != null) 236 { 237 inputStream.close(); 238 } 239 } 240 finally 241 { 242 inputStream = null; 243 conn = null; 244 } 245 } 246 } 247 } 248 | Popular Tags |