1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package integration.extensions;
25 
26 import com.sun.star.uno.XComponentContext;
27 import com.sun.star.lang.XSingleComponentFactory;
28 import java.lang.reflect.Constructor;
29 
30 /**
31  *
32  * @author fs93730
33  */
34 public class ComponentFactory implements XSingleComponentFactory
35 {
36     private Class       m_handlerClass;
37     private Constructor m_defaultConstructor;
38     private Constructor m_initConstructor;
39 
ComponentFactory( Class _handlerClass )40     public ComponentFactory( Class _handlerClass )
41     {
42         m_handlerClass = _handlerClass;
43 
44         Class objectArrayClass = null;
45         try
46         {
47             objectArrayClass = Class.forName("[Ljava.lang.Object;");
48         }
49         catch ( java.lang.ClassNotFoundException e ) { }
50 
51         Constructor ctors[] = _handlerClass.getConstructors();
52         for ( int i = 0; i < ctors.length && ctors != null; ++i)
53         {
54             Class ctorParams[] = ctors[i].getParameterTypes();
55             if ( ( ctorParams.length == 1 ) && ( ctorParams[0].equals( XComponentContext.class ) ) )
56                 m_defaultConstructor = ctors[i];
57             if  (   ( ctorParams.length == 2 )
58                 &&  ( ctorParams[0].equals( XComponentContext.class ) )
59                 &&  ( ctorParams[1].equals( objectArrayClass ) )
60                 )
61                 m_initConstructor = ctors[i];
62         }
63         if ( m_defaultConstructor == null )
64             throw new java.lang.IllegalArgumentException();
65     }
66 
ipml_createInstance( Constructor _ctor, Object[] _arguments )67     private Object ipml_createInstance( Constructor _ctor, Object[] _arguments )
68     {
69         Object newInstance = null;
70         try
71         {
72             newInstance = _ctor.newInstance( _arguments );
73         }
74         catch( InstantiationException e )
75         {
76             System.err.println( "InstantiationException: Could not instantiate an instance of " + m_handlerClass.getName() );
77         }
78         catch( IllegalAccessException e )
79         {
80             System.err.println( "IllegalAccessException: Could not instantiate an instance of " + m_handlerClass.getName() );
81         }
82         catch( java.lang.reflect.InvocationTargetException e )
83         {
84             System.err.println( "InvocationTargetException: Could not instantiate an instance of " + m_handlerClass.getName() );
85         }
86         return newInstance;
87     }
88 
createInstanceWithArgumentsAndContext(Object[] _arguments, XComponentContext _componentContext)89     public Object createInstanceWithArgumentsAndContext(Object[] _arguments, XComponentContext _componentContext) throws com.sun.star.uno.Exception
90     {
91         if ( m_initConstructor != null )
92             return ipml_createInstance( m_initConstructor, new Object[] { _componentContext, _arguments } );
93         else
94             return createInstanceWithContext( _componentContext );
95     }
96 
createInstanceWithContext(XComponentContext _componentContext)97     public Object createInstanceWithContext(XComponentContext _componentContext) throws com.sun.star.uno.Exception
98     {
99         return ipml_createInstance( m_defaultConstructor, new Object[] { _componentContext } );
100     }
101 }
102 
103