Kaydet (Commit) 9e50970c authored tarafından Joachim Lingner's avatar Joachim Lingner

#97822# Removed ComponentFactoryWrapper

üst 070d22be
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
* *
* $RCSfile: JavaLoader.java,v $ * $RCSfile: JavaLoader.java,v $
* *
* $Revision: 1.8 $ * $Revision: 1.9 $
* *
* last change: $Author: dbo $ $Date: 2002-09-02 14:36:28 $ * last change: $Author: jl $ $Date: 2002-10-01 13:30:46 $
* *
* The Contents of this file are made available subject to the terms of * The Contents of this file are made available subject to the terms of
* either of the following licenses * either of the following licenses
...@@ -106,7 +106,7 @@ import java.net.MalformedURLException; ...@@ -106,7 +106,7 @@ import java.net.MalformedURLException;
* service. Therefor the <code>JavaLoader</code> activates external UNO components which are implemented in Java. * service. Therefor the <code>JavaLoader</code> activates external UNO components which are implemented in Java.
* The loader is used by the <code>ServiceManger</code>. * The loader is used by the <code>ServiceManger</code>.
* <p> * <p>
* @version $Revision: 1.8 $ $ $Date: 2002-09-02 14:36:28 $ * @version $Revision: 1.9 $ $ $Date: 2002-10-01 13:30:46 $
* @author Markus Herzog * @author Markus Herzog
* @see com.sun.star.loader.XImplementationLoader * @see com.sun.star.loader.XImplementationLoader
* @see com.sun.star.loader.Java * @see com.sun.star.loader.Java
...@@ -297,7 +297,6 @@ public class JavaLoader implements XImplementationLoader, ...@@ -297,7 +297,6 @@ public class JavaLoader implements XImplementationLoader,
{ {
locationUrl = expand_url( locationUrl ); locationUrl = expand_url( locationUrl );
boolean needFactoryWrapper = false;
Object returnObject = null; Object returnObject = null;
Class clazz = null; Class clazz = null;
...@@ -306,7 +305,9 @@ public class JavaLoader implements XImplementationLoader, ...@@ -306,7 +305,9 @@ public class JavaLoader implements XImplementationLoader,
// first we must get the class of the implementation // first we must get the class of the implementation
// 1. If a location URL is given it is assumed that this points to a JAR file. // 1. If a location URL is given it is assumed that this points to a JAR file.
// The components class name is stored in the manifest file. // The components class name is stored in the manifest file.
// 2. If only the implementation name is given, the class is loaded with the Class.forName() method // 2. If only the implementation name is given, the class is loaded with the
// Class.forName() method. This is a hack to load bootstrap components.
// Normally a string must no be null.
try { try {
if ( locationUrl != null ) { if ( locationUrl != null ) {
RegistrationClassFinder classFinder = new RegistrationClassFinder( locationUrl ); RegistrationClassFinder classFinder = new RegistrationClassFinder( locationUrl );
...@@ -392,11 +393,13 @@ public class JavaLoader implements XImplementationLoader, ...@@ -392,11 +393,13 @@ public class JavaLoader implements XImplementationLoader,
} }
} }
} }
catch ( NoSuchMethodException noSuchMethodEx) { catch ( NoSuchMethodException e) {
needFactoryWrapper = true; throw new CannotActivateFactoryException("Can not activate the factory for "
+ implementationName + " because " + e.toString() );
} }
catch ( SecurityException secEx) { catch ( SecurityException e) {
needFactoryWrapper = true; throw new CannotActivateFactoryException("Can not activate the factory for "
+ implementationName + " because " + e.toString() );
} }
catch ( IllegalAccessException e ) { catch ( IllegalAccessException e ) {
throw new CannotActivateFactoryException("Can not activate the factory for " throw new CannotActivateFactoryException("Can not activate the factory for "
...@@ -410,14 +413,6 @@ public class JavaLoader implements XImplementationLoader, ...@@ -410,14 +413,6 @@ public class JavaLoader implements XImplementationLoader,
throw new CannotActivateFactoryException("Can not activate the factory for " throw new CannotActivateFactoryException("Can not activate the factory for "
+ implementationName + " because " + e.getTargetException().toString() ); + implementationName + " because " + e.getTargetException().toString() );
} }
// if no method is found make a factory wrapper for the implementation and return it
if ( needFactoryWrapper ) {
DEBUG ("create factory wrapper for " + implementationName);
ComponentFactoryWrapper wrapp = new ComponentFactoryWrapper( implementationName, locationUrl );
returnObject = wrapp.getServiceFactory(implementationName,
multiServiceFactory,
xKey);
}
return returnObject; return returnObject;
} }
...@@ -509,498 +504,3 @@ public class JavaLoader implements XImplementationLoader, ...@@ -509,498 +504,3 @@ public class JavaLoader implements XImplementationLoader,
} }
} }
//**********************************************************************************************************
//**********************************************************************************************************
/**
* The <code>ComponentFactoryWrapper</code> class provides methods to create a factory for a component and
* the registration at a registry in a default manner. The class is used by the <code>JavaLoader</code> if the
* a component does not comes with its own methods for creating a factory or for the registration.
* <p>
* @version $Revision: 1.8 $ $ $Date: 2002-09-02 14:36:28 $
* @author Markus Herzog
* @since UDK1.0
*/
class ComponentFactoryWrapper
implements XServiceInfo,
XSingleServiceFactory
{
private static final boolean DEBUG = false;
private String serviceName = null;
private String implName = null;
private String locationUrl = null;
private Class serviceClass = null;
private XMultiServiceFactory aServiceManager = null;
private boolean bServiceManagerAlreadySet = false;
/**
* Constructor for a <code>ComponentFactoryWrapper</code> object.
* <p>
* @param name specifies the name of the implementation. If no loaction URL is given
* the implementation name must be the class name of the component.
* @param lUrl points to an archive (JAR file) which contains a component.
*/
public ComponentFactoryWrapper( String name, String lUrl ) {
implName = name;
locationUrl = lUrl;
}
private static final void DEBUG( String dbg ) {
if (DEBUG) System.err.println(">>>ComponentFactoryWrapper - " + dbg);
}
/**
* Registers the component at the registry. First it is verified if component includes the optional
* method __writeRegistryServiceInfo ( writeRegistryServiceInfo - DEPRECATED ). If so the call is delegated
* to this method. Otherwise the component will be registered under its implementation name.
* <p>
* @return true if registration succseeded - otherwise false
* @param regKey root key under which the <code>JavaLoader</code> should be regidstered
*/
public boolean writeRegistryServiceInfo( XRegistryKey regKey )
throws CannotRegisterImplementationException,
com.sun.star.uno.RuntimeException
{
boolean success = false;
boolean defaultRegistration = false;
Class clazz = getServiceClass();
if ( clazz != null ) {
try {
Class[] paramTypes = { XRegistryKey.class };
Object[] params = { regKey };
Method method = null;
try {
method = clazz.getMethod("__writeRegistryServiceInfo", paramTypes);
}
catch (NoSuchMethodException noSuchMethodEx) {
method = null;
}
catch (SecurityException securityEx) {
method = null;
}
if (method == null) {
method = clazz.getMethod("writeRegistryServiceInfo", paramTypes);
}
Object oRet = method.invoke(clazz, params);
if ( (oRet != null) && (oRet instanceof Boolean) )
success = ((Boolean) oRet).booleanValue();
}
catch (NoSuchMethodException noSuchMethodEx) {
defaultRegistration = true;
}
catch (SecurityException securityEx) {
defaultRegistration = true;
}
catch (IllegalAccessException e) {
throw new CannotRegisterImplementationException("Can not register " + implName + " because " + e.toString() );
}
catch (IllegalArgumentException e) {
throw new CannotRegisterImplementationException("Can not register " + implName + " because " + e.toString() );
}
catch (InvocationTargetException e) {
throw new CannotRegisterImplementationException("Can not register " + implName + " because " + e.getTargetException() );
}
if (defaultRegistration) {
try {
XRegistryKey newKey = regKey.createKey("/" + implName + "/UNO/SERVICES");
String names[] = getServiceNames();
for (int i=0; i<names.length; i++)
newKey.createKey( names[i] );
success = true;
}
catch (com.sun.star.registry.InvalidRegistryException e) {
throw new CannotRegisterImplementationException("Can not register " + implName + " because " + e.toString() );
}
}
}
return success;
}
/**
* Supplies the factory for the component. First it is verified if component includes the optional
* method __getServiceFactory ( getServiceFactory - DEPRECATED ). If so the call is delegated
* to this method. Otherwise the ComponentFactoryWrapper is returned.
* <p>
* @return the factory for the component
* @param impName the components implementation name
* @param multiFac specifices the ServiceManager
* @param regKey only used if the call is delegated
*/
public Object getServiceFactory( String impName,
XMultiServiceFactory multiFac,
XRegistryKey regKey)
{
aServiceManager = multiFac;
try {
Class clazz = getServiceClass();
Class[] paramTypes = { String.class, XMultiServiceFactory.class, XRegistryKey.class};
Object[] params = { impName, multiFac, regKey };
// try to get factory form implemetation class
Method method = null;
try {
method = clazz.getMethod("__getServiceFactory", paramTypes);
}
catch (NoSuchMethodException noSuchMethodEx) {
method = null;
}
catch (SecurityException securityEx) {
method = null;
}
if (method == null) {
method = clazz.getMethod("getServiceFactory", paramTypes);
}
return UnoRuntime.queryInterface( XSingleServiceFactory.class,method.invoke(clazz, params) );
}
catch (NoSuchMethodException noSuchMethodEx) {
}
catch (SecurityException securityEx) {
}
catch (IllegalAccessException e) {
}
catch (IllegalArgumentException e) {
}
catch (InvocationTargetException e) {
}
// if any execption occurred this will be returned
return this;
}
/**
* set the ServiceManager at the component,
* if the method __setSericeFactory( XMultiServiceFactor ) is found
*/
/**
* Set the <code>ServiceManager</code> at the component. For that the component must support
* the <code>__setServiceManager</code> methode. The method is called after a new instance of the
* component is created.
* <p>
* @param obj the newly created component
* @see createInstanceWithArguments
* @see createInstance
*/
private void setServiceManager( Object obj ) {
Class clazz = getServiceClass();
Class paramTypes[] = { XMultiServiceFactory.class };
Object[] args = { aServiceManager };
try {
clazz.getDeclaredMethod( "__setServiceManager", paramTypes).invoke(obj, args);
}
catch (NoSuchMethodException e) {}
catch (SecurityException e) {}
catch (IllegalAccessException e) {}
catch (IllegalArgumentException e) {}
catch (InvocationTargetException e) {}
}
/**
* Tries to instantiate a component by its implementation name. For it the Beans.instantiate
* method is called. If any exception occured a com.sun.star.uno.Exception will be thrown.
* <p>
* @return
* @see java.beans.Beans
*/
private Object createInstanceWithDefaultConstructor()
throws com.sun.star.uno.Exception,
com.sun.star.uno.RuntimeException
{
Object resObj = null;
try {
DEBUG ("try to instantiate " + implName );
resObj = java.beans.Beans.instantiate(getClass().getClassLoader(), implName);
}
catch (IOException e) {
if (DEBUG) e.printStackTrace();
throw new com.sun.star.uno.Exception("Can not create an instance of " + implName + "\n" +
e.toString());
}
catch (ClassNotFoundException e) {
if (DEBUG) e.printStackTrace();
throw new com.sun.star.uno.Exception("Can not create an instance of " + implName + "\n" +
e.toString());
}
catch (NoSuchMethodError e) {
if (DEBUG) e.printStackTrace();
throw new com.sun.star.uno.Exception("Can not create an instance of " + implName + "\n" +
e.toString());
}
return resObj;
}
/**
* Creates a new instance of the component.
* <p>
* @return newly instanciated component
* @see com.sun.star.lang.XSimpleServiceFactory
*/
public Object createInstance()
throws com.sun.star.uno.Exception,
com.sun.star.uno.RuntimeException
{
Object resObj = createInstanceWithDefaultConstructor();
setServiceManager( resObj );
return resObj;
}
/**
* Creates a new component with arguments. How the arguments are commited depends on the component.
* For that the following order is used:
* 1. The component supports the XInitialization interface: An object is created using Beans.instantiate.
* The arguments are set with the <code>initialize</code> method.
* 2. The component has a constructor with an array of objects as a argument.
* 3. The component has any other constructor which can take the arguments.
* <p>
* @return newly created component
* @param args the arguments which should be used
*/
public Object createInstanceWithArguments( Object[] args )
throws com.sun.star.uno.Exception,
com.sun.star.uno.RuntimeException
{
java.lang.Object resObj = null;
boolean useDefaultCtor = false;
// first we try if the component supports the XInitialization interface
Class clazz = getServiceClass();
if ( clazz.isAssignableFrom( XInitialization.class ) )
useDefaultCtor = true;
else {
try {
Class parameterTypes[] = { Class.class, Object.class };
Method queryMeth = clazz.getMethod("queryInterface", parameterTypes );
useDefaultCtor = true;
}
catch (NoSuchMethodException e) {}
catch (SecurityException e) {}
}
if (useDefaultCtor) {
resObj = createInstanceWithDefaultConstructor();
XInitialization iniObj = (XInitialization) UnoRuntime.queryInterface( XInitialization.class, resObj );
iniObj.initialize( args );
}
else {
// try the constructor A(java.lang.Object[])
Class[] clazzParams = { Object[].class };
java.lang.reflect.Constructor ctor = null;
try {
ctor = clazz.getConstructor( clazzParams );
}
catch (NoSuchMethodException noSuchMethodEx) {
ctor = null;
}
catch (SecurityException securityEx) {
ctor = null;
}
if (ctor == null) {
// look for the first matching constructor
java.lang.reflect.Constructor ctors[] = clazz.getDeclaredConstructors();
int i=0;
while ( (i<ctors.length) && (resObj==null) ) {
try {
resObj = ctors[i++].newInstance( args );
}
catch (InstantiationException instantiationEx) {}
catch (IllegalAccessException illegalAccessEx) {}
catch (IllegalArgumentException illegalArgumentEx) {}
catch (InvocationTargetException invocationTargetEx) {}
}
if (resObj == null)
throw new com.sun.star.uno.Exception( "Can not create instance with arguments for " + implName );
}
else
{
try {
resObj = ctor.newInstance( args );
}
catch (InstantiationException e) {
throw new com.sun.star.uno.Exception("Can not create instance with arguments for "
+ implName + " because " + e.toString() );
}
catch (IllegalAccessException e) {
throw new com.sun.star.uno.Exception("Can not create instance with arguments for "
+ implName + " because " + e.toString() );
}
catch (IllegalArgumentException e) {
throw new com.sun.star.uno.Exception("Can not create instance with arguments for "
+ implName + " because " + e.toString() );
}
catch (InvocationTargetException e) {
throw new com.sun.star.uno.Exception("Can not create instance with arguments for "
+ implName + " because " + e.getTargetException() );
}
}
setServiceManager( resObj );
}
return resObj;
}
/**
* Supplies the implementation name of the component.
* <p>
* @return the name of the implementation
* @see com.sun.star.lang.XServiceInfo
*/
public String getImplementationName()
throws com.sun.star.uno.RuntimeException
{
return implName;
}
/**
* Verifies whether or not a service is supportet by the component.
* <p>
* @return true if the service is supported - otherwise false
* @param requestedService name of the requested service
*/
public boolean supportsService(String requestedService)
throws com.sun.star.uno.RuntimeException
{
boolean found = false;
int i = 0;
if (requestedService == null)
throw new com.sun.star.uno.RuntimeException("no service requested");
String names[] = getServiceNames();
if (names.length == 0)
throw new com.sun.star.uno.RuntimeException("no service name found");
while (i<names.length && !found)
found = names[i++].equals(requestedService);
return found;
}
/**
* Provides a list of all supported services by the component.
* <p>
* @return list of the service names which are supported by the component
* @see com.sun.star.lang.XServiceInfo
*/
public String[] getSupportedServiceNames()
throws com.sun.star.uno.RuntimeException
{
return getServiceNames();
}
/**
* Supplies the class of the component. If the location URL is specified the
* <code>com.sun.star.comp.loader.RegistrationClassFinder</code> is used to obtain the class.
* Otherwise the <code>java.lang.Class.forName</code> is called with implementation name.
* <p>
* @return the class of the component
* @see com.sun.star.comp.loader.RegistrationClassFinder
*/
Class getServiceClass()
throws com.sun.star.uno.RuntimeException
{
if (serviceClass == null) {
try {
if ( locationUrl != null ) {
RegistrationClassFinder classFinder = new RegistrationClassFinder( locationUrl );
serviceClass = classFinder.getRegistrationClass();
}
else {
serviceClass = Class.forName( implName );
}
}
catch ( java.net.MalformedURLException e ) {
throw new com.sun.star.uno.RuntimeException( "can't get the class " + implName + ".\n" + e );
}
catch ( java.io.IOException e ) {
throw new com.sun.star.uno.RuntimeException( "can't get the class " + implName + ".\n" + e );
}
catch ( java.lang.ClassNotFoundException e ) {
throw new com.sun.star.uno.RuntimeException( "can't get the class " + implName + ".\n" + e );
}
}
return serviceClass;
}
/**
* Extract the service names supported by the component. The optional static member
* <code>__serviceName</code> ( <code>serviceName</code> DEPRECATED ) is used to specify the supported
* service names. If no member can be found the implementation name is return.
* <p>
* @return a list with the supported service names
*/
public String[] getServiceNames()
throws com.sun.star.uno.RuntimeException
{
String result[] = null;
try {
Class clazz = getServiceClass();
Object attrib = null;
try {
attrib = clazz.getDeclaredField("__serviceName").get(clazz);
} catch (NoSuchFieldException e) {
//********************* DEPRECATED ******************************************
attrib = clazz.getDeclaredField("serviceName").get(clazz);
//***************************************************************************
}
if ( attrib instanceof String ) {
String str = (String) attrib;
result = new String[1];
result[0] = str;
} else
result = (String[]) attrib; // expecting an array of Strings - otherwise an exception will be thrown
}
catch (NoSuchFieldException e) {}
catch (SecurityException e) {}
catch (IllegalAccessException e) {}
if (result == null) {
result = new String[1];
result[0] = implName;
}
return result;
}
public String toString() {
String result = super.toString();
result += " implementation name: " +getImplementationName();
result += " supported services:";
String[] services = getSupportedServiceNames();
for (int i=0; i<services.length; i++)
result += " " + services[i];
return result;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment