User: user57  
  Date: 02/02/12 20:26:40

  Modified:    src/main/org/jboss/ejb/plugins
                        StatefulSessionFilePersistenceManager.java
  Log:
   o These are all kinda related, so I am commiting them together
   o This is the second half of the migration to using ObjectName OBJECT_NAME
   o Not using jboss.system.* properties anywhere (one place in testsuite
     which I am ignoring for now)
   o StateManager will now read its config from a url (configURL), and only
     attempt to write it back out if that is a file URL.  Need to fix this
     to not need to write back to a config file.
   o Still setting jboss.home & jboss.system.home, but use ServerConfigMBean
     to get the proper bits, will eventually abstract all file access out
   o Added a simple locator to find a mbean server.  This is trivial code,
     but helps clean up client code and makes it obvious what it does.
  
  Revision  Changes    Path
  1.30      +303 -317  
jboss/src/main/org/jboss/ejb/plugins/StatefulSessionFilePersistenceManager.java
  
  Index: StatefulSessionFilePersistenceManager.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/StatefulSessionFilePersistenceManager.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- StatefulSessionFilePersistenceManager.java        5 Jan 2002 12:08:50 -0000      
 1.29
  +++ StatefulSessionFilePersistenceManager.java        13 Feb 2002 04:26:40 -0000     
 1.30
  @@ -1,9 +1,10 @@
   /*
  -* JBoss, the OpenSource J2EE webOS
  -*
  -* Distributable under LGPL license.
  -* See terms of license at gnu.org.
  -*/
  + * JBoss, the OpenSource J2EE webOS
  + *
  + * Distributable under LGPL license.
  + * See terms of license at gnu.org.
  + */
  +
   package org.jboss.ejb.plugins;
   
   import java.io.File;
  @@ -33,342 +34,327 @@
   import org.jboss.ejb.StatefulSessionPersistenceManager;
   import org.jboss.ejb.StatefulSessionEnterpriseContext;
   import org.jboss.logging.Logger;
  -
  +import org.jboss.system.ServerConfigMBean;
  +import org.jboss.util.MBeanServerLocator;
   
   /**
  -*  StatefulSessionFilePersistenceManager
  -*
  -*  This class is one of the passivating plugins for JBoss.
  -*  It is fairly simple and can work from the file system from wich JBoss is 
operating
  -*
  -*  @see <related>
  -*  @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>
  -*  @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
  -*  @author <a href="mailto:[EMAIL PROTECTED]";>Sebastien Alborini</a>
  -*  @version $Revision: 1.29 $
  -*/
  + *  This class is one of the passivating plugins for JBoss.
  + *  It is fairly simple and can work from the file system from wich JBoss is 
