| 1 |
package org.astrogrid.samp.xmlrpc; |
| 2 |
|
| 3 |
import java.io.ByteArrayOutputStream; |
| 4 |
import java.io.File; |
| 5 |
import java.io.FileInputStream; |
| 6 |
import java.io.FileOutputStream; |
| 7 |
import java.io.IOException; |
| 8 |
import java.io.OutputStream; |
| 9 |
import java.net.InetAddress; |
| 10 |
import java.net.URL; |
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.Date; |
| 13 |
import java.util.Random; |
| 14 |
import java.util.logging.Level; |
| 15 |
import java.util.logging.Logger; |
| 16 |
import org.astrogrid.samp.SampUtils; |
| 17 |
import org.astrogrid.samp.client.ClientProfile; |
| 18 |
import org.astrogrid.samp.client.DefaultClientProfile; |
| 19 |
import org.astrogrid.samp.httpd.ServerResource; |
| 20 |
import org.astrogrid.samp.httpd.UtilServer; |
| 21 |
import org.astrogrid.samp.hub.HubProfile; |
| 22 |
import org.astrogrid.samp.hub.KeyGenerator; |
| 23 |
import org.astrogrid.samp.hub.LockWriter; |
| 24 |
import org.astrogrid.samp.hub.MessageRestriction; |
| 25 |
|
| 26 |
/** |
| 27 |
* HubProfile implementation for the SAMP Standard Profile. |
| 28 |
* |
| 29 |
* @author Mark Taylor |
| 30 |
* @since 31 Jan 2011 |
| 31 |
*/ |
| 32 |
public class StandardHubProfile implements HubProfile { |
| 33 |
|
| 34 |
private final SampXmlRpcClientFactory xClientFactory_; |
| 35 |
private final SampXmlRpcServerFactory xServerFactory_; |
| 36 |
private final File lockfile_; |
| 37 |
private final String secret_; |
| 38 |
private URL lockUrl_; |
| 39 |
private SampXmlRpcServer server_; |
| 40 |
private volatile HubXmlRpcHandler hubHandler_; |
| 41 |
private LockInfo lockInfo_; |
| 42 |
private static final Logger logger_ = |
| 43 |
Logger.getLogger( StandardHubProfile.class.getName() ); |
| 44 |
private static final Random random_ = KeyGenerator.createRandom(); |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructs a hub profile with given configuration information. |
| 48 |
* If the supplied <code>lockfile</code> is null, no lockfile will |
| 49 |
* be written at hub startup. |
| 50 |
* |
| 51 |
* @param xClientFactory XML-RPC client factory implementation |
| 52 |
* @param xServerFactory XML-RPC server implementation |
| 53 |
* @param lockfile location to use for hub lockfile, or null |
| 54 |
* @param secret value for samp.secret lockfile key |
| 55 |
*/ |
| 56 |
public StandardHubProfile( SampXmlRpcClientFactory xClientFactory, |
| 57 |
SampXmlRpcServerFactory xServerFactory, |
| 58 |
File lockfile, String secret ) { |
| 59 |
xClientFactory_ = xClientFactory; |
| 60 |
xServerFactory_ = xServerFactory; |
| 61 |
lockfile_ = lockfile; |
| 62 |
secret_ = secret; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructs a hub profile with default configuration. |
| 67 |
*/ |
| 68 |
public StandardHubProfile() throws IOException { |
| 69 |
this( XmlRpcKit.getInstance().getClientFactory(), |
| 70 |
XmlRpcKit.getInstance().getServerFactory(), |
| 71 |
SampUtils.urlToFile( StandardClientProfile.getLockUrl() ), |
| 72 |
createSecret() ); |
| 73 |
} |
| 74 |
|
| 75 |
public String getProfileName() { |
| 76 |
return "Standard"; |
| 77 |
} |
| 78 |
|
| 79 |
public MessageRestriction getMessageRestriction() { |
| 80 |
return null; |
| 81 |
} |
| 82 |
|
| 83 |
public synchronized void start( ClientProfile profile ) throws IOException { |
| 84 |
|
| 85 |
// Check state. |
| 86 |
if ( isRunning() ) { |
| 87 |
logger_.info( "Profile already started" ); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Check for running hub. If there is a running hub, bail out. |
| 92 |
// If there is a lockfile but apparently no running hub, |
| 93 |
// continue preparations to start; the hub reference by the lockfile |
| 94 |
// may either be moribund or in the process of starting up. |
| 95 |
// To deal with the latter case, make all our preparations to give |
| 96 |
// it more time to get going before preempting it. |
| 97 |
if ( lockfile_ != null && lockfile_.exists() ) { |
| 98 |
if ( isHubAlive( xClientFactory_, lockfile_ ) ) { |
| 99 |
throw new IOException( "A hub is already running" ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Start up server. |
| 104 |
try { |
| 105 |
server_ = xServerFactory_.getServer(); |
| 106 |
} |
| 107 |
catch ( IOException e ) { |
| 108 |
throw e; |
| 109 |
} |
| 110 |
catch ( Exception e ) { |
| 111 |
throw (IOException) new IOException( "Can't start XML-RPC server" ) |
| 112 |
.initCause( e ); |
| 113 |
} |
| 114 |
hubHandler_ = |
| 115 |
new HubXmlRpcHandler( xClientFactory_, profile, secret_, |
| 116 |
new KeyGenerator( "k:", 16, random_ ) ); |
| 117 |
server_.addHandler( hubHandler_ ); |
| 118 |
|
| 119 |
// Prepare lockfile information. |
| 120 |
lockInfo_ = new LockInfo( secret_, server_.getEndpoint().toString() ); |
| 121 |
lockInfo_.put( "hub.impl", profile.getClass().getName() ); |
| 122 |
lockInfo_.put( "profile.impl", this.getClass().getName() ); |
| 123 |
lockInfo_.put( "profile.start.date", new Date().toString() ); |
| 124 |
|
| 125 |
// Write lockfile information to file if required. |
| 126 |
if ( lockfile_ != null ) { |
| 127 |
|
| 128 |
// If the lockfile already exists, wait a little while in case |
| 129 |
// a hub is in the process of waking up. If it's still not |
| 130 |
// present, overwrite the lockfile with a warning. |
| 131 |
if ( ! lockfile_.createNewFile() ) { |
| 132 |
try { |
| 133 |
Thread.sleep( 500 ); |
| 134 |
} |
| 135 |
catch ( InterruptedException e ) { |
| 136 |
} |
| 137 |
if ( isHubAlive( xClientFactory_, lockfile_ ) ) { |
| 138 |
server_.removeHandler( hubHandler_ ); |
| 139 |
hubHandler_ = null; |
| 140 |
throw new IOException( "A hub is already running" ); |
| 141 |
} |
| 142 |
else { |
| 143 |
logger_.warning( "Overwriting " + lockfile_ + " lockfile " |
| 144 |
+ "for apparently dead hub" ); |
| 145 |
} |
| 146 |
} |
| 147 |
FileOutputStream out = new FileOutputStream( lockfile_ ); |
| 148 |
try { |
| 149 |
writeLockInfo( lockInfo_, out ); |
| 150 |
logger_.info( "Wrote new lockfile " + lockfile_ ); |
| 151 |
try { |
| 152 |
LockWriter.setLockPermissions( lockfile_ ); |
| 153 |
logger_.info( "Lockfile permissions set to " |
| 154 |
+ "user access only" ); |
| 155 |
} |
| 156 |
catch ( IOException e ) { |
| 157 |
logger_.log( Level.WARNING, |
| 158 |
"Failed attempt to change " + lockfile_ |
| 159 |
+ " permissions to user access only" |
| 160 |
+ " - possible security implications", e ); |
| 161 |
} |
| 162 |
} |
| 163 |
finally { |
| 164 |
try { |
| 165 |
out.close(); |
| 166 |
} |
| 167 |
catch ( IOException e ) { |
| 168 |
logger_.log( Level.WARNING, "Error closing lockfile?", e ); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// If the lockfile is not the default one, write a message through |
| 174 |
// the logging system. |
| 175 |
URL lockfileUrl = lockfile_ == null |
| 176 |
? publishLockfile() |
| 177 |
: SampUtils.fileToUrl( lockfile_ ); |
| 178 |
boolean isDflt = StandardClientProfile.getDefaultLockUrl().toString() |
| 179 |
.equals( lockfileUrl.toString() ); |
| 180 |
String hubassign = DefaultClientProfile.HUBLOC_ENV + "=" |
| 181 |
+ StandardClientProfile.STDPROFILE_HUB_PREFIX |
| 182 |
+ lockfileUrl; |
| 183 |
logger_.log( isDflt ? Level.INFO : Level.WARNING, hubassign ); |
| 184 |
} |
| 185 |
|
| 186 |
public synchronized boolean isRunning() { |
| 187 |
return hubHandler_ != null; |
| 188 |
} |
| 189 |
|
| 190 |
public synchronized void stop() { |
| 191 |
if ( ! isRunning() ) { |
| 192 |
logger_.info( "Profile already stopped" ); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
// Delete the lockfile if it exists and if it is the one originally |
| 197 |
// written by this runner. |
| 198 |
if ( lockInfo_ != null && lockfile_ != null ) { |
| 199 |
if ( lockfile_.exists() ) { |
| 200 |
try { |
| 201 |
LockInfo lockInfo = readLockFile( lockfile_ ); |
| 202 |
if ( lockInfo_.getSecret() |
| 203 |
.equals( lockInfo.getSecret() ) ) { |
| 204 |
assert lockInfo.equals( lockInfo_ ); |
| 205 |
boolean deleted = lockfile_.delete(); |
| 206 |
logger_.info( "Lockfile " + lockfile_ + " " |
| 207 |
+ ( deleted ? "deleted" |
| 208 |
: "deletion attempt failed" ) ); |
| 209 |
} |
| 210 |
else { |
| 211 |
logger_.warning( "Lockfile " + lockfile_ + " has been " |
| 212 |
+ " overwritten - not deleting" ); |
| 213 |
} |
| 214 |
} |
| 215 |
catch ( Throwable e ) { |
| 216 |
logger_.log( Level.WARNING, |
| 217 |
"Failed to delete lockfile " + lockfile_, |
| 218 |
e ); |
| 219 |
} |
| 220 |
} |
| 221 |
else { |
| 222 |
logger_.warning( "Lockfile " + lockfile_ + " has disappeared" ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Withdraw service of the lockfile, if one has been published. |
| 227 |
if ( lockUrl_ != null ) { |
| 228 |
try { |
| 229 |
UtilServer.getInstance().getResourceHandler() |
| 230 |
.removeResource( lockUrl_ ); |
| 231 |
} |
| 232 |
catch ( IOException e ) { |
| 233 |
logger_.warning( "Failed to withdraw lockfile URL" ); |
| 234 |
} |
| 235 |
lockUrl_ = null; |
| 236 |
} |
| 237 |
|
| 238 |
// Remove the hub XML-RPC handler from the server. |
| 239 |
if ( hubHandler_ != null && server_ != null ) { |
| 240 |
server_.removeHandler( hubHandler_ ); |
| 241 |
} |
| 242 |
server_ = null; |
| 243 |
hubHandler_ = null; |
| 244 |
lockInfo_ = null; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns the lockfile information associated with this object. |
| 249 |
* Only present when running. |
| 250 |
* |
| 251 |
* @return lock info |
| 252 |
*/ |
| 253 |
public LockInfo getLockInfo() { |
| 254 |
return lockInfo_; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Returns an HTTP URL at which the lockfile for this hub can be found. |
| 259 |
* The first call to this method causes the lockfile to be published |
| 260 |
* in this way; subsequent calls return the same value. |
| 261 |
* |
| 262 |
* <p>Use this with care; publishing your lockfile means that other people |
| 263 |
* can connect to your hub and potentially do disruptive things. |
| 264 |
* |
| 265 |
* @return lockfile information URL |
| 266 |
*/ |
| 267 |
public URL publishLockfile() throws IOException { |
| 268 |
if ( lockUrl_ == null ) { |
| 269 |
ByteArrayOutputStream infoStrm = new ByteArrayOutputStream(); |
| 270 |
writeLockInfo( lockInfo_, infoStrm ); |
| 271 |
infoStrm.close(); |
| 272 |
final byte[] infoBuf = infoStrm.toByteArray(); |
| 273 |
URL url = UtilServer.getInstance().getResourceHandler() |
| 274 |
.addResource( "samplock", new ServerResource() { |
| 275 |
public long getContentLength() { |
| 276 |
return infoBuf.length; |
| 277 |
} |
| 278 |
public String getContentType() { |
| 279 |
return "text/plain"; |
| 280 |
} |
| 281 |
public void writeBody( OutputStream out ) throws IOException { |
| 282 |
out.write( infoBuf ); |
| 283 |
} |
| 284 |
} ); |
| 285 |
|
| 286 |
// Attempt to replace whatever host name is used by the FQDN, |
| 287 |
// for maximal usefulness to off-host clients. |
| 288 |
try { |
| 289 |
url = new URL( url.getProtocol(), |
| 290 |
InetAddress.getLocalHost() |
| 291 |
.getCanonicalHostName(), |
| 292 |
url.getPort(), url.getFile() ); |
| 293 |
} |
| 294 |
catch ( IOException e ) { |
| 295 |
} |
| 296 |
lockUrl_ = url; |
| 297 |
} |
| 298 |
return lockUrl_; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Returns a string suitable for use as a Standard Profile Secret. |
| 303 |
* |
| 304 |
* @return new secret |
| 305 |
*/ |
| 306 |
public static synchronized String createSecret() { |
| 307 |
return Long.toHexString( random_.nextLong() ); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Attempts to determine whether a given lockfile corresponds to a hub |
| 312 |
* which is still alive. |
| 313 |
* |
| 314 |
* @param xClientFactory XML-RPC client factory implementation |
| 315 |
* @param lockfile lockfile location |
| 316 |
* @return true if the hub described at <code>lockfile</code> appears |
| 317 |
* to be alive and well |
| 318 |
*/ |
| 319 |
private static boolean isHubAlive( SampXmlRpcClientFactory xClientFactory, |
| 320 |
File lockfile ) { |
| 321 |
LockInfo info; |
| 322 |
try { |
| 323 |
info = readLockFile( lockfile ); |
| 324 |
} |
| 325 |
catch ( Exception e ) { |
| 326 |
logger_.log( Level.WARNING, "Failed to read lockfile", e ); |
| 327 |
return false; |
| 328 |
} |
| 329 |
if ( info == null ) { |
| 330 |
return false; |
| 331 |
} |
| 332 |
URL xurl = info.getXmlrpcUrl(); |
| 333 |
if ( xurl != null ) { |
| 334 |
try { |
| 335 |
xClientFactory.createClient( xurl ) |
| 336 |
.callAndWait( "samp.hub.ping", new ArrayList() ); |
| 337 |
return true; |
| 338 |
} |
| 339 |
catch ( Exception e ) { |
| 340 |
logger_.log( Level.WARNING, "Hub ping method failed", e ); |
| 341 |
return false; |
| 342 |
} |
| 343 |
} |
| 344 |
else { |
| 345 |
logger_.warning( "No XMLRPC URL in lockfile" ); |
| 346 |
return false; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Reads lockinfo from a file. |
| 352 |
* |
| 353 |
* @param lockFile file |
| 354 |
* @return info from file |
| 355 |
*/ |
| 356 |
private static LockInfo readLockFile( File lockFile ) throws IOException { |
| 357 |
return LockInfo.readLockFile( new FileInputStream( lockFile ) ); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Writes lockfile information to a given output stream. |
| 362 |
* The stream is not closed. |
| 363 |
* |
| 364 |
* @param info lock info to write |
| 365 |
* @param out destination stream |
| 366 |
*/ |
| 367 |
private static void writeLockInfo( LockInfo info, OutputStream out ) |
| 368 |
throws IOException { |
| 369 |
LockWriter writer = new LockWriter( out ); |
| 370 |
writer.writeComment( "SAMP Standard Profile lockfile written " |
| 371 |
+ new Date() ); |
| 372 |
writer.writeComment( "Note contact URL hostname may be " |
| 373 |
+ "configured using " |
| 374 |
+ SampUtils.LOCALHOST_PROP + " property" ); |
| 375 |
writer.writeAssignments( info ); |
| 376 |
out.flush(); |
| 377 |
} |
| 378 |
} |