Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.ejb3.cache.simple;
 
 import java.io.File;
 
 import java.util.List;
 
 import  org.jboss.serial.io.JBossObjectInputStream;
 import  org.jboss.serial.io.JBossObjectOutputStream;
A file-based stateful session bean persistence manager.

Reads and writes session bean objects to files by using the standard Java serialization mechanism.

Passivated state files are stored under: jboss-server-data-dir/storeDirectoryName/ejb-name-unique-id.

Since ejb-name is not unique across deployments we generate a unique-id to make sure that beans with the same EJB name do not collide.

Author(s):
Rickard ?berg
Marc Fleury
Sebastien Alborini
Jason Dillon
Bill Burke
Version:
$Revision: 80079 $
Jmx:
mbean extends="org.jboss.system.ServiceMBean"
 
 {

   
The sub-directory name under the server data directory where session data is stored.

See also:
DEFAULT_STORE_DIRECTORY_NAME
setStoreDirectoryName
 
The base directory where sessions state files are stored for our container.
 
    private File storeDir;
 
    private Container con;

   
Enable purging leftover state files at create and destroy time (default is true).
 
    private boolean purgeEnabled = true;

   
Set the sub-directory name under the server data directory where session data will be stored.

This value will be appened to the value of jboss-server-data-dir.

This value is only used during creation and will not dynamically change the store directory when set after the create step has finished.

Parameters:
dirName A sub-directory name.
   public void setStoreDirectoryName(final String dirName)
   {
      this. = dirName;
   }

   
Get the sub-directory name under the server data directory where session data is stored.

Returns:
A sub-directory name.
See also:
setStoreDirectoryName
   {
      return ;
   }

   
Set the stale session state purge enabled flag.

Parameters:
flag The toggle flag to enable or disable purging.
   public void setPurgeEnabled(final boolean flag)
   {
      this. = flag;
   }

   
Get the stale session state purge enabled flag.

Returns:
True if purge is enabled.
   public boolean getPurgeEnabled()
   {
      return ;
   }

   
Returns the directory used to store session passivation state files.

Returns:
The directory used to store session passivation state files.
   public File getStoreDirectory()
   {
      return ;
   }
   public void setContainer(Container con)
   {
      this. = con;
   }

   
Setup the session data storage directory.

Purges any existing session data found.

   public void initialize(Container conthrows Exception
   {
      this. = con;
      boolean debug = .isDebugEnabled();
      // Initialize the dataStore
      String ejbName = con.getEjbName();
      // Get the system data directory
      String sysPropJBossTempDir = "jboss.server.temp.dir";
      String sysPropJavaTempDir = "java.io.tmpdir";
      String tempDir = System.getProperty(sysPropJBossTempDir);
      if(tempDir==null||tempDir.trim().length()==0)
      {
         tempDir = System.getProperty(sysPropJavaTempDir);
      }
      File dir = new File(tempDir);
      // Setup the reference to the session data store directory
      dir = new File(dir);
      // ejbName is not unique across all deployments, so use a unique token
      dir = new File(direjbName + "-" + new UID().toString());
       = dir;
      if (debug)
      {
         .debug("Storing sessions for '" + ejbName + "' in: " + );
      }
      // if the directory does not exist then try to create it
      if (!.exists())
      {
         if (MkdirsFileAction.mkdirs() == false)
         {
            throw new IOException("Failed to create directory: " + );
         }
      }
      // make sure we have a directory
      if (!.isDirectory())
      {
         throw new IOException("File exists where directory expected: " + );
      }
      // make sure we can read and write to it
      if (!.canWrite() || !.canRead())
      {
         throw new IOException("Directory must be readable and writable: " + );
      }
      // Purge state session state files, should be none, due to unique directory
      purgeAllSessionData();
   }

   
Removes any state files left in the storgage directory.
   public void purgeAllSessionData()
   {
      if (!return;
      .debug("Purging all session data in: " + );
      File[] sessions = .listFiles();
      for (int i = 0; i < sessions.lengthi++)
      {
         if (!sessions[i].delete())
         {
            .warn("Failed to delete session state file: " + sessions[i]);
         }
         else
         {
            .debug("Removed stale session state: " + sessions[i]);
         }
      }
   }

   
Purge any data in the store, and then the store directory too.
   public void destroy() throws Exception
   {
      // Purge data and attempt to delete directory
      purgeAllSessionData();
      // Nuke the directory too if purge is enabled
      if ( && !.delete())
      {
         .warn("Failed to delete session state storage directory: " + );
      }
   }

   
Make a session state file for the given instance id.
   private File getFile(final Object id)
   {
      //
      // jason: may have to translate id into a os-safe string, though
      //        the format of UID is safe on Unix and win32 already...
      //
      return new File(, String.valueOf(id) + ".ser");
   }

   
Restores session state from the serialized file & invokes javax.ejb.SessionBean.ejbActivate on the target bean.
   {
      boolean debug = .isDebugEnabled();
      if (debug)
      {
         .debug("Attempting to activate; id=" + id);
      }
      // Load state
      File file = getFile(id);
      if (!file.exists()) return null;
      
      if (debug)
      {
         .debug("Reading session state from: " + file);
      }
      StatefulBeanContext bean = null;
      try
      {
         FileInputStream fis = FISAction.open(file);
         // todo need to rewrite SessionObjectInputStream to support EJB3 classes
         ObjectInputStream in;
         in = new JBossObjectInputStream(new BufferedInputStream(fis));
         try
         {
            bean = (StatefulBeanContextin.readObject();
         }
         finally
         {
            fis.close();
            in.close();
         }
      }
      catch (EJBException e)
      {
         throw e;
      }
      catch (Exception e)
      {
         throw new EJBException("Could not activate; failed to " +
                                "restore state"e);
      }
      removePassivated(id);
      bean.postActivate();
      return bean;
   }
   
   {
      List beans = new LinkedList();
      
      File[] files = .listFiles();
      for (File file : files)
      {
         try
         {
            ObjectInputStream in;
            
            FileInputStream fis = FISAction.open(file);
   
            in = new JBossObjectInputStream(new BufferedInputStream(fis));
            try
            {
               StatefulBeanContext bean = (StatefulBeanContextin.readObject();
               beans.add(bean);
            }
            finally
            {
               fis.close();
               in.close();
            }
         }
         catch (Exception e)
         {
            .warn("Could not read for timeout removal for file " + file.getName(), e);
         }
      }
      
      return beans;
   }

   
Invokes javax.ejb.SessionBean.ejbPassivate on the target bean and saves the state of the session to a file.
   public void passivateSession(StatefulBeanContext ctx)
   {
      boolean debug = .isDebugEnabled();
      if (debug)
      {
         .debug("Attempting to passivate; id=" + ctx.getId());
      }
      ctx.prePassivate();
      // Store state
      File file = getFile(ctx.getId());
      if (debug)
      {
         .debug("Saving session state to: " + file);
      }
      try
      {
         FileOutputStream fos = FOSAction.open(file);
         // todo need to rewrite SessionObjectOutputStream to support EJB3 classes
         ObjectOutputStream out;
         out = new JBossObjectOutputStream(fosfalse);
         try
         {
            out.writeObject(ctx);
            out.flush();
            fos.flush();
            fos.close();
         }
         finally
         {
            out.close();
         }
      }
      catch (EJBException e)
      {
         throw e;
      }
      catch (Exception e)
      {
         throw new EJBException("Could not passivate; failed to save state"e);
      }
      if (debug)
      {
         .debug("Passivation complete; id=" + ctx.getId());
      }
   }

   
Removes the saved state file (if any) for the given session id.
   public void removePassivated(Object id)
   {
      boolean debug = .isDebugEnabled();
      File file = getFile(id);
      // only attempt to delete if the file exists
      if (file.exists())
      {
         if (debug)
         {
            .debug("Removing passivated state file: " + file);
         }
         if (DeleteFileAction.delete(file) == false)
         {
            .warn("Failed to delete passivated state file: " + file);
         }
      }
   }
   static class DeleteFileAction implements PrivilegedAction
   {
      File file;
      DeleteFileAction(File file)
      {
         this. = file;
      }
      public Object run()
      {
         boolean deleted = .delete();
         return new Boolean(deleted);
      }
      static boolean delete(File file)
      {
         DeleteFileAction action = new DeleteFileAction(file);
         Boolean deleted = (Boolean) AccessController.doPrivileged(action);
         return deleted.booleanValue();
      }
   }
   static class MkdirsFileAction implements PrivilegedAction
   {
      File file;
      MkdirsFileAction(File file)
      {
         this. = file;
      }
      public Object run()
      {
         boolean ok = .mkdirs();
         return new Boolean(ok);
      }
      static boolean mkdirs(File file)
      {
         MkdirsFileAction action = new MkdirsFileAction(file);
         Boolean ok = (Boolean) AccessController.doPrivileged(action);
         return ok.booleanValue();
      }
   }
   static class FISAction implements PrivilegedExceptionAction
   {
      File file;
      FISAction(File file)
      {
         this. = file;
      }
      public Object run() throws Exception
      {
         FileInputStream fis = new FileInputStream();
         return fis;
      }
      static FileInputStream open(File filethrows FileNotFoundException
      {
         FISAction action = new FISAction(file);
         FileInputStream fis = null;
         try
         {
            fis = (FileInputStream) AccessController.doPrivileged(action);
         }
         catch (PrivilegedActionException e)
         {
            throw (FileNotFoundExceptione.getException();
         }
         return fis;
      }
   }
   static class FOSAction implements PrivilegedExceptionAction
   {
      File file;
      FOSAction(File file)
      {
         this. = file;
      }
      public Object run() throws Exception
      {
         FileOutputStream fis = new FileOutputStream();
         return fis;
      }
      static FileOutputStream open(File filethrows FileNotFoundException
      {
         FOSAction action = new FOSAction(file);
         FileOutputStream fos = null;
         try
         {
            fos = (FileOutputStream) AccessController.doPrivileged(action);
         }
         catch (PrivilegedActionException e)
         {
            throw (FileNotFoundExceptione.getException();
         }
         return fos;
      }
   }
New to GrepCode? Check out our FAQ X