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