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