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