Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / ProfUNO / InterprocessConn / ConnectionAwareClient.java
blob136400544095f14c3dfd0bcd419eb214d76f6bf7
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 import java.awt.*;
37 import java.awt.event.*;
39 import com.sun.star.uno.XComponentContext;
40 import com.sun.star.lang.XMultiComponentFactory;
41 import com.sun.star.frame.XComponentLoader;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.connection.XConnector;
44 import com.sun.star.connection.XConnection;
46 import com.sun.star.beans.XPropertySet;
48 import com.sun.star.lang.XComponent;
50 import com.sun.star.bridge.XBridgeFactory;
51 import com.sun.star.bridge.XBridge;
54 public class ConnectionAwareClient extends java.awt.Frame
55 implements ActionListener , com.sun.star.lang.XEventListener
57 private final Button _btnWriter;
58 private final Label _txtLabel;
59 private final String _url;
61 private final XComponentContext _ctx;
63 private com.sun.star.frame.XComponentLoader _officeComponentLoader;
65 public ConnectionAwareClient( XComponentContext ctx , String url )
67 _url = url;
68 _ctx = ctx;
70 Panel p1 = new Panel();
71 _btnWriter = new Button("New writer");
72 Button _btnCalc = new Button("New calc");
73 _txtLabel = new Label( "disconnected" );
75 _btnWriter.addActionListener(this);
76 _btnCalc.addActionListener(this);
77 p1.add( _btnWriter );
78 p1.add( _btnCalc );
79 p1.add( _txtLabel );
81 addWindowListener(
82 new WindowAdapter()
84 @Override
85 public void windowClosing(WindowEvent event)
87 System.exit(0);
92 add( p1 );
95 public void disposing( com.sun.star.lang.EventObject event )
97 // remote bridge has gone down, because the office crashed or was terminated.
98 _officeComponentLoader = null;
99 _txtLabel.setText( "disconnected" );
102 public void actionPerformed( ActionEvent event )
106 String sUrl;
107 if( event.getSource() == _btnWriter )
109 sUrl = "private:factory/swriter";
111 else
113 sUrl = "private:factory/scalc";
115 getComponentLoader().loadComponentFromURL(
116 sUrl, "_blank", 0,new com.sun.star.beans.PropertyValue[0] );
117 _txtLabel.setText( "connected" );
119 catch ( com.sun.star.connection.NoConnectException exc )
121 _txtLabel.setText( exc.getMessage() );
123 catch ( com.sun.star.uno.Exception exc )
125 _txtLabel.setText( exc.getMessage() );
126 throw new java.lang.RuntimeException( exc );
130 /** separates the uno-url into 3 different parts.
132 protected static String[] parseUnoUrl( String url )
134 String [] aRet = new String [3];
136 if( ! url.startsWith( "uno:" ) )
138 return null;
141 int semicolon = url.indexOf( ';' );
142 if( semicolon == -1 )
143 return null;
145 aRet[0] = url.substring( 4 , semicolon );
146 int nextSemicolon = url.indexOf( ';' , semicolon+1);
148 if( semicolon == -1 )
149 return null;
150 aRet[1] = url.substring( semicolon+1, nextSemicolon );
152 aRet[2] = url.substring( nextSemicolon+1);
153 return aRet;
158 protected com.sun.star.frame.XComponentLoader getComponentLoader()
159 throws com.sun.star.uno.Exception
161 XComponentLoader officeComponentLoader = _officeComponentLoader;
163 if( officeComponentLoader == null )
165 // instantiate connector service
166 Object x = _ctx.getServiceManager().createInstanceWithContext(
167 "com.sun.star.connection.Connector", _ctx );
169 XConnector xConnector = UnoRuntime.queryInterface(XConnector.class, x);
171 String a[] = parseUnoUrl( _url );
172 if( null == a )
174 throw new com.sun.star.uno.Exception( "Couldn't parse uno-url "+ _url );
177 // connect using the connection string part of the uno-url only.
178 XConnection connection = xConnector.connect( a[0] );
180 x = _ctx.getServiceManager().createInstanceWithContext(
181 "com.sun.star.bridge.BridgeFactory", _ctx );
183 XBridgeFactory xBridgeFactory = UnoRuntime.queryInterface(
184 XBridgeFactory.class , x );
186 // create a nameless bridge with no instance provider
187 // using the middle part of the uno-url
188 XBridge bridge = xBridgeFactory.createBridge( "" , a[1] , connection , null );
190 // query for the XComponent interface and add this as event listener
191 XComponent xComponent = UnoRuntime.queryInterface(
192 XComponent.class, bridge );
193 xComponent.addEventListener( this );
195 // get the remote instance
196 x = bridge.getInstance( a[2] );
198 // Did the remote server export this object ?
199 if( null == x )
201 throw new com.sun.star.uno.Exception(
202 "Server didn't provide an instance for" + a[2], null );
205 // Query the initial object for its main factory interface
206 XMultiComponentFactory xOfficeMultiComponentFactory = UnoRuntime.queryInterface( XMultiComponentFactory.class, x );
208 // retrieve the component context (it's not yet exported from the office)
209 // Query for the XPropertySet interface.
210 XPropertySet xProperySet = UnoRuntime.queryInterface( XPropertySet.class, xOfficeMultiComponentFactory );
212 // Get the default context from the office server.
213 Object oDefaultContext =
214 xProperySet.getPropertyValue( "DefaultContext" );
216 // Query for the interface XComponentContext.
217 XComponentContext xOfficeComponentContext =
218 UnoRuntime.queryInterface(
219 XComponentContext.class, oDefaultContext );
222 // now create the desktop service
223 // NOTE: use the office component context here !
224 Object oDesktop = xOfficeMultiComponentFactory.createInstanceWithContext(
225 "com.sun.star.frame.Desktop", xOfficeComponentContext );
227 officeComponentLoader = UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );
229 if( officeComponentLoader == null )
231 throw new com.sun.star.uno.Exception(
232 "Couldn't instantiate com.sun.star.frame.Desktop" , null );
234 _officeComponentLoader = officeComponentLoader;
236 return officeComponentLoader;
239 public static void main( String [] args ) throws java.lang.Exception
241 if( args.length != 1 )
243 System.out.println( "usage: ConnectionAwareClient uno-url" );
244 return;
246 XComponentContext ctx =
247 com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
249 ConnectionAwareClient connAware = new ConnectionAwareClient( ctx, args[0]);
250 connAware.pack();
251 connAware.setVisible( true );
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */