jmcnally    2002/06/22 14:34:39

  Modified:    src/java/org/apache/torque Torque.java
               src/templates/om BaseManager.vm
  Log:
  patch by      Stephen Haberman <[EMAIL PROTECTED]>
  
  I've attached a patch that lets you not specify managers in the
  configuration and instead they default to the generated peers. It could
  be considered a hack, but I think it works.
  
  Basically in the managers template, I added DEFAULT_MANAGER_CLASSNAME,
  which is a static string of the manager class itself. Then in
  getManager(), instead of just calling Torque.getManager(key), I pass
  that static string as the default classname.
  
  Inside the Torque.getManager(key, defaultClassname), it checks to see if
  the key is already in the managers map. If not, it adds the key with the
  defaultClassname, just as if it had come from the configuration via the
  initManager method.
  
  So you can't use the straight Torque.getManager("ThisClass") from your
  client code, but you can use ThisClassManager.getInstance() and it'll
  work fine (and unless I'm using the managers wrong, I go with the
  ThisClassManager.getInstance method anyway).
  
  jmcnally - I modified this to synchronize the initManager method.  Not sure
  that this does not still suffer MT problems, but if a new manager is
  placed into the map, it would not cause problems with standard usage.
  
  Revision  Changes    Path
  1.62      +40 -3     jakarta-turbine-torque/src/java/org/apache/torque/Torque.java
  
  Index: Torque.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-torque/src/java/org/apache/torque/Torque.java,v
  retrieving revision 1.61
  retrieving revision 1.62
  diff -u -r1.61 -r1.62
  --- Torque.java       27 May 2002 12:17:43 -0000      1.61
  +++ Torque.java       22 Jun 2002 21:34:38 -0000      1.62
  @@ -432,7 +432,7 @@
        * @param className
        * @throws TorqueException
        */
  -    private static void initManager(String name, String className)
  +    private synchronized static void initManager(String name, String className)
           throws TorqueException
       {
           AbstractBaseManager manager = (AbstractBaseManager) managers.get(name);
  @@ -599,7 +599,44 @@
        */
       public static AbstractBaseManager getManager(String name)
       {
  -        return (AbstractBaseManager) managers.get(name);
  +        AbstractBaseManager m = (AbstractBaseManager) managers.get(name);
  +        if (m == null)
  +        {
  +            category.error("No configured manager for key " + name + ".");
  +        }
  +        return m;
  +    }
  +
  +    /**
  +     * This methods returns either the Manager from the configuration file,
  +     * or the default one provided by the generated code.
  +     *
  +     * @param name
  +     * @param defaultClassName the class to use if name has not been configured
  +     * @return a Manager
  +     */
  +    public static AbstractBaseManager getManager(String name, String 
defaultClassName)
  +    {
  +        AbstractBaseManager m = (AbstractBaseManager) managers.get(name);
  +        if (m == null)
  +        {
  +            category.debug("Added late Mapping for Manager: "
  +                           + name + " -> " + defaultClassName);
  +
  +            try
  +            {
  +                initManager(name, defaultClassName);
  +            }
  +            catch (TorqueException e)
  +            {
  +                category.error(e.getMessage(), e);
  +            }
  +        }
  +
  +        // Try again now that the default manager should be in the map
  +        m = (AbstractBaseManager) managers.get(name);
  +
  +        return m;
       }
   
       /**
  
  
  
  1.10      +7 -2      jakarta-turbine-torque/src/templates/om/BaseManager.vm
  
  Index: BaseManager.vm
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-torque/src/templates/om/BaseManager.vm,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BaseManager.vm    20 Jun 2002 22:07:09 -0000      1.9
  +++ BaseManager.vm    22 Jun 2002 21:34:39 -0000      1.10
  @@ -42,6 +42,10 @@
   {
       /** The name of the manager */
       protected static String MANAGER_NAME = "${interfaceName}Manager";
  +    
  +    /** The name of our class to pass to Torque as the default manager. */
  +    protected static String DEFAULT_MANAGER_CLASS
  +        = "${package}.${interfaceName}Manager";
   
       /**
        * Retrieves an implementation of the manager, based on the settings in
  @@ -52,7 +56,8 @@
       public static ${interfaceName}Manager getManager()
       {
           return (${interfaceName}Manager)
  -            Torque.getManager(${interfaceName}Manager.MANAGER_NAME);    
  +            Torque.getManager(${interfaceName}Manager.MANAGER_NAME, 
  +                ${interfaceName}Manager.DEFAULT_MANAGER_CLASS);
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to