I'll write codes inline.
As said this is rally all the needed code to be able to inject
HiveMind services in a Tapestry5 page/component/service, just add this
code to your module builder class and use the 'hivemind' prefix for
reaching your HiveMind services, it will just works.

  @Contribute("tapestry.ioc.MasterObjectProvider")
   public static void contributeHiveMindToMasterObjectProvider(
           MappedConfiguration<String, ObjectProvider> configuration,
           @InjectService("HiveMind")
           ObjectProvider hivemind)
   {
       configuration.add("hivemind", hivemind);
   }


   public static ObjectProvider buildHiveMind(Log log)
   {
       return new HiveMindObjectProvider(log);
   }


This is the HiveMindObjectProvider:

/**
* This is Copyright Massimo Lusetti [EMAIL PROTECTED]
* released under the BSD License.
*/
package it.datacode.t5app.services;

import org.apache.commons.logging.Log;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;
import org.apache.tapestry.ioc.ObjectProvider;
import org.apache.tapestry.ioc.ServiceLocator;

/**
* This is the HiveMind ObjectProvider which will bring HiveMind
* services to your Tapestry5 pages/components/services.
*
* @author Massimo Lusetti <[EMAIL PROTECTED]>
*
*/
public class HiveMindObjectProvider implements ObjectProvider
{
   private final Log _log;

   private Registry _registry;


   public HiveMindObjectProvider(Log log)
   {
       _log = log;
//        initialise();
   }

   /* (non-Javadoc)
    * @see org.apache.tapestry.ioc.ObjectProvider#provide(java.lang.String,
java.lang.Class, org.apache.tapestry.ioc.ServiceLocator)
    */
   public <T> T provide(String expression, Class<T> objectType,
ServiceLocator locator)
   {
       initialise();

       if (_registry == null)
           throw new RuntimeException("HiveMind Registry not configured");

       try
       {
           Object service = _registry.getService(expression, objectType);
           // NOTE: ClassCastException should be trapped by HiveMind
registry method
           return objectType.cast(service);
       }
       catch (ApplicationRuntimeException are)
       {
           String msg = "Impossibile to locate the service " +
                        expression + " of type " +
objectType.getCanonicalName() +
                        " from HiveMind registry";
           _log.error(msg, are);
           throw new RuntimeException(msg, are);
       }
   }


   void initialise()
   {
       if (_registry == null)
       {
           _log.debug("Initializing HiveMind registry with default builder");
           _registry = RegistryBuilder.constructDefaultRegistry();
       }
   }

}


If you're still interested I'll pack up in a maven2 module to produce
a jar acceptable by Tapestry5 IoC as a drop in module.  I'm willing to
maintain it.

Let me know what you think.
--
Massimo
http://meridio.blogspot.com

Reply via email to