01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.cms.janitor;
19:
20: import java.io.File;
21:
22: import org.apache.cocoon.environment.Request;
23: import org.apache.lenya.cms.cocoon.components.context.ContextUtility;
24: import org.apache.lenya.cms.cocoon.source.SourceUtil;
25: import org.apache.lenya.cms.observation.AbstractRepositoryListener;
26: import org.apache.lenya.cms.observation.DocumentEvent;
27: import org.apache.lenya.cms.observation.RepositoryEvent;
28: import org.apache.lenya.cms.publication.DocumentFactory;
29: import org.apache.lenya.cms.publication.DocumentUtil;
30: import org.apache.lenya.cms.publication.Publication;
31:
32: /**
33: * The content janitor cleans up empty directories after a document is removed.
34: */
35: public class ContentJanitor extends AbstractRepositoryListener {
36:
37: public void eventFired(RepositoryEvent repoEvent) {
38:
39: if (!(repoEvent instanceof DocumentEvent)) {
40: return;
41: }
42: DocumentEvent event = (DocumentEvent) repoEvent;
43:
44: if (!event.getDescriptor().equals(DocumentEvent.REMOVED)) {
45: return;
46: }
47:
48: ContextUtility util = null;
49: try {
50: util = (ContextUtility) this .manager
51: .lookup(ContextUtility.ROLE);
52: Request request = util.getRequest();
53: DocumentFactory factory = DocumentUtil.getDocumentFactory(
54: this .manager, request);
55: Publication pub = factory.getPublication(event
56: .getPublicationId());
57: File contentFile = pub.getContentDirectory(event.getArea());
58: String contentUri = contentFile.toURI().toString();
59: SourceUtil.deleteEmptyCollections(contentUri, this .manager);
60: } catch (Exception e) {
61: throw new RuntimeException(e);
62: } finally {
63: if (util != null) {
64: this.manager.release(util);
65: }
66: }
67: }
68:
69: }
|