portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggol...@apache.org
Subject cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/psmlmanager/db DatabasePsmlManagerService.java
Date Thu, 12 Sep 2002 13:38:05 GMT
ggolden     2002/09/12 06:38:05

  Modified:    src/java/org/apache/jetspeed/services/psmlmanager/db
                        DatabasePsmlManagerService.java
  Log:
  The DatabasePsmlManagerService now takes an optional parameter:
  database
  which can name a torque pool to use for all database access in the service.
  If not specified, the default pool is used.
  (Also tuned the logging).
  
  The JetspeedGroup/Role/UserProfilePeer classes were updated to take
  DBConnection parameters - the service gets the connection from the proper
  pool and gives it to the peers to use.
  
  Revision  Changes    Path
  1.22      +180 -74   jakarta-jetspeed/src/java/org/apache/jetspeed/services/psmlmanager/db/DatabasePsmlManagerService.java
  
  Index: DatabasePsmlManagerService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/psmlmanager/db/DatabasePsmlManagerService.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- DatabasePsmlManagerService.java	29 Jul 2002 02:25:27 -0000	1.21
  +++ DatabasePsmlManagerService.java	12 Sep 2002 13:38:05 -0000	1.22
  @@ -82,6 +82,11 @@
   import org.apache.turbine.services.resources.ResourceService;
   import org.apache.turbine.services.servlet.TurbineServlet;
   
  +// torque
  +import org.apache.torque.Torque;
  +import org.apache.torque.pool.DBConnection;
  +import org.apache.torque.TorqueException;
  +
   // jetspeed security
   import org.apache.jetspeed.om.security.JetspeedUser;
   import org.apache.jetspeed.om.security.JetspeedUserFactory;
  @@ -91,7 +96,6 @@
   import org.apache.jetspeed.om.security.JetspeedGroupFactory;
   import org.apache.jetspeed.services.security.JetspeedSecurityException;
   
  -
   //Servlet API
   import javax.servlet.ServletConfig;
    
  @@ -155,6 +159,8 @@
       private final static String REFRESH_RATE = "refresh-rate";
       private final static long DEFAULT_REFRESH_RATE = 60 * 60 * 8 * 1000; //8hrs
   
  +    private final static String POOL_NAME = "database";
  +
       /** the import/export consumer service **/
       private PsmlManagerService consumer = null;
   
  @@ -163,6 +169,9 @@
       String mapFile = null;
       /** the Castor mapping file name */
       private Mapping mapping = null;
  +    
  +    /** The pool name to use for database requests. */
  +    private String poolName = null;
   
       /**
        * This is the early initialization method called by the
  @@ -201,6 +210,9 @@
               refreshRate = 
                         serviceConf.getLong(REFRESH_RATE, DEFAULT_REFRESH_RATE);
   
  +            // get the name of the torque database pool to use
  +            poolName = serviceConf.getString(POOL_NAME);
  +
   //            new DatabaseInitializer(this, conf, serviceConf);
   
           // psml castor mapping file
  @@ -221,7 +233,7 @@
   
       /** Late init method from Turbine Service model */
       public void init() throws InitializationException
  -    {
  +    { 
           //Mark that we are done
           setInit(true);
   /*
  @@ -232,8 +244,7 @@
           }
           catch (Exception e)
           {
  -            Log.error("Error importing: " , e);
  -            e.printStackTrace();
  +            Log.warn("DatabasePsmlManagerService.init: exception while importing:" , e);
           }
     */
       }
  @@ -246,7 +257,8 @@
           if (mapFile != null)
           {
               File map = new File(mapFile);
  -            Log.debug("Loading psml mapping file "+mapFile);
  +            if (Log.getLogger().isDebugEnabled())
  +                Log.debug("Loading psml mapping file " + mapFile);
               if (map.exists() && map.isFile() && map.canRead())
               {
                   try
  @@ -316,10 +328,11 @@
               {
                   while (!done)
                   {
  -                    Log.info("Cache Refresher thread sleeping now!");
  +                    if (Log.getLogger().isDebugEnabled())
  +                        Log.debug("Cache Refresher thread sleeping now!");
                       sleep (refreshRate);
  -                    Log.info("Cache Refresher thread woke up!");
  -                    Log.info("Cache Refresher thread working now!");
  +                    if (Log.getLogger().isDebugEnabled())
  +                        Log.debug("Cache Refresher thread working now!");
   
                       try
                       {
  @@ -342,13 +355,14 @@
                       }
                       catch (Exception e)
                       {
  -                        Log.error("CacheRefresher: Error in cache refresher...", e);
  +                        Log.warn("DatabasePsmlManagerService.CacheRefresher: Error in cache
refresher...", e);
                       }
                   }
               }
               catch (InterruptedException e)
               {
  -                Log.info("CacheRefresher: recieved interruption, aborting.");
  +                if (Log.getLogger().isDebugEnabled())
  +                    Log.debug("DatabasePsmlManagerService.CacheRefresher: recieved interruption,
aborting.");
               }
           }     
       }
  @@ -401,7 +415,8 @@
                   keybuf.append('$').append("Language:").append(language);
               }
           }
  -        Log.info("Returning locator string: " + keybuf.toString());
  +        if (Log.getLogger().isDebugEnabled())
  +            Log.debug("DatabasePsmlManagerService: Returning locator string: " + keybuf.toString());
   
           return keybuf.toString();
       }
  @@ -411,7 +426,8 @@
           ProfileLocator locator = new BaseProfileLocator();
           String entity = null;
   
  -        Log.info("Creating locator for string: " + locstr);
  +        if (Log.getLogger().isDebugEnabled())
  +            Log.debug("DatabasePsmlManagerService: Creating locator for string: " + locstr);
   
           StringTokenizer dollarTokens = new StringTokenizer(locstr, "$");
           while (dollarTokens.hasMoreTokens())
  @@ -456,7 +472,8 @@
                   locator.setLanguage(entity);
               }
           }
  -        Log.info("Returning locator for string: " + locatorToString(locator));
  +        if (Log.getLogger().isDebugEnabled())
  +            Log.debug("DatabasePsmlManagerService: Returning locator for string: " + locatorToString(locator));
       
           return locator;
   
  @@ -465,21 +482,21 @@
       public PSMLDocument getDocument(String name)
       {
           // do nothing, deprecated
  -        Log.info("*** NOT SUPPORTED: GETDOC FROM DATABASE PSML MANAGER!!!");        
  +        Log.warn("*** NOT SUPPORTED: GETDOC FROM DATABASE PSML MANAGER!!!");        
           return null;
       }
   
       public boolean saveDocument(String fileOrUrl, PSMLDocument doc)
       {
           // do nothing, deprecated
  -        Log.info("*** NOT SUPPORTED: SAVING DOC FROM DATABASE PSML MANAGER!!!");
  +        Log.warn("*** NOT SUPPORTED: SAVING DOC FROM DATABASE PSML MANAGER!!!");
           return false;
       }
   
       public boolean saveDocument(PSMLDocument doc) 
       {
           // do nothing, will be deprecated 
  -        Log.info("*** NOT SUPPORTED: SAVING DOC FROM DATABASE PSML MANAGER!!!");      
 
  +        Log.warn("*** NOT SUPPORTED: SAVING DOC FROM DATABASE PSML MANAGER!!!");      
 
           return false;
       }
   
  @@ -499,7 +516,7 @@
           if (locator == null)
           {
               String message = "PSMLManager: Must specify a locator";
  -            Log.error(message);
  +            Log.warn("DatabasePsmlManagerService.getDocument: " + message);
               throw new IllegalArgumentException(message);
           }
   
  @@ -518,8 +535,9 @@
                   psmldoc = (PSMLDocument)psmlCache.get(locStr);
               }
            }
  -         //Log.info("DatabasePsmlManagerService.getDocument(): psmlcache: " +
  -         //  (inCache ? ((psmldoc == null) ? "null present" : "doc present") : "not in
