// 2012-08-29 johnpfeiffer requires JohnStringUtils, validation-api, bval-core, bval-jsr303, commons-beanutils-core, commons-lang3, outbound internet connection for InetAddress.getByName(String)
// TODO: NFS
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
public class StorageGatewayService
{
protected static final String CLASSVERSION = "2.8.6";
protected static final String PARAMSTORAGEGATEWAYTYPES = "# storageAdaptor,EMCstorageAdaptor,CIFSstorageAdaptor,NFSstorageAdaptor,S3storageAdaptor,NirvanixRESTstorageAdaptor,KeylessStorageAdaptor" ;
protected static final String PARAMSTORAGETYPE = "storageGatewayType";
protected static final String PARAMOID = "storageGatewayOid";
protected static final String PARAMHASH = "storageGatewaySignedHash";
protected static final String PARAMDOMAIN = "domain";
protected static final String PARAMDOWNLOADURL = "downloadWeblinkPrefixURL";
protected static final String PARAMUPLOADURL = "directUploadPrefixUrl";
protected static final String PARAMTHRIFTSOCKETPORT = "thrift.socket.port";
protected static final String PARAMENCRYPTIONPROVIDERMASTERKEY = "encryptionProvider.masterkey";
protected static final String PARAMBLOCKSIZE = "blockSizeInKB";
protected static final String PARAMSESSIONFOLDERPATH = "sessionFolderPath";
protected static final String PARAMPERSISTENTSESSIONTRACKERREF = "persistentSessionTrackerRef";
protected static final String PARAMJDBCMAXRESULTS = "jdbc.maxResults";
protected static final String PARAMJDBCMAXPOOLSIZE = "jdbc.maxPoolSize";
protected static final String PARAMRDBMSMAXLISTRESULT = "rdbms.maxListResult";
protected static final String PARAMJDBCDATABASEDRIVER = "jdbc.databaseDriver";
protected static final String PARAMJDBCDATABASEURL = "jdbc.databaseURL";
protected static final String PARAMJDBCDATABASEUSERNAME = "jdbc.databaseUsername";
protected static final String PARAMJDBCDATABASEPASSWORD = "jdbc.databasePassword";
protected static final String PARAMJDBCDATABASEDIALECT = "jdbc.databaseDialect";
protected static final String PARAMUSECOMMONCACHELOCK = "useCommonCacheLock";
protected static final String PARAMCACHEPOOL = "cache.cachePool";
protected static final String PARAMCACHEFACTORY = "cache.factory";
protected static final String PARAMCACHEHOSTS = "cache.hosts";
protected static final String PARAMCACHEEXPIRATIONMINUTES = "cache.cacheExpiryInMinute";
protected static final String PARAMCACHEMAXIDLE = "cache.maxIdle";
protected static final String PARAMCACHEMAXACTIVE = "cache.maxActive";
protected static final String PARAMCACHEMAXWAITINMILLISEC = "cache.maxWaitInMillisec";
protected static final String PARAMROOTFOLDER = "rootFolder";
protected static final String PARAMCHUNKFILESYSTEMISAVAILABLE = "chunkFilesystem.isAvailable";
protected static final String PARAMCHUNKFILESYSTEMSTORAGEPATH = "chunkFilesystem.storagePath";
protected static final String PARAMFULLFILESYSTEMISAVAILABLE = "fullFilesystem.isAvailable";
protected static final String PARAMFULLFILESYSTEMSTORAGEPATH = "fullFilesystem.storagePath";
protected static final String LOCALSTORAGETYPE = "storageAdaptor";
protected static final String NFSSTORAGETYPE = "NFSstorageAdaptor";
protected static final String S3STORAGETYPE = "S3storageAdaptor";
protected static final String AMPLIDATASTORAGETYPE = "AmplidatastorageAdaptor";
protected static final String SWIFTSTORAGETYPE = "SwiftstorageAdaptor";
protected static final String DEFAULTTYPE = "";
protected static final String DEFAULTOID = "not-configured";
protected static final String DEFAULTHASH = "not-configured";
protected static final String DEFAULTDOMAIN = "not-configured.oxygencloud.com";
protected static final String DEFAULTDOWNLOADURL = "https://" + DEFAULTDOMAIN + "/storagegateway/download";
protected static final String DEFAULTUPLOADURL = "https://" + DEFAULTDOMAIN + "/storagegateway/upload";
protected static final String DEFAULTSESSIONFOLDERPATH = "/var/lib/tomcat6/oxygen-storagegateway/session";
protected static final String DEFAULTPERSISTENCEMETHOD = "flatfilesPersistenceSessionTracker"; // alternate=rdbmsPersistenceSessionTracker
protected static final boolean DEFAULTUSECOMMONCACHELOCK = false;
protected static final int DEFAULTJDBCMAXRESULTS = 1000;
protected static final int DEFAULTJDBCMAXPOOLSIZE = 300;
protected static final int DEFAULTRDBMSMAXLISTRESULT = 50;
protected static final String DEFAULTJDBCDATABASEDRIVER = "";
protected static final String DEFAULTJDBCDATABASEURL = "";
protected static final String DEFAULTJDBCDATABASEUSERNAME = "";
protected static final String DEFAULTJDBCDATABASEPASSWORD = "";
protected static final String DEFAULTJDBCDATABASEDIALECT = "";
protected static final int DEFAULTTHRIFTPORT = 9005;
protected static final int DEFAULTBLOCKSIZE = 512;
protected static final String DEFAULTENCRYPTIONMASTERKEY = "\"masterkey!\"";
protected static final String DEFAULTCACHEPOOL = "storagegateway.cache.queueableCachePool";
protected static final String DEFAULTCACHEFACTORY = "storagegateway.cache.memcachedFactory";
protected static final String DEFAULTCACHEHOSTS = "localhost:11211";
protected static final int DEFAULTCACHEEXPIRATIONMINUTES = 1;
protected static final int DEFAULTCACHEMAXIDLE = 8;
protected static final int DEFAULTCACHEMAXACTIVE = 8;
protected static final int DEFAULTCACHEMAXWAITINMILLISEC = 100;
protected static final boolean DEFAULTCHUNKFILESYSTEMISAVAILABLE = false;
protected static final String DEFAULTCHUNKFILESYSTEMSTORAGEPATH = "";
protected static final boolean DEFAULTFULLFILESYSTEMISAVAILABLE = false;
protected static final String DEFAULTFULLFILESYSTEMSTORAGEPATH = "";
protected static final String DEFAULTROOTFOLDER = "";
@NotNull( message = "ERROR: OID cannot be null" )
private String storageGatewayOid = DEFAULTOID;
@NotNull( message = "ERROR: Key (Hash) cannot be null" )
private String storageGatewaySignedHash = DEFAULTHASH;
@NotNull( message = "ERROR: Type cannot be null" )
private String storageGatewayType = DEFAULTTYPE;
private boolean chunkFilesystemIsAvailable = DEFAULTCHUNKFILESYSTEMISAVAILABLE;
@NotNull( message = "ERROR: chunkFilesystem.storagePath cannot be null" )
private String chunkFilesystemStoragePath = DEFAULTCHUNKFILESYSTEMSTORAGEPATH;
@NotNull( message = "ERROR: Domain cannot be null" )
private URI domain = URI.create( DEFAULTDOMAIN );
@NotNull( message = "ERROR: Download URL cannot be null" )
private URI downloadWeblinkPrefixUrl = URI.create( DEFAULTDOWNLOADURL );
@NotNull( message = "ERROR: Upload URL cannot be null" )
private URI directUploadPrefixUrl = URI.create( DEFAULTUPLOADURL );
@NotNull( message = "ERROR: CIFS object cannot be null" )
private CIFSService cifs;
@NotNull( message = "ERROR: Atmos object cannot be null" )
private AtmosService atmos;
@NotNull( message = "ERROR: Nirvanix object cannot be null" )
private NirvanixService nirvanix;
@NotNull( message = "ERROR: S3 object cannot be null" )
private S3Service s3;
public static class Builder
{
@NotNull( message = "ERROR: OID cannot be null" )
private String storageGatewayOid = DEFAULTOID;
@NotNull( message = "ERROR: Hash cannot be null" )
private String storageGatewaySignedHash = DEFAULTHASH;
@NotNull( message = "ERROR: Type cannot be null" )
private String storageGatewayType = DEFAULTTYPE;
private boolean chunkFilesystemIsAvailable = DEFAULTCHUNKFILESYSTEMISAVAILABLE;
@NotNull( message = "ERROR: chunkFilesystem.storagePath cannot be null" )
private String chunkFilesystemStoragePath = DEFAULTCHUNKFILESYSTEMSTORAGEPATH;
@NotNull( message = "ERROR: Domain cannot be null" )
private URI domain;
@NotNull( message = "ERROR: Download URL cannot be null" )
private URI downloadWeblinkPrefixUrl;
@NotNull( message = "ERROR: Upload URL cannot be null" )
private URI directUploadPrefixUrl;
@NotNull( message = "ERROR: CIFS object cannot be null" )
private CIFSService cifs = new CIFSService.Builder().build();
@NotNull( message = "ERROR: Atmos object cannot be null" )
private AtmosService atmos = new AtmosService.Builder().build();
@NotNull( message = "ERROR: Nirvanix object cannot be null" )
private NirvanixService nirvanix = new NirvanixService.Builder().build();
@NotNull( message = "ERROR: S3 object cannot be null" )
private S3Service s3 = new S3Service();
public Builder()
{
this.domain = URI.create( DEFAULTDOMAIN );
this.downloadWeblinkPrefixUrl = URI.create( DEFAULTDOWNLOADURL );
this.directUploadPrefixUrl = URI.create( DEFAULTUPLOADURL );
}
public Builder oid( String value )
{
this.storageGatewayOid = value;
return this;
}
public Builder hash( String value )
{
this.storageGatewaySignedHash = value;
return this;
}
// TODO: better way to embed the https and traililng
public Builder domain( URI value )
{
this.domain = value;
// TODO: two levels of validation required?
if( domain != null )
{
this.downloadWeblinkPrefixUrl = URI.create( "https://" + this.domain.toString() + "/storagegateway/download" );
this.directUploadPrefixUrl = URI.create( "https://" + this.domain.toString() + "/storagegateway/upload" );
}
return this;
}
public Builder cifs( CIFSService value )
{
this.storageGatewayType = CIFSService.CIFSTORAGETYPE;
this.chunkFilesystemIsAvailable = true;
this.chunkFilesystemStoragePath = CIFSServiceDAO.PARAMCIFSSTORAGEFOLDER;
this.cifs = value;
return this;
}
public Builder atmos( AtmosService value )
{
this.storageGatewayType = AtmosService.ATMOSSTORAGETYPE;
this.atmos = value;
return this;
}
public Builder nirvanix( NirvanixService value )
{
if( value == null )
{
throw new IllegalArgumentException( "ERROR: Nirvanix cannot be null" );
}
this.storageGatewayType = NirvanixService.NIRVANIXSTORAGETYPE;
this.nirvanix = value;
return this;
}
public Builder s3( S3Service value ) throws IllegalArgumentException
{
if( value == null )
{
throw new IllegalArgumentException( "ERROR: S3 cannot be null" );
}
this.storageGatewayType = StorageGatewayService.S3STORAGETYPE;
this.s3 = value;
return this;
}
public StorageGatewayService build()
{
StorageGatewayService sgw = new StorageGatewayService( this );
StorageGatewayServiceValidator( sgw );
return sgw;
}
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
private void StorageGatewayServiceValidator( StorageGatewayService sgw ) throws IllegalArgumentException
{
Set <ConstraintViolation <StorageGatewayService>> violations = validator.validate( sgw );
if( !violations.isEmpty() )
{
for( ConstraintViolation <?> v : violations )
{
throw new IllegalArgumentException( v.getMessage() );
}
}
// ConstraintViolations ensure all of the values are non null
if( this.storageGatewayOid.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: OID cannot be empty" );
}
if( JohnStringUtils.containsWhiteSpace( this.storageGatewayOid ) )
{
throw new IllegalArgumentException( "ERROR: OID cannot contain whitespace = " + this.storageGatewayOid );
}
if( !this.storageGatewayOid.contains( "-" ) )
{
throw new IllegalArgumentException( "ERROR: OID must contain a dash '-' " );
}
if( this.storageGatewaySignedHash.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: Hash cannot be empty" );
}
if( JohnStringUtils.containsWhiteSpace( this.storageGatewaySignedHash ) )
{
throw new IllegalArgumentException( "ERROR: Hash cannot contain whitespace = " + this.storageGatewaySignedHash );
}
if( !this.storageGatewaySignedHash.contains( "-" ) )
{
throw new IllegalArgumentException( "ERROR: Hash must contain a dash '-' " );
}
try
{
this.directUploadPrefixUrl.toURL();
}catch( MalformedURLException mfe )
{
throw new IllegalArgumentException( "ERROR: Upload URL is malformed = " + this.directUploadPrefixUrl.toString() );
}
try
{
this.downloadWeblinkPrefixUrl.toURL();
}catch( MalformedURLException mfe )
{
throw new IllegalArgumentException( "ERROR: Download URL is malformed = " + this.downloadWeblinkPrefixUrl.toString() );
}
}
} // end inner class Builder
// private StorageGatewayService( Builder builder ) throws IllegalStateException
private StorageGatewayService( Builder builder )
{
this.storageGatewayOid = builder.storageGatewayOid;
this.storageGatewaySignedHash = builder.storageGatewaySignedHash;
this.domain = builder.domain;
this.directUploadPrefixUrl = builder.directUploadPrefixUrl;
this.downloadWeblinkPrefixUrl = builder.downloadWeblinkPrefixUrl;
this.chunkFilesystemIsAvailable = builder.chunkFilesystemIsAvailable;
this.chunkFilesystemStoragePath = builder.chunkFilesystemStoragePath;
this.storageGatewayType = builder.storageGatewayType;
this.cifs = builder.cifs;
this.atmos = builder.atmos;
this.nirvanix = builder.nirvanix;
this.s3 = builder.s3;
}
protected String getOid()
{
return storageGatewayOid;
}
protected String getHash()
{
return storageGatewaySignedHash;
}
protected String getType()
{
return storageGatewayType;
}
protected String getDomain()
{
return domain.toString();
}
protected String getAtmosHTMLForm()
{
return atmos.getHTMLForm();
}
protected String getNirvanixHTMLForm()
{
return nirvanix.getHTMLForm();
}
protected String getCIFSHTMLForm()
{
return cifs.getHTMLForm();
}
protected CIFSService getCIFS()
{
return cifs;
}
// primary use is for saving to persistence (file) //TODO: cleanup
// LOCALSTORAGE vs CIFS vs NFS
protected String getString( String header )
{
if( header == null )
{
throw new IllegalArgumentException( "header cannot be null" );
}
String newline = System.getProperty( "line.separator" );
StringBuilder strb = new StringBuilder();
strb.append( "# version" + CLASSVERSION + newline );
strb.append( header + PARAMSTORAGETYPE + "=" + storageGatewayType + newline );
strb.append( header + PARAMOID + "=" + storageGatewayOid + newline );
strb.append( header + PARAMHASH + "=" + storageGatewaySignedHash + newline );
strb.append( header + PARAMDOWNLOADURL + "=" + downloadWeblinkPrefixUrl.toString() + newline );
strb.append( header + PARAMUPLOADURL + "=" + directUploadPrefixUrl.toString() + newline );
strb.append( newline );
strb.append( PARAMSTORAGEGATEWAYTYPES + newline );
strb.append( newline );
strb.append( header + PARAMPERSISTENTSESSIONTRACKERREF + "=" + DEFAULTPERSISTENCEMETHOD + newline );
strb.append( header + PARAMSESSIONFOLDERPATH + "=" + DEFAULTSESSIONFOLDERPATH + newline );
strb.append( header + PARAMTHRIFTSOCKETPORT + "=" + DEFAULTTHRIFTPORT + newline );
strb.append( header + PARAMENCRYPTIONPROVIDERMASTERKEY + "=" + DEFAULTENCRYPTIONMASTERKEY + newline );
strb.append( header + PARAMBLOCKSIZE + "=" + DEFAULTBLOCKSIZE + newline );
strb.append( newline );
strb.append( header + PARAMCHUNKFILESYSTEMISAVAILABLE + "=" + chunkFilesystemIsAvailable + newline );
strb.append( header + PARAMCHUNKFILESYSTEMSTORAGEPATH + "=" + chunkFilesystemStoragePath + newline );
strb.append( newline );
strb.append( header + PARAMFULLFILESYSTEMISAVAILABLE + "=" + DEFAULTFULLFILESYSTEMISAVAILABLE + newline );
strb.append( header + PARAMFULLFILESYSTEMSTORAGEPATH + "=" + DEFAULTFULLFILESYSTEMSTORAGEPATH + newline );
strb.append( header + PARAMROOTFOLDER + "=" + DEFAULTROOTFOLDER + newline );
strb.append( newline );
if( atmos == null )
{
System.out.println( "DEBUG: atmos in sgwservice.getString is null" );
}else if( atmos.getString( header ).isEmpty() )
{
System.out.println( "DEBUG: atmos in sgwservice.getString is empty" );
}else
{
strb.append( atmos.getString( header ) );
strb.append( newline );
}
if( nirvanix == null )
{
System.out.println( "DEBUG: nirvanix in sgwservice.getString is null" );
}else if( nirvanix.getString( header ).isEmpty() )
{
System.out.println( "DEBUG: nirvanix in sgwservice.getString is empty" );
}else
{
strb.append( nirvanix.getString( header ) );
strb.append( newline );
}
if( s3 == null )
{
System.out.println( "DEBUG: s3 in sgwservice.getString is null" );
}else if( s3.getString( header ).isEmpty() )
{
System.out.println( "DEBUG: s3 in sgwservice.getString is empty" );
}else
{
strb.append( s3.getString( header ) );
strb.append( newline );
}
strb.append( header + PARAMJDBCMAXRESULTS + "=" + DEFAULTJDBCMAXRESULTS + newline );
strb.append( header + PARAMJDBCMAXPOOLSIZE + "=" + DEFAULTJDBCMAXPOOLSIZE + newline );
strb.append( header + PARAMRDBMSMAXLISTRESULT + "=" + DEFAULTRDBMSMAXLISTRESULT + newline );
strb.append( header + PARAMJDBCDATABASEDRIVER + "=" + DEFAULTJDBCDATABASEDRIVER + newline );
strb.append( header + PARAMJDBCDATABASEURL + "=" + DEFAULTJDBCDATABASEURL + newline );
strb.append( header + PARAMJDBCDATABASEUSERNAME + "=" + DEFAULTJDBCDATABASEUSERNAME + newline );
strb.append( header + PARAMJDBCDATABASEPASSWORD + "=" + DEFAULTJDBCDATABASEPASSWORD + newline );
strb.append( header + PARAMJDBCDATABASEDIALECT + "=" + DEFAULTJDBCDATABASEDIALECT + newline );
strb.append( newline );
strb.append( header + PARAMUSECOMMONCACHELOCK + "=" + DEFAULTUSECOMMONCACHELOCK + newline );
strb.append( header + PARAMCACHEPOOL + "=" + DEFAULTCACHEPOOL + newline );
strb.append( header + PARAMCACHEFACTORY + "=" + DEFAULTCACHEFACTORY + newline );
strb.append( header + PARAMCACHEHOSTS + "=" + DEFAULTCACHEHOSTS + newline );
strb.append( header + PARAMCACHEEXPIRATIONMINUTES + "=" + DEFAULTCACHEEXPIRATIONMINUTES + newline );
strb.append( header + PARAMCACHEMAXIDLE + "=" + DEFAULTCACHEMAXIDLE + newline );
strb.append( header + PARAMCACHEMAXACTIVE + "=" + DEFAULTCACHEMAXACTIVE + newline );
strb.append( header + PARAMCACHEMAXWAITINMILLISEC + "=" + DEFAULTCACHEMAXWAITINMILLISEC + newline );
return strb.toString();
}
} // end class