1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.openoffice.accessibility.misc;
23 
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.io.PrintStream;
27 import java.util.Timer;
28 import java.util.TimerTask;
29 import java.util.Vector;
30 
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.bridge.XUnoUrlResolver;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.comp.helper.Bootstrap;
35 
36 /** This class establishes a connection to a StarOffice application.
37  */
38 public class OfficeConnection
39     extends TimerTask
40 {
41     final public static long snDelay = 3000;
42 
Instance()43 	public static synchronized OfficeConnection Instance ()
44 	{
45 		if (saInstance == null)
46 			saInstance = new OfficeConnection ();
47 		return saInstance;
48 	}
49 
50 
51 
52 
SetPipeName(String sPipeName)53 	static public void SetPipeName (String sPipeName)
54 	{
55 		ssDefaultPipeName = sPipeName;
56 	}
57 
58 
59 
60 
AddConnectionListener(ActionListener aListener)61     public void AddConnectionListener (ActionListener aListener)
62     {
63         SimpleOffice aOffice = SimpleOffice.Instance();
64         if (IsValid())
65             aListener.actionPerformed (
66                 new ActionEvent (aOffice,0,"<connected>"));
67         maListeners.add (aListener);
68     }
69 
70 
71 
72     /** @descr Return the service manager that represents the connected
73                 StarOffice application
74     */
GetServiceManager()75     public XMultiServiceFactory GetServiceManager ()
76     {
77         return maServiceManager;
78     }
79 
80 
81 
82 
83     /** Return a flag that indicates if the constructor has been able to
84 		establish a valid connection.
85     */
IsValid()86     public boolean IsValid ()
87     {
88         return (maServiceManager != null);
89     }
90 
91 
92 
93 
94     /** Connect to a already running StarOffice application that has
95 		been started with a command line argument like
96 		"-accept=pipe,name=<username>;urp;"
97     */
Connect()98     private boolean Connect ()
99     {
100         mbInitialized = true;
101         //  Set up connection string.
102 		String sConnectString = "uno:pipe,name=" + msPipeName
103 				+ ";urp;StarOffice.ServiceManager";
104 
105 		// connect to a running office and get the ServiceManager
106         try
107         {
108             //  Create a URL Resolver.
109             XMultiServiceFactory aLocalServiceManager =
110                 Bootstrap.createSimpleServiceManager();
111             XUnoUrlResolver aURLResolver =
112 				(XUnoUrlResolver) UnoRuntime.queryInterface (
113 					XUnoUrlResolver.class,
114 					aLocalServiceManager.createInstance (
115 						"com.sun.star.bridge.UnoUrlResolver")
116 					);
117 
118             maServiceManager =
119 				(XMultiServiceFactory) UnoRuntime.queryInterface (
120                     XMultiServiceFactory.class,
121                     aURLResolver.resolve (sConnectString)
122                     );
123         }
124 
125         catch (Exception e)
126         {
127             if (maOut != null)
128             {
129                 maOut.println ("Could not connect with "
130                     + sConnectString + " : " + e);
131                 maOut.println ("Please start OpenOffice/StarOffice with "
132                     + "\"-accept=pipe,name=" + msPipeName + ";urp;\"");
133             }
134         }
135 
136 		return maServiceManager != null;
137     }
138 
139 
run()140     public void run ()
141     {
142 		if ( ! IsValid())
143         {
144             MessageArea.println ("trying to connect");
145             if (Connect())
146             {
147                 // Stop the timer.
148                 cancel ();
149 
150                 ActionEvent aEvent = new ActionEvent (this,0,"<connected>");
151                 for (int i=0; i<maListeners.size(); i++)
152                     ((ActionListener)maListeners.elementAt(i)).actionPerformed(aEvent);
153             }
154         }
155     }
156 
OfficeConnection()157     private OfficeConnection ()
158     {
159         this (null);
160     }
161 
162 
OfficeConnection(PrintStream aOut)163     private OfficeConnection (PrintStream aOut)
164     {
165         msPipeName = ssDefaultPipeName;
166         maOut = aOut;
167         maListeners = new Vector();
168 		maServiceManager = null;
169 
170         maTimer = new Timer (true);
171         maTimer.schedule (this, 0, snDelay);
172     }
173 
174 
175 	private static OfficeConnection saInstance = null;
176     private static String ssDefaultPipeName = System.getenv( "USER" );
177 
178     private XMultiServiceFactory maServiceManager;
179     String msPipeName;
180 
181     /** A value of true just indicates that it has been tried to establish a connection,
182         not that that has been successfull.
183     */
184     private boolean mbInitialized = false;
185 
186     /// Stream used to print messages.
187     private PrintStream maOut;
188     private Timer maTimer;
189     private Vector maListeners;
190 }
191