KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > ContentStoreCleanupJob


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content;
18
19 import java.util.List JavaDoc;
20
21 import org.alfresco.error.AlfrescoRuntimeException;
22 import org.alfresco.service.cmr.repository.ContentReader;
23 import org.alfresco.service.cmr.search.SearchService;
24 import org.quartz.Job;
25 import org.quartz.JobDataMap;
26 import org.quartz.JobExecutionContext;
27 import org.quartz.JobExecutionException;
28
29 /**
30  * Removes all content form the store that is not referenced by any content node.
31  * <p>
32  * The following parameters are required:
33  * <ul>
34  * <li><b>contentStore</b>: The content store bean to clean up</li>
35  * <li><b>searcher</b>: The index searcher that searches for content in the store</li>
36  * <li><b>protectHours</b>: The number of hours to protect content that isn't referenced</li>
37  * </ul>
38  *
39  * @author Derek Hulley
40  */

41 public class ContentStoreCleanupJob implements Job
42 {
43     /**
44      * Gets all content URLs from the store, checks if it is in use by any node
45      * and deletes those that aren't.
46      */

47     public void execute(JobExecutionContext context) throws JobExecutionException
48     {
49         JobDataMap jobData = context.getJobDetail().getJobDataMap();
50         // extract the content store to use
51
Object JavaDoc contentStoreObj = jobData.get("contentStore");
52         if (contentStoreObj == null || !(contentStoreObj instanceof ContentStore))
53         {
54             throw new AlfrescoRuntimeException(
55                     "ContentStoreCleanupJob data must contain valid 'contentStore' reference");
56         }
57         ContentStore contentStore = (ContentStore) contentStoreObj;
58         // extract the search to use
59
Object JavaDoc searcherObj = jobData.get("searcher");
60         if (searcherObj == null || !(searcherObj instanceof SearchService))
61         {
62             throw new AlfrescoRuntimeException(
63                     "ContentStoreCleanupJob data must contain valid 'searcher' reference");
64         }
65         SearchService searcher = (SearchService) searcherObj;
66         // get the number of hourse to protect content
67
Object JavaDoc protectHoursObj = jobData.get("protectHours");
68         if (protectHoursObj == null || !(protectHoursObj instanceof String JavaDoc))
69         {
70             throw new AlfrescoRuntimeException(
71                     "ContentStoreCleanupJob data must contain valid 'protectHours' value");
72         }
73         long protectHours = 24L;
74         try
75         {
76             protectHours = Long.parseLong((String JavaDoc) protectHoursObj);
77         }
78         catch (NumberFormatException JavaDoc e)
79         {
80             throw new AlfrescoRuntimeException(
81                     "ContentStoreCleanupJob data 'protectHours' value is not a valid integer");
82         }
83         
84         long protectMillis = protectHours * 3600L * 1000L; // 3600s in an hour; 1000ms in a second
85
long now = System.currentTimeMillis();
86         long lastModifiedSafeTimeMs = (now - protectMillis); // able to remove anything modified before this
87

88         // get all URLs in the store
89
List JavaDoc<String JavaDoc> contentUrls = contentStore.listUrls();
90         for (String JavaDoc contentUrl : contentUrls)
91         {
92             // TODO here we need to get hold of all the orphaned content in this store
93

94             // not found - it is not in the repo, but check that it is old enough to delete
95
ContentReader reader = contentStore.getReader(contentUrl);
96             if (reader == null || !reader.exists())
97             {
98                 // gone missing in the meantime
99
continue;
100             }
101             long lastModified = reader.getLastModified();
102             if (lastModified >= lastModifiedSafeTimeMs)
103             {
104                 // not old enough
105
continue;
106             }
107             
108             // it is not in the repo and is old enough
109
boolean result = contentStore.delete(contentUrl);
110             System.out.println(contentUrl + ": " + Boolean.toString(result));
111         }
112         
113         // TODO for now throw this exception to ensure that this job does not get run until
114
// the orphaned content can be correctly retrieved
115
throw new UnsupportedOperationException JavaDoc();
116     }
117 }
118
Popular Tags