Hi Oliver,
i think you don't necessarily need your own factory. Or better it
depends on what exactly do you want to achieve.
Using the default factory helper and the generated get() function should
give also and always the same single instance of the object. Where using
createInstanceWithContext(...) or in Basic createUnoService() will
create always a new instance.
The key is the "get" function and of course the SINGLETON key entry in
the writeRegistryInfo function. If you want ensure that you always get
the same instance independent which function is used (get or
createInstanceWitrhContext) it's fine to implement an own factory but as
i mentioned above it's not necessary.
I think i will create a complete example and will document it in detail
to make this clear.
Juergen
Oliver Brinzing wrote:
Hi,
i think i finally did it :-)
the trick is to implement your own SingletonFactory ...
public class SingletonFactory implements XSingleComponentFactory {
private transient LogServiceImpl instance = null;
public Object createInstanceWithArgumentsAndContext(final Object[] args,
final XComponentContext xComponentContext) throws
Exception {
return createInstanceWithContext(xComponentContext);
}
public Object createInstanceWithContext(
final XComponentContext xComponentContext) throws
Exception {
if (this.instance == null)
this.instance = new LogServiceImpl(xComponentContext);
return this.instance;
}
and changing ServiceProvider class:
public class ServiceProvider {
public static XSingleComponentFactory __getComponentFactory(final
String sImplName) {
XSingleComponentFactory xSingleComponentFactory = null;
if (sImplName.equals(LogServiceImpl.class.getName()))
xSingleComponentFactory = new SingletonFactory();
return xSingleComponentFactory;
}
public static boolean __writeRegistryServiceInfo(final XRegistryKey
xRegistryKey) {
try {
final XRegistryKey newKey = xRegistryKey
.createKey(LogServiceImpl.class.getName()
+ "/UNO/SINGLETONS/" +
LogServiceImpl._serviceName);
newKey.setStringValue(LogServiceImpl._serviceName);
} catch (final InvalidRegistryException e) {
return false;
}
return
Factory.writeRegistryServiceInfo(LogServiceImpl.class.getName(),
new String[] { LogServiceImpl._serviceName },
xRegistryKey);
}
}
now EqualUnoObjects(oA,oB) returns true ...
oA = CreateUnoService("logging.LogService")
oB = CreateUnoService("logging.LogService")
and generated LogService.class has a static method get(xComponentContext)
Oliver
GnuPG key 0xCFD04A45: 8822 057F 4956 46D3 352C 1A06 4E2C AB40 CFD0 4A45
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]