Hi,
I've just started playing with creating my first HiveMind services, to
create JDO persistence managers and what I have ended up with is shown
below.
My questions are:
1. I'd like to avoid the pattern where my service implementation looks like:
FooImpl(...)
{
create a Foo using a third-party factory
}
Foo get()
{
return the Foo
}
I'd rather have a service who's type is Foo, but Foo may be a concrete
type (e.g. I'd like a properties service, but Properties is concrete)
or may be a third-party interface which needs a bit of code to
instantiate (i.e. more than just passing some parameters to a
constructor) -- PersistenceManagerFactory is an example of this.
Can I get rid of the 'get()' above?
2. Can I parameterise services? In this example I need three services
for each data source. It would be nice if I could declare the
PersistenceManagerFactory and PersistenceManager services once, and
have the declarations of the various PM services pass them parameters.
Or maybe I don't need todo these things -- I'm still getting to grips
with the HiveMind philosophy.
Thanks,
Tom
<?xml version="1.0"?>
<module id="au.com.ids.hivemind" version="1.0.0">
<!-- create a persistence manager factory using the properties
file name passed to the constructor -->
<service-point
id="PersistenceManagerFactory"
interface="au.com.ids.services.PersistenceManagerFactoryService">
<invoke-factory>
<construct
class="au.com.ids.services.impls.PersistenceManagerFactoryServiceImpl">
<string>kodo.properties</string>
</construct>
</invoke-factory>
</service-point>
<!-- create a persistence manager using the PM factory service above.
Implement Discardable to close the PM appropriately -->
<service-point id="PersistenceManager"
interface="au.com.ids.services.PersistenceManagerService">
<invoke-factory model="threaded">
<construct
class="au.com.ids.services.impls.PersistenceManagerServiceImpl">
<service>PersistenceManagerFactory</service>
</construct>
</invoke-factory>
</service-point>
<!-- make the persistence manager easier to get by exposing the
'persistenceManager'
property of the PM service above as a service in its own right -->
<service-point id="PM" interface="javax.jdo.PersistenceManager">
<invoke-factory
service-id="hivemind.lib.ServicePropertyFactory">
<construct service-id="PersistenceManager"
property="persistenceManager"/>
</invoke-factory>
</service-point>
</module>
public class PersistenceManagerServiceImpl implements
PersistenceManagerService, Discardable {
public void threadDidDiscardService() {
_pm.close();
_pm = null;
}
private PersistenceManager _pm;
public PersistenceManagerServiceImpl(
PersistenceManagerFactoryService pmFactoryService) {
_pm = pmFactoryService.get().getPersistenceManager();
}
public PersistenceManager getPersistenceManager() {
return _pm;
}
}
public class PersistenceManagerFactoryServiceImpl implements
PersistenceManagerFactoryService {
private PersistenceManagerFactory pmf = null;
public PersistenceManagerFactoryServiceImpl(String propertiesFile) {
Properties props = new Properties();
InputStream propertyRes = Persistence.class.getClassLoader()
.getResourceAsStream(propertiesFile);
try {
props.load(propertyRes);
} catch (IOException ioe) {
throw new RuntimeException("error loading properties '"
+ propertiesFile + "'", ioe);
}
pmf = JDOHelper.getPersistenceManagerFactory(props);
}
public PersistenceManagerFactory get() {
return pmf;
}
}
--
[EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]