Adrian Crum wrote:
> Adam Heath wrote: >> However, if there was a central factory, that used the ServiceRegistry >> pattern, and framework/entity implemented that, getGenericDelegator >> then called the ServiceRegistry implementation, logging a warning(from >> perspective of caller) while doing so, and the base factory then >> loaded the delegator thru ServiceRegistry, then existing code will >> still work, you'd get a logged warning, and you'd support the new design. > > Are you referring to this: > > http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service%20Provider Yes, using javax.imageio.spi.ServiceRegistry; an example of it's use is in CachedClassLoader. java 1.6 has java.util.ServiceLoader, but it's a slightly different api. |
Adam Heath wrote:
> Adrian Crum wrote: >> Adam Heath wrote: >>> However, if there was a central factory, that used the ServiceRegistry >>> pattern, and framework/entity implemented that, getGenericDelegator >>> then called the ServiceRegistry implementation, logging a warning(from >>> perspective of caller) while doing so, and the base factory then >>> loaded the delegator thru ServiceRegistry, then existing code will >>> still work, you'd get a logged warning, and you'd support the new design. >> Are you referring to this: >> >> http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service%20Provider > > Yes, using javax.imageio.spi.ServiceRegistry; an example of it's use > is in CachedClassLoader. > > java 1.6 has java.util.ServiceLoader, but it's a slightly different api. > So, the code: GenericDelegator delegator = GenericDelegator.getGenericDelegator(String delegatorName); would be replaced with something like: DelegatorFactory factory = (DelegatorFactory) UtilObject.getServiceProviderByClass(DelegatorFactory.class); DelegatorInterface delegator = DelegatorFactory .getGenericDelegator(String delegatorName); where the method public static Object UtilObject.getServiceProviderByClass(Class providerClass); would use javax.imageio.spi.ServiceRegistry to look up the provider class. Is that correct? -Adrian |
Adrian Crum wrote:
> So, the code: > > GenericDelegator delegator = GenericDelegator.getGenericDelegator(String > delegatorName); > > would be replaced with something like: > > DelegatorFactory factory = (DelegatorFactory) > UtilObject.getServiceProviderByClass(DelegatorFactory.class); > DelegatorInterface delegator = DelegatorFactory > .getGenericDelegator(String delegatorName); == public interface Factory<T> { T getInstance(String name); } public interface GenericDelegatorFactory implements Factory<GenericDelegator> { } public class GenericDelegatorFactoryImpl implements GenericDelegatorFactory { public GenericDelegator getInstance(String name) { } } public class UtilObject { public static <T> T getImplementation(Factory<T> factory, String name) { Iterator<Factory<T>> it = ServiceRegistry.lookupProviders(factory, loader); while (it.hasNext()) { Factory<T> factory = it.next(); T instance = factory.getInstance(name); if (instance != null) return instance; } throw GeneralException("foo"); } } GenericDelegator delegator = UtilObject.getImplementation(GenericDelegatorFactory.class, name); src/META-INF/services/org.ofbiz.entity.GenericDelegatorFactory: org.ofbiz.entity.GenericDelegatorFactoryImpl == The above is completely untested. |
Adam Heath wrote:
> Adrian Crum wrote: >> So, the code: >> >> GenericDelegator delegator = GenericDelegator.getGenericDelegator(String >> delegatorName); >> >> would be replaced with something like: >> >> DelegatorFactory factory = (DelegatorFactory) >> UtilObject.getServiceProviderByClass(DelegatorFactory.class); >> DelegatorInterface delegator = DelegatorFactory >> .getGenericDelegator(String delegatorName); > > == > public interface Factory<T> { > T getInstance(String name); > } > public interface GenericDelegatorFactory implements > Factory<GenericDelegator> { > } > public class GenericDelegatorFactoryImpl implements > GenericDelegatorFactory { > public GenericDelegator getInstance(String name) { > } > } > public class UtilObject { > public static <T> T getImplementation(Factory<T> factory, String name) { > > Iterator<Factory<T>> it = ServiceRegistry.lookupProviders(factory, > loader); > while (it.hasNext()) { > Factory<T> factory = it.next(); > T instance = factory.getInstance(name); > if (instance != null) return instance; > } > throw GeneralException("foo"); > } > } > GenericDelegator delegator = > UtilObject.getImplementation(GenericDelegatorFactory.class, name); > > src/META-INF/services/org.ofbiz.entity.GenericDelegatorFactory: > org.ofbiz.entity.GenericDelegatorFactoryImpl > == > > The above is completely untested. Cool - thanks! -Adrian |
Free forum by Nabble | Edit this page |