cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / OfficeConnect.java
blobd8db72aad323d113e7c2d9db23d52df03e977a91
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // __________ Imports __________
38 import com.sun.star.uno.UnoRuntime;
40 // __________ Implementation __________
42 /**
43 * support ONE singleton uno connection to a running office installation!
44 * Can be used to open/use/close connection to uno environment of an already running office.
45 * ctor isn't available from outside. You should call static function "getConnection()"
46 * to open or use internal set connection which is created one times only.
49 public class OfficeConnect
53 /**
54 * At first call we create static connection object and get the remote office
55 * context and the remote office service manager. A new office process is
56 * started if necessary.
57 * Then - and for all further requests we return these static connection member.
59 public static synchronized OfficeConnect createConnection()
60 throws java.lang.Exception
62 if (maConnection == null)
64 maConnection = new OfficeConnect();
66 return maConnection;
75 /**
76 * ctor
77 * We try to open the connection in our ctor ... transparently for the user.
78 * We made it private to support singleton pattern of these implementation.
79 * see getConnection() for further information
81 private OfficeConnect() throws java.lang.Exception
83 // get the remote office context. If necessary a new office
84 // process is started
85 mxOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
86 System.out.println("Connected to a running office ...");
87 mxServiceManager = mxOfficeContext.getServiceManager();
92 /**
93 * create uno components inside remote office process
94 * After connection of these process to a running office we have access to remote service manager of it.
95 * So we can use it to create all existing services. Use this method to create components by name and
96 * get her interface. Casting of it to right target interface is part of your implementation.
98 * @param aType describe class type of created service
99 * Returned object can be casted directly to this one.
100 * Uno query was done by this method automatically.
101 * @param sServiceSpecifier name of service which should be created
102 * @return Description of the Returned Value
104 public <T> T createRemoteInstance(Class<T> aType, String sServiceSpecifier)
106 T aResult = null;
109 aResult = UnoRuntime.queryInterface(
110 aType, mxServiceManager.createInstanceWithContext(
111 sServiceSpecifier, mxOfficeContext));
113 catch (com.sun.star.uno.Exception ex)
115 System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
116 ex.printStackTrace();
117 System.exit(0);
119 return aResult;
129 * member
131 // singleton connection instance
132 private static OfficeConnect maConnection;
134 // reference to remote office context
135 private final com.sun.star.uno.XComponentContext mxOfficeContext;
136 // reference to remote service manager
137 private final com.sun.star.lang.XMultiComponentFactory mxServiceManager;
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */