1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
// __________ Imports __________
// structs, const, ...
import com.sun.star.beans.PropertyValue;
import com.sun.star.bridge.XUnoUrlResolver;
// exceptions
import com.sun.star.container.NoSuchElementException;
// interfaces
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.Any;
import com.sun.star.uno.Exception;
// helper
import com.sun.star.uno.IBridge;
import com.sun.star.uno.RuntimeException;
import com.sun.star.uno.UnoRuntime;
// others
import java.lang.String;
// __________ Implementation __________
/**
* support ONE singleton uno connection to an running office installation!
* Can be used to open/use/close connection to uno environment of an already running office.
* ctor isn't available from outside. You should call static function "getConnection()"
* to open or use internal set connection which is created one times only.
*
* @author Andreas Schlüns
* @created 7. Februar 2002
* @modified 05.02.2002 12:10
*/
public class OfficeConnect
{
// ____________________
/**
* At first call we create static connection object and open connection to already runing office - if we can.
* Then - and for all further requests we return these static connection member.
*
* @param sHost host on which office runs
* @param sPort port on which office can be found
* @return Description of the Returned Value
*/
public static synchronized OfficeConnect createConnection(String sHost, String sPort)
{
if (maConnection == null)
{
maConnection = new OfficeConnect(sHost, sPort);
}
return maConnection;
}
// ____________________
public static synchronized OfficeConnect getConnection()
{
return maConnection;
}
// ____________________
/**
* ctor
* We try to open the connection in our ctor ... transparently for user.
* After it was successfully you will find an internal set member m_xFactory wich
* means remote uno service manager of connected office.
* We made it private to support singleton pattern of these implementation.
* see getConnection() for further informations
*
* @param sHost host on which office runs
* @param sPort port on which office can be found
*/
private OfficeConnect(String sHost, String sPort)
{
try
{
String sConnectString = "uno:socket,host=" + sHost + ",port=" + sPort + ";urp;StarOffice.ServiceManager";
com.sun.star.lang.XMultiServiceFactory xLocalServiceManager = com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
com.sun.star.bridge.XUnoUrlResolver xURLResolver = (com.sun.star.bridge.XUnoUrlResolver)UnoRuntime.queryInterface(
com.sun.star.bridge.XUnoUrlResolver.class,
xLocalServiceManager.createInstance("com.sun.star.bridge.UnoUrlResolver"));
mxServiceManager = (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
com.sun.star.lang.XMultiServiceFactory.class,
xURLResolver.resolve(sConnectString));
}
catch (com.sun.star.uno.RuntimeException exUNO)
{
System.out.println("connection failed" + exUNO);
}
catch (com.sun.star.uno.Exception exRun)
{
System.out.println("connection failed" + exRun);
}
catch (java.lang.Exception exJava)
{
System.out.println("connection failed" + exJava);
}
}
// ____________________
/**
* URL's must be parsed before they can be used for dispatch.
* We use special service to do so.
*
* @param sURL Description of Parameter
* @return Description of the Returned Value
*/
public static com.sun.star.util.URL parseURL(String sURL)
{
com.sun.star.util.XURLTransformer xParser = (com.sun.star.util.XURLTransformer)OfficeConnect.getConnection().createRemoteInstance(com.sun.star.util.XURLTransformer.class, "com.sun.star.util.URLTransformer");
com.sun.star.util.URL[] aURL = new com.sun.star.util.URL[1];
aURL[0] = new com.sun.star.util.URL();
aURL[0].Complete = sURL;
xParser.parseStrict(aURL);
return aURL[0];
}
// ____________________
/**
* create uno components inside remote office process
* After connection of these proccess to a running office we have access to remote service manager of it.
* So we can use it to create all existing services. Use this method to create components by name and
* get her interface. Casting of it to right target interface is part of your implementation.
*
* @param aType describe class type of created service
* Returned object can be casted directly to this one.
* Uno query was done by this method automaticly.
* @param sServiceSpecifier name of service which should be created
* @return Description of the Returned Value
*/
public Object createRemoteInstance(Class aType, String sServiceSpecifier)
{
Object aResult = null;
try
{
aResult = UnoRuntime.queryInterface(
aType,
mxServiceManager.createInstance(sServiceSpecifier));
}
catch (com.sun.star.uno.Exception ex)
{
System.out.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
System.exit(0);
}
return aResult;
}
// ____________________
/**
* same as "createRemoteInstance()" but supports additional parameter for initializing created object
*
* @param lArguments optional arguments
* They are used to initialize new created service.
* @param aType Description of Parameter
* @param sServiceSpecifier Description of Parameter
* @return Description of the Returned Value
*/
public Object createRemoteInstanceWithArguments(Class aType, String sServiceSpecifier, Any[] lArguments)
{
Object aResult = null;
try
{
aResult = UnoRuntime.queryInterface(
aType,
mxServiceManager.createInstanceWithArguments(
sServiceSpecifier,
lArguments));
}
catch (com.sun.star.uno.Exception ex)
{
System.out.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
System.exit(0);
}
return aResult;
}
// ____________________
/**
* member
*/
private static OfficeConnect maConnection ; // singleton connection instance
private com.sun.star.lang.XMultiServiceFactory mxServiceManager; // reference to remote service manager of singleton connection object
}
|