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