/*
  Copyright (C) 2006 Helge Hess

  This file is part of JOPE.

  JOPE 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, or (at your option) any
  later version.

  JOPE 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 JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/
package org.opengroupware.jope.appserver.publisher;

/*
 * JoSecuredObject
 * 
 * This interface should be implemented by JoObject's which implement their
 * own security strategy, which is often not necessary.
 * In all other cases this will pass over control to the JoSecurityManager.
 */
public interface JoSecuredObject {

  public Exception validateName(String _name, JoContext _ctx);
  
  /* utility class (use those methods in code to work on arbitary objects) */
  public static class Utility {
    
    public static Exception validateNameOfObject
      (Object _self, String _name, JoContext _ctx)
    {
      if (_self == null)
        return null;
      
      if (_self instanceof JoSecuredObject)
        return ((JoSecuredObject)_self).validateName(_name, _ctx);
      
      if (_ctx == null)
        return new JoAccessDeniedException("missing JoContext");
      
      JoSecurityManager jm = _ctx.joSecurityManager();
      if (jm == null)
        return new JoAccessDeniedException("no security manager is configured");
      
      return jm.validateNameOfObject(_self, _name, _ctx);
    }
    
    public static Exception validateValueForNameOfObject
      (Object _self, String _name, Object _value, JoContext _ctx)
    {
      if (_self == null || _value == null)
        return null;
      
      if (_ctx == null)
        return new JoAccessDeniedException("missing JoContext");
      
      JoSecurityManager jm = _ctx.joSecurityManager();
      if (jm == null)
        return new JoAccessDeniedException("no security manager is configured");
      
      return jm.validateValueForNameOfObject(_self, _name, _value, _ctx);
    }
  }
}
