xref: /AOO41X/main/odk/examples/DevelopersGuide/ProfUNO/InterprocessConn/ConnectionAwareClient.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import java.awt.*;
36*cdf0e10cSrcweir import java.awt.event.*;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
39*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
40*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
43*cdf0e10cSrcweir import com.sun.star.io.IOException;
44*cdf0e10cSrcweir import com.sun.star.connection.XConnector;
45*cdf0e10cSrcweir import com.sun.star.connection.XConnection;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
50*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
53*cdf0e10cSrcweir import com.sun.star.bridge.XBridge;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir public class ConnectionAwareClient extends java.awt.Frame
57*cdf0e10cSrcweir     implements ActionListener , com.sun.star.lang.XEventListener
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     private Button _btnWriter,_btnCalc;
60*cdf0e10cSrcweir     private Label _txtLabel;
61*cdf0e10cSrcweir     private String _url;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     private XComponentContext _ctx;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     private com.sun.star.frame.XComponentLoader _officeComponentLoader;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     public ConnectionAwareClient( XComponentContext ctx , String url )
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         _url = url;
70*cdf0e10cSrcweir         _ctx = ctx;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir         Panel p1 = new Panel();
73*cdf0e10cSrcweir         _btnWriter = new Button("New writer");
74*cdf0e10cSrcweir         _btnCalc = new Button("New calc");
75*cdf0e10cSrcweir         _txtLabel = new Label( "disconnected" );
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         _btnWriter.addActionListener(this);
78*cdf0e10cSrcweir         _btnCalc.addActionListener(this);
79*cdf0e10cSrcweir         p1.add( _btnWriter );
80*cdf0e10cSrcweir         p1.add( _btnCalc );
81*cdf0e10cSrcweir         p1.add( _txtLabel );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         addWindowListener(
84*cdf0e10cSrcweir             new WindowAdapter()
85*cdf0e10cSrcweir             {
86*cdf0e10cSrcweir                 public void windowClosing(WindowEvent event)
87*cdf0e10cSrcweir                 {
88*cdf0e10cSrcweir                     System.exit(0);
89*cdf0e10cSrcweir                 }
90*cdf0e10cSrcweir             }
91*cdf0e10cSrcweir             );
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         add( p1 );
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     public void disposing( com.sun.star.lang.EventObject event )
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         // remote bridge has gone down, because the office crashed or was terminated.
99*cdf0e10cSrcweir         _officeComponentLoader = null;
100*cdf0e10cSrcweir         _txtLabel.setText( "disconnected" );
101*cdf0e10cSrcweir     }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     public void actionPerformed( ActionEvent event )
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         try
106*cdf0e10cSrcweir         {
107*cdf0e10cSrcweir             String sUrl;
108*cdf0e10cSrcweir             if( event.getSource() == _btnWriter )
109*cdf0e10cSrcweir             {
110*cdf0e10cSrcweir                 sUrl = "private:factory/swriter";
111*cdf0e10cSrcweir             }
112*cdf0e10cSrcweir             else
113*cdf0e10cSrcweir             {
114*cdf0e10cSrcweir                 sUrl = "private:factory/scalc";
115*cdf0e10cSrcweir             }
116*cdf0e10cSrcweir             getComponentLoader().loadComponentFromURL(
117*cdf0e10cSrcweir                 sUrl, "_blank", 0,new com.sun.star.beans.PropertyValue[0] );
118*cdf0e10cSrcweir             _txtLabel.setText( "connected" );
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir         catch ( com.sun.star.connection.NoConnectException exc )
121*cdf0e10cSrcweir         {
122*cdf0e10cSrcweir             _txtLabel.setText( exc.getMessage() );
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir         catch ( com.sun.star.uno.Exception exc )
125*cdf0e10cSrcweir         {
126*cdf0e10cSrcweir             _txtLabel.setText( exc.getMessage() );
127*cdf0e10cSrcweir             exc.printStackTrace();
128*cdf0e10cSrcweir             throw new java.lang.RuntimeException( exc.getMessage()  );
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     /** separtates the uno-url into 3 different parts.
133*cdf0e10cSrcweir      */
134*cdf0e10cSrcweir     protected static String[] parseUnoUrl(  String url )
135*cdf0e10cSrcweir     {
136*cdf0e10cSrcweir         String [] aRet = new String [3];
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         if( ! url.startsWith( "uno:" ) )
139*cdf0e10cSrcweir         {
140*cdf0e10cSrcweir             return null;
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         int semicolon = url.indexOf( ';' );
144*cdf0e10cSrcweir         if( semicolon == -1 )
145*cdf0e10cSrcweir             return null;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         aRet[0] = url.substring( 4 , semicolon );
148*cdf0e10cSrcweir         int nextSemicolon = url.indexOf( ';' , semicolon+1);
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         if( semicolon == -1 )
151*cdf0e10cSrcweir             return null;
152*cdf0e10cSrcweir         aRet[1] = url.substring( semicolon+1, nextSemicolon );
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir         aRet[2] = url.substring( nextSemicolon+1);
155*cdf0e10cSrcweir         return aRet;
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     protected com.sun.star.frame.XComponentLoader getComponentLoader()
161*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
162*cdf0e10cSrcweir     {
163*cdf0e10cSrcweir         XComponentLoader officeComponentLoader = _officeComponentLoader;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         if( officeComponentLoader == null )
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             // instantiate connector service
168*cdf0e10cSrcweir             Object x = _ctx.getServiceManager().createInstanceWithContext(
169*cdf0e10cSrcweir                 "com.sun.star.connection.Connector", _ctx );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir             XConnector xConnector = (XConnector )
172*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XConnector.class, x);
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir             String a[] = parseUnoUrl( _url );
175*cdf0e10cSrcweir             if( null == a )
176*cdf0e10cSrcweir             {
177*cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception( "Couldn't parse uno-url "+ _url );
178*cdf0e10cSrcweir             }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir             // connect using the connection string part of the uno-url only.
181*cdf0e10cSrcweir             XConnection connection = xConnector.connect( a[0] );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             x = _ctx.getServiceManager().createInstanceWithContext(
184*cdf0e10cSrcweir                 "com.sun.star.bridge.BridgeFactory", _ctx );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir             XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(
187*cdf0e10cSrcweir                 XBridgeFactory.class , x );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir             // create a nameless bridge with no instance provider
190*cdf0e10cSrcweir             // using the middle part of the uno-url
191*cdf0e10cSrcweir             XBridge bridge = xBridgeFactory.createBridge( "" , a[1] , connection , null );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir             // query for the XComponent interface and add this as event listener
194*cdf0e10cSrcweir             XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
195*cdf0e10cSrcweir                 XComponent.class, bridge );
196*cdf0e10cSrcweir             xComponent.addEventListener( this );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir             // get the remote instance
199*cdf0e10cSrcweir             x = bridge.getInstance( a[2] );
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir             // Did the remote server export this object ?
202*cdf0e10cSrcweir             if( null == x )
203*cdf0e10cSrcweir             {
204*cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception(
205*cdf0e10cSrcweir                     "Server didn't provide an instance for" + a[2], null );
206*cdf0e10cSrcweir             }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir             // Query the initial object for its main factory interface
209*cdf0e10cSrcweir             XMultiComponentFactory xOfficeMultiComponentFactory = ( XMultiComponentFactory )
210*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XMultiComponentFactory.class, x );
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir             // retrieve the component context (it's not yet exported from the office)
213*cdf0e10cSrcweir             // Query for the XPropertySet interface.
214*cdf0e10cSrcweir             XPropertySet xProperySet = ( XPropertySet )
215*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XPropertySet.class, xOfficeMultiComponentFactory );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir             // Get the default context from the office server.
218*cdf0e10cSrcweir             Object oDefaultContext =
219*cdf0e10cSrcweir                 xProperySet.getPropertyValue( "DefaultContext" );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir             // Query for the interface XComponentContext.
222*cdf0e10cSrcweir             XComponentContext xOfficeComponentContext =
223*cdf0e10cSrcweir                 ( XComponentContext ) UnoRuntime.queryInterface(
224*cdf0e10cSrcweir                     XComponentContext.class, oDefaultContext );
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir             // now create the desktop service
228*cdf0e10cSrcweir             // NOTE: use the office component context here !
229*cdf0e10cSrcweir             Object oDesktop = xOfficeMultiComponentFactory.createInstanceWithContext(
230*cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xOfficeComponentContext );
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir             officeComponentLoader = ( XComponentLoader )
233*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir             if( officeComponentLoader == null )
236*cdf0e10cSrcweir             {
237*cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception(
238*cdf0e10cSrcweir                     "Couldn't instantiate com.sun.star.frame.Desktop" , null );
239*cdf0e10cSrcweir             }
240*cdf0e10cSrcweir             _officeComponentLoader = officeComponentLoader;
241*cdf0e10cSrcweir         }
242*cdf0e10cSrcweir         return officeComponentLoader;
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     public static void main( String [] args ) throws java.lang.Exception
246*cdf0e10cSrcweir         {
247*cdf0e10cSrcweir             if( args.length != 1 )
248*cdf0e10cSrcweir             {
249*cdf0e10cSrcweir                 System.out.println( "usage: ConnectionAwareClient uno-url" );
250*cdf0e10cSrcweir                 return;
251*cdf0e10cSrcweir             }
252*cdf0e10cSrcweir             XComponentContext ctx =
253*cdf0e10cSrcweir                 com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir             ConnectionAwareClient connAware = new ConnectionAwareClient( ctx, args[0]);
256*cdf0e10cSrcweir             connAware.pack();
257*cdf0e10cSrcweir             connAware.setVisible( true );
258*cdf0e10cSrcweir         }
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261