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 import com.sun.star.lang.XSingleServiceFactory;
25 import com.sun.star.lang.XServiceInfo;
26 import com.sun.star.lang.XInitialization;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.beans.XPropertySet;
29 import com.sun.star.uno.XInterface;
30 import com.sun.star.uno.Any;
31 import com.sun.star.uno.UnoRuntime;
32 
33 import java.lang.reflect.Constructor;
34 
35 //
36 // purpose of this class is to provide a service factory that instantiates
37 // the services only once (as long as this factory itself exists)
38 // and returns only reference to that instance.
39 //
40 
41 public class OneInstanceFactory implements
42         XSingleServiceFactory,
43         XServiceInfo
44 {
45     Class       aMyClass;
46     String      aSvcImplName;
47     String[]    aSupportedSvcNames;
48     XInterface  xInstantiatedService;
49     XMultiServiceFactory    xMultiFactory;
50 
OneInstanceFactory( Class aMyClass, String aSvcImplName, String[] aSupportedSvcNames, XMultiServiceFactory xMultiFactory )51     public OneInstanceFactory(
52             Class       aMyClass,
53             String      aSvcImplName,
54             String[]    aSupportedSvcNames,
55             XMultiServiceFactory    xMultiFactory )
56     {
57         this.aMyClass           = aMyClass;
58         this.aSvcImplName       = aSvcImplName;
59         this.aSupportedSvcNames = aSupportedSvcNames;
60         this.xMultiFactory      = xMultiFactory;
61         xInstantiatedService = null;
62     }
63 
64     //**********************
65     // XSingleServiceFactory
66     //**********************
createInstance()67     public Object createInstance()
68         throws com.sun.star.uno.Exception,
69                com.sun.star.uno.RuntimeException
70     {
71         if (xInstantiatedService == null)
72         {
73             //!! the here used services all have exact one constructor!!
74             Constructor [] aCtor = aMyClass.getConstructors();
75             try {
76                 xInstantiatedService = (XInterface) aCtor[0].newInstance( (Object[])null );
77             }
78             catch( Exception e ) {
79             }
80 
81             //!! workaround for services not always being created
82             //!! via 'createInstanceWithArguments'
83             XInitialization xIni = (XInitialization) UnoRuntime.queryInterface(
84                 XInitialization.class, createInstance());
85             if (xIni != null)
86             {
87                 Object[] aArguments = new Object[]{ null, null };
88                 if (xMultiFactory != null)
89                 {
90                     XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
91                         XPropertySet.class ,  xMultiFactory.createInstance(
92                             "com.sun.star.linguistic2.LinguProperties" ) );
93                     aArguments[0] = xPropSet;
94                 }
95                 xIni.initialize( aArguments );
96             }
97         }
98         return xInstantiatedService;
99     }
100 
createInstanceWithArguments( Object[] aArguments )101     public Object createInstanceWithArguments( Object[] aArguments )
102         throws com.sun.star.uno.Exception,
103                com.sun.star.uno.RuntimeException
104     {
105         if (xInstantiatedService == null)
106         {
107             XInitialization xIni = (XInitialization) UnoRuntime.queryInterface(
108                 XInitialization.class, createInstance());
109             if (xIni != null)
110                 xIni.initialize( aArguments );
111         }
112         return xInstantiatedService;
113     }
114 
115 
116     //*************
117     // XServiceInfo
118     //*************
supportsService( String aServiceName )119     public boolean supportsService( String aServiceName )
120         throws com.sun.star.uno.RuntimeException
121     {
122         boolean bFound = false;
123         int nCnt = aSupportedSvcNames.length;
124         for (int i = 0;  i < nCnt && !bFound;  ++i)
125         {
126             if (aServiceName.equals( aSupportedSvcNames[i] ))
127                 bFound = true;
128         }
129         return bFound;
130     }
131 
getImplementationName()132     public String getImplementationName()
133         throws com.sun.star.uno.RuntimeException
134     {
135         return aSvcImplName;
136     }
137 
getSupportedServiceNames()138     public String[] getSupportedServiceNames()
139         throws com.sun.star.uno.RuntimeException
140     {
141         return aSupportedSvcNames;
142     }
143 };
144 
145