@ -1,19 +1,17 @@
package org.gcube.smartgears.configuration.container ;
import static org.gcube.smartgears.Constants.default_container_publication_frequency_in_seconds ;
import static org.gcube.smartgears.utils.Utils.notNull ;
import java.io.File ;
import java.io.InputStream ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Map.Entry ;
import java.util.Set ;
import java.util.stream.Collectors ;
import javax.xml.bind.annotation.XmlAttribute ;
import javax.xml.bind.annotation.XmlElement ;
import javax.xml.bind.annotation.XmlElementRef ;
import javax.xml.bind.annotation.XmlRootElement ;
import javax.xml.bind.annotation.XmlTransient ;
import org.gcube.common.validator.ValidationError ;
@ -23,10 +21,16 @@ import org.gcube.common.validator.annotations.IsValid;
import org.gcube.common.validator.annotations.NotEmpty ;
import org.gcube.common.validator.annotations.NotNull ;
import org.gcube.smartgears.configuration.Mode ;
import org.gcube.smartgears.configuration.ProxyAddress ;
import org.gcube.smartgears.configuration.application.ApplicationConfiguration ;
import org.gcube.smartgears.configuration.application.DefaultApplicationConfiguration ;
import org.gcube.smartgears.persistence.DefaultPersistence ;
import org.gcube.smartgears.persistence.Persistence ;
import org.gcube.smartgears.persistence.LocalPersistence ;
import org.gcube.smartgears.persistence.PersistenceWriter ;
import org.gcube.smartgears.security.AuthorizationProvider ;
import org.gcube.smartgears.security.Credentials ;
import org.gcube.smartgears.security.DefaultAuthorizationProvider ;
import org.gcube.smartgears.utils.Utils ;
import org.ini4j.Ini ;
import org.ini4j.Profile.Section ;
/ * *
* The configuration of the container .
@ -34,78 +38,45 @@ import org.gcube.smartgears.persistence.Persistence;
* @author Fabio Simeoni
* @author Luca Frosini ( ISTI - CNR )
* /
@XmlRootElement ( name = "container" )
public class ContainerConfiguration {
@XmlAttribute
private Mode mode = Mode . online ;
@XmlElement
@NotNull @IsValid
String hostname ;
@XmlElement
@NotNull
Integer port ;
@XmlElement ( name = "authentication-endpoint" )
String authenticationEnpoint = null ;
@XmlElement ( name = "protocol" )
@NotNull @IsValid
String protocol = "http" ;
@XmlElement
boolean authorizeChildrenContext = false ;
@XmlElement
@NotNull @IsValid
String infrastructure ;
@XmlElement
@NotNull @IsValid
private BaseConfiguration baseConfiguration ;
@IsValid
private Map < String , String > properties = new HashMap < String , String > ( ) ;
@NotNull @IsValid
Site site ;
@XmlElement ( name = "token" )
@NotNull @NotEmpty
List < String > tokens = new ArrayList < String > ( ) ;
private Site site ;
@XmlTransient
Set < String > allowedContext = new HashSet < String > ( ) ;
@XmlElementRef ( type = DefaultApplicationConfiguration . class )
List < ApplicationConfiguration > apps = new ArrayList < ApplicationConfiguration > ( ) ;
@XmlElement ( name = "property" )
@IsValid
List < Property > properties = new ArrayList < Property > ( ) ;
private ProxyAddress proxy ;
@NotEmpty @NotNull
private String accountingFallbackLocation ;
@XmlElement ( name = "publication-frequency" )
long publicationFrequency = default_container_publication_frequency_in_seconds ;
@XmlTransient
private Set < String > allowedContext = new HashSet < String > ( ) ;
private List < ApplicationConfiguration > apps = new ArrayList < ApplicationConfiguration > ( ) ;
@NotNull @IsValid
private PersistenceWriter persistenceManager ;
@NotNull @IsValid
private AuthorizationProvider authorizationProvider ;
@XmlElementRef ( type = DefaultPersistence . class )
@IsValid
private Persistence persistenceManager ;
/ * *
* Returns the management mode for the container .
* @return the management mode
* /
public Mode mode ( ) {
return mode ;
return baseConfiguration. getMode ( ) ;
}
/ * *
* Sets the management mode for the container .
* @param mode the management mode
* @return this configuration
* /
public ContainerConfiguration mode ( Mode mode ) {
this . mode = mode ;
return this ;
}
/ * *
* Returns the application configurations included in this configuration .
* @return the application configurations
@ -113,21 +84,21 @@ public class ContainerConfiguration {
public List < ApplicationConfiguration > apps ( ) {
return apps ;
}
/ * *
* Returns the configuration of an application with a given context path .
* @param context the context path
* @return the application configuration
* /
public ApplicationConfiguration app ( String context ) {
for ( ApplicationConfiguration app : apps )
if ( context . equals ( app . context ( ) ) )
return app ;
return null ;
}
/ * *
* Adds the configuration of an application to this configuration .
* @param app the application configuration
@ -146,7 +117,7 @@ public class ContainerConfiguration {
apps . add ( app ) ;
return this ;
}
/ * *
* Returns the geographical site of the container .
* @return the site
@ -155,186 +126,96 @@ public class ContainerConfiguration {
return site ;
}
/ * *
* Sets the geographical site of the container .
* @param site the site
* @return this configuration
* /
public ContainerConfiguration site ( Site site ) {
this . site = site ;
return this ;
}
/ * *
* Returns the infrastructure in which the container is running .
* @return the infrastructure
* /
public String infrastructure ( ) {
return infrastructure;
return baseConfiguration . getInfrastructure ( ) ;
}
/ * *
* Sets the infrastructure in which the container is running .
* @param infrastructure the infrastructure
* @return this configuration
* /
public ContainerConfiguration infrastructure ( String infrastructure ) {
this . infrastructure = infrastructure ;
return this ;
}
/ * *
* Returns the host name of the container .
* @return the host name ;
* /
public String hostname ( ) {
return hostname ;
return baseConfiguration . getHostname ( ) ;
}
/ * *
* Sets the host name of the container .
* @param name the host name
* @return this configuration
* /
public ContainerConfiguration hostname ( String name ) {
this . hostname = name ;
return this ;
}
/ * *
* Returns the port at which the container is listening for requests .
* @return the port
* /
public int port ( ) {
return port ;
return baseConfiguration . getPort ( ) ;
}
/ * *
* Returns the port at which the container is listening for requests .
* @return the port
* /
public String protocol ( ) {
return protocol ;
}
public String authenticationEnpoint ( ) {
return authenticationEnpoint ;
}
public ContainerConfiguration authenticationEnpoint ( String endpoint ) {
this . authenticationEnpoint = endpoint ;
return this ;
return baseConfiguration . getProtocol ( ) ;
}
/ * *
* Sets the port at which the container is listening for requests .
* @param port the port
* @return this configuration
* /
public ContainerConfiguration port ( int port ) {
this . port = port ;
return this ;
}
public ContainerConfiguration protocol ( String protocol ) {
this . protocol = protocol ;
return this ;
}
public boolean authorizeChildrenContext ( ) {
return authorizeChildrenContext ;
}
public ContainerConfiguration authorizeChildrenContext ( boolean authorizeChildrenContext ) {
this . authorizeChildrenContext = authorizeChildrenContext ;
return this ;
public boolean authorizeChildrenContext ( ) {
return baseConfiguration . isAuthorizeChildrenContext ( ) ;
}
/ * *
* Returns the VOs in which the container initially operate s.
* @return the VO s
* Returns the credentials .
* @return the credentials
* /
public List< String > startTokens ( ) {
return tokens ;
public AuthorizationProvider authorizationProvider ( ) {
return authorizationProvider ;
}
/ * *
* Sets the VOs in which the container initially operates .
* @param vos the VOs
* @return this configuration
* Returns the proxy of the container .
* @return the proxy
* /
public ContainerConfiguration startTokens ( List < String > tokens ) {
notNull ( "start Tokens" , tokens ) ;
this . tokens = tokens ;
return this ;
public ProxyAddress proxy ( ) {
return proxy ;
}
/ * *
* Returns the persistence manager of the container .
* @return the manager
* /
public Persistence persistence ( ) {
public Persistence Writer persistence ( ) {
return persistenceManager ;
}
/ * *
* Sets the persistence manager of the container .
* @param manager the manager
* @return this configuration
* Returns the persistence manager of the container .
* @return the manager
* /
public ContainerConfiguration persistence ( Persistence manager ) {
this . persistenceManager = manager ;
return this ;
public String accountingFallbackLocation ( ) {
return accountingFallbackLocation ;
}
/ * *
* Returns the configuration properties of the container .
* @return the properties
* /
public Map < String , String > properties ( ) {
Map < String , String > map = new HashMap < String , String > ( ) ;
for ( Property prop : properties )
map . put ( prop . name , prop . value ) ;
return map ;
return Collections . unmodifiableMap ( properties ) ;
}
/ * *
* Adds a configuration property to the container .
* @param the name of the property
* @param the value of the property
* @return this configuration
* /
public ContainerConfiguration property ( String name , String value ) {
properties . add ( new Property ( name , value ) ) ;
return this ;
}
/ * *
* Returns the publication frequency for the container ' s profile .
* @return the frquency ;
* /
public long publicationFrequency ( ) {
return publicationFrequency ;
}
/ * *
* Sets the publication frequency for the container ' s profile .
* @param frequency the frequency
* @return this configuration
* /
public ContainerConfiguration publicationFrequency ( long frequency ) {
this . publicationFrequency = frequency ;
return this ;
return baseConfiguration . getPublicationFrequencyInSeconds ( ) ;
}
public Set < String > allowedContexts ( ) {
return allowedContext ;
}
@ -353,7 +234,7 @@ public class ContainerConfiguration {
List < String > msgs = new ArrayList < String > ( ) ;
Validator validator = ValidatorFactory . validator ( ) ;
for ( ValidationError error : validator . validate ( this ) )
msgs . add ( error . toString ( ) ) ;
@ -362,159 +243,116 @@ public class ContainerConfiguration {
}
static class Property {
@XmlAttribute @NotNull
String name ;
@XmlAttribute @NotNull
String value ;
Property ( ) { }
Property ( String key , String value ) {
this . name = key ;
this . value = value ;
}
public static ContainerConfiguration load ( InputStream stream ) {
try {
Ini configurator = new Ini ( stream ) ;
ContainerConfiguration conf = new ContainerConfiguration ( ) ;
@Override
public int hashCode ( ) {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ( ( name = = null ) ? 0 : name . hashCode ( ) ) ;
result = prime * result + ( ( value = = null ) ? 0 : value . hashCode ( ) ) ;
return result ;
}
Section nodeSection = configurator . get ( "node" ) ;
if ( nodeSection ! = null ) {
BaseConfiguration nodeConf = new BaseConfiguration ( ) ;
nodeSection . to ( nodeConf ) ;
conf . baseConfiguration = nodeConf ;
}
@Override
public boolean equals ( Object obj ) {
if ( this = = obj )
return true ;
if ( obj = = null )
return false ;
if ( getClass ( ) ! = obj . getClass ( ) )
return false ;
Property other = ( Property ) obj ;
if ( name = = null ) {
if ( other . name ! = null )
return false ;
} else if ( ! name . equals ( other . name ) )
return false ;
if ( value = = null ) {
if ( other . value ! = null )
return false ;
} else if ( ! value . equals ( other . value ) )
return false ;
return true ;
}
}
Section propertiesSection = configurator . get ( "properties" ) ;
if ( propertiesSection ! = null )
conf . properties = propertiesSection . entrySet ( ) . stream ( )
. collect ( Collectors . toMap ( Entry : : getKey , Entry : : getValue ) ) ;
Section siteSection = configurator . get ( "site" ) ;
if ( siteSection ! = null ) {
Site siteConf = new Site ( ) ;
siteSection . to ( siteConf ) ;
conf . site = siteConf ;
}
initAuthorizationPart ( configurator , conf ) ;
initPersistencePart ( configurator , conf ) ;
initProxyPart ( configurator , conf ) ;
@Override
public int hashCode ( ) {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ( ( allowedContext = = null ) ? 0 : allowedContext . hashCode ( ) ) ;
result = prime * result + ( ( apps = = null ) ? 0 : apps . hashCode ( ) ) ;
result = prime * result + ( ( authenticationEnpoint = = null ) ? 0 : authenticationEnpoint . hashCode ( ) ) ;
result = prime * result + ( authorizeChildrenContext ? 1231 : 1237 ) ;
result = prime * result + ( ( hostname = = null ) ? 0 : hostname . hashCode ( ) ) ;
result = prime * result + ( ( infrastructure = = null ) ? 0 : infrastructure . hashCode ( ) ) ;
result = prime * result + ( ( mode = = null ) ? 0 : mode . hashCode ( ) ) ;
result = prime * result + ( ( persistenceManager = = null ) ? 0 : persistenceManager . hashCode ( ) ) ;
result = prime * result + ( ( port = = null ) ? 0 : port . hashCode ( ) ) ;
result = prime * result + ( ( properties = = null ) ? 0 : properties . hashCode ( ) ) ;
result = prime * result + ( ( protocol = = null ) ? 0 : protocol . hashCode ( ) ) ;
result = prime * result + ( int ) ( publicationFrequency ^ ( publicationFrequency > > > 32 ) ) ;
result = prime * result + ( ( site = = null ) ? 0 : site . hashCode ( ) ) ;
result = prime * result + ( ( tokens = = null ) ? 0 : tokens . hashCode ( ) ) ;
return result ;
//TODO: find a solution for this shit
String location = Utils . home ( ) + "/state" ;
File dir = new File ( location ) ;
if ( ! dir . exists ( ) )
dir . mkdirs ( ) ;
conf . accountingFallbackLocation = location ;
// END Shit
return conf ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public boolean equals ( Object obj ) {
if ( this = = obj )
return true ;
if ( obj = = null )
return false ;
if ( getClass ( ) ! = obj . getClass ( ) )
return false ;
ContainerConfiguration other = ( ContainerConfiguration ) obj ;
if ( allowedContext = = null ) {
if ( other . allowedContext ! = null )
return false ;
} else if ( ! allowedContext . equals ( other . allowedContext ) )
return false ;
if ( apps = = null ) {
if ( other . apps ! = null )
return false ;
} else if ( ! apps . equals ( other . apps ) )
return false ;
if ( authenticationEnpoint = = null ) {
if ( other . authenticationEnpoint ! = null )
return false ;
} else if ( ! authenticationEnpoint . equals ( other . authenticationEnpoint ) )
return false ;
if ( authorizeChildrenContext ! = other . authorizeChildrenContext )
return false ;
if ( hostname = = null ) {
if ( other . hostname ! = null )
return false ;
} else if ( ! hostname . equals ( other . hostname ) )
return false ;
if ( infrastructure = = null ) {
if ( other . infrastructure ! = null )
return false ;
} else if ( ! infrastructure . equals ( other . infrastructure ) )
return false ;
if ( mode ! = other . mode )
return false ;
if ( persistenceManager = = null ) {
if ( other . persistenceManager ! = null )
return false ;
} else if ( ! persistenceManager . equals ( other . persistenceManager ) )
return false ;
if ( port = = null ) {
if ( other . port ! = null )
return false ;
} else if ( ! port . equals ( other . port ) )
return false ;
if ( properties = = null ) {
if ( other . properties ! = null )
return false ;
} else if ( ! properties . equals ( other . properties ) )
return false ;
if ( protocol = = null ) {
if ( other . protocol ! = null )
return false ;
} else if ( ! protocol . equals ( other . protocol ) )
return false ;
if ( publicationFrequency ! = other . publicationFrequency )
return false ;
if ( site = = null ) {
if ( other . site ! = null )
return false ;
} else if ( ! site . equals ( other . site ) )
return false ;
if ( tokens = = null ) {
if ( other . tokens ! = null )
return false ;
} else if ( ! tokens . equals ( other . tokens ) )
return false ;
return true ;
private static void initProxyPart ( Ini configurator , ContainerConfiguration conf ) throws Exception {
Section proxySection = configurator . get ( "proxy" ) ;
if ( proxySection ! = null ) {
ProxyAddress proxyConf = new ProxyAddress ( ) ;
proxySection . to ( proxyConf ) ;
conf . proxy = proxyConf ;
}
}
@Override
public String toString ( ) {
return "ContainerConfiguration [mode=" + mode + ", hostname=" + hostname + ", port=" + port + ", authenticationEnpoint=" + authenticationEnpoint + ", protocol=" + protocol
+ ", authorizeChildrenContext=" + authorizeChildrenContext + ", infrastructure=" + infrastructure
+ ", site=" + site + ", tokens=" + tokens + ", allowedContext=" + allowedContext + ", apps=" + apps
+ ", properties=" + properties + ", publicationFrequency=" + publicationFrequency
+ ", persistenceManager=" + persistenceManager + "]" ;
private static void initPersistencePart ( Ini configurator , ContainerConfiguration conf ) throws Exception {
Section persistenceSection = configurator . get ( "persistence" ) ;
if ( persistenceSection ! = null ) {
String type = persistenceSection . get ( "class" ) ;
if ( type = = null )
throw new Exception ( "ini file error: type not found in \"persistence\" section" ) ;
PersistenceWriter persistenceWriter ;
try {
Object persistenceImpl = Class . forName ( type ) . newInstance ( ) ;
persistenceWriter = PersistenceWriter . class . cast ( persistenceImpl ) ;
} catch ( Exception e ) {
throw new Exception ( "ini file error: invalid persistence type in \"persistence\" section" , e ) ;
}
persistenceSection . to ( persistenceWriter ) ;
conf . persistenceManager = persistenceWriter ;
} else {
String location = Utils . home ( ) + "/state" ;
File dir = new File ( location ) ;
if ( ! dir . exists ( ) )
dir . mkdirs ( ) ;
conf . persistenceManager = new LocalPersistence ( location ) ;
}
}
private static void initAuthorizationPart ( Ini configurator , ContainerConfiguration conf ) throws Exception {
Section authorizationSection = configurator . get ( "authorization" ) ;
if ( authorizationSection ! = null ) {
String provider = authorizationSection . get ( "provider" ) ;
AuthorizationProvider authProvider ;
if ( provider ! = null ) {
try {
Object authProviderImpl = Class . forName ( provider ) . newInstance ( ) ;
authProvider = AuthorizationProvider . class . cast ( authProviderImpl ) ;
} catch ( Exception e ) {
throw new Exception ( "ini file error: invalid provider type in \"authorization\" section" , e ) ;
}
} else
authProvider = new DefaultAuthorizationProvider ( ) ;
String type = authorizationSection . get ( "credentials.class" ) ;
if ( type = = null )
throw new Exception ( "ini file error: credentials type not found in \"authorization\" section" ) ;
Credentials credentials ;
try {
Object credentialsImpl = Class . forName ( type ) . newInstance ( ) ;
credentials = Credentials . class . cast ( credentialsImpl ) ;
} catch ( Exception e ) {
throw new Exception ( "ini file error: invalid credentials type in \"authorization\" section" , e ) ;
}
authorizationSection . to ( credentials , "credentials." ) ;
authProvider . connect ( credentials ) ;
conf . authorizationProvider = authProvider ;
}
}
}