1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.javascript;
18
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Set;
22
23 import javax.jcr.Node;
24 import javax.jcr.NodeIterator;
25 import javax.jcr.PathNotFoundException;
26 import javax.jcr.RepositoryException;
27 import javax.jcr.Session;
28 import javax.jcr.ValueFormatException;
29 import javax.jcr.query.Query;
30 import javax.jcr.query.QueryManager;
31 import javax.jcr.query.QueryResult;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.picocontainer.Startable;
35
36 import org.exoplatform.services.cache.CacheService;
37 import org.exoplatform.services.cache.ExoCache;
38 import org.exoplatform.services.handler.SiteJavascriptHandler;
39 import org.exoplatform.services.jcr.RepositoryService;
40 import org.exoplatform.services.jcr.core.ManageableRepository;
41 import org.exoplatform.services.jcr.ext.common.SessionProvider;
42 import org.exoplatform.services.log.ExoLogger;
43 import org.exoplatform.services.log.Log;
44 import org.exoplatform.services.wcm.core.NodeLocation;
45 import org.exoplatform.services.wcm.core.NodetypeConstant;
46 import org.exoplatform.services.wcm.core.WCMConfigurationService;
47 import org.exoplatform.services.wcm.portal.LivePortalManagerService;
48 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
49 import org.exoplatform.web.application.javascript.JavascriptConfigService;
50
51
52
53
54
55
56
57 public class XJavascriptService implements Startable {
58
59 private static String WEBCONTENT_JS_QUERY = "select * from exo:jsFile "
60 + "where jcr:path like '{path}/%' and exo:active='true' "
61 + "order by exo:priority ASC";
62
63
64 final private String MODULE_NAME = "eXo.WCM.Live";
65
66 public final static String JS_PATH_REGEXP = "/(.*)/javascript/eXo/(.*)/live";
67
68
69 final private String PATH = "/javascript/eXo/{portalName}/live";
70
71
72 private JavascriptConfigService jsConfigService ;
73
74
75 private WCMConfigurationService configurationService;
76
77 private LivePortalManagerService livePortalManagerService_;
78
79
80 private static final Log LOG = ExoLogger.getLogger(XJavascriptService.class.getName());
81
82 private Set<String> loadedJSModule = new HashSet<String>();
83 private Set<String> loadedSharedJSModule = new HashSet<String>();
84
85 private ExoCache<String, Object> jsCache_;
86
87
88
89
90
91
92
93
94 public XJavascriptService(WCMConfigurationService wcmConfigurationService, JavascriptConfigService javascriptConfigService, LivePortalManagerService livePortalService) throws Exception{
95 this.livePortalManagerService_ = livePortalService;
96 this.jsConfigService = javascriptConfigService;
97 this.configurationService = wcmConfigurationService;
98
99 jsCache_ = WCMCoreUtils.getService(CacheService.class).getCacheInstance(SiteJavascriptHandler.CACHE_REGION);
100
101 this.jsConfigService.addResourceResolver(new WCMJavascriptResourceResolver(livePortalManagerService_, jsConfigService));
102 }
103
104
105
106
107
108
109
110
111 public String getActiveJavaScript(Node webcontent) throws Exception {
112 StringBuffer buffer = new StringBuffer();
113 String jsQuery = StringUtils.replaceOnce(WEBCONTENT_JS_QUERY, "{path}", webcontent.getPath());
114
115
116 NodeLocation webcontentLocation = NodeLocation.getNodeLocationByNode(webcontent);
117 RepositoryService repositoryService = WCMCoreUtils.getService(RepositoryService.class);
118 ManageableRepository repository = repositoryService.getCurrentRepository();
119 Session session = null;
120 if (webcontentLocation.getPath().startsWith("/jcr:system"))
121 session = repository.getSystemSession(repository.getConfiguration().getSystemWorkspaceName());
122 else {
123 session = repository.getSystemSession(webcontentLocation.getWorkspace());
124 }
125
126 QueryManager queryManager = session.getWorkspace().getQueryManager();
127 Query query = queryManager.createQuery(jsQuery, Query.SQL);
128 QueryResult queryResult = query.execute();
129 NodeIterator iterator = queryResult.getNodes();
130 while(iterator.hasNext()) {
131 Node registeredJSFile = iterator.nextNode();
132 buffer.append(getActivedJSData(registeredJSFile));
133 }
134 session.logout();
135 return buffer.toString();
136 }
137
138
139
140
141
142
143
144
145
146 public void updatePortalJSOnModify(Node portalNode, Node jsFile) throws Exception {
147 String sharedPortalName = configurationService.getSharedPortalName();
148 if(sharedPortalName.equals(portalNode.getName())) {
149 addSharedPortalJavascript(portalNode, jsFile, false);
150 }else {
151 addPortalJavascript(portalNode, jsFile, false);
152 }
153 }
154
155
156
157
158
159
160
161
162
163 public void updatePortalJSOnRemove(Node portalNode, Node jsFile) throws Exception {
164 String sharedPortalName = configurationService.getSharedPortalName();
165 if(sharedPortalName.equals(portalNode.getName())) {
166 addSharedPortalJavascript(portalNode, jsFile, false);
167 }else {
168 addPortalJavascript(portalNode, jsFile, false);
169 }
170 }
171
172
173
174
175
176
177
178
179 private void addPortalJavascript(Node portalNode, Node jsFile, boolean isStartup) throws Exception {
180 String javascriptPath = StringUtils.replaceOnce(PATH, "{portalName}", portalNode.getName());
181
182 String moduleName = MODULE_NAME + '.' + portalNode.getName();
183
184
185 if (!loadedJSModule.contains(moduleName)) {
186 loadedJSModule.add(moduleName);
187
188
189
190 }
191 jsCache_.clearCache();
192 }
193
194
195
196
197
198
199
200
201 private void addSharedPortalJavascript(Node portalNode, Node jsFile, boolean isStartup) throws Exception {
202 if (portalNode != null) {
203 String moduleName = MODULE_NAME + '.' + portalNode.getName();
204 if (!loadedSharedJSModule.contains(moduleName)) {
205 loadedSharedJSModule.add(moduleName);
206 }
207 jsCache_.clearCache();
208 }
209 }
210
211 private String getActivedJSData(Node jsFile) throws ValueFormatException,
212 RepositoryException,
213 PathNotFoundException {
214 if (jsFile != null && !jsFile.isNodeType("exo:restoreLocation")
215 && jsFile.hasNode(NodetypeConstant.JCR_CONTENT)
216 && jsFile.getNode(NodetypeConstant.JCR_CONTENT).hasProperty(NodetypeConstant.JCR_DATA)
217 && jsFile.hasProperty(NodetypeConstant.EXO_ACTIVE)
218 && jsFile.getProperty(NodetypeConstant.EXO_ACTIVE).getBoolean() == true) {
219
220 return jsFile.getNode(NodetypeConstant.JCR_CONTENT).getProperty(NodetypeConstant.JCR_DATA).getString();
221 }
222 return "";
223 }
224
225
226
227
228 public void start() {
229 SessionProvider sessionProvider = SessionProvider.createSystemProvider();
230 try {
231 LivePortalManagerService livePortalManagerService = WCMCoreUtils.getService(LivePortalManagerService.class);
232 List<Node> livePortals = livePortalManagerService.getLivePortals(sessionProvider);
233 for(Node portal: livePortals) {
234 addPortalJavascript(portal, null, true);
235 }
236 Node sharedPortal = livePortalManagerService.getLiveSharedPortal(sessionProvider);
237 addSharedPortalJavascript(sharedPortal, null, true);
238 } catch (PathNotFoundException e) {
239 if (LOG.isWarnEnabled()) {
240 LOG.warn("Exception when merging inside Portal : WCM init is not completed.");
241 }
242 } catch (Exception e) {
243 LOG.error("Exception when start XJavascriptService", e);
244 } finally {
245 sessionProvider.close();
246 }
247 }
248
249
250
251
252 public void stop() {
253 }
254
255 }