cache") + " : " + locStr);
  +//        if (Log.getLogger().isDebugEnabled())
  +//            Log.info("DatabasePsmlManagerService.getDocument(): psmlcache: " +
  +//                (inCache ? ((psmldoc == null) ? "null present" : "doc present") : "not
in cache") + " : " + locStr);
    
   
           // if in the cache, doc or null, return what's in the cache
  @@ -534,8 +552,7 @@
           }
           catch (Exception e)
           {
  -            Log.error("Error occurred in Database PSML Manager: " + e);
  -            Log.error("Unable to get a psml document from the database.", e);
  +            Log.warn("DatabasePSMLManagerService.getDocument: exception:", e);
               throw new RuntimeException("Could not get profile from DB");
           }
       }
  @@ -570,7 +587,7 @@
           if (profile == null)
           {
               String message = "PSMLManager: Must specify a profile";
  -            Log.error(message);
  +            Log.warn("DatabasePsmlManagerService.createOrSaveDocument: " + message);
               throw new IllegalArgumentException(message);
           }
   
  @@ -579,6 +596,8 @@
           Group group = profile.getGroup();
           String tableName = null;
   
  +        DBConnection dbCon = getDbConnection();
  +
           try 
           {
               if (user != null)
  @@ -586,11 +605,11 @@
                   tableName = "JETSPEED_USER_PROFILE";
                   if (operation == INSERT) 
                   {
  -                    new JetspeedUserProfilePeer().insert(profile);
  +                    new JetspeedUserProfilePeer().insert(profile, dbCon);
                   }
                   else if (operation == UPDATE)
                   {
  -                    new JetspeedUserProfilePeer().update(profile);
  +                    new JetspeedUserProfilePeer().update(profile, dbCon);
                   }
               }
               else if (role != null)
  @@ -598,11 +617,11 @@
                   tableName = "JETSPEED_ROLE_PROFILE";
                   if (operation == INSERT) 
                   {
  -                    new JetspeedRoleProfilePeer().insert(profile);
  +                    new JetspeedRoleProfilePeer().insert(profile, dbCon);
                   }
                   else if (operation == UPDATE)
                   {
  -                    new JetspeedRoleProfilePeer().update(profile);
  +                    new JetspeedRoleProfilePeer().update(profile, dbCon);
                   }
               }  
               else if (group != null)
  @@ -610,19 +629,19 @@
                   tableName = "JETSPEED_GROUP_PROFILE";
                   if (operation == INSERT) 
                   {
  -                    new JetspeedGroupProfilePeer().insert(profile);
  +                    new JetspeedGroupProfilePeer().insert(profile, dbCon);
                   }
                   else if (operation == UPDATE)
                   {
  -                    new JetspeedGroupProfilePeer().update(profile);
  -
  +                    new JetspeedGroupProfilePeer().update(profile, dbCon);
                   }
               }   
               
               // insert successful
               synchronized (psmlCache)
               {
  -                Log.info("Putting profile in cache, in create document method, Locator
string: " + locatorToString(profile));
  +                if (Log.getLogger().isDebugEnabled())
  +                    Log.debug("DatabasePsmlManagerService.createOrSaveDocument: caching
document: profile: " + locatorToString(profile));
                   psmlCache.put(locatorToString(profile), profile.getDocument());
               }
   
  @@ -630,11 +649,15 @@
           }
           catch (Exception e) // insert failed
           {
  -            Log.error("Error saving profile:"+profile);
  -            Log.error("Error occurred in Database PSML Manager: " , e);
  -            Log.error("Insert in table " + tableName + " failed");
  +            Log.warn("DatabasePsmlManagerService.createOrSaveDocument: profile: "
  +                        + profile + " tableName: " + tableName, e);
               throw new RuntimeException("Could not create new profile in DB");
           }
  +        finally
  +        {
  +            // make sure to release the database connection
  +            releaseDbConnection(dbCon);
  +        }
   
       }
   
  @@ -648,7 +671,7 @@
           if (locator == null)
           {
               String message = "PSMLManager: Must specify a locator";
  -            Log.error(message);
  +            Log.warn("DatabasePsmlManagerService.removeDocument: " + message);
               throw new IllegalArgumentException(message);
           }
   
  @@ -657,21 +680,24 @@
           Group group = locator.getGroup();
           String tableName = null;
   
  +        // get a database connection
  +        DBConnection dbCon = getDbConnection();
  +
           try 
           {  
               if (user != null)
               {   
  -                new JetspeedUserProfilePeer().delete(locator);
  +                new JetspeedUserProfilePeer().delete(locator, dbCon);
                   tableName = "JETSPEED_USER_PROFILE";
               }
               else if (role != null)
               {  
  -                new JetspeedRoleProfilePeer().delete(locator);
  +                new JetspeedRoleProfilePeer().delete(locator, dbCon);
                   tableName = "JETSPEED_ROLE_PROFILE";
               } 
               else if (group != null)
               {
  -                new JetspeedGroupProfilePeer().delete(locator);
  +                new JetspeedGroupProfilePeer().delete(locator, dbCon);
                   tableName = "JETSPEED_GROUP_PROFILE";
               }  
               
  @@ -683,10 +709,15 @@
           }
           catch (Exception e) // insert failed
           {
  -            Log.error("Error occurred in Database PSML Manager: " , e);
  -            Log.error("Insert in table " + tableName + " failed");
  +            Log.warn("DatabasePsmlManagerService.removeDocument: profile: "
  +                        + locatorToString(locator) + " tableName: " + tableName, e);
               throw new RuntimeException("Could not delete profile for given locator from
DB");
           }
  +        finally
  +        {
  +            // make sure to release the database connection
  +            releaseDbConnection(dbCon);
  +        }
       }
   
       /** 
  @@ -701,10 +732,12 @@
           if (locator == null)
           {
               String message = "PSMLManager: Must specify a locator";
  -            Log.error(message);
  +            Log.warn("DatabasePsmlManagerService.query: " + message);
               throw new IllegalArgumentException(message);
           }
   
  +        DBConnection dbCon = getDbConnection();
  +
           try 
           {  
               Vector userData = null;
  @@ -718,7 +751,7 @@
               switch (queryMode)
               {
                   case QueryLocator.QUERY_USER:    
  -                    userData = new JetspeedUserProfilePeer().selectOrdered(locator);
  +                    userData = new JetspeedUserProfilePeer().selectOrdered(locator, dbCon);
                       if (userData != null)
                       {
                           list = getProfiles(userData);
  @@ -726,7 +759,7 @@
                       break;
   
                   case QueryLocator.QUERY_GROUP:
  -                    groupData = new JetspeedGroupProfilePeer().selectOrdered(locator);
  +                    groupData = new JetspeedGroupProfilePeer().selectOrdered(locator, dbCon);
                       if (groupData != null)
                       {
                           list = getProfiles(groupData);
  @@ -734,7 +767,7 @@
                       break;
    
                   case QueryLocator.QUERY_ROLE:    
  -                    roleData = new JetspeedRoleProfilePeer().selectOrdered(locator);
  +                    roleData = new JetspeedRoleProfilePeer().selectOrdered(locator, dbCon);
                       if (roleData != null)
                       {
                           list = getProfiles(roleData);
  @@ -743,19 +776,19 @@
    
   
                   default:  //QUERY_ALL
  -                    userData = new JetspeedUserProfilePeer().selectOrdered(locator);
  +                    userData = new JetspeedUserProfilePeer().selectOrdered(locator, dbCon);
                       if (userData != null)
                       {
                           list.addAll(getProfiles(userData));
                       }
   
  -                    groupData = new JetspeedGroupProfilePeer().selectOrdered(locator);
  +                    groupData = new JetspeedGroupProfilePeer().selectOrdered(locator, dbCon);
                       if (groupData != null)
                       {
                           list.addAll(getProfiles(groupData));
                       }
   
  -                    roleData = new JetspeedRoleProfilePeer().selectOrdered(locator);
  +                    roleData = new JetspeedRoleProfilePeer().selectOrdered(locator, dbCon);
                       if (roleData != null)
                       {
                           list.addAll(getProfiles(roleData));
  @@ -768,8 +801,12 @@
           }
           catch (Exception e)
           {
  -            Log.error("Error occurred in Database PSML Manager: " , e);
  -            e.printStackTrace();
  +            Log.warn("DatabasePsmlManagerService.query: exception" , e);
  +        }
  +        finally
  +        {
  +            // make sure to release the databased connection
  +            releaseDbConnection(dbCon);
           }
   
           return new ArrayList().iterator();  // return empty non-null iterator
  @@ -841,7 +878,7 @@
           if (locators == null)
           {
               String message = "PSMLManager: Must specify a list of locators";
  -            Log.error(message);
  +            Log.warn("DatabasePsmlManagerService.getDocument: " + message);
               throw new IllegalArgumentException(message);
           }
   
  @@ -872,7 +909,7 @@
           if (locator == null)
           {
               String message = "PSMLManager: Must specify a locator";
  -            Log.error(message);
  +            Log.warn("DatabasePsmlManagerService.refresh: " + message);
               throw new IllegalArgumentException(message);
           }
   
  @@ -885,12 +922,14 @@
           PSMLDocument psmldoc = null;
           String page = null;
   
  +        DBConnection dbCon = getDbConnection();
  +
           try
           {
               if (user != null)
               {
                   tableName = "JETSPEED_USER_PROFILE";
  -                records = new JetspeedUserProfilePeer().select(locator);
  +                records = new JetspeedUserProfilePeer().select(locator, dbCon);
                   Iterator iterator = records.iterator(); 
                   while (iterator.hasNext())
                   {
  @@ -903,7 +942,7 @@
               else if (role != null)
               {  
                   tableName = "JETSPEED_ROLE_PROFILE";
  -                records = new JetspeedRoleProfilePeer().select(locator);
  +                records = new JetspeedRoleProfilePeer().select(locator, dbCon);
                   Iterator iterator = records.iterator(); 
                   while (iterator.hasNext())
                   {
  @@ -916,7 +955,7 @@
               else if (group != null)
               {
                   tableName = "JETSPEED_GROUP_PROFILE";
  -                records = new JetspeedGroupProfilePeer().select(locator);
  +                records = new JetspeedGroupProfilePeer().select(locator, dbCon);
                   Iterator iterator = records.iterator(); 
                   while (iterator.hasNext())
                   {
  @@ -932,7 +971,8 @@
                   psmldoc = getPSMLDocument(page, portlets);
                   synchronized (psmlCache)
                   {
  -                Log.info("Putting profile in cache, in refresh method, Locator string:
" + locatorToString(locator));
  +                    if (Log.getLogger().isDebugEnabled())
  +                        Log.debug("DatabasePsmlManagerService.refresh: caching document:
profile: " + locatorToString(locator));
                       psmlCache.put(locatorToString(locator), psmldoc);
                   }
                   return psmldoc;
  @@ -941,18 +981,25 @@
               {
                   // cache the fact that there is NO document matching this profile
                   psmlCache.put(locatorToString(locator), null);
  -                Log.info("Putting null in cache, in refresh method, Locator string: " +
locatorToString(locator));
  +                if (Log.getLogger().isDebugEnabled())
  +                    Log.debug("DatabasePsmlManagerService.refresh: caching 'document not
found': profile: " + locatorToString(locator));
               }
           }
           catch (Exception e)
           {
  -            Log.error("Error occurred in Database PSML Manager: " + e);
  -            Log.error("Select from table " + tableName + " failed");
  -            Log.error("Unable to get a psml document from the database.", e);
  -            Log.error("Refresh failed: " + locatorToString(locator));
  +            Log.warn("DatabasePsmlManagerService.refresh: profile: " + locatorToString(locator)
  +                + " tableName: " + tableName, e);
               throw new RuntimeException("Could not refresh profile from DB");
           }
  -        Log.info("DatabasePsmlManagerService: Returning null document");
  +        finally
  +        {
  +            // make sure to release the database connection
  +            releaseDbConnection(dbCon);
  +        }
  +
  +        if (Log.getLogger().isDebugEnabled())
  +            Log.debug("DatabasePsmlManagerService.refresh: no document found: profile:
"
  +                    + locatorToString(locator));
           return null;
       }
   
  @@ -962,19 +1009,25 @@
        */
       public void removeUserDocuments(JetspeedUser user)
       {
  +        DBConnection dbCon = getDbConnection();
  +
           try 
           {  
               if (user != null)
               {   
  -                new JetspeedUserProfilePeer().delete(user);
  -            }   
  +                new JetspeedUserProfilePeer().delete(user, dbCon);
  +            }
           }
           catch (Exception e) // delete failed
           {
  -            Log.error("Error occurred in Database PSML Manager: " , e);
  -            Log.error("Delete from table JETSPEED_USER_PROFILE failed");
  +            Log.warn("DatabasePsmlManagerService.removeUserDocuments: exception:", e);
               throw new RuntimeException("Could not delete documents for given user from
DB");
           }
  +        finally
  +        {
  +            // make sure to release the database connection
  +            releaseDbConnection(dbCon);
  +        }
   
       }
   
  @@ -984,19 +1037,25 @@
        */
       public void removeRoleDocuments(Role role)
       {
  +        DBConnection dbCon = getDbConnection();
  +
           try
           {
               if (role != null)
               {
  -                new JetspeedRoleProfilePeer().delete(role);
  +                new JetspeedRoleProfilePeer().delete(role, dbCon);
               } 
           }
           catch (Exception e) // delete failed
           {
  -            Log.error("Error occurred in Database PSML Manager: " , e);
  -            Log.error("Delete from table JETSPEED_ROLE_PROFILE failed");
  +            Log.warn("DatabasePsmlManagerService.removeRoleDocuments: exception:", e);
               throw new RuntimeException("Could not delete documents for given role from
DB");
           }
  +        finally
  +        {
  +            // make sure to release the database connection
  +            releaseDbConnection(dbCon);
  +        }
       }
   
       /** Removes all documents for a given group.
  @@ -1005,19 +1064,25 @@
        */
       public void removeGroupDocuments(Group group)
       {
  +        DBConnection dbCon = getDbConnection();
  +
           try
           {
               if (group != null)
               {
  -                new JetspeedGroupProfilePeer().delete(group);
  +                new JetspeedGroupProfilePeer().delete(group, dbCon);
               }  
           }
           catch (Exception e) // delete failed
           {
  -            Log.error("Error occurred in Database PSML Manager: " + e);
  -            Log.error("Delete from table JETSPEED_GROUP_PROFILE failed");
  +            Log.warn("DatabasePsmlManagerService.removeGroupDocuments: exception:", e);
               throw new RuntimeException("Could not delete documents for given group from
DB");
           }
  +        finally
  +        {
  +            // make sure to release the database connection
  +            releaseDbConnection(dbCon);
  +        }
       }
   
       /** Query for a collection of profiles given a profile locator criteria.
  @@ -1053,15 +1118,15 @@
                       }
                       catch (Exception e)
                       {
  -                        Log.error("Failed to export profile to File System: " + profile,
ex);
  +                        Log.warn("DatabasePsmlManagerService.export: profile: "
  +                                + profile, ex);
                       }
                   }
               }
           }
           catch(Exception e)
           {
  -            e.printStackTrace();
  -            Log.error("Failed to export profiles to File System: " , e );
  +            Log.warn("DatabasePsmlManagerService.export: exception:", e);
   
           }
           finally
  @@ -1171,4 +1236,45 @@
       }
   
   
  +    /**
  +    * Get a database connection to the default or specifed torque database pool
  +    */
  +    private DBConnection getDbConnection()
  +    {
  +        try
  +        {
  +            // use the default pool if not specified
  +            if (poolName == null)
  +            {
  +                return Torque.getConnection();
  +            }
  +            
  +            // otherwise use the specified pool name
  +            else
  +            {
  +                return Torque.getConnection(poolName);
  +            }
  +        }
  +        catch (TorqueException e)
  +        {
  +            Log.warn("DatabasePsmlManagerService.getDbConnection: exception: " + e);
  +            return null;
  +        }
  +    }
  +
  +    /**
  +    * Release a previously gotten database connection back to the torque pool
  +    */
  +    private void releaseDbConnection(DBConnection connection)
  +    {
  +        try
  +        {
  +            Torque.releaseConnection(connection);
  +        }
  +        catch (TorqueException e)
  +        {
  +            Log.warn("DatabasePsmlManagerService.releaseDbConnection: exception: " + e);
  +        }
  +    }
   }
  +
  
  
  

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


Mime
View raw message