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