Package org.nextime.ion.frontoffice.smartCache

Source Code of org.nextime.ion.frontoffice.smartCache.SmartCacheManager

/*
* �ON content management system.
* Copyright (C) 2002  Guillaume Bort(gbort@msn.com). All rights reserved.
*
* Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
* Copyright 2000-2002 (C) Intalio Inc. All Rights Reserved.
*
* �ON is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* �ON core framework, �ON content server, �ON backoffice, �ON frontoffice
* and �ON admin application are parts of �ON and are distributed under
* same terms of licence.
*
*
* �ON includes software developed by the Apache Software Foundation (http://www.apache.org/)
* and software developed by the Exolab Project (http://www.exolab.org).
*
* �ON is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

package org.nextime.ion.frontoffice.smartCache;

import java.io.File;
import java.io.FileInputStream;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.nextime.ion.framework.business.Publication;
import org.nextime.ion.framework.business.Section;
import org.nextime.ion.framework.event.WcmEvent;
import org.nextime.ion.framework.event.WcmListener;

public class SmartCacheManager extends HttpServlet implements WcmListener {
 
  private static String cachePath;
  private static String frontUrl;
  private static boolean cacheValid;
 
  public void init() throws ServletException {
    setCacheValid(true);
    cachePath = getServletContext().getRealPath(getInitParameter("relativePath"));
    frontUrl = getInitParameter("frontUrl");
    cleanCache();
    // events
    Publication.addListener(this);
    Section.addListener(this);
  }
 
  public static boolean isSectionCached( String sectionId ) {
    return new File(cachePath,"section$"+sectionId+".html").exists();
  }
 
  public static boolean isPublicationCached( String publicationId ) {
    return new File(cachePath,"publication$"+publicationId+".html").exists();
  }
 
  public static byte[] getCachedSection( String sectionId ) {
    try {
      File content = new File(cachePath,"section$"+sectionId+".html");
      byte[] buffer = new byte[(int)content.length()];
      FileInputStream fis = new FileInputStream(content);
      fis.read(buffer);
      fis.close();
      return buffer;
    } catch( Exception e ) {
      return new byte[0];
    }
  }
 
  public static byte[] getCachedPublication( String publicationId ) {
    try {
      File content = new File(cachePath,"publication$"+publicationId+".html");
      byte[] buffer = new byte[(int)content.length()];
      FileInputStream fis = new FileInputStream(content);
      fis.read(buffer);
      fis.close();
      return buffer;
    } catch( Exception e ) {
      return new byte[0];
    }
  }
 
  public static void cacheSection( String sectionId ) {
    new SmartCacheAgent(cachePath,"section",sectionId,frontUrl+"/section/"+sectionId);
  }
 
  public static void cachePublication( String publicationId ) {
    new SmartCacheAgent(cachePath,"publication",publicationId,frontUrl+"/publication/"+publicationId);
  }


  /**
   * @see org.nextime.ion.framework.event.WcmListener#objectCreated(WcmEvent)
   */
  public void objectCreated(WcmEvent event) {
    Object o = event.getSource();
    if( o instanceof Publication ) {
      Publication p = (Publication)o;
      Vector v = p.listSections();
      for( int i=0; i<v.size(); i++ ) {
        Section s = (Section)v.get(i);
        new File(cachePath,"section$"+s.getId()+".html").delete();
      }
      new File(cachePath,"publication$"+p.getId()+".html").delete();
    }
    if( o instanceof Section ) {
      cleanCache();
    }
  }

  /**
   * @see org.nextime.ion.framework.event.WcmListener#objectDeleted(WcmEvent)
   */
  public void objectDeleted(WcmEvent event) {
    Object o = event.getSource();
    if( o instanceof Publication ) {
      Publication p = (Publication)o;
      Vector v = p.listSections();
      for( int i=0; i<v.size(); i++ ) {
        Section s = (Section)v.get(i);
        new File(cachePath,"section$"+s.getId()+".html").delete();
      }
      new File(cachePath,"publication$"+p.getId()+".html").delete();
    }
    if( o instanceof Section ) {
      cleanCache();
    }
  }

  /**
   * @see org.nextime.ion.framework.event.WcmListener#objectModified(WcmEvent)
   */
  public void objectModified(WcmEvent event) {
    Object o = event.getSource();
    if( o instanceof Publication ) {
      Publication p = (Publication)o;
      Vector v = p.listSections();
      for( int i=0; i<v.size(); i++ ) {
        Section s = (Section)v.get(i);
        new File(cachePath,"section$"+s.getId()+".html").delete();
      }
      new File(cachePath,"publication$"+p.getId()+".html").delete();
    }
    if( o instanceof Section ) {
      cleanCache();
    }
  }

  /**
   * Returns the cacheValid.
   * @return boolean
   */
  public static boolean isCacheValid() {
    return cacheValid;
  }

  /**
   * Sets the cacheValid.
   * @param cacheValid The cacheValid to set
   */
  public static void setCacheValid(boolean cacheValid) {
    SmartCacheManager.cacheValid = cacheValid;
  }
 
  public static void cleanCache() {
    File[] files = new File(cachePath).listFiles();
    for( int i=0; i<files.length; i++ ) {
      files[i].delete();
    }
  }

}

TOP

Related Classes of org.nextime.ion.frontoffice.smartCache.SmartCacheManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.