// 2012-08-29 johnpfeiffer requires JohnStringUtils, validation-api, bval-core, bval-jsr303, commons-beanutils-core, commons-lang3
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Properties;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
public class StorageGatewayServiceDAO
{
protected static final String STORAGEGATEWAYAPPPROPERTIES = "/var/lib/tomcat6/webapps/storagegateway/WEB-INF/app.properties";
protected static final String PROPERTYHEADER = "storagegateway.";
@NotNull( message = "ERROR: StorageGatewayService cannot be null" )
private StorageGatewayService storagegateway = null;
public static class Builder
{
@NotNull( message = "ERROR: OID cannot be null" )
private String oid = null;
@NotNull( message = "ERROR: Hash cannot be null" )
private String hash = null;
@NotNull( message = "ERROR: Domain cannot be null" )
private URI domain = null;
@NotNull( message = "ERROR: StorageGatewayService cannot be null" )
private StorageGatewayService storagegateway = null;
@NotNull( message = "ERROR: appProperties cannot be null" )
private Properties appProperties = null;
// read from data source
Builder() throws InstantiationException
{
try
{
initializeDataSource();
if( appProperties == null )
{
throw new InstantiationException( "ERROR: unable to load the existing properties" );
}
}catch( FileNotFoundException e )
{
throw new InstantiationException( "ERROR: data source could not find file: " + STORAGEGATEWAYAPPPROPERTIES + " " + e.getMessage() );
}catch( IOException ioe )
{
throw new InstantiationException( "ERROR: data source access issue: " + STORAGEGATEWAYAPPPROPERTIES + " " + ioe.getMessage() );
}
oid = appProperties.getProperty( PROPERTYHEADER + StorageGatewayService.PARAMOID );
if( oid == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Storage Gateway OID" );
}
hash = appProperties.getProperty( PROPERTYHEADER + StorageGatewayService.PARAMHASH );
if( hash == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Storage Gateway Key" );
}
String downloadWeblinkPrefixUrl = appProperties.getProperty( PROPERTYHEADER + StorageGatewayService.PARAMDOWNLOADURL );
if( downloadWeblinkPrefixUrl == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Storage Gateway URL" );
}
URL url;
try
{
url = new URL( downloadWeblinkPrefixUrl );
String hostname = url.getHost();
this.domain = URI.create( hostname );
}catch( MalformedURLException e )
{
throw new InstantiationException( "ERROR: downloadURL from data source is a malformed url: " + domain + " " + downloadWeblinkPrefixUrl );
}
String storageGatewayType = appProperties.getProperty( PROPERTYHEADER + StorageGatewayService.PARAMSTORAGETYPE );
if( storageGatewayType == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Storage Gateway Type" );
}
if( storageGatewayType.equals( CIFSService.CIFSTORAGETYPE ) )
{
try
{
CIFSServiceDAO cifsDAO = new CIFSServiceDAO.Builder().build();
CIFSService cifs = cifsDAO.getCIFSService();
this.storagegateway = new StorageGatewayService.Builder().oid( this.oid ).hash( this.hash ).domain( this.domain ).cifs( cifs ).build();
if( storagegateway == null )
{
throw new InstantiationException( "ERROR: unable to initialize a CIFS storagegateway" );
}
}catch( IllegalArgumentException iae )
{
throw new InstantiationException( "ERROR: unable to get valid inputs from data source: " + iae.getMessage() );
}catch( IllegalStateException e )
{
throw new InstantiationException( "ERROR: data source Illegal State: " + e.getMessage() );
}
}
if( storageGatewayType.equals( AtmosService.ATMOSSTORAGETYPE ) )
{
String emcIpAddress = appProperties.getProperty( PROPERTYHEADER + AtmosService.PARAMEMCIPADDRESS );
if( emcIpAddress == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Atmos Server Hostname" );
}
String emcPortNumberString = appProperties.getProperty( PROPERTYHEADER + AtmosService.PARAMEMCPORT );
if( emcPortNumberString == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Atmos Server Port" );
}
int emcPortNumber = Integer.parseInt( emcPortNumberString );
String emcUid = appProperties.getProperty( PROPERTYHEADER + AtmosService.PARAMEMCUID );
if( emcUid == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Atmos UID" );
}
String emcSharedSecret = appProperties.getProperty( PROPERTYHEADER + AtmosService.PARAMEMCSHAREDSECRET );
if( emcSharedSecret == null )
{
throw new InstantiationException( "ERROR: unable to load the existing Atmos Shared Secret" );
}
try
{
AtmosService atmos = new AtmosService.Builder().isAvailable( true ).hostname( emcIpAddress ).port( emcPortNumber ).uid( emcUid )
.sharedSecret( emcSharedSecret ).build();
this.storagegateway = new StorageGatewayService.Builder().oid( this.oid ).hash( this.hash ).domain( this.domain ).atmos( atmos ).build();
}catch( IllegalArgumentException iae )
{
throw new InstantiationException( "ERROR: loading Atmos unable to get valid inputs from data source: " + STORAGEGATEWAYAPPPROPERTIES + ":"
+ iae.getMessage() );
}
}
if( storageGatewayType.equals( NirvanixService.NIRVANIXSTORAGETYPE ) )
{
String nirvanixAPIHost = appProperties.getProperty( PROPERTYHEADER + NirvanixService.PARAMNIRVANIXAPIHOSTNAME );
String nirvanixUser = appProperties.getProperty( PROPERTYHEADER + NirvanixService.PARAMNIRVANIXUSERNAME );
String nirvanixPassword = appProperties.getProperty( PROPERTYHEADER + NirvanixService.PARAMNIRVANIXPASSWORD );
String nirvanixAppKey = appProperties.getProperty( PROPERTYHEADER + NirvanixService.PARAMNIRVANIXAPPKEY );
String nirvanixAppName = appProperties.getProperty( PROPERTYHEADER + NirvanixService.PARAMNIRVANIXAPPNAME );
try
{
NirvanixService nirvanix = new NirvanixService.Builder().isAvailable( true ).apihostname( nirvanixAPIHost ).user( nirvanixUser ).password( nirvanixPassword )
.appkey( nirvanixAppKey ).appname( nirvanixAppName ).build();
this.storagegateway = new StorageGatewayService.Builder().oid( this.oid ).hash( this.hash ).domain( this.domain ).nirvanix( nirvanix ).build();
}catch( IllegalArgumentException iae )
{
throw new InstantiationException( "ERROR: loading Nirvanix unable to get valid inputs from data source: " + STORAGEGATEWAYAPPPROPERTIES + ":"
+ iae.getMessage() );
}
}
}
public Builder oid( String value )
{
this.oid = value;
return this;
}
public Builder hash( String value )
{
this.hash = value;
return this;
}
public Builder domain( String value )
{
this.domain = URI.create( value );
return this;
}
public Builder cifs( CIFSService cifs )
{
this.storagegateway = new StorageGatewayService.Builder().oid( this.oid ).hash( this.hash ).domain( this.domain ).cifs( cifs ).build();
return this;
}
public Builder nirvanix( NirvanixService newNirvanix )
{
this.storagegateway = new StorageGatewayService.Builder().oid( this.oid ).hash( this.hash ).domain( this.domain ).nirvanix( newNirvanix ).build();
return this;
}
public Builder atmos( AtmosService newAtmos )
{
this.storagegateway = new StorageGatewayService.Builder().oid( this.oid ).hash( this.hash ).domain( this.domain ).atmos( newAtmos ).build();
return this;
}
public StorageGatewayServiceDAO build()
{
StorageGatewayServiceDAO sgwDAO = new StorageGatewayServiceDAO( this );
StorageGatewayServiceDAOValidator( sgwDAO );
return sgwDAO;
}
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
private void StorageGatewayServiceDAOValidator( StorageGatewayServiceDAO sgwDAO ) throws IllegalArgumentException , IllegalStateException
{
Set <ConstraintViolation <StorageGatewayServiceDAO>> violations = validator.validate( sgwDAO );
if( !violations.isEmpty() )
{
for( ConstraintViolation <?> v : violations )
{
throw new IllegalArgumentException( v.getMessage() );
}
}
// ConstraintViolations ensure all of the values are non null
if( JohnStringUtils.containsWhiteSpace( this.oid ) )
{
throw new IllegalArgumentException( "ERROR: OID cannot contain whitespace" );
}
if( JohnStringUtils.containsWhiteSpace( this.hash ) )
{
throw new IllegalArgumentException( "ERROR: Hash cannot contain whitespace" );
}
}
protected void initializeDataSource() throws FileNotFoundException , IOException
{
FileSystem fs = new FileSystem();
String pathFile = "";
pathFile = fs.convertFileSystemPath( STORAGEGATEWAYAPPPROPERTIES );
if( pathFile == null )
{
throw new IllegalArgumentException( "ERROR: pathFile cannot be null" );
}
if( pathFile.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: pathFile cannot be empty" );
}
FileInputStream fileInputStream = new FileInputStream( pathFile );
InputStreamReader inStream = new InputStreamReader( fileInputStream );
appProperties = new Properties();
appProperties.load( inStream );
}
} // end inner class Builder
private StorageGatewayServiceDAO( Builder builder )
{
this.storagegateway = builder.storagegateway;
}
// TODO: NFS, S3, builds Object fromPersistence (file)
// If pushing a new config then don't read the Type from the persistence
protected String getHTMLForm( String type ) throws IllegalArgumentException
{
if( type == null )
{
throw new IllegalArgumentException( "When getting the storage HTML form type cannot be null" );
}
StringBuilder strb = new StringBuilder();
if( storagegateway != null )
{
if( type.isEmpty() )
{
type = storagegateway.getType();
}
String newline = "<br /><br />" + System.getProperty( "line.separator" );
String space = " ";
strb.append( newline + "<label>Oid: </label>" + space + space + "<input type='text' name='" + StorageGatewayService.PARAMOID + "' size='40' value='"
+ JohnStringUtils.safeHTML( storagegateway.getOid() ) + "' /> " + newline );
strb.append( "<label>Key: </label>" + space + space + "<input type='text' name='" + StorageGatewayService.PARAMHASH + "' size='40' value='"
+ JohnStringUtils.safeHTML( storagegateway.getHash() ) + "' /> " + newline );
strb.append( "<label>Public DNS name for the OxygenVM: </label>" + space + space + "<input type='text' name='" + StorageGatewayService.PARAMDOMAIN
+ "' size='40' value='" + JohnStringUtils.safeHTML( storagegateway.getDomain() ) + "' /> " + newline + newline );
if( type.equals( CIFSService.CIFSTORAGETYPE ) )
{
strb.append( "<input type='hidden' name='" + StorageGatewayService.PARAMSTORAGETYPE + "' value='" + CIFSService.CIFSTORAGETYPE + "' /> " + newline );
strb.append( storagegateway.getCIFSHTMLForm() );
strb.append( newline );
}else if( type.equals( AtmosService.ATMOSSTORAGETYPE ) )
{
strb.append( "<input type='hidden' name='" + StorageGatewayService.PARAMSTORAGETYPE + "' value='" + AtmosService.ATMOSSTORAGETYPE + "' /> " + newline );
strb.append( storagegateway.getAtmosHTMLForm() );
strb.append( AtmosService.getValidateFormOnSubmit() );
}else if( type.equals( NirvanixService.NIRVANIXSTORAGETYPE ) )
{
strb.append( "<input type='hidden' name='" + StorageGatewayService.PARAMSTORAGETYPE + "' value='" + NirvanixService.NIRVANIXSTORAGETYPE + "' /> "
+ newline );
strb.append( storagegateway.getNirvanixHTMLForm() );
strb.append( NirvanixService.getValidateFormOnSubmit() );
}
}else
{
strb.append( "ERROR: Unable to retrieve data" );
}
return strb.toString();
}
protected String getHTMLFormOnSubmitFunctionCall()
{
String functionCall = " onsubmit='return validateFormOnSubmit()' ";
return functionCall;
}
// TODO: save NFS, S3
protected String saveService() throws InstantiationException , IllegalStateException
{
FileSystem fs = null;
String fileLocation = null;
try
{
fs = new FileSystem();
fileLocation = fs.convertFileSystemPath( STORAGEGATEWAYAPPPROPERTIES );
if( fs == null || fileLocation == null || fileLocation.isEmpty() )
{
throw new IOException();
}
}catch( IOException e )
{
throw new InstantiationException( "ERROR: could not access the data source " + STORAGEGATEWAYAPPPROPERTIES );
}
StringBuilder strb = new StringBuilder();
String type = storagegateway.getType();
if( type == null || type.isEmpty() )
{
throw new IllegalStateException( "ERROR: StorageGateway Type is null or empty " );
}
if( isSaveAvailable( type ) )
{
try
{
fs.writeStringToFile( storagegateway.getString( "storagegateway." ) , STORAGEGATEWAYAPPPROPERTIES );
if( type.equals( AtmosService.ATMOSSTORAGETYPE ) )
{
strb.append( "Atmos storage settings saved successfully." );
}else if( type.equals( NirvanixService.NIRVANIXSTORAGETYPE ) )
{
strb.append( "Nirvanix storage settings saved successfully." );
}else if( type.equals( CIFSService.CIFSTORAGETYPE ) )
{
CIFSService cifs = storagegateway.getCIFS();
CIFSServiceDAO cifsDAO = new CIFSServiceDAO.Builder().cifs( cifs ).build();
cifsDAO.writeToFile();
strb.append( "CIFS storage settings saved successfully." );
}
}catch( IOException ioe )
{
strb.append( "ERROR: Settings unchanged, IOException " + ioe.getMessage() );
}
}else
{
strb.append( storagegateway.getType() + " save not yet available" );
}
if( strb.toString().isEmpty() )
{
strb.append( "Unknown Error: unable to save, configuration not changed." );
}
return strb.toString();
}
private boolean isSaveAvailable( String type )
{
boolean result = false;
if( type.equals( AtmosService.ATMOSSTORAGETYPE ) || type.equals( NirvanixService.NIRVANIXSTORAGETYPE ) || type.equals( CIFSService.CIFSTORAGETYPE ) )
{
result = true;
}
return result;
}
} // end class