1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.cmp;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.io.XPersistObject;
26cdf0e10cSrcweir import com.sun.star.io.XObjectInputStream;
27cdf0e10cSrcweir import com.sun.star.io.XObjectOutputStream;
28cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
29cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
30cdf0e10cSrcweir import com.sun.star.beans.Property;
31cdf0e10cSrcweir import com.sun.star.beans.XPropertyChangeListener;
32cdf0e10cSrcweir import com.sun.star.beans.XVetoableChangeListener;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
34cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
35cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
36cdf0e10cSrcweir import com.sun.star.uno.XInterface;
37cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
38cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
39cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
40cdf0e10cSrcweir import com.sun.star.uno.Type;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /**
43cdf0e10cSrcweir  * Class MyPersistObject implements an XPersistObject, XServiceInfo,
44cdf0e10cSrcweir  * XTypeProvider and XPropertySet.
45cdf0e10cSrcweir  *
46cdf0e10cSrcweir  * Warning: In XPropertySet only the following methods that are
47cdf0e10cSrcweir  *          used for testing are really implemented:
48cdf0e10cSrcweir  *
49cdf0e10cSrcweir  *          - public XPropertySetInfo getPropertySetInfo()
50cdf0e10cSrcweir  *          - public void setPropertyValue(String property, Object value)
51cdf0e10cSrcweir  *          - public Object getPropertyValue(String property)
52cdf0e10cSrcweir  */
53cdf0e10cSrcweir public class MyPersistObject implements XPersistObject, XTypeProvider,
54cdf0e10cSrcweir                                                 XServiceInfo, XPropertySet {
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     private class MyPropertySetInfo implements XPropertySetInfo {
57cdf0e10cSrcweir         Property[] _props;
MyPropertySetInfo(Property[] props)58cdf0e10cSrcweir         public MyPropertySetInfo(Property[] props) {
59cdf0e10cSrcweir             _props = props;
60cdf0e10cSrcweir         }
getProperties()61cdf0e10cSrcweir         public Property[] getProperties() {
62cdf0e10cSrcweir             return _props;
63cdf0e10cSrcweir         }
getPropertyByName(String name)64cdf0e10cSrcweir         public Property getPropertyByName(String name) {
65cdf0e10cSrcweir             int i = getPropertyIndexByName(name);
66cdf0e10cSrcweir             return i>0?_props[i]:null;
67cdf0e10cSrcweir         }
getPropertyIndexByName(String name)68cdf0e10cSrcweir         public int getPropertyIndexByName(String name) {
69cdf0e10cSrcweir             for ( int i=0; i<_props.length; i++ )
70cdf0e10cSrcweir                 if (name.equals(_props[i].Name))
71cdf0e10cSrcweir                     return i;
72cdf0e10cSrcweir             return -1;
73cdf0e10cSrcweir         }
hasPropertyByName(String name)74cdf0e10cSrcweir         public boolean hasPropertyByName(String name) {
75cdf0e10cSrcweir             int i = getPropertyIndexByName(name);
76cdf0e10cSrcweir             return i>0?true:false;
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     static private final boolean verbose = false;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     static public final String __serviceName =
83cdf0e10cSrcweir                                         "com.sun.star.cmp.PersistObject";
84cdf0e10cSrcweir     static public final String __implName =
85cdf0e10cSrcweir                                         "com.sun.star.cmp.MyPersistObject";
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     // lots of props to write
88cdf0e10cSrcweir     Property[] props;
89cdf0e10cSrcweir     private byte by;
90cdf0e10cSrcweir     private int i;
91cdf0e10cSrcweir     private char c;
92cdf0e10cSrcweir     private double d;
93cdf0e10cSrcweir     private float f;
94cdf0e10cSrcweir     private short s;
95cdf0e10cSrcweir     private String st;
96cdf0e10cSrcweir     // property set info
97cdf0e10cSrcweir     XPropertySetInfo xInfo;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     /**
100cdf0e10cSrcweir      * Constructor: sets all properties
101cdf0e10cSrcweir      **/
MyPersistObject()102cdf0e10cSrcweir     public MyPersistObject() {
103cdf0e10cSrcweir         int prop_count = 7;
104cdf0e10cSrcweir         props = new Property[prop_count];
105cdf0e10cSrcweir         for (int i=0; i<prop_count; i++ ) {
106cdf0e10cSrcweir             props[i] = new Property();
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir         by = 1;
109cdf0e10cSrcweir         props[0].Name = "byte";
110cdf0e10cSrcweir         i = 3;
111cdf0e10cSrcweir         props[1].Name = "int";
112cdf0e10cSrcweir         c = 'c';
113cdf0e10cSrcweir         props[2].Name = "char";
114cdf0e10cSrcweir         d = 3.142;
115cdf0e10cSrcweir         props[3].Name = "double";
116cdf0e10cSrcweir         f = 2.718f;
117cdf0e10cSrcweir         props[4].Name = "float";
118cdf0e10cSrcweir         s = 1;
119cdf0e10cSrcweir         props[5].Name = "short";
120cdf0e10cSrcweir         st = "Though this be madness, yet there is method in 't.";
121cdf0e10cSrcweir         props[6].Name = "String";
122cdf0e10cSrcweir         xInfo = new MyPropertySetInfo(props);
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir     /**
125cdf0e10cSrcweir      * This function provides the service name
126cdf0e10cSrcweir      * @return the service name
127cdf0e10cSrcweir      * @see com.sun.star.io.XPersistObject
128cdf0e10cSrcweir      */
getServiceName()129cdf0e10cSrcweir     public String getServiceName() {
130cdf0e10cSrcweir         if ( verbose ) {
131cdf0e10cSrcweir             System.out.println("get service name");
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir         return __serviceName;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     /**
137cdf0e10cSrcweir      * Fuction reads properties from this input stream
138cdf0e10cSrcweir      * @param inStream the input stream
139cdf0e10cSrcweir      * @see com.sun.star.io.XPersistObject
140cdf0e10cSrcweir      */
read(XObjectInputStream inStream)141cdf0e10cSrcweir     public void read(XObjectInputStream inStream)
142cdf0e10cSrcweir                             throws com.sun.star.io.IOException {
143cdf0e10cSrcweir         s = inStream.readShort();
144cdf0e10cSrcweir         i = inStream.readLong();
145cdf0e10cSrcweir         by = inStream.readByte();
146cdf0e10cSrcweir         c = inStream.readChar();
147cdf0e10cSrcweir         d = inStream.readDouble();
148cdf0e10cSrcweir         f = inStream.readFloat();
149cdf0e10cSrcweir         st = inStream.readUTF();
150cdf0e10cSrcweir         if ( verbose )
151cdf0e10cSrcweir             System.out.println("read called" + s + " " + i + " " + st);
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     /**
155cdf0e10cSrcweir      * Fuction writes properties on this output stream
156cdf0e10cSrcweir      * @param outStream the output stream
157cdf0e10cSrcweir      * @see com.sun.star.io.XPersistObject
158cdf0e10cSrcweir      */
write(XObjectOutputStream outStream)159cdf0e10cSrcweir     public void write(XObjectOutputStream outStream)
160cdf0e10cSrcweir                             throws com.sun.star.io.IOException {
161cdf0e10cSrcweir         if ( verbose )
162cdf0e10cSrcweir             System.out.println("write called");
163cdf0e10cSrcweir         outStream.writeShort(s);
164cdf0e10cSrcweir         outStream.writeLong(i);
165cdf0e10cSrcweir         outStream.writeByte(by);
166cdf0e10cSrcweir         outStream.writeChar(c);
167cdf0e10cSrcweir         outStream.writeDouble(d);
168cdf0e10cSrcweir         outStream.writeFloat(f);
169cdf0e10cSrcweir         outStream.writeUTF(st);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     /**
175*e6b649b5SPedro Giffuni      * Function to get information about the property set.
176cdf0e10cSrcweir      * @return The information
177cdf0e10cSrcweir      * @see com.sun.star.io.XPropertySet
178cdf0e10cSrcweir      */
getPropertySetInfo()179cdf0e10cSrcweir     public XPropertySetInfo getPropertySetInfo() {
180cdf0e10cSrcweir         return xInfo;
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     /**
184cdf0e10cSrcweir      * Set a property value
185cdf0e10cSrcweir      * @param property The name of the property.
186cdf0e10cSrcweir      * @param value The new value of the property.
187cdf0e10cSrcweir      * @see com.sun.star.io.XPropertySet
188cdf0e10cSrcweir      */
setPropertyValue(String property, Object value)189cdf0e10cSrcweir     public void setPropertyValue(String property, Object value) {
190cdf0e10cSrcweir         if ( property.equals(props[0].Name))
191cdf0e10cSrcweir             by = ((Byte)value).byteValue();
192cdf0e10cSrcweir         if ( property.equals(props[1].Name))
193cdf0e10cSrcweir             i = ((Integer)value).intValue();
194cdf0e10cSrcweir         if ( property.equals(props[2].Name))
195cdf0e10cSrcweir             c = ((Character)value).charValue();
196cdf0e10cSrcweir         if ( property.equals(props[3].Name))
197cdf0e10cSrcweir             d = ((Double)value).doubleValue();
198cdf0e10cSrcweir         if ( property.equals(props[4].Name))
199cdf0e10cSrcweir             f = ((Float)value).floatValue();
200cdf0e10cSrcweir         if ( property.equals(props[5].Name))
201cdf0e10cSrcweir             s = ((Short)value).shortValue();
202cdf0e10cSrcweir         if ( property.equals(props[6].Name))
203cdf0e10cSrcweir             st = (String)value;
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     /**
207cdf0e10cSrcweir      * Get a property value
208cdf0e10cSrcweir      * @param property The property name.
209cdf0e10cSrcweir      * @return The value of the property.
210cdf0e10cSrcweir      * @see com.sun.star.io.XPropertySet
211cdf0e10cSrcweir      */
getPropertyValue(String property)212cdf0e10cSrcweir     public Object getPropertyValue(String property) {
213cdf0e10cSrcweir         if ( property.equals(props[0].Name))
214cdf0e10cSrcweir             return new Byte(by);
215cdf0e10cSrcweir         if ( property.equals(props[1].Name))
216cdf0e10cSrcweir             return new Integer(i);
217cdf0e10cSrcweir         if ( property.equals(props[2].Name))
218cdf0e10cSrcweir             return new Character(c);
219cdf0e10cSrcweir         if ( property.equals(props[3].Name))
220cdf0e10cSrcweir             return new Double(d);
221cdf0e10cSrcweir         if ( property.equals(props[4].Name))
222cdf0e10cSrcweir             return new Float(f);
223cdf0e10cSrcweir         if ( property.equals(props[5].Name))
224cdf0e10cSrcweir             return new Short(s);
225cdf0e10cSrcweir         if ( property.equals(props[6].Name))
226cdf0e10cSrcweir             return st;
227cdf0e10cSrcweir         return new Object();
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     /**
231cdf0e10cSrcweir      * Empty implementation: not needed for tests.
232cdf0e10cSrcweir      */
addPropertyChangeListener(String aPropertyName, XPropertyChangeListener xListener )233cdf0e10cSrcweir     public void addPropertyChangeListener(String aPropertyName,
234cdf0e10cSrcweir              XPropertyChangeListener xListener ) {}
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     /**
237cdf0e10cSrcweir      * Empty implementation: not needed for tests.
238cdf0e10cSrcweir      */
removePropertyChangeListener(String aPropertyName, XPropertyChangeListener aListener )239cdf0e10cSrcweir     public void removePropertyChangeListener(String aPropertyName,
240cdf0e10cSrcweir              XPropertyChangeListener aListener ) {}
241cdf0e10cSrcweir 
242cdf0e10cSrcweir     /**
243cdf0e10cSrcweir      * Empty implementation: not needed for tests.
244cdf0e10cSrcweir      */
addVetoableChangeListener(String PropertyName, XVetoableChangeListener aListener )245cdf0e10cSrcweir     public void addVetoableChangeListener(String PropertyName,
246cdf0e10cSrcweir              XVetoableChangeListener aListener ) {}
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     /**
249cdf0e10cSrcweir      * Empty implementation: not needed for tests.
250cdf0e10cSrcweir      */
removeVetoableChangeListener(String PropertyName, XVetoableChangeListener aListener )251cdf0e10cSrcweir     public void removeVetoableChangeListener(String PropertyName,
252cdf0e10cSrcweir              XVetoableChangeListener aListener ) {}
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     /**
255cdf0e10cSrcweir      * Get all implemented types of this class.
256cdf0e10cSrcweir      * @return An array of implemented interface types.
257cdf0e10cSrcweir      * @see com.sun.star.lang.XTypeProvider
258cdf0e10cSrcweir      */
getTypes()259cdf0e10cSrcweir     public Type[] getTypes() {
260cdf0e10cSrcweir         Type[] type = new Type[5];
261cdf0e10cSrcweir         type[0] = new Type(XInterface.class);
262cdf0e10cSrcweir         type[1] = new Type(XTypeProvider.class);
263cdf0e10cSrcweir         type[2] = new Type(XPersistObject.class);
264cdf0e10cSrcweir         type[3] = new Type(XServiceInfo.class);
265cdf0e10cSrcweir         type[4] = new Type(XPropertySet.class);
266cdf0e10cSrcweir         return type;
267cdf0e10cSrcweir     }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     /**
270cdf0e10cSrcweir      * Get the implementation id.
271cdf0e10cSrcweir      * @return An empty implementation id.
272cdf0e10cSrcweir      * @see com.sun.star.lang.XTypeProvider
273cdf0e10cSrcweir      */
getImplementationId()274cdf0e10cSrcweir     public byte[] getImplementationId() {
275cdf0e10cSrcweir         return new byte[0];
276cdf0e10cSrcweir     }
277cdf0e10cSrcweir     /**
278cdf0e10cSrcweir      * Function for reading the implementation name.
279cdf0e10cSrcweir      *
280cdf0e10cSrcweir      * @return the implementation name
281cdf0e10cSrcweir      * @see com.sun.star.lang.XServiceInfo
282cdf0e10cSrcweir      */
getImplementationName()283cdf0e10cSrcweir     public String getImplementationName() {
284cdf0e10cSrcweir         return __implName;
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     /**
288cdf0e10cSrcweir      * Does the implementation support this service?
289cdf0e10cSrcweir      *
290cdf0e10cSrcweir      * @param serviceName The name of the service in question
291cdf0e10cSrcweir      * @return true, if service is supported, false otherwise
292cdf0e10cSrcweir      * @see com.sun.star.lang.XServiceInfo
293cdf0e10cSrcweir      */
supportsService(String serviceName)294cdf0e10cSrcweir     public boolean supportsService(String serviceName) {
295cdf0e10cSrcweir         if(serviceName.equals(__serviceName))
296cdf0e10cSrcweir             return true;
297cdf0e10cSrcweir         return false;
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     /**
301cdf0e10cSrcweir      * Function for reading all supported services
302cdf0e10cSrcweir      *
303cdf0e10cSrcweir      * @return An aaray with all supported service names
304cdf0e10cSrcweir      * @see com.sun.star.lang.XServiceInfo
305cdf0e10cSrcweir      */
getSupportedServiceNames()306cdf0e10cSrcweir     public String[] getSupportedServiceNames() {
307cdf0e10cSrcweir         String[] supServiceNames = {__serviceName};
308cdf0e10cSrcweir         return supServiceNames;
309cdf0e10cSrcweir     }
310cdf0e10cSrcweir 
311cdf0e10cSrcweir    /**
312cdf0e10cSrcweir    *
313cdf0e10cSrcweir    * Gives a factory for creating the service.
314cdf0e10cSrcweir    * This method is called by the <code>JavaLoader</code>
315cdf0e10cSrcweir    * <p>
316cdf0e10cSrcweir    * @return  returns a <code>XSingleServiceFactory</code> for creating the component
317cdf0e10cSrcweir    * @param   implName     the name of the implementation for which a service is desired
318cdf0e10cSrcweir    * @param   multiFactory the service manager to be used if needed
319cdf0e10cSrcweir    * @param   regKey       the registryKey
320cdf0e10cSrcweir    * @see                  com.sun.star.comp.loader.JavaLoader
321cdf0e10cSrcweir    */
__getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)322cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(String implName,
323cdf0e10cSrcweir                     XMultiServiceFactory multiFactory, XRegistryKey regKey)
324cdf0e10cSrcweir     {
325cdf0e10cSrcweir         XSingleServiceFactory xSingleServiceFactory = null;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         if (implName.equals(MyPersistObject.class.getName()))
328cdf0e10cSrcweir             xSingleServiceFactory = FactoryHelper.getServiceFactory(
329cdf0e10cSrcweir                 MyPersistObject.class, __serviceName, multiFactory, regKey);
330cdf0e10cSrcweir 
331cdf0e10cSrcweir         return xSingleServiceFactory;
332cdf0e10cSrcweir     }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir   /**
335cdf0e10cSrcweir    * Writes the service information into the given registry key.
336cdf0e10cSrcweir    * This method is called by the <code>JavaLoader</code>
337cdf0e10cSrcweir    * <p>
338cdf0e10cSrcweir    * @return  returns true if the operation succeeded
339cdf0e10cSrcweir    * @param   regKey       the registryKey
340cdf0e10cSrcweir    * @see                  com.sun.star.comp.loader.JavaLoader
341cdf0e10cSrcweir    */
__writeRegistryServiceInfo(XRegistryKey regKey)342cdf0e10cSrcweir     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
343cdf0e10cSrcweir         return FactoryHelper.writeRegistryServiceInfo(MyPersistObject.class.getName(),
344cdf0e10cSrcweir         __serviceName, regKey);
345cdf0e10cSrcweir     }
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 
350cdf0e10cSrcweir }   // finish class MyPersistObject
351cdf0e10cSrcweir 
352cdf0e10cSrcweir 
353