| 1 |
package org.astrogrid.samp.xmlrpc; |
| 2 |
|
| 3 |
import java.io.BufferedInputStream; |
| 4 |
import java.io.File; |
| 5 |
import java.io.FileInputStream; |
| 6 |
import java.io.IOException; |
| 7 |
import java.io.InputStream; |
| 8 |
import java.net.URL; |
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.Map; |
| 11 |
import java.util.regex.Matcher; |
| 12 |
import java.util.regex.Pattern; |
| 13 |
import java.util.logging.Logger; |
| 14 |
import org.astrogrid.samp.DataException; |
| 15 |
import org.astrogrid.samp.SampMap; |
| 16 |
import org.astrogrid.samp.SampUtils; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents the information read from a SAMP Standard Profile Lockfile. |
| 20 |
* This contains a key-value entry for each assignment read from the file. |
| 21 |
* Any non-assignment lines are not represented by this object. |
| 22 |
* |
| 23 |
* @author Mark Taylor |
| 24 |
* @since 14 Jul 2008 |
| 25 |
*/ |
| 26 |
public class LockInfo extends SampMap { |
| 27 |
|
| 28 |
private static final Logger logger_ = |
| 29 |
Logger.getLogger( LockInfo.class.getName() ); |
| 30 |
|
| 31 |
/** Key for opaque text string required by the hub for registration. */ |
| 32 |
public static final String SECRET_KEY = "samp.secret"; |
| 33 |
|
| 34 |
/** Key for XML-RPC endpoint for communication with the hub. */ |
| 35 |
public static final String XMLRPCURL_KEY = "samp.hub.xmlrpc.url"; |
| 36 |
|
| 37 |
/** Key for the SAMP Standard Profile version implemented by the hub. */ |
| 38 |
public static final String VERSION_KEY = "samp.profile.version"; |
| 39 |
|
| 40 |
private static final String[] KNOWN_KEYS = new String[] { |
| 41 |
SECRET_KEY, |
| 42 |
XMLRPCURL_KEY, |
| 43 |
VERSION_KEY, |
| 44 |
}; |
| 45 |
|
| 46 |
/** SAMP Standard Profile version for this toolkit implementation. */ |
| 47 |
public static final String DEFAULT_VERSION_VALUE = "1.3"; |
| 48 |
|
| 49 |
private static final Pattern TOKEN_REGEX = |
| 50 |
Pattern.compile( "[a-zA-Z0-9\\-_\\.]+" ); |
| 51 |
private static final Pattern ASSIGNMENT_REGEX = |
| 52 |
Pattern.compile( "(" + TOKEN_REGEX.pattern() + ")=(.*)" ); |
| 53 |
private static final Pattern COMMENT_REGEX = |
| 54 |
Pattern.compile( "#[\u0020-\u007f]*" ); |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructs an empty LockInfo. |
| 58 |
*/ |
| 59 |
public LockInfo() { |
| 60 |
super( KNOWN_KEYS ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Constructs a LockInfo based on an existing map. |
| 65 |
* |
| 66 |
* @param map map containing initial data for this object |
| 67 |
*/ |
| 68 |
public LockInfo( Map map ) { |
| 69 |
this(); |
| 70 |
putAll( map ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Constructs a LockInfo from a given SAMP secret and XML-RPC URL. |
| 75 |
* The version string is set to the default for this toolkit. |
| 76 |
* |
| 77 |
* @param secret value for {@link #SECRET_KEY} key |
| 78 |
* @param xmlrpcurl value for {@link #XMLRPCURL_KEY} key |
| 79 |
*/ |
| 80 |
public LockInfo( String secret, String xmlrpcurl ) { |
| 81 |
this(); |
| 82 |
put( SECRET_KEY, secret ); |
| 83 |
put( XMLRPCURL_KEY, xmlrpcurl ); |
| 84 |
put( VERSION_KEY, DEFAULT_VERSION_VALUE ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the value of the {@link #XMLRPCURL_KEY} key. |
| 89 |
* |
| 90 |
* @return hub XML-RPC connection URL |
| 91 |
*/ |
| 92 |
public URL getXmlrpcUrl() { |
| 93 |
return getUrl( XMLRPCURL_KEY ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Returns the value of the {@link #VERSION_KEY} key. |
| 98 |
* |
| 99 |
* @return version of the SAMP standard profile implemented |
| 100 |
*/ |
| 101 |
public String getVersion() { |
| 102 |
return getString( VERSION_KEY ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns the value of the {@link #SECRET_KEY} key. |
| 107 |
* |
| 108 |
* @return password for hub connection |
| 109 |
*/ |
| 110 |
public String getSecret() { |
| 111 |
return getString( SECRET_KEY ); |
| 112 |
} |
| 113 |
|
| 114 |
public void check() { |
| 115 |
super.check(); |
| 116 |
checkHasKeys( new String[] { SECRET_KEY, XMLRPCURL_KEY, } ); |
| 117 |
for ( Iterator it = entrySet().iterator(); it.hasNext(); ) { |
| 118 |
Map.Entry entry = (Map.Entry) it.next(); |
| 119 |
Object key = entry.getKey(); |
| 120 |
if ( key instanceof String ) { |
| 121 |
if ( ! TOKEN_REGEX.matcher( key.toString() ).matches() ) { |
| 122 |
throw new DataException( "Bad key syntax: " + key + |
| 123 |
" does not match " + |
| 124 |
TOKEN_REGEX.pattern() ); |
| 125 |
} |
| 126 |
} |
| 127 |
else { |
| 128 |
throw new DataException( "Map key " + entry.getKey() |
| 129 |
+ " is not a string" ); |
| 130 |
} |
| 131 |
Object value = entry.getValue(); |
| 132 |
if ( value instanceof String ) { |
| 133 |
String sval = (String) value; |
| 134 |
for ( int i = 0; i < sval.length(); i++ ) { |
| 135 |
int c = sval.charAt( i ); |
| 136 |
if ( c < 0x20 || c > 0x7f ) { |
| 137 |
throw new DataException( "Value contains illegal " |
| 138 |
+ "character 0x" |
| 139 |
+ Integer.toHexString( c ) ); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
else { |
| 144 |
throw new DataException( "Map value " + value + |
| 145 |
" is not a string" ); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns a LockInfo as read from a lockfile at a given location. |
| 152 |
* If the lockfile does not exist, null is returned. |
| 153 |
* An exception may be thrown if it exists but is cannot be read. |
| 154 |
* |
| 155 |
* @param url lockfile location |
| 156 |
* @return lockfile contents, or null if it is absent |
| 157 |
*/ |
| 158 |
public static LockInfo readLockFile( URL url ) throws IOException { |
| 159 |
final InputStream in; |
| 160 |
File file = SampUtils.urlToFile( url ); |
| 161 |
if ( file != null ) { |
| 162 |
if ( file.exists() ) { |
| 163 |
in = new FileInputStream( file ); |
| 164 |
} |
| 165 |
else { |
| 166 |
return null; |
| 167 |
} |
| 168 |
} |
| 169 |
else { |
| 170 |
try { |
| 171 |
in = url.openStream(); |
| 172 |
} |
| 173 |
catch ( IOException e ) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
} |
| 177 |
return readLockFile( in ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns the LockInfo read from a given stream. |
| 182 |
* The stream is closed if the read is successful. |
| 183 |
* |
| 184 |
* @param in input stream to read |
| 185 |
* @return lockfile information |
| 186 |
*/ |
| 187 |
public static LockInfo readLockFile( InputStream in ) throws IOException { |
| 188 |
LockInfo info = new LockInfo(); |
| 189 |
in = new BufferedInputStream( in ); |
| 190 |
for ( String line; ( line = readLine( in ) ) != null; ) { |
| 191 |
Matcher assigner = ASSIGNMENT_REGEX.matcher( line ); |
| 192 |
if ( assigner.matches() ) { |
| 193 |
info.put( assigner.group( 1 ), assigner.group( 2 ) ); |
| 194 |
} |
| 195 |
else if ( COMMENT_REGEX.matcher( line ).matches() ) { |
| 196 |
} |
| 197 |
else if ( line.length() == 0 ) { |
| 198 |
} |
| 199 |
else { |
| 200 |
logger_.warning( "Ignoring lockfile line with bad syntax" ); |
| 201 |
logger_.info( "Bad line: " + line ); |
| 202 |
} |
| 203 |
} |
| 204 |
in.close(); |
| 205 |
return info; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Returns a given map as a LockInfo object. |
| 210 |
* |
| 211 |
* @param map map |
| 212 |
* @return lock info |
| 213 |
*/ |
| 214 |
public static LockInfo asLockInfo( Map map ) { |
| 215 |
return map instanceof LockInfo ? (LockInfo) map |
| 216 |
: new LockInfo( map ); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns a line from a lockfile-type input stream. |
| 222 |
* |
| 223 |
* @param in input stream |
| 224 |
* @return next line |
| 225 |
*/ |
| 226 |
private static String readLine( InputStream in ) throws IOException { |
| 227 |
StringBuffer sbuf = new StringBuffer(); |
| 228 |
while ( true ) { |
| 229 |
int c = in.read(); |
| 230 |
switch ( c ) { |
| 231 |
case '\r': |
| 232 |
case '\n': |
| 233 |
return sbuf.toString(); |
| 234 |
case -1: |
| 235 |
return sbuf.length() > 0 ? sbuf.toString() : null; |
| 236 |
default: |
| 237 |
sbuf.append( (char) c ); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |