001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.caching.impl;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026:
027: import org.apache.avalon.framework.context.Context;
028: import org.apache.avalon.framework.context.ContextException;
029: import org.apache.avalon.framework.context.Contextualizable;
030: import org.apache.cocoon.Constants;
031: import org.apache.cocoon.caching.EventRegistry;
032:
033: /**
034: * This implementation of <code>EventRegistry</code> handles
035: * persistence by serializing an <code>EventRegistryDataWrapper</code> to
036: * disk.
037: *
038: * @since 2.1
039: * @author <a href="mailto:ghoward@apache.org">Geoff Howard</a>
040: * @version $Id: DefaultEventRegistryImpl.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042: public class DefaultEventRegistryImpl extends
043: AbstractDoubleMapEventRegistry implements EventRegistry,
044: Contextualizable {
045:
046: private static final String PERSISTENT_FILE = "/WEB-INF/ev_cache.ser";
047:
048: private File m_persistentFile;
049:
050: /**
051: * Set up the persistence file.
052: */
053: public void contextualize(Context context) throws ContextException {
054: org.apache.cocoon.environment.Context ctx = (org.apache.cocoon.environment.Context) context
055: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
056: // set up file
057: String path = ctx.getRealPath(PERSISTENT_FILE);
058: if (path == null) {
059: throw new ContextException(
060: "The cache event registry cannot be used inside an unexpanded WAR file. "
061: + "Real path for <" + PERSISTENT_FILE
062: + "> is null.");
063: }
064:
065: m_persistentFile = new File(path);
066: }
067:
068: /**
069: * Persist by simple object serialization. If the serialization fails, an
070: * error is logged but not thrown because missing/invalid state is handled
071: * at startup.
072: */
073: protected void persist(EventRegistryDataWrapper registryWrapper) {
074: ObjectOutputStream oos = null;
075: try {
076: oos = new ObjectOutputStream(new FileOutputStream(
077: this .m_persistentFile));
078: oos.writeObject(registryWrapper);
079: oos.flush();
080: } catch (FileNotFoundException e) {
081: getLogger().error("Unable to persist EventRegistry", e);
082: } catch (IOException e) {
083: getLogger().error("Unable to persist EventRegistry", e);
084: } finally {
085: try {
086: if (oos != null)
087: oos.close();
088: } catch (IOException e) { /* ignored */
089: }
090: }
091: }
092:
093: /*
094: * I don't think this needs to get synchronized because it should
095: * only be called during initialize, which should only be called
096: * once by the container.
097: */
098: protected boolean recover() {
099: if (this .m_persistentFile.exists()) {
100: ObjectInputStream ois = null;
101: EventRegistryDataWrapper ecdw = null;
102: try {
103: ois = new ObjectInputStream(new FileInputStream(
104: this .m_persistentFile));
105: ecdw = (EventRegistryDataWrapper) ois.readObject();
106: } catch (FileNotFoundException e) {
107: getLogger()
108: .error("Unable to retrieve EventRegistry", e);
109: createBlankCache();
110: return false;
111: } catch (IOException e) {
112: getLogger()
113: .error("Unable to retrieve EventRegistry", e);
114: createBlankCache();
115: return false;
116: } catch (ClassNotFoundException e) {
117: getLogger()
118: .error("Unable to retrieve EventRegistry", e);
119: createBlankCache();
120: return false;
121: } finally {
122: try {
123: if (ois != null)
124: ois.close();
125: } catch (IOException e) {
126: // ignore
127: }
128: }
129: unwrapRegistry(ecdw);
130: } else {
131: getLogger().warn(
132: this .m_persistentFile
133: + " does not exist - Unable to "
134: + "retrieve EventRegistry.");
135: createBlankCache();
136: return false;
137: }
138: return true;
139: }
140:
141: }
|