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