Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / OfficeConnect.java
blobd330e5ff16b286ad3bac22acbf09534ff9fa4932
1 /*************************************************************************
3 * $RCSfile: OfficeConnect.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:35:51 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
43 // structs, const, ...
44 import com.sun.star.beans.PropertyValue;
45 import com.sun.star.bridge.XUnoUrlResolver;
47 // exceptions
48 import com.sun.star.container.NoSuchElementException;
50 // interfaces
51 import com.sun.star.lang.XMultiServiceFactory;
52 import com.sun.star.uno.Any;
53 import com.sun.star.uno.Exception;
55 import com.sun.star.uno.UnoRuntime;
57 // others
58 import java.lang.String;
60 // __________ Implementation __________
62 /**
63 * support ONE singleton uno connection to an running office installation!
64 * Can be used to open/use/close connection to uno environment of an already running office.
65 * ctor isn't available from outside. You should call static function "getConnection()"
66 * to open or use internal set connection which is created one times only.
68 * @author Andreas Schlüns
69 * @created 7. Februar 2002
70 * @modified 05.02.2002 12:10
72 public class OfficeConnect
74 // ____________________
76 /**
77 * At first call we create static connection object and get the remote office
78 * context and the remote office service manager. A new office process is
79 * started if necessary.
80 * Then - and for all further requests we return these static connection member.
82 public static synchronized OfficeConnect createConnection()
83 throws java.lang.Exception
85 if (maConnection == null)
87 maConnection = new OfficeConnect();
89 return maConnection;
92 // ____________________
94 public static synchronized OfficeConnect getConnection()
96 return maConnection;
99 // ____________________
102 * ctor
103 * We try to open the connection in our ctor ... transparently for the user.
104 * We made it private to support singleton pattern of these implementation.
105 * see getConnection() for further informations
107 private OfficeConnect() throws java.lang.Exception
109 // get the remote office context. If necessary a new office
110 // process is started
111 mxOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
112 System.out.println("Connected to a running office ...");
113 mxServiceManager = mxOfficeContext.getServiceManager();
116 // ____________________
119 * create uno components inside remote office process
120 * After connection of these proccess to a running office we have access to remote service manager of it.
121 * So we can use it to create all existing services. Use this method to create components by name and
122 * get her interface. Casting of it to right target interface is part of your implementation.
124 * @param aType describe class type of created service
125 * Returned object can be casted directly to this one.
126 * Uno query was done by this method automaticly.
127 * @param sServiceSpecifier name of service which should be created
128 * @return Description of the Returned Value
130 public Object createRemoteInstance(Class aType, String sServiceSpecifier)
132 Object aResult = null;
135 aResult = UnoRuntime.queryInterface(
136 aType, mxServiceManager.createInstanceWithContext(
137 sServiceSpecifier, mxOfficeContext));
139 catch (com.sun.star.uno.Exception ex)
141 System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
142 ex.printStackTrace();
143 System.exit(0);
145 return aResult;
148 // ____________________
151 * same as "createRemoteInstance()" but supports additional parameter for initializing created object
153 * @param lArguments optional arguments
154 * They are used to initialize new created service.
155 * @param aType Description of Parameter
156 * @param sServiceSpecifier Description of Parameter
157 * @return Description of the Returned Value
159 public Object createRemoteInstanceWithArguments(Class aType, String sServiceSpecifier, Any[] lArguments)
161 Object aResult = null;
164 aResult = UnoRuntime.queryInterface(
165 aType, mxServiceManager.createInstanceWithArgumentsAndContext(
166 sServiceSpecifier, lArguments, mxOfficeContext));
168 catch (com.sun.star.uno.Exception ex)
170 System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
171 ex.printStackTrace();
172 System.exit(0);
174 return aResult;
177 // ____________________
180 * member
182 // singleton connection instance
183 private static OfficeConnect maConnection;
185 // reference to remote office context
186 private com.sun.star.uno.XComponentContext mxOfficeContext;
187 // reference to remote service manager
188 private com.sun.star.lang.XMultiComponentFactory mxServiceManager;