KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > caching > impl > DefaultEventRegistryImpl


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.caching.impl;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.cocoon.Constants;
30 import org.apache.cocoon.caching.EventRegistry;
31
32 /**
33  * This implementation of <code>EventRegistry</code> handles
34  * persistence by serializing an <code>EventRegistryDataWrapper</code> to
35  * disk.
36  *
37  * @since 2.1
38  * @author <a HREF="mailto:ghoward@apache.org">Geoff Howard</a>
39  * @version $Id: DefaultEventRegistryImpl.java 292275 2005-09-28 19:26:13Z vgritsenko $
40  */

41 public class DefaultEventRegistryImpl extends AbstractDoubleMapEventRegistry
42                                       implements EventRegistry, Contextualizable {
43
44     private static final String JavaDoc PERSISTENT_FILE = "/WEB-INF/ev_cache.ser";
45
46     private File JavaDoc m_persistentFile;
47
48     /**
49      * Set up the persistence file.
50      */

51     public void contextualize(Context context) throws ContextException {
52         org.apache.cocoon.environment.Context ctx =
53                 (org.apache.cocoon.environment.Context) context.get(
54                                     Constants.CONTEXT_ENVIRONMENT_CONTEXT);
55         // set up file
56
String JavaDoc path = ctx.getRealPath(PERSISTENT_FILE);
57         if (path == null) {
58             throw new ContextException("The cache event registry cannot be used inside an unexpanded WAR file. " +
59                                        "Real path for <" + PERSISTENT_FILE + "> is null.");
60         }
61
62         m_persistentFile = new File JavaDoc(path);
63     }
64
65     /**
66      * Persist by simple object serialization. If the serialization fails, an
67      * error is logged but not thrown because missing/invalid state is handled
68      * at startup.
69      */

70     protected void persist(EventRegistryDataWrapper registryWrapper) {
71         ObjectOutputStream JavaDoc oos = null;
72         try {
73             oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(this.m_persistentFile));
74             oos.writeObject(registryWrapper);
75             oos.flush();
76         } catch (FileNotFoundException JavaDoc e) {
77             getLogger().error("Unable to persist EventRegistry", e);
78         } catch (IOException JavaDoc e) {
79             getLogger().error("Unable to persist EventRegistry", e);
80         } finally {
81             try {
82                 if (oos != null) oos.close();
83             } catch (IOException JavaDoc e) { /* ignored */ }
84         }
85     }
86
87     /*
88      * I don't think this needs to get synchronized because it should
89      * only be called during initialize, which should only be called
90      * once by the container.
91      */

92     protected boolean recover() {
93         if (this.m_persistentFile.exists()) {
94             ObjectInputStream JavaDoc ois = null;
95             EventRegistryDataWrapper ecdw = null;
96             try {
97                 ois = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(this.m_persistentFile));
98                 ecdw = (EventRegistryDataWrapper) ois.readObject();
99             } catch (FileNotFoundException JavaDoc e) {
100                 getLogger().error("Unable to retrieve EventRegistry", e);
101                 createBlankCache();
102                 return false;
103             } catch (IOException JavaDoc e) {
104                 getLogger().error("Unable to retrieve EventRegistry", e);
105                 createBlankCache();
106                 return false;
107             } catch (ClassNotFoundException JavaDoc e) {
108                 getLogger().error("Unable to retrieve EventRegistry", e);
109                 createBlankCache();
110                 return false;
111             } finally {
112                 try {
113                     if (ois != null) ois.close();
114                 } catch (IOException JavaDoc e) {
115                     // ignore
116
}
117             }
118             unwrapRegistry(ecdw);
119         } else {
120             getLogger().warn(this.m_persistentFile + " does not exist - Unable to " +
121                              "retrieve EventRegistry.");
122             createBlankCache();
123             return false;
124         }
125         return true;
126     }
127
128 }
129
Popular Tags