| 1 |
package org.astrogrid.samp.xmlrpc; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.net.MalformedURLException; |
| 6 |
import java.net.URL; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.logging.Logger; |
| 9 |
import org.astrogrid.samp.DataException; |
| 10 |
import org.astrogrid.samp.Platform; |
| 11 |
import org.astrogrid.samp.SampUtils; |
| 12 |
import org.astrogrid.samp.client.ClientProfile; |
| 13 |
import org.astrogrid.samp.client.DefaultClientProfile; |
| 14 |
import org.astrogrid.samp.client.HubConnection; |
| 15 |
import org.astrogrid.samp.client.SampException; |
| 16 |
|
| 17 |
/** |
| 18 |
* Standard Profile implementation of ClientProfile. |
| 19 |
* It is normally appropriate to use one of the static methods |
| 20 |
* to obtain an instance based on a particular XML-RPC implementation. |
| 21 |
* |
| 22 |
* @author Mark Taylor |
| 23 |
* @since 15 Jul 2008 |
| 24 |
*/ |
| 25 |
public class StandardClientProfile implements ClientProfile { |
| 26 |
|
| 27 |
private final SampXmlRpcClientFactory xClientFactory_; |
| 28 |
private final SampXmlRpcServerFactory xServerFactory_; |
| 29 |
|
| 30 |
private static StandardClientProfile defaultInstance_; |
| 31 |
private static URL dfltLockUrl_; |
| 32 |
private static URL lockUrl_; |
| 33 |
private static final Logger logger_ = |
| 34 |
Logger.getLogger( StandardClientProfile.class.getName() ); |
| 35 |
|
| 36 |
/** Filename used for lockfile in home directory by default ({@value}). */ |
| 37 |
public static final String LOCKFILE_NAME = ".samp"; |
| 38 |
|
| 39 |
/** Prefix in SAMP_HUB value indicating lockfile URL ({@value}). */ |
| 40 |
public static final String STDPROFILE_HUB_PREFIX = "std-lockurl:"; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructs a profile given client and server factory implementations. |
| 44 |
* |
| 45 |
* @param xClientFactory XML-RPC client factory implementation |
| 46 |
* @param xServerFactory XML-RPC server factory implementation |
| 47 |
*/ |
| 48 |
public StandardClientProfile( SampXmlRpcClientFactory xClientFactory, |
| 49 |
SampXmlRpcServerFactory xServerFactory ) { |
| 50 |
xClientFactory_ = xClientFactory; |
| 51 |
xServerFactory_ = xServerFactory; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Constructs a profile given an XmlRpcKit object. |
| 56 |
* |
| 57 |
* @param xmlrpc XML-RPC implementation |
| 58 |
*/ |
| 59 |
public StandardClientProfile( XmlRpcKit xmlrpc ) { |
| 60 |
this( xmlrpc.getClientFactory(), xmlrpc.getServerFactory() ); |
| 61 |
} |
| 62 |
|
| 63 |
public boolean isHubRunning() { |
| 64 |
try { |
| 65 |
LockInfo lockInfo = getLockInfo(); |
| 66 |
if ( lockInfo == null ) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
URL xurl = lockInfo.getXmlrpcUrl(); |
| 70 |
if ( xurl == null ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
SampXmlRpcClient xClient = xClientFactory_.createClient( xurl ); |
| 74 |
xClient.callAndWait( "samp.hub.ping", new ArrayList() ); |
| 75 |
return true; |
| 76 |
} |
| 77 |
catch ( IOException e ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public HubConnection register() throws SampException { |
| 83 |
LockInfo lockInfo; |
| 84 |
try { |
| 85 |
lockInfo = getLockInfo(); |
| 86 |
} |
| 87 |
catch ( SampException e ) { |
| 88 |
throw (SampException) e; |
| 89 |
} |
| 90 |
catch ( IOException e ) { |
| 91 |
throw new SampException( "Error reading lockfile", e ); |
| 92 |
} |
| 93 |
if ( lockInfo == null ) { |
| 94 |
return null; |
| 95 |
} |
| 96 |
else { |
| 97 |
try { |
| 98 |
lockInfo.check(); |
| 99 |
} |
| 100 |
catch ( DataException e ) { |
| 101 |
String msg = "Incomplete/broken lock file"; |
| 102 |
try { |
| 103 |
File lockFile = SampUtils.urlToFile( getLockUrl() ); |
| 104 |
if ( lockFile != null ) { |
| 105 |
msg += " - try deleting " + lockFile; |
| 106 |
} |
| 107 |
} |
| 108 |
catch ( IOException e2 ) { |
| 109 |
// never mind |
| 110 |
} |
| 111 |
throw new SampException( msg, e ); |
| 112 |
} |
| 113 |
SampXmlRpcClient xClient; |
| 114 |
URL xurl = lockInfo.getXmlrpcUrl(); |
| 115 |
try { |
| 116 |
xClient = xClientFactory_.createClient( xurl ); |
| 117 |
} |
| 118 |
catch ( IOException e ) { |
| 119 |
throw new SampException( "Can't connect to " + xurl, e ); |
| 120 |
} |
| 121 |
return new StandardHubConnection( xClient, xServerFactory_, |
| 122 |
lockInfo.getSecret() ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the LockInfo which indicates how to locate the hub. |
| 128 |
* If no lockfile exists (probably becuause no appropriate hub |
| 129 |
* is running), null is returned. |
| 130 |
* The default implementation returns |
| 131 |
* <code>LockInfo.readLockFile(getLockUrl())</code>; |
| 132 |
* it may be overridden to provide a non-standard client profiles. |
| 133 |
* |
| 134 |
* @return hub location information |
| 135 |
* @throws IOException if the lockfile exists but cannot be read for |
| 136 |
* some reason |
| 137 |
*/ |
| 138 |
public LockInfo getLockInfo() throws IOException { |
| 139 |
return LockInfo.readLockFile( getLockUrl() ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the location of the Standard Profile lockfile. |
| 144 |
* By default this is the file <code>.samp</code> in the user's "home" |
| 145 |
* directory, unless overridden by a value of the SAMP_HUB environment |
| 146 |
* variable starting with "std-lockurl". |
| 147 |
* |
| 148 |
* @return lockfile URL |
| 149 |
*/ |
| 150 |
public static URL getLockUrl() throws IOException { |
| 151 |
if ( lockUrl_ == null ) { |
| 152 |
String hublocEnv = DefaultClientProfile.HUBLOC_ENV; |
| 153 |
String hubloc = Platform.getPlatform().getEnv( hublocEnv ); |
| 154 |
final URL lockUrl; |
| 155 |
if ( hubloc != null && |
| 156 |
hubloc.startsWith( STDPROFILE_HUB_PREFIX ) ) { |
| 157 |
lockUrl = new URL( hubloc.substring( STDPROFILE_HUB_PREFIX |
| 158 |
.length() ) ); |
| 159 |
logger_.info( "Lockfile as set by env var: " |
| 160 |
+ hublocEnv + "=" + hubloc ); |
| 161 |
} |
| 162 |
else if ( hubloc != null && hubloc.trim().length() > 0 ) { |
| 163 |
logger_.warning( "Ignoring non-Standard " + hublocEnv + "=" |
| 164 |
+ hubloc ); |
| 165 |
lockUrl = getDefaultLockUrl(); |
| 166 |
} |
| 167 |
else { |
| 168 |
lockUrl = getDefaultLockUrl(); |
| 169 |
logger_.info( "Using default Standard Profile lockfile: " |
| 170 |
+ SampUtils.urlToFile( lockUrl ) ); |
| 171 |
} |
| 172 |
lockUrl_ = lockUrl; |
| 173 |
} |
| 174 |
return lockUrl_; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns the lockfile URL which will be used in absence of any |
| 179 |
* SAMP_HUB environment variable. |
| 180 |
* |
| 181 |
* @return URL for file .samp in user's home directory |
| 182 |
*/ |
| 183 |
public static URL getDefaultLockUrl() throws IOException { |
| 184 |
if ( dfltLockUrl_ == null ) { |
| 185 |
dfltLockUrl_ = |
| 186 |
SampUtils.fileToUrl( new File( Platform.getPlatform() |
| 187 |
.getHomeDirectory(), |
| 188 |
LOCKFILE_NAME ) ); |
| 189 |
} |
| 190 |
return dfltLockUrl_; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns an instance based on the default XML-RPC implementation. |
| 195 |
* This can be configured using system properties. |
| 196 |
* |
| 197 |
* @see XmlRpcKit#getInstance |
| 198 |
* @see org.astrogrid.samp.client.DefaultClientProfile#getProfile |
| 199 |
* @return a client profile instance |
| 200 |
*/ |
| 201 |
public static StandardClientProfile getInstance() { |
| 202 |
if ( defaultInstance_ == null ) { |
| 203 |
XmlRpcKit xmlrpc = XmlRpcKit.getInstance(); |
| 204 |
defaultInstance_ = |
| 205 |
new StandardClientProfile( xmlrpc.getClientFactory(), |
| 206 |
xmlrpc.getServerFactory() ); |
| 207 |
} |
| 208 |
return defaultInstance_; |
| 209 |
} |
| 210 |
} |