1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.cms.impl;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.exoplatform.container.configuration.ConfigurationManager;
21 import org.exoplatform.services.cache.CacheService;
22 import org.exoplatform.services.cache.ExoCache;
23 import org.exoplatform.services.cms.actions.ActionServiceContainer;
24 import org.exoplatform.services.cms.jcrext.activity.ActivityCommonService;
25 import org.exoplatform.services.cms.scripts.CmsScript;
26 import org.exoplatform.services.jcr.RepositoryService;
27 import org.exoplatform.services.jcr.core.ManageableRepository;
28 import org.exoplatform.services.jcr.ext.common.SessionProvider;
29 import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
30 import org.exoplatform.services.wcm.core.NodetypeConstant;
31 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32 import org.picocontainer.Startable;
33
34 import javax.jcr.Node;
35 import javax.jcr.NodeIterator;
36 import javax.jcr.PathNotFoundException;
37 import javax.jcr.Session;
38 import javax.jcr.nodetype.NodeType;
39 import javax.jcr.nodetype.PropertyDefinition;
40 import java.io.ByteArrayInputStream;
41 import java.io.InputStream;
42 import java.util.Collection;
43 import java.util.GregorianCalendar;
44 import java.util.List;
45
46 public abstract class BaseResourceLoaderService implements Startable{
47
48 protected NodeHierarchyCreator nodeHierarchyCreator_;
49
50 protected RepositoryService repositoryService_;
51
52 protected ConfigurationManager cservice_;
53
54 protected ExoCache<String, CmsScript> resourceCache_;
55
56 private ActivityCommonService activityService = null;
57
58 private static final String EDITED_CONFIGURED_SCRIPTS = "EditedConfiguredScripts";
59
60 private static final String CACHE_NAME = "ecms.BaseResourceCache";
61
62
63
64
65 private DMSConfiguration dmsConfiguration_;
66
67
68
69
70
71
72
73
74
75
76
77 public BaseResourceLoaderService(ConfigurationManager cservice,
78 NodeHierarchyCreator nodeHierarchyCreator, RepositoryService repositoryService,
79 CacheService cacheService, DMSConfiguration dmsConfiguration) throws Exception {
80 nodeHierarchyCreator_ = nodeHierarchyCreator;
81 repositoryService_ = repositoryService;
82 cservice_ = cservice;
83 resourceCache_ = cacheService.getCacheInstance(CACHE_NAME);
84 dmsConfiguration_ = dmsConfiguration;
85 }
86
87
88
89
90
91 abstract protected String getBasePath();
92
93
94
95
96
97
98 abstract protected void removeFromCache(String resourceName);
99
100
101
102
103 public void start(){};
104
105
106
107
108 public void stop(){};
109
110
111
112
113
114
115
116
117
118
119
120 protected void init(Session session, ResourceConfig resourceConfig, String location) throws Exception {
121 addScripts(session, resourceConfig.getRessources(),location) ;
122 }
123
124
125
126
127
128
129
130
131
132 protected void addScripts(Session session, List<ResourceConfig.Resource> resources, String location) throws Exception{
133 String resourcesPath = getBasePath();
134 if (resources.size() == 0) return;
135 try {
136 String firstResourceName = resources.get(0).getName();
137 session.getItem(resourcesPath + "/" + firstResourceName);
138 return;
139 } catch (PathNotFoundException e) {
140 Node resourcesHome = (Node) session.getItem(resourcesPath);
141 String warPath = location + resourcesPath.substring(resourcesPath.lastIndexOf("/")) ;
142 for (ResourceConfig.Resource resource : resources) {
143 String name = resource.getName();
144 if(Utils.getAllEditedConfiguredData(this.getClass().getSimpleName(), EDITED_CONFIGURED_SCRIPTS, true).contains(name)) {
145 continue;
146 }
147 String description = resource.getDescription();
148 String path = warPath + "/" + name;
149 InputStream in = cservice_.getInputStream(path);
150 addResource(resourcesHome, name, description, in);
151 }
152 resourcesHome.save();
153 }
154 }
155
156
157
158
159
160
161
162
163 public void addResource(Node resourcesHome, String resourceName, InputStream in)
164 throws Exception {
165 addResource(resourcesHome, resourceName, resourceName, in);
166 }
167
168
169
170
171
172
173
174
175 public void addResource(Node resourcesHome, String resourceName, String resourceDescription, InputStream in)
176 throws Exception {
177 Node contentNode = null;
178 if(resourceName.lastIndexOf("/")>-1) {
179 String realParenPath = StringUtils.substringBeforeLast(resourceName,"/") ;
180 Node parentResource = resourcesHome.getNode(realParenPath) ;
181 resourcesHome = parentResource ;
182 resourceName = StringUtils.substringAfterLast(resourceName,"/") ;
183 }
184 Node script = null;
185 try {
186 script = resourcesHome.getNode(resourceName);
187 contentNode = script.getNode(NodetypeConstant.JCR_CONTENT);
188 if(!contentNode.isCheckedOut()) contentNode.checkout() ;
189 } catch (PathNotFoundException e) {
190 script = resourcesHome.addNode(resourceName, NodetypeConstant.NT_FILE);
191 contentNode = script.addNode(NodetypeConstant.JCR_CONTENT, NodetypeConstant.EXO_RESOURCES);
192 contentNode.setProperty(NodetypeConstant.JCR_ENCODING, "UTF-8");
193 contentNode.setProperty(NodetypeConstant.JCR_MIME_TYPE, "application/x-groovy");
194 }
195 if (activityService==null) {
196 activityService = WCMCoreUtils.getService(ActivityCommonService.class);
197 }
198 activityService.setCreating(script, true);
199 contentNode.setProperty(NodetypeConstant.JCR_DATA, in);
200 contentNode.setProperty(NodetypeConstant.DC_DESCRIPTION, new String[] { resourceDescription });
201 contentNode.setProperty(NodetypeConstant.JCR_LAST_MODIFIED, new GregorianCalendar());
202 activityService.setCreating(script, false);
203 resourcesHome.save() ;
204 }
205
206
207
208
209
210
211
212
213
214
215 protected Node getResourcesHome(SessionProvider sessionProvider) throws Exception {
216 ManageableRepository manageableRepository = null;
217 manageableRepository = repositoryService_.getCurrentRepository();
218 DMSRepositoryConfiguration dmsRepoConfig = dmsConfiguration_.getConfig();
219 Session session = sessionProvider.getSession(dmsRepoConfig.getSystemWorkspace(),
220 manageableRepository);
221 String resourcesPath = getBasePath();
222 return (Node) session.getItem(resourcesPath);
223 }
224
225
226
227
228
229
230
231 public String getResourceAsText(String resourceName) throws Exception {
232 SessionProvider systemProvider = SessionProvider.createSystemProvider();
233 try {
234 Node resourceNode = getResourceByName(systemProvider, resourceName);
235 String text = resourceNode.getNode("jcr:content").getProperty("jcr:data").getString();
236 return text;
237 } finally {
238 systemProvider.close();
239 }
240 }
241
242
243
244
245
246
247
248 public Node getResourceByName(SessionProvider systemProvider, String resourceName) throws Exception {
249 Node resourcesHome = getResourcesHome(systemProvider);
250 return resourcesHome.getNode(resourceName);
251 }
252
253
254
255
256
257
258
259 public String getResourceNameByNodeType(NodeType nodeType) throws Exception {
260 if(nodeType.isNodeType("exo:scriptAction")) {
261 PropertyDefinition[] arrProperties = nodeType.getPropertyDefinitions();
262 for(PropertyDefinition property : arrProperties) {
263 if(property.getName().equals("exo:script")) {
264 return property.getDefaultValues()[0].getString();
265 }
266 }
267 }
268 return StringUtils.EMPTY;
269 }
270
271
272
273
274
275
276
277 public NodeType getNodeTypeByResourceName(String resourceName) throws Exception {
278 ActionServiceContainer actionsServiceContainer = WCMCoreUtils.getService(ActionServiceContainer.class) ;
279 Collection<NodeType> actionList = actionsServiceContainer.getCreatedActionTypes(
280 WCMCoreUtils.getRepository().getConfiguration().getName()) ;
281 for(NodeType nodeType : actionList) {
282 if(nodeType.isNodeType("exo:scriptAction")) {
283 PropertyDefinition[] arrProperties = nodeType.getPropertyDefinitions();
284 for(PropertyDefinition property : arrProperties) {
285 if(property.getName().equals("exo:script") && property.getDefaultValues()[0].getString().equals(resourceName)) {
286 return nodeType;
287 }
288 }
289 }
290 }
291 return null;
292 }
293
294
295
296
297
298
299
300 public String getResourceDescription(String resourceName) throws Exception {
301 Node resource = getResourceByName(WCMCoreUtils.getSystemSessionProvider(), resourceName);
302 return resource.getNode(NodetypeConstant.JCR_CONTENT).getProperty(NodetypeConstant.DC_DESCRIPTION).getValues()[0].getString();
303 }
304
305
306
307
308
309
310
311 public InputStream getResourceAsStream(String resourceName) throws Exception {
312 SessionProvider systemProvider = SessionProvider.createSystemProvider();
313 try {
314 Node resourceNode = getResourceByName(systemProvider, resourceName);
315 InputStream stream = resourceNode.getNode("jcr:content").getProperty("jcr:data").getStream();
316 return stream;
317 } finally {
318 systemProvider.close();
319 }
320 }
321
322
323
324
325
326
327
328
329 public NodeIterator getResources(SessionProvider sessionProvider) throws Exception {
330 Node resourcesHome = getResourcesHome(sessionProvider);
331 return resourcesHome.getNodes();
332 }
333
334
335
336
337
338
339
340
341 public boolean hasResources(SessionProvider sessionProvider) throws Exception {
342 Node resourcesHome = getResourcesHome(sessionProvider);
343 return resourcesHome.hasNodes();
344 }
345
346
347
348
349
350
351
352
353
354
355 public void addResource(String name, String text,SessionProvider provider) throws Exception {
356 Node resourcesHome = getResourcesHome(provider);
357 InputStream in = new ByteArrayInputStream(text.getBytes());
358 addResource(resourcesHome, name, in);
359 resourcesHome.save();
360 }
361
362
363
364
365
366
367
368
369
370 public void removeResource(String resourceName,SessionProvider provider) throws Exception {
371 removeFromCache(resourceName);
372 Node resourcesHome = getResourcesHome(provider);
373 Node resource2remove = resourcesHome.getNode(resourceName);
374 resource2remove.remove();
375 resourcesHome.save();
376 }
377 }