1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.lang.XSingleServiceFactory;
36 import com.sun.star.lang.XServiceInfo;
37 import com.sun.star.lang.XInitialization;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.uno.Any;
42 import com.sun.star.uno.UnoRuntime;
43 
44 import java.lang.reflect.Constructor;
45 
46 //
47 // purpose of this class is to provide a service factory that instantiates
48 // the services only once (as long as this factory itself exists)
49 // and returns only reference to that instance.
50 //
51 
52 public class OneInstanceFactory implements
53         XSingleServiceFactory,
54         XServiceInfo
55 {
56     Class       aMyClass;
57     String      aSvcImplName;
58     String[]    aSupportedSvcNames;
59     XInterface  xInstantiatedService;
60     XMultiServiceFactory    xMultiFactory;
61 
62     public OneInstanceFactory(
63             Class       aMyClass,
64             String      aSvcImplName,
65             String[]    aSupportedSvcNames,
66             XMultiServiceFactory    xMultiFactory )
67     {
68         this.aMyClass           = aMyClass;
69         this.aSvcImplName       = aSvcImplName;
70         this.aSupportedSvcNames = aSupportedSvcNames;
71         this.xMultiFactory      = xMultiFactory;
72         xInstantiatedService = null;
73     }
74 
75     //**********************
76     // XSingleServiceFactory
77     //**********************
78     public Object createInstance()
79         throws com.sun.star.uno.Exception,
80                com.sun.star.uno.RuntimeException
81     {
82         if (xInstantiatedService == null)
83         {
84             //!! the here used services all have exact one constructor!!
85             Constructor [] aCtor = aMyClass.getConstructors();
86             try {
87                 xInstantiatedService = (XInterface) aCtor[0].newInstance( (Object[])null );
88             }
89             catch( Exception e ) {
90             }
91 
92             //!! workaround for services not always being created
93             //!! via 'createInstanceWithArguments'
94             XInitialization xIni = (XInitialization) UnoRuntime.queryInterface(
95                 XInitialization.class, createInstance());
96             if (xIni != null)
97             {
98                 Object[] aArguments = new Object[]{ null, null };
99                 if (xMultiFactory != null)
100                 {
101                     XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
102                         XPropertySet.class ,  xMultiFactory.createInstance(
103                             "com.sun.star.linguistic2.LinguProperties" ) );
104                     aArguments[0] = xPropSet;
105                 }
106                 xIni.initialize( aArguments );
107             }
108         }
109         return xInstantiatedService;
110     }
111 
112     public Object createInstanceWithArguments( Object[] aArguments )
113         throws com.sun.star.uno.Exception,
114                com.sun.star.uno.RuntimeException
115     {
116         if (xInstantiatedService == null)
117         {
118             XInitialization xIni = (XInitialization) UnoRuntime.queryInterface(
119                 XInitialization.class, createInstance());
120             if (xIni != null)
121                 xIni.initialize( aArguments );
122         }
123         return xInstantiatedService;
124     }
125 
126 
127     //*************
128     // XServiceInfo
129     //*************
130     public boolean supportsService( String aServiceName )
131         throws com.sun.star.uno.RuntimeException
132     {
133         boolean bFound = false;
134         int nCnt = aSupportedSvcNames.length;
135         for (int i = 0;  i < nCnt && !bFound;  ++i)
136         {
137             if (aServiceName.equals( aSupportedSvcNames[i] ))
138                 bFound = true;
139         }
140         return bFound;
141     }
142 
143     public String getImplementationName()
144         throws com.sun.star.uno.RuntimeException
145     {
146         return aSvcImplName;
147     }
148 
149     public String[] getSupportedServiceNames()
150         throws com.sun.star.uno.RuntimeException
151     {
152         return aSupportedSvcNames;
153     }
154 };
155 
156