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.lib.uno.helper.Factory;
36*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
37*cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
38*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase;
39*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
41*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
42*cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
43*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
44*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
45*cdf0e10cSrcweir import com.sun.star.uno.Type;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir /** This class capsulates the class, that implements the minimal component, a
48*cdf0e10cSrcweir  * factory for creating the service (<CODE>__getComponentFactory</CODE>) and a
49*cdf0e10cSrcweir  * method, that writes the information into the given registry key
50*cdf0e10cSrcweir  * (<CODE>__writeRegistryServiceInfo</CODE>).
51*cdf0e10cSrcweir  */
52*cdf0e10cSrcweir public class MinimalComponent {
53*cdf0e10cSrcweir     /** This class implements the component. At least the interfaces XServiceInfo,
54*cdf0e10cSrcweir      * XTypeProvider, and XInitialization should be provided by the service.
55*cdf0e10cSrcweir      */
56*cdf0e10cSrcweir     public static class _MinimalComponent extends WeakBase
57*cdf0e10cSrcweir         implements XInitialization, XServiceInfo {
58*cdf0e10cSrcweir         /** The service name, that must be used to get an instance of this service.
59*cdf0e10cSrcweir          */
60*cdf0e10cSrcweir         static private final String __serviceName =
61*cdf0e10cSrcweir         "org.openoffice.MinimalComponent";
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir         /** The initial component contextr, that gives access to
64*cdf0e10cSrcweir          * the service manager, supported singletons, ...
65*cdf0e10cSrcweir          * It's often later used
66*cdf0e10cSrcweir          */
67*cdf0e10cSrcweir         private XComponentContext m_cmpCtx;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir         /** The service manager, that gives access to all registered services.
70*cdf0e10cSrcweir          * It's often later used
71*cdf0e10cSrcweir          */
72*cdf0e10cSrcweir         private XMultiComponentFactory m_xMCF;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir         /** The constructor of the inner class has a XMultiServiceFactory parameter.
75*cdf0e10cSrcweir          * @param xmultiservicefactoryInitialization A special service factory
76*cdf0e10cSrcweir          * could be introduced while initializing.
77*cdf0e10cSrcweir          */
78*cdf0e10cSrcweir         public _MinimalComponent(XComponentContext xCompContext) {
79*cdf0e10cSrcweir             try {
80*cdf0e10cSrcweir                 m_cmpCtx = xCompContext;
81*cdf0e10cSrcweir                 m_xMCF = m_cmpCtx.getServiceManager();
82*cdf0e10cSrcweir             }
83*cdf0e10cSrcweir             catch( Exception e ) {
84*cdf0e10cSrcweir                 e.printStackTrace();
85*cdf0e10cSrcweir             }
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         /** This method is a member of the interface for initializing an object
89*cdf0e10cSrcweir          * directly after its creation.
90*cdf0e10cSrcweir          * @param object This array of arbitrary objects will be passed to the
91*cdf0e10cSrcweir          * component after its creation.
92*cdf0e10cSrcweir          * @throws Exception Every exception will not be handled, but will be
93*cdf0e10cSrcweir          * passed to the caller.
94*cdf0e10cSrcweir          */
95*cdf0e10cSrcweir         public void initialize( Object[] object )
96*cdf0e10cSrcweir             throws com.sun.star.uno.Exception {
97*cdf0e10cSrcweir             /* The component describes what arguments its expected and in which
98*cdf0e10cSrcweir              * order!At this point you can read the objects and can intialize
99*cdf0e10cSrcweir              * your component using these objects.
100*cdf0e10cSrcweir              */
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         /** This method returns an array of all supported service names.
104*cdf0e10cSrcweir          * @return Array of supported service names.
105*cdf0e10cSrcweir          */
106*cdf0e10cSrcweir         public String[] getSupportedServiceNames() {
107*cdf0e10cSrcweir             return getServiceNames();
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         /** This method is a simple helper function to used in the
111*cdf0e10cSrcweir          * static component initialisation functions as well as in
112*cdf0e10cSrcweir          * getSupportedServiceNames.
113*cdf0e10cSrcweir          */
114*cdf0e10cSrcweir         public static String[] getServiceNames() {
115*cdf0e10cSrcweir             String[] sSupportedServiceNames = { __serviceName };
116*cdf0e10cSrcweir             return sSupportedServiceNames;
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         /** This method returns true, if the given service will be
120*cdf0e10cSrcweir          * supported by the component.
121*cdf0e10cSrcweir          * @param sServiceName Service name.
122*cdf0e10cSrcweir          * @return True, if the given service name will be supported.
123*cdf0e10cSrcweir          */
124*cdf0e10cSrcweir         public boolean supportsService( String sServiceName ) {
125*cdf0e10cSrcweir             return sServiceName.equals( __serviceName );
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         /** Return the class name of the component.
129*cdf0e10cSrcweir          * @return Class name of the component.
130*cdf0e10cSrcweir          */
131*cdf0e10cSrcweir         public String getImplementationName() {
132*cdf0e10cSrcweir             return  _MinimalComponent.class.getName();
133*cdf0e10cSrcweir         }
134*cdf0e10cSrcweir     }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     /**
138*cdf0e10cSrcweir      * Gives a factory for creating the service.
139*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
140*cdf0e10cSrcweir      * <p>
141*cdf0e10cSrcweir      * @return  returns a <code>XSingleComponentFactory</code> for creating
142*cdf0e10cSrcweir      *          the component
143*cdf0e10cSrcweir      * @param   sImplName the name of the implementation for which a
144*cdf0e10cSrcweir      *          service is desired
145*cdf0e10cSrcweir      * @see     com.sun.star.comp.loader.JavaLoader
146*cdf0e10cSrcweir      */
147*cdf0e10cSrcweir     public static XSingleComponentFactory __getComponentFactory(String sImplName)
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         XSingleComponentFactory xFactory = null;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         if ( sImplName.equals( _MinimalComponent.class.getName() ) )
152*cdf0e10cSrcweir             xFactory = Factory.createComponentFactory(_MinimalComponent.class,
153*cdf0e10cSrcweir                                              _MinimalComponent.getServiceNames());
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         return xFactory;
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     /**
159*cdf0e10cSrcweir      * Writes the service information into the given registry key.
160*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
161*cdf0e10cSrcweir      * <p>
162*cdf0e10cSrcweir      * @return  returns true if the operation succeeded
163*cdf0e10cSrcweir      * @param   regKey the registryKey
164*cdf0e10cSrcweir      * @see     com.sun.star.comp.loader.JavaLoader
165*cdf0e10cSrcweir      */
166*cdf0e10cSrcweir     // This method not longer necessary since OOo 3.4 where the component registration
167*cdf0e10cSrcweir     // was changed to passive component registration. For more details see
168*cdf0e10cSrcweir     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir //     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
171*cdf0e10cSrcweir //         return Factory.writeRegistryServiceInfo(_MinimalComponent.class.getName(),
172*cdf0e10cSrcweir //                                                 _MinimalComponent.getServiceNames(),
173*cdf0e10cSrcweir //                                                 regKey);
174*cdf0e10cSrcweir //     }
175*cdf0e10cSrcweir }
176