| 1 |
package org.astrogrid.samp.client; |
| 2 |
|
| 3 |
import java.util.logging.Logger; |
| 4 |
import org.astrogrid.samp.Platform; |
| 5 |
import org.astrogrid.samp.web.WebClientProfile; |
| 6 |
import org.astrogrid.samp.xmlrpc.StandardClientProfile; |
| 7 |
|
| 8 |
/** |
| 9 |
* Factory which supplies the default ClientProfile for use by SAMP clients. |
| 10 |
* By using this class to obtain ClientProfile instances, applications |
| 11 |
* can be used with non-standard profiles supplied at runtime without |
| 12 |
* requiring any code changes. |
| 13 |
* |
| 14 |
* <p>The profile returned by this class depends on the SAMP_HUB environment |
| 15 |
* variable ({@link #HUBLOC_ENV}). |
| 16 |
* If it consists of the prefix "<code>jsamp-class:</code>" |
| 17 |
* ({@link #HUBLOC_CLASS_PREFIX}) followed by the classname of a class |
| 18 |
* which implements {@link ClientProfile} and has a no-arg constructor, |
| 19 |
* then an instance of the named class is used. |
| 20 |
* Otherwise, an instance of {@link StandardClientProfile} or |
| 21 |
* {@link WebClientProfile} is returned. |
| 22 |
* |
| 23 |
* @author Mark Taylor |
| 24 |
* @since 4 Aug 2009 |
| 25 |
*/ |
| 26 |
public class DefaultClientProfile { |
| 27 |
|
| 28 |
private static ClientProfile profile_; |
| 29 |
private static final Logger logger_ = |
| 30 |
Logger.getLogger( DefaultClientProfile.class.getName() ); |
| 31 |
|
| 32 |
/** Environment variable used for hub location ({@value}). */ |
| 33 |
public static final String HUBLOC_ENV = "SAMP_HUB"; |
| 34 |
|
| 35 |
/** |
| 36 |
* Prefix for SAMP_HUB env var indicating a supplied ClientProfile |
| 37 |
* implementation ({@value}). |
| 38 |
*/ |
| 39 |
public static final String HUBLOC_CLASS_PREFIX = "jsamp-class:"; |
| 40 |
|
| 41 |
/** |
| 42 |
* No-arg constructor prevents instantiation. |
| 43 |
*/ |
| 44 |
private DefaultClientProfile() { |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns a ClientProfile instance suitable for general purpose use. |
| 49 |
* By default this is currently the Standard Profile |
| 50 |
* ({@link org.astrogrid.samp.xmlrpc.StandardClientProfile#getInstance |
| 51 |
* StandardClientProfile.getInstance()}), |
| 52 |
* but the instance may be modified programatically or by use of |
| 53 |
* the SAMP_HUB environment variable. |
| 54 |
* |
| 55 |
* <p>If no instance has been set, the SAMP_HUB environment variable |
| 56 |
* is examined. If it consists of the prefix "<code>jsamp-class:</code>" |
| 57 |
* ({@link #HUBLOC_CLASS_PREFIX}) followed by the classname of a class |
| 58 |
* which implements {@link ClientProfile} and has a no-arg constructor, |
| 59 |
* then an instance of the named class is used. |
| 60 |
* Otherwise, an instance of {@link StandardClientProfile} is returned. |
| 61 |
* |
| 62 |
* <p>The instance is obtained lazily. |
| 63 |
* |
| 64 |
* @return client profile instance |
| 65 |
*/ |
| 66 |
public static ClientProfile getProfile() { |
| 67 |
if ( profile_ == null ) { |
| 68 |
final ClientProfile profile; |
| 69 |
String hubloc = Platform.getPlatform().getEnv( HUBLOC_ENV ); |
| 70 |
if ( hubloc == null || hubloc.trim().length() == 0 ) { |
| 71 |
profile = StandardClientProfile.getInstance(); |
| 72 |
} |
| 73 |
else if ( hubloc.startsWith( HUBLOC_CLASS_PREFIX ) ) { |
| 74 |
String cname = hubloc.substring( HUBLOC_CLASS_PREFIX.length() ); |
| 75 |
final Class clazz; |
| 76 |
try { |
| 77 |
clazz = Class.forName( cname ); |
| 78 |
} |
| 79 |
catch ( ClassNotFoundException e ) { |
| 80 |
throw (IllegalArgumentException) |
| 81 |
new IllegalArgumentException( "No profile class " |
| 82 |
+ cname ) |
| 83 |
.initCause( e ); |
| 84 |
} |
| 85 |
try { |
| 86 |
profile = (ClientProfile) clazz.newInstance(); |
| 87 |
logger_.info( "Using non-standard hub location: " |
| 88 |
+ HUBLOC_ENV + "=" + hubloc ); |
| 89 |
} |
| 90 |
catch ( Throwable e ) { |
| 91 |
throw (RuntimeException) |
| 92 |
new RuntimeException( "Error instantiating custom " |
| 93 |
+ "profile " + clazz.getName() ) |
| 94 |
.initCause( e ); |
| 95 |
} |
| 96 |
} |
| 97 |
else if ( hubloc.startsWith( StandardClientProfile |
| 98 |
.STDPROFILE_HUB_PREFIX ) ) { |
| 99 |
profile = StandardClientProfile.getInstance(); |
| 100 |
} |
| 101 |
else if ( hubloc.startsWith( WebClientProfile |
| 102 |
.WEBPROFILE_HUB_PREFIX ) ) { |
| 103 |
profile = WebClientProfile.getInstance(); |
| 104 |
} |
| 105 |
else { |
| 106 |
throw new RuntimeException( "Can't make sense of " + HUBLOC_ENV |
| 107 |
+ "=" + hubloc ); |
| 108 |
} |
| 109 |
profile_ = profile; |
| 110 |
} |
| 111 |
return profile_; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Sets the profile object which will be returned by {@link #getProfile}. |
| 116 |
* |
| 117 |
* @param profile default profile instance |
| 118 |
*/ |
| 119 |
public static void setProfile( ClientProfile profile ) { |
| 120 |
profile_ = profile; |
| 121 |
} |
| 122 |
} |