| 1 |
package org.astrogrid.samp.xmlrpc; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.Random; |
| 6 |
import org.astrogrid.samp.TestProfile; |
| 7 |
import org.astrogrid.samp.DataException; |
| 8 |
import org.astrogrid.samp.SampUtils; |
| 9 |
import org.astrogrid.samp.client.ClientProfile; |
| 10 |
import org.astrogrid.samp.client.HubConnection; |
| 11 |
import org.astrogrid.samp.client.SampException; |
| 12 |
import org.astrogrid.samp.hub.HubProfile; |
| 13 |
|
| 14 |
/** |
| 15 |
* Test Profile implementation based on the Standard Profile. |
| 16 |
* It puts the lockfile in a non-standard place though, so it can |
| 17 |
* run without interfering with any Standard Profile hub that |
| 18 |
* happens to be running at the same time. |
| 19 |
* |
| 20 |
* @author Mark Taylor |
| 21 |
* @since 4 Feb 2011 |
| 22 |
*/ |
| 23 |
public class StandardTestProfile extends TestProfile { |
| 24 |
|
| 25 |
private final Random random_; |
| 26 |
private final File lockFile_; |
| 27 |
private final SampXmlRpcClientFactory hubClientFactory_; |
| 28 |
private final SampXmlRpcServerFactory hubServerFactory_; |
| 29 |
private final SampXmlRpcClientFactory clientClientFactory_; |
| 30 |
private final SampXmlRpcServerFactory clientServerFactory_; |
| 31 |
private static StandardTestProfile[] testProfiles_; |
| 32 |
|
| 33 |
public StandardTestProfile( Random random, XmlRpcKit xmlrpc ) { |
| 34 |
this( random, xmlrpc.getClientFactory(), xmlrpc.getServerFactory(), |
| 35 |
xmlrpc.getClientFactory(), xmlrpc.getServerFactory() ); |
| 36 |
} |
| 37 |
|
| 38 |
public StandardTestProfile( Random random, |
| 39 |
SampXmlRpcClientFactory hubClientFactory, |
| 40 |
SampXmlRpcServerFactory hubServerFactory, |
| 41 |
SampXmlRpcClientFactory clientClientFactory, |
| 42 |
SampXmlRpcServerFactory clientServerFactory ) { |
| 43 |
super( random ); |
| 44 |
random_ = random; |
| 45 |
hubClientFactory_ = hubClientFactory; |
| 46 |
hubServerFactory_ = hubServerFactory; |
| 47 |
clientClientFactory_ = clientClientFactory; |
| 48 |
clientServerFactory_ = clientServerFactory; |
| 49 |
File dir = new File( System.getProperty( "user.dir", "." ) ); |
| 50 |
try { |
| 51 |
lockFile_ = File.createTempFile( "samp", ".lock", dir ); |
| 52 |
lockFile_.delete(); |
| 53 |
lockFile_.deleteOnExit(); |
| 54 |
} |
| 55 |
catch ( IOException e ) { |
| 56 |
throw new RuntimeException( e ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public HubProfile createHubProfile() { |
| 61 |
return new StandardHubProfile( hubClientFactory_, hubServerFactory_, |
| 62 |
lockFile_, |
| 63 |
Long.toHexString( random_.nextLong() ) ); |
| 64 |
} |
| 65 |
|
| 66 |
public boolean isHubRunning() { |
| 67 |
try { |
| 68 |
return LockInfo.readLockFile( SampUtils.fileToUrl( lockFile_ ) ) |
| 69 |
!= null; |
| 70 |
} |
| 71 |
catch ( IOException e ) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public HubConnection register() throws SampException { |
| 77 |
LockInfo lockInfo; |
| 78 |
try { |
| 79 |
lockInfo = |
| 80 |
LockInfo.readLockFile( SampUtils.fileToUrl( lockFile_ ) ); |
| 81 |
if ( lockInfo == null ) { |
| 82 |
return null; |
| 83 |
} |
| 84 |
else { |
| 85 |
try { |
| 86 |
lockInfo.check(); |
| 87 |
} |
| 88 |
catch ( DataException e ) { |
| 89 |
return null; |
| 90 |
} |
| 91 |
SampXmlRpcClient xClient = |
| 92 |
clientClientFactory_.createClient( lockInfo |
| 93 |
.getXmlrpcUrl() ); |
| 94 |
return new StandardHubConnection( xClient, clientServerFactory_, |
| 95 |
lockInfo.getSecret() ); |
| 96 |
} |
| 97 |
} |
| 98 |
catch ( IOException e ) { |
| 99 |
throw new SampException( e ); |
| 100 |
} |
| 101 |
} |
| 102 |
} |