KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > velocity > DotResourceLoader


1 package com.dotmarketing.velocity;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 import org.apache.velocity.exception.ResourceNotFoundException;
10 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
11
12 import com.dotmarketing.beans.Identifier;
13 import com.dotmarketing.cache.PageNotFoundCache;
14 import com.dotmarketing.factories.InodeFactory;
15 import com.dotmarketing.portlets.banners.model.Banner;
16 import com.dotmarketing.portlets.containers.model.Container;
17 import com.dotmarketing.portlets.contentlet.model.Contentlet;
18 import com.dotmarketing.portlets.folders.model.Folder;
19 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
20 import com.dotmarketing.portlets.templates.model.Template;
21 import com.dotmarketing.services.BannerServices;
22 import com.dotmarketing.services.ContainerServices;
23 import com.dotmarketing.services.ContentletMapServices;
24 import com.dotmarketing.services.ContentletServices;
25 import com.dotmarketing.services.PageServices;
26 import com.dotmarketing.services.TemplateServices;
27 import com.dotmarketing.util.Config;
28 import com.dotmarketing.util.Logger;
29 import com.dotmarketing.util.UtilMethods;
30
31 public class DotResourceLoader extends FileResourceLoader {
32
33     final String JavaDoc[] velocityCMSExtenstions = { Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"),
34             Config.getStringProperty("VELOCITY_CONTENT_EXTENSION"), Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION"),
35             Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION"), Config.getStringProperty("VELOCITY_CONTENT_MAP_EXTENSION"),
36             Config.getStringProperty("VELOCITY_BANNER_EXTENSION") };
37
38     public DotResourceLoader() {
39         super();
40     }
41
42     private boolean isACMSVelocityFile(String JavaDoc arg0) {
43
44         for (int i = 0; i < velocityCMSExtenstions.length; i++) {
45             if (arg0.endsWith(velocityCMSExtenstions[i])) {
46                 return true;
47             }
48         }
49         return false;
50     }
51
52     public InputStream JavaDoc getResourceStream(String JavaDoc arg0) throws ResourceNotFoundException {
53
54         try {
55
56             boolean preview = arg0.indexOf("working") > -1;
57             
58             String JavaDoc velocityRootPath = Config.getStringProperty("VELOCITY_ROOT");
59             if (velocityRootPath.startsWith("/WEB-INF")) {
60                 velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath);
61             }
62             String JavaDoc lookingFor = velocityRootPath + File.separator + arg0;
63
64             Logger.debug(this, "DotResourceLoader:\targ0:" + arg0);
65             Logger.debug(this, "DotResourceLoader:\tlooking for " + lookingFor);
66
67             java.io.File JavaDoc f = null;
68
69             if (isACMSVelocityFile(arg0)){
70                 f = new java.io.File JavaDoc(lookingFor);
71             }
72             else{
73                 f = new java.io.File JavaDoc(arg0);
74             }
75
76             if (f.exists()) {
77                 Logger.debug(this, "DotResourceLoader:\tfound " + lookingFor);
78                 return new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
79             }
80             Logger.debug(this, "DotResourceLoader:\tdid not find " + lookingFor);
81
82             // get the identifier
83
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(arg0, "/.");
84             String JavaDoc x = "0";
85             while (st.hasMoreTokens()) {
86                 String JavaDoc y = st.nextToken();
87                 if (st.hasMoreTokens()) {
88                     x = y;
89                 }
90             }
91
92             Logger.debug(this, "DotResourceLoader:\tInode: " + x);
93
94             // if we have a container
95
if (arg0.endsWith(Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"))) {
96                 
97                 try {
98                     Integer.parseInt(x);
99                     Identifier identifier = (Identifier) InodeFactory.getInode(x, Identifier.class);
100                     Container container = null;
101                     if (preview) {
102                         container = (Container) InodeFactory.getChildOfClassbyCondition(identifier, Container.class, " working = "
103                                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
104                     } else {
105                         container = (Container) InodeFactory.getChildOfClassbyCondition(identifier, Container.class, " live = "
106                                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
107                     }
108     
109                     Logger.debug(this, "DotResourceLoader:\tWriting out container inode = " + container.getInode());
110     
111                     ContainerServices.writeContainerToFile(container, identifier, preview);
112                 } catch (NumberFormatException JavaDoc e) {
113                     Logger.warn(this, "getResourceStream: Invalid resource path provided = " + arg0 + ", request discarded.");
114                 }
115             }
116
117             // if we have a contentlet
118
else if (arg0.endsWith(Config.getStringProperty("VELOCITY_CONTENT_EXTENSION"))) {
119                 String JavaDoc language = "";
120                 if (x.indexOf("_") > -1) {
121                     Logger.debug(this, "x=" + x);
122                     language = x.substring(x.indexOf("_") + 1, x.length());
123                     Logger.debug(this, "language=" + language);
124                     x = x.substring(0, x.indexOf("_"));
125                     Logger.debug(this, "x=" + x);
126                 }
127                 try {
128                     Integer.parseInt(x);
129                     Identifier identifier = (Identifier) InodeFactory.getInode(x, Identifier.class);
130     
131                     Contentlet contentlet = null;
132                     String JavaDoc condition = "";
133                     if (UtilMethods.isSet(language)) {
134                         condition = "language_id = " + language + " and";
135                     }
136                     if (preview) {
137                         condition += " working = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
138                         contentlet = (Contentlet) InodeFactory.getChildOfClassbyCondition(identifier, Contentlet.class, condition);
139                     } else {
140                         condition += " live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
141                         contentlet = (Contentlet) InodeFactory.getChildOfClassbyCondition(identifier, Contentlet.class, condition);
142                     }
143     
144                     Logger.debug(this, "DotResourceLoader:\tWriting out contentlet inode = " + contentlet.getInode());
145     
146                     ContentletServices.writeContentletToFile(contentlet, identifier, preview);
147                 } catch (NumberFormatException JavaDoc e) {
148                     Logger.warn(this, "getResourceStream: Invalid resource path provided = " + arg0 + ", request discarded.");
149                 }
150             }
151
152             // if we have a contentlet Object (dynamic list)
153
else if (arg0.endsWith(Config.getStringProperty("VELOCITY_CONTENT_MAP_EXTENSION"))) {
154                 String JavaDoc language = "";
155                 if (x.indexOf("_") > -1) {
156                     Logger.debug(this, "x=" + x);
157                     language = x.substring(x.indexOf("_") + 1, x.length());
158                     Logger.debug(this, "language=" + language);
159                     x = x.substring(0, x.indexOf("_"));
160                     Logger.debug(this, "x=" + x);
161                 }
162
163                 Contentlet contentlet = null;
164                 String JavaDoc condition = "";
165                 if (UtilMethods.isSet(language)) {
166                     condition = "language_id = " + language + " and";
167                 }
168                 if (preview) {
169                     condition += " working = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and identifier =" + x;
170                     contentlet = (Contentlet) InodeFactory.getInodeOfClassByCondition(Contentlet.class, condition);
171                 } else {
172                     condition += " live = " + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and identifier =" + x;
173                     contentlet = (Contentlet) InodeFactory.getInodeOfClassByCondition(Contentlet.class, condition);
174                 }
175
176                 Logger.debug(this, "DotResourceLoader:\tWriting out contentlet inode = " + contentlet.getInode());
177  
178                 ContentletMapServices.writeContentletMapToFile(contentlet, preview);
179             }
180
181             // if we have an HTML PAge
182
else if (arg0.endsWith(Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION"))) {
183                 if (PageNotFoundCache.getPageFromCache(arg0) == null) {
184                     try {
185                         Integer.parseInt(x);
186                         Identifier identifier = (Identifier) InodeFactory.getInode(x, Identifier.class);
187     
188                         HTMLPage page = null;
189                         if (preview) {
190                             page = (HTMLPage) InodeFactory.getChildOfClassbyCondition(identifier, HTMLPage.class, " working = "
191                                     + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
192                         } else {
193                             page = (HTMLPage) InodeFactory.getChildOfClassbyCondition(identifier, HTMLPage.class, " live = "
194                                     + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
195                         }
196     
197                         Logger.debug(this, "DotResourceLoader:\tWriting out HTMLpage inode = " + page.getInode());
198     
199                         if (page.getInode() == 0) {
200                             // Add the entry to the pageNotFoundCache
201
PageNotFoundCache.addPageToCache(arg0);
202                             throw new ResourceNotFoundException("Page " + arg0 + "not found error 404");
203                         } else {
204                             PageServices.writePageToFile(page, preview);
205                         }
206                     } catch (NumberFormatException JavaDoc e) {
207                         Logger.warn(this, "getResourceStream: Invalid resource path provided = " + arg0 + ", request discarded.");
208                     }
209                 } else {
210                     // If the page is in the page not found cache, redirect the
211
// user to the 404 page
212
throw new ResourceNotFoundException("Page " + arg0 + "not found error 404");
213                 }
214             }
215             // if we have a template
216
else if (arg0.endsWith(Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION"))) {
217                 try {
218                     Integer.parseInt(x);
219                     Identifier identifier = (Identifier) InodeFactory.getInode(x, Identifier.class);
220     
221                     Template template = null;
222                     if (preview) {
223                         template = (Template) InodeFactory.getChildOfClassbyCondition(identifier, Template.class, " working = "
224                                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
225                     } else {
226                         template = (Template) InodeFactory.getChildOfClassbyCondition(identifier, Template.class, " live = "
227                                 + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
228                     }
229     
230                     Logger.debug(this, "DotResourceLoader:\tWriting out Template inode = " + template.getInode());
231     
232                     TemplateServices.writeTemplateToFile(template, preview);
233                 } catch (NumberFormatException JavaDoc e) {
234                     Logger.warn(this, "getResourceStream: Invalid resource path provided = " + arg0 + ", request discarded.");
235                 }
236             }
237
238             // if we have a banner
239
else if (arg0.endsWith(Config.getStringProperty("VELOCITY_BANNER_EXTENSION"))) {
240                 Banner b = (Banner) InodeFactory.getInode(x, Banner.class);
241                 Folder currParentFolder = (Folder) InodeFactory.getParentOfClass(b, Folder.class);
242                 HTMLPage parentHTMLPage = (HTMLPage) InodeFactory.getInode(b.getHtmlpage(), HTMLPage.class);
243                 BannerServices.writeBannerToFile(b, currParentFolder, parentHTMLPage);
244                 Logger.debug(this, "DotResourceLoader:\tWriting out Banner = " + b.getInode());
245             }
246             f = new java.io.File JavaDoc(lookingFor);
247             FileInputStream JavaDoc fileIS = new FileInputStream JavaDoc(f);
248             BufferedInputStream JavaDoc bufferedIS = new BufferedInputStream JavaDoc(fileIS);
249             return bufferedIS;
250         }
251
252         catch (Exception JavaDoc e) {
253             Logger.debug(this, e.toString(), e);
254             throw new ResourceNotFoundException(e.toString());
255         }
256     }
257 }
Popular Tags