1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
26cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
27cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /**
30cdf0e10cSrcweir  *
31cdf0e10cSrcweir  * @author  dschulten
32cdf0e10cSrcweir  */
33cdf0e10cSrcweir public class TerminationTest extends java.lang.Object {
34cdf0e10cSrcweir 
35cdf0e10cSrcweir     private static boolean atWork = false;
36cdf0e10cSrcweir     /**
37cdf0e10cSrcweir      * @param args the command line arguments
38cdf0e10cSrcweir      */
main(String[] args)39cdf0e10cSrcweir     public static void main(String[] args) {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir         XComponentContext xRemoteContext = null;
42cdf0e10cSrcweir         XMultiComponentFactory xRemoteServiceManager = null;
43cdf0e10cSrcweir         XDesktop xDesktop = null;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir         try {
46cdf0e10cSrcweir             // get the remote office context. If necessary a new office
47cdf0e10cSrcweir             // process is started
48cdf0e10cSrcweir             xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
49cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
50cdf0e10cSrcweir             xRemoteServiceManager = xRemoteContext.getServiceManager();
51cdf0e10cSrcweir 
52cdf0e10cSrcweir             Object desktop = xRemoteServiceManager.createInstanceWithContext(
53cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xRemoteContext);
54cdf0e10cSrcweir             xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, desktop);
55cdf0e10cSrcweir 
56cdf0e10cSrcweir             TerminateListener terminateListener = new TerminateListener();
57cdf0e10cSrcweir             xDesktop.addTerminateListener(terminateListener);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir             atWork = true;
60cdf0e10cSrcweir             // try to terminate while we are at work
61cdf0e10cSrcweir             boolean terminated = xDesktop.terminate();
62cdf0e10cSrcweir             System.out.println("The Office " +
63cdf0e10cSrcweir                 (terminated == true ?
64cdf0e10cSrcweir                  "has been terminated" :
65cdf0e10cSrcweir                  "is still running, we are at work"));
66cdf0e10cSrcweir 
67cdf0e10cSrcweir             // no longer at work
68cdf0e10cSrcweir             atWork = false;
69cdf0e10cSrcweir             // once more: try to terminate
70cdf0e10cSrcweir             terminated = xDesktop.terminate();
71cdf0e10cSrcweir             System.out.println("The Office " +
72cdf0e10cSrcweir                 (terminated == true ?
73cdf0e10cSrcweir                     "has been terminated" :
74cdf0e10cSrcweir                     "is still running. Someone else prevents termination, " +
75cdf0e10cSrcweir                     "e.g. the quickstarter"));
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir         catch (java.lang.Exception e){
78cdf0e10cSrcweir             e.printStackTrace();
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         finally {
81cdf0e10cSrcweir             System.exit(0);
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     }
isAtWork()86cdf0e10cSrcweir     public static boolean isAtWork() {
87cdf0e10cSrcweir         return atWork;
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir }
91