1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.awt.*;
25cdf0e10cSrcweir import java.awt.event.*;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
29cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
31cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
32cdf0e10cSrcweir import com.sun.star.io.IOException;
33cdf0e10cSrcweir import com.sun.star.connection.XConnector;
34cdf0e10cSrcweir import com.sun.star.connection.XConnection;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
39cdf0e10cSrcweir import com.sun.star.lang.XComponent;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
42cdf0e10cSrcweir import com.sun.star.bridge.XBridge;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir public class ConnectionAwareClient extends java.awt.Frame
46cdf0e10cSrcweir     implements ActionListener , com.sun.star.lang.XEventListener
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     private Button _btnWriter,_btnCalc;
49cdf0e10cSrcweir     private Label _txtLabel;
50cdf0e10cSrcweir     private String _url;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     private XComponentContext _ctx;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     private com.sun.star.frame.XComponentLoader _officeComponentLoader;
55cdf0e10cSrcweir 
ConnectionAwareClient( XComponentContext ctx , String url )56cdf0e10cSrcweir     public ConnectionAwareClient( XComponentContext ctx , String url )
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         _url = url;
59cdf0e10cSrcweir         _ctx = ctx;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir         Panel p1 = new Panel();
62cdf0e10cSrcweir         _btnWriter = new Button("New writer");
63cdf0e10cSrcweir         _btnCalc = new Button("New calc");
64cdf0e10cSrcweir         _txtLabel = new Label( "disconnected" );
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         _btnWriter.addActionListener(this);
67cdf0e10cSrcweir         _btnCalc.addActionListener(this);
68cdf0e10cSrcweir         p1.add( _btnWriter );
69cdf0e10cSrcweir         p1.add( _btnCalc );
70cdf0e10cSrcweir         p1.add( _txtLabel );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         addWindowListener(
73cdf0e10cSrcweir             new WindowAdapter()
74cdf0e10cSrcweir             {
75cdf0e10cSrcweir                 public void windowClosing(WindowEvent event)
76cdf0e10cSrcweir                 {
77cdf0e10cSrcweir                     System.exit(0);
78cdf0e10cSrcweir                 }
79cdf0e10cSrcweir             }
80cdf0e10cSrcweir             );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         add( p1 );
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir 
disposing( com.sun.star.lang.EventObject event )85cdf0e10cSrcweir     public void disposing( com.sun.star.lang.EventObject event )
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         // remote bridge has gone down, because the office crashed or was terminated.
88cdf0e10cSrcweir         _officeComponentLoader = null;
89cdf0e10cSrcweir         _txtLabel.setText( "disconnected" );
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
actionPerformed( ActionEvent event )92cdf0e10cSrcweir     public void actionPerformed( ActionEvent event )
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         try
95cdf0e10cSrcweir         {
96cdf0e10cSrcweir             String sUrl;
97cdf0e10cSrcweir             if( event.getSource() == _btnWriter )
98cdf0e10cSrcweir             {
99cdf0e10cSrcweir                 sUrl = "private:factory/swriter";
100cdf0e10cSrcweir             }
101cdf0e10cSrcweir             else
102cdf0e10cSrcweir             {
103cdf0e10cSrcweir                 sUrl = "private:factory/scalc";
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir             getComponentLoader().loadComponentFromURL(
106cdf0e10cSrcweir                 sUrl, "_blank", 0,new com.sun.star.beans.PropertyValue[0] );
107cdf0e10cSrcweir             _txtLabel.setText( "connected" );
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir         catch ( com.sun.star.connection.NoConnectException exc )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             _txtLabel.setText( exc.getMessage() );
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir         catch ( com.sun.star.uno.Exception exc )
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             _txtLabel.setText( exc.getMessage() );
116cdf0e10cSrcweir             exc.printStackTrace();
117cdf0e10cSrcweir             throw new java.lang.RuntimeException( exc.getMessage()  );
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     /** separtates the uno-url into 3 different parts.
122cdf0e10cSrcweir      */
parseUnoUrl( String url )123cdf0e10cSrcweir     protected static String[] parseUnoUrl(  String url )
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         String [] aRet = new String [3];
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         if( ! url.startsWith( "uno:" ) )
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             return null;
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         int semicolon = url.indexOf( ';' );
133cdf0e10cSrcweir         if( semicolon == -1 )
134cdf0e10cSrcweir             return null;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         aRet[0] = url.substring( 4 , semicolon );
137cdf0e10cSrcweir         int nextSemicolon = url.indexOf( ';' , semicolon+1);
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         if( semicolon == -1 )
140cdf0e10cSrcweir             return null;
141cdf0e10cSrcweir         aRet[1] = url.substring( semicolon+1, nextSemicolon );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         aRet[2] = url.substring( nextSemicolon+1);
144cdf0e10cSrcweir         return aRet;
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 
getComponentLoader()149cdf0e10cSrcweir     protected com.sun.star.frame.XComponentLoader getComponentLoader()
150cdf0e10cSrcweir         throws com.sun.star.uno.Exception
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         XComponentLoader officeComponentLoader = _officeComponentLoader;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         if( officeComponentLoader == null )
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             // instantiate connector service
157cdf0e10cSrcweir             Object x = _ctx.getServiceManager().createInstanceWithContext(
158cdf0e10cSrcweir                 "com.sun.star.connection.Connector", _ctx );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             XConnector xConnector = (XConnector )
161cdf0e10cSrcweir                 UnoRuntime.queryInterface(XConnector.class, x);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             String a[] = parseUnoUrl( _url );
164cdf0e10cSrcweir             if( null == a )
165cdf0e10cSrcweir             {
166cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception( "Couldn't parse uno-url "+ _url );
167cdf0e10cSrcweir             }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir             // connect using the connection string part of the uno-url only.
170cdf0e10cSrcweir             XConnection connection = xConnector.connect( a[0] );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir             x = _ctx.getServiceManager().createInstanceWithContext(
173cdf0e10cSrcweir                 "com.sun.star.bridge.BridgeFactory", _ctx );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir             XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(
176cdf0e10cSrcweir                 XBridgeFactory.class , x );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir             // create a nameless bridge with no instance provider
179cdf0e10cSrcweir             // using the middle part of the uno-url
180cdf0e10cSrcweir             XBridge bridge = xBridgeFactory.createBridge( "" , a[1] , connection , null );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir             // query for the XComponent interface and add this as event listener
183cdf0e10cSrcweir             XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
184cdf0e10cSrcweir                 XComponent.class, bridge );
185cdf0e10cSrcweir             xComponent.addEventListener( this );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir             // get the remote instance
188cdf0e10cSrcweir             x = bridge.getInstance( a[2] );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir             // Did the remote server export this object ?
191cdf0e10cSrcweir             if( null == x )
192cdf0e10cSrcweir             {
193cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception(
194cdf0e10cSrcweir                     "Server didn't provide an instance for" + a[2], null );
195cdf0e10cSrcweir             }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir             // Query the initial object for its main factory interface
198cdf0e10cSrcweir             XMultiComponentFactory xOfficeMultiComponentFactory = ( XMultiComponentFactory )
199cdf0e10cSrcweir                 UnoRuntime.queryInterface( XMultiComponentFactory.class, x );
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             // retrieve the component context (it's not yet exported from the office)
202cdf0e10cSrcweir             // Query for the XPropertySet interface.
203cdf0e10cSrcweir             XPropertySet xProperySet = ( XPropertySet )
204cdf0e10cSrcweir                 UnoRuntime.queryInterface( XPropertySet.class, xOfficeMultiComponentFactory );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir             // Get the default context from the office server.
207cdf0e10cSrcweir             Object oDefaultContext =
208cdf0e10cSrcweir                 xProperySet.getPropertyValue( "DefaultContext" );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir             // Query for the interface XComponentContext.
211cdf0e10cSrcweir             XComponentContext xOfficeComponentContext =
212cdf0e10cSrcweir                 ( XComponentContext ) UnoRuntime.queryInterface(
213cdf0e10cSrcweir                     XComponentContext.class, oDefaultContext );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
216cdf0e10cSrcweir             // now create the desktop service
217cdf0e10cSrcweir             // NOTE: use the office component context here !
218cdf0e10cSrcweir             Object oDesktop = xOfficeMultiComponentFactory.createInstanceWithContext(
219cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xOfficeComponentContext );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir             officeComponentLoader = ( XComponentLoader )
222cdf0e10cSrcweir                 UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir             if( officeComponentLoader == null )
225cdf0e10cSrcweir             {
226cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception(
227cdf0e10cSrcweir                     "Couldn't instantiate com.sun.star.frame.Desktop" , null );
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir             _officeComponentLoader = officeComponentLoader;
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir         return officeComponentLoader;
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
main( String [] args )234cdf0e10cSrcweir     public static void main( String [] args ) throws java.lang.Exception
235cdf0e10cSrcweir         {
236cdf0e10cSrcweir             if( args.length != 1 )
237cdf0e10cSrcweir             {
238cdf0e10cSrcweir                 System.out.println( "usage: ConnectionAwareClient uno-url" );
239cdf0e10cSrcweir                 return;
240cdf0e10cSrcweir             }
241cdf0e10cSrcweir             XComponentContext ctx =
242cdf0e10cSrcweir                 com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir             ConnectionAwareClient connAware = new ConnectionAwareClient( ctx, args[0]);
245cdf0e10cSrcweir             connAware.pack();
246cdf0e10cSrcweir             connAware.setVisible( true );
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250