portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tay...@apache.org
Subject cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/om/dbregistry SecurityDbEntryPeer.java
Date Tue, 13 Jul 2004 22:58:53 GMT
taylor      2004/07/13 15:58:53

  Added:       src/java/org/apache/jetspeed/om/dbregistry
                        SecurityDbEntryPeer.java
  Log:
  Torque specific implementation of security registry persistence
  
  CVS: ----------------------------------------------------------------------
  CVS: PR:
  CVS:   If this change addresses a PR in the problem report tracking
  CVS:   database, then enter the PR number(s) here.
  CVS: Obtained from:
  CVS:   If this change has been taken from another system, such as NCSA,
  CVS:   then name the system in this line, otherwise delete it.
  CVS: Submitted by:
  CVS:   If this code has been contributed to Apache by someone else; i.e.,
  CVS:   they sent us a patch or a new module, then include their name/email
  CVS:   address here. If this is your work then delete this line.
  CVS: Reviewed by:
  CVS:   If we are doing pre-commit code reviews and someone else has
  CVS:   reviewed your changes, include their name(s) here.
  CVS:   If you have not had it reviewed then delete this line.
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/om/dbregistry/SecurityDbEntryPeer.java
  
  Index: SecurityDbEntryPeer.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.om.dbregistry;
  
  import java.sql.Connection;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.jetspeed.om.registry.RegistryException;
  import org.apache.jetspeed.om.registry.SecurityAccess;
  import org.apache.jetspeed.om.registry.SecurityAllow;
  import org.apache.jetspeed.om.registry.SecurityEntry;
  import org.apache.jetspeed.om.registry.base.BaseSecurityAccess;
  import org.apache.jetspeed.om.registry.base.BaseSecurityAllow;
  import org.apache.jetspeed.om.registry.base.BaseSecurityEntry;
  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
  import org.apache.jetspeed.services.logging.JetspeedLogger;
  import org.apache.torque.Torque;
  import org.apache.torque.TorqueException;
  import org.apache.torque.util.Criteria;
  
  
  /**
   * Implements the Security Registry database operations
   *
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @version $Id: SecurityDbEntryPeer.java,v 1.1 2004/07/13 22:58:53 taylor Exp $
   */
  public class SecurityDbEntryPeer
      extends org.apache.jetspeed.om.dbregistry.BaseSecurityDbEntryPeer
  {
      public static final String ALLOW_OWNER = "owner";
      public static final String ALLOW_ROLE = "role";
      public static final String ALLOW_GROUP = "group";
      public static final String ALLOW_USER = "user";
      
      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(SecurityDbEntryPeer.class.getName());
      
      public static SecurityEntry lookupSecurityEntry(String entryName)
      {
          SecurityEntry se = null;
          try
          {
              SecurityDbEntry sde = lookup(entryName);
              se = mapDatabaseToEntry(sde);
          }
          catch (Exception e)
          {
              logger.error("Exception retrieving Security Entry: " + entryName, e);
          }
          return se;
      }
  
      private static SecurityDbEntry lookup(String entryName)
      throws TorqueException
      {
          Criteria criteria = new Criteria();
          criteria.add(SecurityDbEntryPeer.NAME, entryName);
          List result = SecurityDbEntryPeer.doSelect(criteria);
          if (null == result || result.isEmpty())
          {
              return null;
          }            
          return (SecurityDbEntry)result.get(0);
      }
      
      
      public static void storeSecurityEntry(SecurityEntry se)
      throws RegistryException
      {
          Connection conn = null;
          boolean autoCommit = false;
          try
          {
              // get fresh copy in case its changed on another node
              SecurityDbEntry sde = lookup(se.getName());
              if (null != sde)
              {
                  SecurityDbEntryPeer.removeSecurityEntryAccess(sde.getId());
              }
              else
              {
                  sde = new SecurityDbEntry();
              }
              conn = Torque.getConnection(DATABASE_NAME);
              autoCommit = conn.getAutoCommit();
              conn.setAutoCommit(false);
                          
              sde.setName(se.getName());
              sde.setTitle(se.getTitle());
              sde.setDescription(se.getDescription());
              sde.save(conn);
              
              Iterator accesses = se.getAccesses().iterator();
              while (accesses.hasNext())
              {
                  SecurityAccess access = (SecurityAccess)accesses.next();
                  SecurityAccessDbEntry sade = new SecurityAccessDbEntry();              
 
                  sade.setAction(access.getAction());
                  sade.setEntryId(sde.getId());
                  sade.save(conn);
                  
                  Iterator allows = access.getAllows().iterator();
                  while (allows.hasNext())
                  {
                      SecurityAllow allow = (SecurityAllow)allows.next();
                      SecurityAllowDbEntry sa = new SecurityAllowDbEntry();
                      if (allow.isOwner())
                      {
                          sa.setAllowType(ALLOW_OWNER);
                          sa.setAllowValue(ALLOW_OWNER);
                      }
                      else if (allow.getRole() != null)
                      {
                          sa.setAllowType(ALLOW_ROLE);
                          sa.setAllowValue(allow.getRole());                        
                      }
                      else if (allow.getGroup() != null)
                      {
                          sa.setAllowType(ALLOW_GROUP);
                          sa.setAllowValue(allow.getGroup());                        
                      }
                      else 
                      {
                          sa.setAllowType(ALLOW_USER);
                          sa.setAllowValue(allow.getUser());
                      }
                      sa.setAccessId(sade.getId());
                      sa.save(conn);    
                  }                
              }            
         
          }
          catch (Exception e)
          {
              try
              {
                  conn.rollback();
              }
              catch (Exception e2)
              {
                  logger.error("Failed to rollback", e2);
              }
              logger.error("Exception storing Security Entry: " + se.getName(), e);
              throw new RegistryException("Exception storing Security Entry: " +  se.getName()
+ ", " + e.toString());
          }
          finally
          {
              // make sure to release the database connection
              Torque.closeConnection(conn);
              try
              {
                  conn.setAutoCommit(autoCommit);
              }
              catch (Exception e)
              {
              }
          }
          
      }
      
      public static SecurityEntry mapDatabaseToEntry(SecurityDbEntry sde)
      throws TorqueException
      {
          SecurityEntry se = new BaseSecurityEntry(sde.getId());            
          se.setName(sde.getName());
          se.setTitle(sde.getTitle());
          se.setDescription(sde.getDescription());
          
          Iterator accesses = sde.getSecurityAccessDbEntrys().iterator();
          while (accesses.hasNext())
          {
              SecurityAccessDbEntry sade = (SecurityAccessDbEntry)accesses.next();
              SecurityAccess access = new BaseSecurityAccess(sade.getId());
              access.setAction(sade.getAction());
                              
              Iterator allows = sade.getSecurityAllowDbEntrys().iterator();
              while (allows.hasNext())
              {
                  SecurityAllowDbEntry sa = (SecurityAllowDbEntry)allows.next();
                  SecurityAllow allow = new BaseSecurityAllow(sa.getId());
                  if (sa.getAllowType().equals(ALLOW_OWNER))
                  {
                      allow.setOwner(true);
                      access.getOwnerAllows().add(allow);
                  }
                  else if (sa.getAllowType().equals(ALLOW_ROLE))
                  {
                      allow.setRole(sa.getAllowValue());
                      access.getAllows().add(allow);
                  }
                  else if (sa.getAllowType().equals(ALLOW_GROUP))
                  {
                      allow.setGroup(sa.getAllowValue());
                      access.getAllows().add(allow);
                  }
                  else 
                  {
                      allow.setUser(sa.getAllowValue());
                      access.getAllows().add(allow);
                  }                    
              }
              se.getAccesses().add(access);
          }
          return se;
      }
      
      public static void removeSecurityEntry(String entryName)
      {
          try
          {
              Criteria criteria = new Criteria();
              criteria.add(SecurityDbEntryPeer.NAME, entryName);            
              SecurityDbEntryPeer.doDelete(criteria);
          }
          catch (TorqueException e)
          {
              e.printStackTrace();
              logger.error("Exception Removing Security Entry: " + entryName, e);
              // throw new RegistryException("Exception Removing Security Entry: " +  entryName
+ ", " + e.toString());
          }
      }
  
      public static void removeSecurityEntryAccess(long id)
      {
          try
          {
              Criteria criteria = new Criteria();
              criteria.add(SecurityAccessDbEntryPeer.ENTRY_ID, id);            
              SecurityDbEntryPeer.doDelete(criteria);
          }
          catch (TorqueException e)
          {
              e.printStackTrace();
              logger.error("Exception Removing Security Entry Accesses: " + id, e);
              // throw new RegistryException("Exception Removing Security Entry: " +  entryName
+ ", " + e.toString());
          }
      }
      
      public static List fetchExtent()
      {
          List extent = new ArrayList();         
          try
          {
              Iterator result = SecurityDbEntryPeer.doSelect(new Criteria()).iterator();
              while (result.hasNext())
              {
                  SecurityEntry se = mapDatabaseToEntry((SecurityDbEntry)result.next());
                  extent.add(se);
              }
          }
          catch (TorqueException e)
          {
              e.printStackTrace();
              logger.error("Exception Fetching Extent for Security", e);            
          }
          return extent;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org


Mime
View raw message