Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / ProfUNO / InterprocessConn / ConnectionAwareClient.java
blobff4ef779480936c36977bbf7ffef39fe1d568143
1 /*************************************************************************
3 * $RCSfile: ConnectionAwareClient.java,v $
5 * $Revision: 1.3 $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:44:08 $
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 import java.awt.*;
42 import java.awt.event.*;
44 import com.sun.star.uno.XComponentContext;
45 import com.sun.star.lang.XMultiComponentFactory;
46 import com.sun.star.frame.XComponentLoader;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XComponentContext;
49 import com.sun.star.io.IOException;
50 import com.sun.star.connection.XConnector;
51 import com.sun.star.connection.XConnection;
53 import com.sun.star.beans.XPropertySet;
55 import com.sun.star.lang.XEventListener;
56 import com.sun.star.lang.XComponent;
58 import com.sun.star.bridge.XBridgeFactory;
59 import com.sun.star.bridge.XBridge;
62 public class ConnectionAwareClient extends java.awt.Frame
63 implements ActionListener , com.sun.star.lang.XEventListener
65 private Button _btnWriter,_btnCalc;
66 private Label _txtLabel;
67 private String _url;
69 private XComponentContext _ctx;
71 private com.sun.star.frame.XComponentLoader _officeComponentLoader;
73 public ConnectionAwareClient( XComponentContext ctx , String url )
75 _url = url;
76 _ctx = ctx;
78 Panel p1 = new Panel();
79 _btnWriter = new Button("New writer");
80 _btnCalc = new Button("New calc");
81 _txtLabel = new Label( "disconnected" );
83 _btnWriter.addActionListener(this);
84 _btnCalc.addActionListener(this);
85 p1.add( _btnWriter );
86 p1.add( _btnCalc );
87 p1.add( _txtLabel );
89 addWindowListener(
90 new WindowAdapter()
92 public void windowClosing(WindowEvent event)
94 System.exit(0);
99 add( p1 );
102 public void disposing( com.sun.star.lang.EventObject event )
104 // remote bridge has gone down, because the office crashed or was terminated.
105 _officeComponentLoader = null;
106 _txtLabel.setText( "disconnected" );
109 public void actionPerformed( ActionEvent event )
113 String sUrl;
114 if( event.getSource() == _btnWriter )
116 sUrl = "private:factory/swriter";
118 else
120 sUrl = "private:factory/scalc";
122 getComponentLoader().loadComponentFromURL(
123 sUrl, "_blank", 0,new com.sun.star.beans.PropertyValue[0] );
124 _txtLabel.setText( "connected" );
126 catch ( com.sun.star.connection.NoConnectException exc )
128 _txtLabel.setText( exc.getMessage() );
130 catch ( com.sun.star.uno.Exception exc )
132 _txtLabel.setText( exc.getMessage() );
133 exc.printStackTrace();
134 throw new java.lang.RuntimeException( exc.getMessage() );
138 /** separtates the uno-url into 3 different parts.
140 protected static String[] parseUnoUrl( String url )
142 String [] aRet = new String [3];
144 if( ! url.startsWith( "uno:" ) )
146 return null;
149 int semicolon = url.indexOf( ';' );
150 if( semicolon == -1 )
151 return null;
153 aRet[0] = url.substring( 4 , semicolon );
154 int nextSemicolon = url.indexOf( ';' , semicolon+1);
156 if( semicolon == -1 )
157 return null;
158 aRet[1] = url.substring( semicolon+1, nextSemicolon );
160 aRet[2] = url.substring( nextSemicolon+1);
161 return aRet;
166 protected com.sun.star.frame.XComponentLoader getComponentLoader()
167 throws com.sun.star.uno.Exception
169 XComponentLoader officeComponentLoader = _officeComponentLoader;
171 if( officeComponentLoader == null )
173 // instantiate connector service
174 Object x = _ctx.getServiceManager().createInstanceWithContext(
175 "com.sun.star.connection.Connector", _ctx );
177 XConnector xConnector = (XConnector )
178 UnoRuntime.queryInterface(XConnector.class, x);
180 String a[] = parseUnoUrl( _url );
181 if( null == a )
183 throw new com.sun.star.uno.Exception( "Couldn't parse uno-url "+ _url );
186 // connect using the connection string part of the uno-url only.
187 XConnection connection = xConnector.connect( a[0] );
189 x = _ctx.getServiceManager().createInstanceWithContext(
190 "com.sun.star.bridge.BridgeFactory", _ctx );
192 XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(
193 XBridgeFactory.class , x );
195 // create a nameless bridge with no instance provider
196 // using the middle part of the uno-url
197 XBridge bridge = xBridgeFactory.createBridge( "" , a[1] , connection , null );
199 // query for the XComponent interface and add this as event listener
200 XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
201 XComponent.class, bridge );
202 xComponent.addEventListener( this );
204 // get the remote instance
205 x = bridge.getInstance( a[2] );
207 // Did the remote server export this object ?
208 if( null == x )
210 throw new com.sun.star.uno.Exception(
211 "Server didn't provide an instance for" + a[2], null );
214 // Query the initial object for its main factory interface
215 XMultiComponentFactory xOfficeMultiComponentFactory = ( XMultiComponentFactory )
216 UnoRuntime.queryInterface( XMultiComponentFactory.class, x );
218 // retrieve the component context (it's not yet exported from the office)
219 // Query for the XPropertySet interface.
220 XPropertySet xProperySet = ( XPropertySet )
221 UnoRuntime.queryInterface( XPropertySet.class, xOfficeMultiComponentFactory );
223 // Get the default context from the office server.
224 Object oDefaultContext =
225 xProperySet.getPropertyValue( "DefaultContext" );
227 // Query for the interface XComponentContext.
228 XComponentContext xOfficeComponentContext =
229 ( XComponentContext ) UnoRuntime.queryInterface(
230 XComponentContext.class, oDefaultContext );
233 // now create the desktop service
234 // NOTE: use the office component context here !
235 Object oDesktop = xOfficeMultiComponentFactory.createInstanceWithContext(
236 "com.sun.star.frame.Desktop", xOfficeComponentContext );
238 officeComponentLoader = ( XComponentLoader )
239 UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );
241 if( officeComponentLoader == null )
243 throw new com.sun.star.uno.Exception(
244 "Couldn't instantiate com.sun.star.frame.Desktop" , null );
246 _officeComponentLoader = officeComponentLoader;
248 return officeComponentLoader;
251 public static void main( String [] args ) throws java.lang.Exception
253 if( args.length != 1 )
255 System.out.println( "usage: ConnectionAwareClient uno-url" );
256 return;
258 XComponentContext ctx =
259 com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
261 ConnectionAwareClient connAware = new ConnectionAwareClient( ctx, args[0]);
262 connAware.pack();
263 connAware.setVisible( true );