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 basicrunner;
24 
25 import com.sun.star.uno.UnoRuntime;
26 import com.sun.star.container.XSet;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.container.ElementExistException;
29 import com.sun.star.lang.IllegalArgumentException;
30 import com.sun.star.connection.ConnectionSetupException;
31 import lib.TestParameters;
32 import share.LogWriter;
33 import basicrunner.basichelper.Connector;
34 import basicrunner.basichelper.DocumentHandler;
35 import basicrunner.basichelper.ThreadRunner;
36 import basicrunner.basichelper.AttributeList;
37 import basicrunner.basichelper.Filter;
38 import basicrunner.basichelper.DispatchProviderInterceptor;
39 
40 /**
41  * This class provides a BasicHandler. All classes for the communication with
42  * and handling of  the BASIC tests are instantiated and inserted int the
43  * MultiServiceFactory of StarOffice.
44  */
45 public class BasicHandlerProvider {
46 
47     /** The BassicHandler **/
48     static BasicHandler oHandler = null;
49     /** The Connector **/
50     static Connector oConnector = null;
51     /** The DocumentHandler **/
52     static DocumentHandler oDocumentHandler = null;
53     /** The Thread Runner **/
54     static ThreadRunner oThreadRunner = null;
55     /** The AttributeList **/
56     static AttributeList oAttributeList = null;
57     /** The Filter **/
58     static Filter oFilter = null;
59     /** The DispatchProviderInterceptor **/
60     static DispatchProviderInterceptor oCeptor = null ;
61     /** The MultiServiceFactory from StarOffice **/
62     static XMultiServiceFactory MSF = null;
63     /** IS this a new connection or an existing one? **/
64     static boolean bIsNewConnection = true;
65 
66     /**
67      * Get a BasicHandler
68      * @param tParam Test parameters.
69      * @param log A log writer
70      * @return An instance of BasicHandler
71      */
getHandler(TestParameters tParam, LogWriter log)72     static public BasicHandler getHandler(TestParameters tParam, LogWriter log) {
73 
74         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
75 
76         if (!xMSF.equals(MSF)) {
77             MSF = xMSF;
78             oHandler = new BasicHandler(tParam);
79             oConnector = new Connector();
80             oFilter = new Filter();
81             oDocumentHandler = new DocumentHandler();
82 	    oThreadRunner = new ThreadRunner(xMSF);
83             oCeptor = new DispatchProviderInterceptor() ;
84             oAttributeList = new AttributeList();
85             XSet xMSFSet = (XSet)UnoRuntime.queryInterface(XSet.class, xMSF);
86 
87             try {
88                 xMSFSet.insert(oHandler);
89                 xMSFSet.insert(oConnector);
90                 xMSFSet.insert(oFilter);
91                 xMSFSet.insert(oDocumentHandler);
92 		xMSFSet.insert(oThreadRunner);
93                 xMSFSet.insert(oCeptor);
94                 xMSFSet.insert(oAttributeList);
95             } catch (ElementExistException e) {
96                 System.out.println(e.toString());
97             } catch (IllegalArgumentException e) {
98                 System.out.println(e.toString());
99             }
100 
101             try {
102                 oHandler.Connect(util.utils.getFullURL((String)tParam.get("BASICBRIDGE")),
103                                                                tParam, xMSF, log);
104             } catch (ConnectionSetupException e) {
105                 System.out.println("Can't connect to BASIC !");
106             }
107 
108             bIsNewConnection = true;
109         } else {
110             bIsNewConnection = false;
111         }
112 
113         return oHandler;
114     }
115 
116     /**
117      * Is this a new connection?
118      * @return True, if the connection did not exist before.
119      */
isNewConnection()120     static public boolean isNewConnection() {
121         return bIsNewConnection;
122     }
123 
124     /**
125      * Dispose the BasicHandler
126      */
disposeHandler()127     static public void disposeHandler() {
128 
129         try {
130             if (oHandler != null) {
131                 oHandler.dispose();
132             }
133             if (MSF != null) {
134                 XSet xMSFSet = (XSet)UnoRuntime.queryInterface(XSet.class, MSF);
135                 xMSFSet.remove(oHandler);
136                 xMSFSet.remove(oFilter);
137                 xMSFSet.remove(oConnector);
138                 xMSFSet.remove(oDocumentHandler);
139 		xMSFSet.remove(oThreadRunner);
140                 xMSFSet.remove(oAttributeList);
141             }
142         } catch (Exception e){
143             System.out.println(e.toString());
144         }
145 
146         MSF = null;
147         oHandler = null;
148     }
149 }
150