xref: /AOO41X/main/qadevOOo/testdocs/backend/org/openoffice/JavaSystemBackend.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package org.openoffice;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
30*cdf0e10cSrcweir import com.sun.star.configuration.backend.PropertyInfo;
31*cdf0e10cSrcweir import com.sun.star.configuration.backend.XLayer;
32*cdf0e10cSrcweir import com.sun.star.configuration.backend.XLayerContentDescriber;
33*cdf0e10cSrcweir import com.sun.star.configuration.backend.XLayerHandler;
34*cdf0e10cSrcweir import com.sun.star.configuration.backend.XSingleLayerStratum;
35*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
36*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
37*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
38*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
39*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
40*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
41*cdf0e10cSrcweir import com.sun.star.uno.Type;
42*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
43*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
44*cdf0e10cSrcweir import com.sun.star.util.XStringSubstitution;
45*cdf0e10cSrcweir import com.sun.star.util.XTimeStamped;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /**
49*cdf0e10cSrcweir  *
50*cdf0e10cSrcweir  * @author  sw93809
51*cdf0e10cSrcweir  */
52*cdf0e10cSrcweir public class JavaSystemBackend implements XSingleLayerStratum, XTypeProvider,
53*cdf0e10cSrcweir                                           XServiceInfo, XTimeStamped, XComponent {
54*cdf0e10cSrcweir     public final static String __serviceName = "com.sun.star.configuration.backend.PlatformBackend";
55*cdf0e10cSrcweir     public final static String __implName = "org.openoffice.JavaSystemBackend";
56*cdf0e10cSrcweir     public final static String testComponent = "org.openoffice.Office.Common";
57*cdf0e10cSrcweir     protected static XMultiServiceFactory msf = null;
58*cdf0e10cSrcweir     protected XLayer aLayer = null;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     /**
61*cdf0e10cSrcweir      * Get the implementation id.
62*cdf0e10cSrcweir      * @return An empty implementation id.
63*cdf0e10cSrcweir      * @see com.sun.star.lang.XTypeProvider
64*cdf0e10cSrcweir      */
65*cdf0e10cSrcweir     public byte[] getImplementationId() {
66*cdf0e10cSrcweir         return new byte[0];
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /**
70*cdf0e10cSrcweir      * Function for reading the implementation name.
71*cdf0e10cSrcweir      *
72*cdf0e10cSrcweir      * @return the implementation name
73*cdf0e10cSrcweir      * @see com.sun.star.lang.XServiceInfo
74*cdf0e10cSrcweir      */
75*cdf0e10cSrcweir     public String getImplementationName() {
76*cdf0e10cSrcweir         return __implName;
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     public com.sun.star.configuration.backend.XLayer getLayer(String str,
80*cdf0e10cSrcweir                                                               String str1)
81*cdf0e10cSrcweir         throws com.sun.star.configuration.backend.BackendAccessException,
82*cdf0e10cSrcweir                com.sun.star.lang.IllegalArgumentException {
83*cdf0e10cSrcweir         if (aLayer == null) {
84*cdf0e10cSrcweir             System.out.println("JavaSystemBackend::getLayer() called for " +
85*cdf0e10cSrcweir                                str);
86*cdf0e10cSrcweir             aLayer = new CommonLayer();
87*cdf0e10cSrcweir         }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         return aLayer;
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     /**
93*cdf0e10cSrcweir      * Function for reading all supported services
94*cdf0e10cSrcweir      *
95*cdf0e10cSrcweir      * @return An aaray with all supported service names
96*cdf0e10cSrcweir      * @see com.sun.star.lang.XServiceInfo
97*cdf0e10cSrcweir      */
98*cdf0e10cSrcweir     public String[] getSupportedServiceNames() {
99*cdf0e10cSrcweir         String[] supServiceNames = { __serviceName };
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         return supServiceNames;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     public String getTimestamp() {
105*cdf0e10cSrcweir         //not really implemented
106*cdf0e10cSrcweir         return "2004-03-31";
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     /**
110*cdf0e10cSrcweir      * Get all implemented types of this class.
111*cdf0e10cSrcweir      * @return An array of implemented interface types.
112*cdf0e10cSrcweir      * @see com.sun.star.lang.XTypeProvider
113*cdf0e10cSrcweir      */
114*cdf0e10cSrcweir     public Type[] getTypes() {
115*cdf0e10cSrcweir         Type[] type = new Type[5];
116*cdf0e10cSrcweir         type[0] = new Type(XInterface.class);
117*cdf0e10cSrcweir         type[1] = new Type(XTypeProvider.class);
118*cdf0e10cSrcweir         type[2] = new Type(XSingleLayerStratum.class);
119*cdf0e10cSrcweir         type[3] = new Type(XServiceInfo.class);
120*cdf0e10cSrcweir         type[4] = new Type(XTimeStamped.class);
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         return type;
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     public com.sun.star.configuration.backend.XUpdatableLayer getUpdatableLayer(String str)
126*cdf0e10cSrcweir         throws com.sun.star.configuration.backend.BackendAccessException,
127*cdf0e10cSrcweir                com.sun.star.lang.NoSupportException,
128*cdf0e10cSrcweir                com.sun.star.lang.IllegalArgumentException {
129*cdf0e10cSrcweir         throw new com.sun.star.lang.NoSupportException(
130*cdf0e10cSrcweir                 "Cannot write to test backend", this);
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     /**
134*cdf0e10cSrcweir      * Does the implementation support this service?
135*cdf0e10cSrcweir      *
136*cdf0e10cSrcweir      * @param serviceName The name of the service in question
137*cdf0e10cSrcweir      * @return true, if service is supported, false otherwise
138*cdf0e10cSrcweir      * @see com.sun.star.lang.XServiceInfo
139*cdf0e10cSrcweir      */
140*cdf0e10cSrcweir     public boolean supportsService(String serviceName) {
141*cdf0e10cSrcweir         if (serviceName.equals(__serviceName)) {
142*cdf0e10cSrcweir             return true;
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         return false;
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     /**
149*cdf0e10cSrcweir     *
150*cdf0e10cSrcweir     * Gives a factory for creating the service.
151*cdf0e10cSrcweir     * This method is called by the <code>JavaLoader</code>
152*cdf0e10cSrcweir     * <p>
153*cdf0e10cSrcweir     * @return  returns a <code>XSingleServiceFactory</code> for creating the component
154*cdf0e10cSrcweir     * @param   implName     the name of the implementation for which a service is desired
155*cdf0e10cSrcweir     * @param   multiFactory the service manager to be used if needed
156*cdf0e10cSrcweir     * @param   regKey       the registryKey
157*cdf0e10cSrcweir     * @see                  com.sun.star.comp.loader.JavaLoader
158*cdf0e10cSrcweir     */
159*cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(String implName,
160*cdf0e10cSrcweir                                                             XMultiServiceFactory multiFactory,
161*cdf0e10cSrcweir                                                             XRegistryKey regKey) {
162*cdf0e10cSrcweir         XSingleServiceFactory xSingleServiceFactory = null;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir         msf = multiFactory;
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir         if (implName.equals(JavaSystemBackend.class.getName())) {
167*cdf0e10cSrcweir             xSingleServiceFactory = FactoryHelper.getServiceFactory(
168*cdf0e10cSrcweir                                             JavaSystemBackend.class,
169*cdf0e10cSrcweir                                             __serviceName, multiFactory,
170*cdf0e10cSrcweir                                             regKey);
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         return xSingleServiceFactory;
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     /**
177*cdf0e10cSrcweir      * Writes the service information into the given registry key.
178*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
179*cdf0e10cSrcweir      * <p>
180*cdf0e10cSrcweir      * @return  returns true if the operation succeeded
181*cdf0e10cSrcweir      * @param   regKey       the registryKey
182*cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
183*cdf0e10cSrcweir      */
184*cdf0e10cSrcweir     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
185*cdf0e10cSrcweir         boolean success = FactoryHelper.writeRegistryServiceInfo(__implName,
186*cdf0e10cSrcweir                                                                  __serviceName,
187*cdf0e10cSrcweir                                                                  regKey);
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         if (success) {
190*cdf0e10cSrcweir             try {
191*cdf0e10cSrcweir                 String keyName = "/" + __implName +
192*cdf0e10cSrcweir                                  "/DATA/SupportedComponents";
193*cdf0e10cSrcweir                 XRegistryKey newKey = regKey.createKey(keyName);
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir                 String[] supportedComponents = { testComponent };
196*cdf0e10cSrcweir                 newKey.setAsciiListValue(supportedComponents);
197*cdf0e10cSrcweir             } catch (Exception ex) {
198*cdf0e10cSrcweir                 success = false; // prevent startup loop
199*cdf0e10cSrcweir                 System.out.println("can't register component");
200*cdf0e10cSrcweir             }
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir         return success;
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     public void addEventListener(com.sun.star.lang.XEventListener xEventListener) {
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     public void dispose() {
210*cdf0e10cSrcweir         System.out.println("JavaSystemBackend::dispose() called");
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     public void removeEventListener(com.sun.star.lang.XEventListener xEventListener) {
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     protected class CommonLayer implements XLayer, XTimeStamped {
217*cdf0e10cSrcweir         public void readData(XLayerHandler xLayerHandler)
218*cdf0e10cSrcweir                       throws com.sun.star.lang.NullPointerException,
219*cdf0e10cSrcweir                              com.sun.star.lang.WrappedTargetException,
220*cdf0e10cSrcweir                              com.sun.star.configuration.backend.MalformedDataException {
221*cdf0e10cSrcweir             System.out.println("CommonLayer is read");
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir             if (xLayerHandler == null) {
224*cdf0e10cSrcweir                 throw new com.sun.star.lang.NullPointerException(
225*cdf0e10cSrcweir                         "null is not a legal LayerHandler");
226*cdf0e10cSrcweir             }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir             XLayerContentDescriber xLayerContentDescriber = null;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir             try {
231*cdf0e10cSrcweir                 xLayerContentDescriber = (XLayerContentDescriber) UnoRuntime.queryInterface(
232*cdf0e10cSrcweir                                                  XLayerContentDescriber.class,
233*cdf0e10cSrcweir                                                  msf.createInstance(
234*cdf0e10cSrcweir                                                          "com.sun.star.comp.configuration.backend.LayerDescriber"));
235*cdf0e10cSrcweir             } catch (com.sun.star.uno.Exception e) {
236*cdf0e10cSrcweir                 throw new com.sun.star.lang.NullPointerException(
237*cdf0e10cSrcweir                         "exception while creating LayerDesccriber");
238*cdf0e10cSrcweir             }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir             if (xLayerContentDescriber == null) {
241*cdf0e10cSrcweir                 throw new com.sun.star.lang.NullPointerException(
242*cdf0e10cSrcweir                         "created LayerDescriber isn't valid");
243*cdf0e10cSrcweir             }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir             PropertyInfo[] pInfo = new PropertyInfo[1];
246*cdf0e10cSrcweir             pInfo[0] = new PropertyInfo();
247*cdf0e10cSrcweir             pInfo[0].Name = "org.openoffice.Office.Common/Undo/Steps";
248*cdf0e10cSrcweir             pInfo[0].Value = new Integer(12);
249*cdf0e10cSrcweir             pInfo[0].Type = "int";
250*cdf0e10cSrcweir             pInfo[0].Protected = false;
251*cdf0e10cSrcweir             xLayerContentDescriber.describeLayer(xLayerHandler, pInfo);
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         public String getTimestamp() {
255*cdf0e10cSrcweir             //not really implemented
256*cdf0e10cSrcweir             return "2004-03-31";
257*cdf0e10cSrcweir         }
258*cdf0e10cSrcweir     }
259*cdf0e10cSrcweir }