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