operating
  + *
  + *  @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>
  + *  @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
  + *  @author <a href="mailto:[EMAIL PROTECTED]";>Sebastien Alborini</a>
  + *  @version $Revision: 1.30 $
  + */
   public class StatefulSessionFilePersistenceManager
      implements StatefulSessionPersistenceManager
   {
  -    // Constants -----------------------------------------------------
  -    
  -    // Attributes ----------------------------------------------------
      private static Logger log = 
Logger.getLogger(StatefulSessionFilePersistenceManager.class);
  -    private StatefulSessionContainer con;
  -    
  -    private Method ejbActivate;
  -    private Method ejbPassivate;
  -    private Method ejbRemove;
  -    
  -    private File dir;
  -    
  -    private ArrayList fields;
  -    
  -    // Static --------------------------------------------------------
  -    private static long id = System.currentTimeMillis();
  -    
  -    // Constructors --------------------------------------------------
  -    
  -    // Public --------------------------------------------------------
  -    public void setContainer(Container c)
  -    {
  -        con = (StatefulSessionContainer)c;
  -    }
  +   
  +   private StatefulSessionContainer con;
       
  -    public void create()
  -    throws Exception
  -    {
  -        boolean debug = log.isDebugEnabled();
  -
  -        // Find methods
  -        ejbActivate = SessionBean.class.getMethod("ejbActivate", new Class[0]);
  -
  -        ejbPassivate = SessionBean.class.getMethod("ejbPassivate", new Class[0]);
  -
  -        ejbRemove = SessionBean.class.getMethod("ejbRemove", new Class[0]);
  -
  -        // Initialize the dataStore
  -        String ejbName = con.getBeanMetaData().getEjbName();
  -
  -        // Base dir
  -        log.warn("using jboss.system.home property");
  -        File homeDir = new File(System.getProperty("jboss.system.home"));
  -        File databaseDir = new File(homeDir, "db"+File.separator);
  -        File database = new File(databaseDir, "sessions");
  -
  -        dir = new File(database, ejbName);
  -
  -        dir.mkdirs();
  -
  -        if (debug)
  -           log.debug("Storing sessions for "+ejbName+" in:"+dir);
  -
  -        // Clear dir of old files
  -        File[] sessions = dir.listFiles();
  -        for (int i = 0; i < sessions.length; i++)
  -        {
  -            sessions[i].delete();
  -        }
  -        if (debug)
  -           log.debug(sessions.length + " old sessions removed");
  -
  -        // Get fields of class
  -        Class beanClass = con.getBeanClass();
  -        fields = new ArrayList();
  -        while (!beanClass.equals(Object.class))
  -        {
  -            Field[] f = beanClass.getDeclaredFields();
  -            
  -            // Skip transient fields
  -            for (int i = 0; i < f.length; i++)
  -                if (!Modifier.isTransient(f[i].getModifiers()))
  -            {
  -                fields.add(f[i]);
  -            }
  -            
  -            beanClass = beanClass.getSuperclass();
  -        }
  -    }
  -    
  -    public void start()
  -    throws Exception
  -    {
  -    }
  -    
  -    public void stop()
  -    {
  -    }
  -    
  -    public void destroy()
  -    {
  -    }
  -    
  -    public void createSession(Method m, Object[] args, 
StatefulSessionEnterpriseContext ctx)
  -    throws Exception
  -    {
  +   private Method ejbActivate;
  +   private Method ejbPassivate;
  +   private Method ejbRemove;
  +
  +   private File dataDir;
  +    
  +   private ArrayList fields;
  +    
  +   private static long id = System.currentTimeMillis();
  +    
  +   public void setContainer(Container c)
  +   {
  +      con = (StatefulSessionContainer)c;
  +   }
  +    
  +   public void create()
  +      throws Exception
  +   {
  +      boolean debug = log.isDebugEnabled();
  +
  +      // Find methods
  +      ejbActivate = SessionBean.class.getMethod("ejbActivate", new Class[0]);
  +
  +      ejbPassivate = SessionBean.class.getMethod("ejbPassivate", new Class[0]);
  +
  +      ejbRemove = SessionBean.class.getMethod("ejbRemove", new Class[0]);
  +
  +      // Initialize the dataStore
  +      String ejbName = con.getBeanMetaData().getEjbName();
  +
  +      // Get the system data directory
  +      File systemDataDir = (File)
  +         MBeanServerLocator.locate().getAttribute(ServerConfigMBean.OBJECT_NAME, 
"DataDir");
  +
  +      // Gte a handle on the sysdata/sessions/ejbname directory
  +      dataDir = new File(new File(systemDataDir, "sessions"), ejbName);
  +      if (debug) {
  +         log.debug("Storing sessions for " + ejbName + " in: " + dataDir);
  +      }
  +
  +      dataDir.mkdirs();
  +
  +      // Clear dir of old files
  +      File[] sessions = dataDir.listFiles();
  +      for (int i = 0; i < sessions.length; i++)
  +      {
  +         if (! sessions[i].delete()) {
  +            log.warn("Failed to delete session state file: " + sessions[i]);
  +         }
  +      }
  +      
  +      if (debug) {
  +         log.debug(sessions.length + " old sessions removed");
  +      }
  +
  +      // Get fields of class
  +      Class beanClass = con.getBeanClass();
  +      fields = new ArrayList();
  +      
  +      while (!beanClass.equals(Object.class))
  +      {
  +         Field[] f = beanClass.getDeclaredFields();
  +            
  +         // Skip transient fields
  +         for (int i = 0; i < f.length; i++)
  +            if (!Modifier.isTransient(f[i].getModifiers()))
  +            {
  +               fields.add(f[i]);
  +            }
  +            
  +         beanClass = beanClass.getSuperclass();
  +      }
  +   }
  +    
  +   public void start()
  +      throws Exception
  +   {
  +   }
  +    
  +   public void stop()
  +   {
  +   }
  +    
  +   public void destroy()
  +   {
  +   }
  +    
  +   public void createSession(Method m, Object[] args, 
StatefulSessionEnterpriseContext ctx)
  +      throws Exception
  +   {
           
  -        // Set id
  -        ctx.setId(nextId());
  +      // Set id
  +      ctx.setId(nextId());
           
  -        // Get methods
  -        try
  -        {
  -            Method createMethod = con.getBeanClass().getMethod("ejbCreate", 
m.getParameterTypes());
  +      // Get methods
  +      try
  +      {
  +         Method createMethod = con.getBeanClass().getMethod("ejbCreate", 
m.getParameterTypes());
               
  -            // Call ejbCreate
  -            createMethod.invoke(ctx.getInstance(), args);
  +         // Call ejbCreate
  +         createMethod.invoke(ctx.getInstance(), args);
           
  -        } catch (IllegalAccessException e)
  -        {
  -            // Clear id
  -            ctx.setId(null);
  -            
  -            // Throw this as a bean exception...(?)
  -            throw new EJBException(e);
  -        } catch (InvocationTargetException ite)
  -        {
  -            // Clear id
  -            ctx.setId(null);
  -            
  -            Throwable e = ite.getTargetException();
  -            if (e instanceof EJBException)
  -            {
  -                // Rethrow exception
  -                throw (EJBException)e;
  -            }
  -            else if (e instanceof RuntimeException)
  -            {
  -                // Wrap runtime exceptions
  -                throw new EJBException((Exception)e);
  -            }
  -            else if (e instanceof Exception)
  -            {
  -                // Remote, Create, or custom app. exception
  -                throw (Exception)e;
  -            }
  -            else
  -            {
  -                throw (Error)e;
  -            }
  -        }
  +      } catch (IllegalAccessException e)
  +      {
  +         // Clear id
  +         ctx.setId(null);
  +            
  +         // Throw this as a bean exception...(?)
  +         throw new EJBException(e);
  +      } catch (InvocationTargetException ite)
  +      {
  +         // Clear id
  +         ctx.setId(null);
  +            
  +         Throwable e = ite.getTargetException();
  +         if (e instanceof EJBException)
  +         {
  +            // Rethrow exception
  +            throw (EJBException)e;
  +         }
  +         else if (e instanceof RuntimeException)
  +         {
  +            // Wrap runtime exceptions
  +            throw new EJBException((Exception)e);
  +         }
  +         else if (e instanceof Exception)
  +         {
  +            // Remote, Create, or custom app. exception
  +            throw (Exception)e;
  +         }
  +         else
  +         {
  +            throw (Error)e;
  +         }
  +      }
           
  -        // Insert in cache
  -        ((StatefulSessionContainer)con).getInstanceCache().insert(ctx);
  +      // Insert in cache
  +      ((StatefulSessionContainer)con).getInstanceCache().insert(ctx);
           
  -        // Create EJBObject
  -        if (con.getContainerInvoker() != null)
  -            
ctx.setEJBObject((EJBObject)con.getContainerInvoker().getStatefulSessionEJBObject(ctx.getId()));
  -        // Create EJBLocalObject
  -        if (con.getLocalHomeClass() != null)
  -            
ctx.setEJBLocalObject(con.getLocalContainerInvoker().getStatefulSessionEJBLocalObject(ctx.getId()));
  -    }
  +      // Create EJBObject
  +      if (con.getContainerInvoker() != null)
  +         
ctx.setEJBObject((EJBObject)con.getContainerInvoker().getStatefulSessionEJBObject(ctx.getId()));
  +      // Create EJBLocalObject
  +      if (con.getLocalHomeClass() != null)
  +         
ctx.setEJBLocalObject(con.getLocalContainerInvoker().getStatefulSessionEJBLocalObject(ctx.getId()));
  +   }
       
  -    public void activateSession(StatefulSessionEnterpriseContext ctx)
  -    throws RemoteException
  -    {
  -        try
  -        {
  -            ObjectInputStream in;
  +   public void activateSession(StatefulSessionEnterpriseContext ctx)
  +      throws RemoteException
  +   {
  +      try
  +      {
  +         ObjectInputStream in;
               
               
  -            // Load state
  -            in = new SessionObjectInputStream(ctx, new 
FileInputStream(getFile(ctx.getId())));
  +         // Load state
  +         in = new SessionObjectInputStream(ctx, new 
FileInputStream(getFile(ctx.getId())));
               
  -            ctx.setInstance(in.readObject());
  +         ctx.setInstance(in.readObject());
               
  -            in.close();
  +         in.close();
               
  -            removePassivated(ctx.getId());
  +         removePassivated(ctx.getId());
               
  -            // Call bean
  -            ejbActivate.invoke(ctx.getInstance(), new Object[0]);
  +         // Call bean
  +         ejbActivate.invoke(ctx.getInstance(), new Object[0]);
           
  -        } catch (ClassNotFoundException e)
  -        {
  -            throw new ServerException("Could not activate", e);
  -        } catch (IOException e)
  -        {
  -            throw new ServerException("Could not activate", e);
  -        } catch (IllegalAccessException e)
  -        {
  -            // Throw this as a bean exception...(?)
  -            throw new EJBException(e);
  -        } catch (InvocationTargetException ite)
  -        {
  -            Throwable e = ite.getTargetException();
  -            if (e instanceof EJBException)
  -            {
  -                // Rethrow exception
  -                throw (EJBException)e;
  -            }
  -            else if (e instanceof RuntimeException)
  -            {
  -                // Wrap runtime exceptions
  -                throw new EJBException((Exception)e);
  -            }
  -            else if (e instanceof RemoteException)
  -            {
  -                // Remote, Create, or custom app. exception
  -                throw (RemoteException)e;
  -            }
  -            else
  -            {
  -                throw (Error)e;
  -            }
  -        }
  -    }
  -    
  -    public void passivateSession(StatefulSessionEnterpriseContext ctx)
  -    throws RemoteException
  -    {
  -        try
  -        {
  -            // Call bean
  -            ejbPassivate.invoke(ctx.getInstance(), new Object[0]);
  +      } catch (ClassNotFoundException e)
  +      {
  +         throw new ServerException("Could not activate", e);
  +      } catch (IOException e)
  +      {
  +         throw new ServerException("Could not activate", e);
  +      } catch (IllegalAccessException e)
  +      {
  +         // Throw this as a bean exception...(?)
  +         throw new EJBException(e);
  +      } catch (InvocationTargetException ite)
  +      {
  +         Throwable e = ite.getTargetException();
  +         if (e instanceof EJBException)
  +         {
  +            // Rethrow exception
  +            throw (EJBException)e;
  +         }
  +         else if (e instanceof RuntimeException)
  +         {
  +            // Wrap runtime exceptions
  +            throw new EJBException((Exception)e);
  +         }
  +         else if (e instanceof RemoteException)
  +         {
  +            // Remote, Create, or custom app. exception
  +            throw (RemoteException)e;
  +         }
  +         else
  +         {
  +            throw (Error)e;
  +         }
  +      }
  +   }
  +    
  +   public void passivateSession(StatefulSessionEnterpriseContext ctx)
  +      throws RemoteException
  +   {
  +      try
  +      {
  +         // Call bean
  +         ejbPassivate.invoke(ctx.getInstance(), new Object[0]);
               
  -            // Store state
  -            ObjectOutputStream out = new SessionObjectOutputStream(new 
FileOutputStream(getFile(ctx.getId())));
  +         // Store state
  +         ObjectOutputStream out = new SessionObjectOutputStream(new 
FileOutputStream(getFile(ctx.getId())));
               
  -            out.writeObject(ctx.getInstance());
  +         out.writeObject(ctx.getInstance());
               
  -            out.close();
  +         out.close();
           
  -        } catch (IOException e)
  -        {
  -            throw new ServerException("Could not passivate", e);
  -        }catch (IllegalAccessException e)
  -        {
  -            // Throw this as a bean exception...(?)
  -            throw new EJBException(e);
  -        } catch (InvocationTargetException ite)
  -        {
  -            Throwable e = ite.getTargetException();
  -            if (e instanceof EJBException)
  -            {
  -                // Rethrow exception
  -                throw (EJBException)e;
  -            }
  -            else if (e instanceof RuntimeException)
  -            {
  -                // Wrap runtime exceptions
  -                throw new EJBException((Exception)e);
  -            }
  -            else if (e instanceof RemoteException)
  -            {
  -                // Remote, Create, or custom app. exception
  -                throw (RemoteException)e;
  -            }
  -            else
  -            {
  -                throw (Error)e;
  -            }
  -        }
  -    }
  -    
  -    public void removeSession(StatefulSessionEnterpriseContext ctx)
  -    throws RemoteException, RemoveException
  -    {
  -        // Call bean
  -        try
  -        {
  -            ejbRemove.invoke(ctx.getInstance(), new Object[0]);
  +      } catch (IOException e)
  +      {
  +         throw new ServerException("Could not passivate", e);
  +      }catch (IllegalAccessException e)
  +      {
  +         // Throw this as a bean exception...(?)
  +         throw new EJBException(e);
  +      } catch (InvocationTargetException ite)
  +      {
  +         Throwable e = ite.getTargetException();
  +         if (e instanceof EJBException)
  +         {
  +            // Rethrow exception
  +            throw (EJBException)e;
  +         }
  +         else if (e instanceof RuntimeException)
  +         {
  +            // Wrap runtime exceptions
  +            throw new EJBException((Exception)e);
  +         }
  +         else if (e instanceof RemoteException)
  +         {
  +            // Remote, Create, or custom app. exception
  +            throw (RemoteException)e;
  +         }
  +         else
  +         {
  +            throw (Error)e;
  +         }
  +      }
  +   }
  +    
  +   public void removeSession(StatefulSessionEnterpriseContext ctx)
  +      throws RemoteException, RemoveException
  +   {
  +      // Call bean
  +      try
  +      {
  +         ejbRemove.invoke(ctx.getInstance(), new Object[0]);
           
  -        } catch (IllegalAccessException e)
  -        {
  -            // Throw this as a bean exception...(?)
  -            throw new EJBException(e);
  -        } catch (InvocationTargetException ite)
  -        {
  -            Throwable e = ite.getTargetException();
  -            if (e instanceof EJBException)
  -            {
  -                // Rethrow exception
  -                throw (EJBException)e;
  -            }
  -            else if (e instanceof RuntimeException)
  -            {
  -                // Wrap runtime exceptions
  -                throw new EJBException((Exception)e);
  -            }
  -            else if (e instanceof RemoveException)
  -            {
  -                throw (RemoveException)e;
  -            }
  -            else if (e instanceof RemoteException)
  -            {
  -                throw (RemoteException)e;
  -            }
  -            else
  -            {
  -                throw (Error)e;
  -            }
  -        }
  -    }
  -    
  -    public void removePassivated(Object key)
  -    {
  -        // OK also if the file does not exists
  -        getFile(key).delete();
  -    }
  -    
  -    // Z implementation ----------------------------------------------
  -    
  -    // Package protected ---------------------------------------------
  -    
  -    // Protected -----------------------------------------------------
  -    protected Long nextId()
  -    {
  -        return new Long(id++);
  -    }
  -    
  -    protected File getFile(Object key)
  -    {
  -        return new File(dir, key + ".ser");
  -    }
  -    
  -    // Private -------------------------------------------------------
  -    
  -    // Inner classes -------------------------------------------------
  +      } catch (IllegalAccessException e)
  +      {
  +         // Throw this as a bean exception...(?)
  +         throw new EJBException(e);
  +      } catch (InvocationTargetException ite)
  +      {
  +         Throwable e = ite.getTargetException();
  +         if (e instanceof EJBException)
  +         {
  +            // Rethrow exception
  +            throw (EJBException)e;
  +         }
  +         else if (e instanceof RuntimeException)
  +         {
  +            // Wrap runtime exceptions
  +            throw new EJBException((Exception)e);
  +         }
  +         else if (e instanceof RemoveException)
  +         {
  +            throw (RemoveException)e;
  +         }
  +         else if (e instanceof RemoteException)
  +         {
  +            throw (RemoteException)e;
  +         }
  +         else
  +         {
  +            throw (Error)e;
  +         }
  +      }
  +   }
  +    
  +   public void removePassivated(Object key)
  +   {
  +      // OK also if the file does not exists
  +      getFile(key).delete();
  +   }
  +    
  +   protected Long nextId()
  +   {
  +      return new Long(id++);
  +   }
  +    
  +   protected File getFile(Object key)
  +   {
  +      return new File(dataDir, key + ".ser");
  +   }
   }
  -
  -
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to