com.yahoo.marketing.apt.client
Class ServiceFactory

java.lang.Object
  extended by com.yahoo.marketing.apt.client.ServiceFactory

public class ServiceFactory
extends java.lang.Object

Factory class to obtain service objects for accessing the APT from Yahoo! Web Service API. Include functions to:
- obtain the service objects
- get Quota information from headers
- log the APT error and its soap request, response message
- get the log helper object to log customized message.

Since:
0.5
Version:
3.0.0

Field Summary
private  java.lang.String CLASSNAME
          The name of this class.
private  Credential credential
          Credential instance which contains the credentials to access the APT Web Service API.
private  java.util.Properties defaultConfig
          Properties instance for storing the default settings
private  java.util.Properties locationCache
          Properties instance for caching locations for accounts
private  LogHelper logHelper
          Loghelper to help logging with SOAP request and response.
private  java.util.Map mProperties
          Map instance storing the settings of the servcie factory.
 
Constructor Summary
ServiceFactory()
          Default constructor to instantiate ServiceFactory.
ServiceFactory(Credential credential)
          Constructor to instantiate ServiceFactory with credential.
ServiceFactory(Credential credential, java.util.Properties factoryConfig, java.util.Properties logConfig)
          Constructor to instantiate ServiceFactory with credential.
ServiceFactory(Credential credential, java.lang.String factoryConfigFileName, java.lang.String logConfigFileName)
          Constructor to instantiate ServiceFactory with credential.
ServiceFactory(java.util.Properties aptConfig, java.util.Properties logConfig)
          Constructor to instantiate ServiceFactory.
ServiceFactory(java.lang.String factoryConfigFileName, java.lang.String logConfigFileName)
          Constructor to instantiate ServiceFactory.
 
Method Summary
private  org.apache.axis.message.SOAPHeaderElement createAxisWSSEHeader(java.lang.String username, java.lang.String pwd)
          Creates WS-Security compliant header
private  boolean createDir(java.lang.String filepath, java.lang.String filename, java.lang.String instructions)
          Creates directory where a required file is to be created.
 Credential getCredential()
          Return the Credential object.
 java.lang.String getDir(java.lang.String filepath)
          Utility method to return directory structure from filepath String
 java.lang.String getEndPointLocation()
          Retrieves the service endpoint address prefix for the specific accountID in the default credential using the APT LocationService.
 java.lang.String getEndPointLocation(Credential credential)
          Retrieves the service endpoint address prefix for the specific accountID stored in credential using the APT LocationService.
private  java.lang.String getLocationCacheFileName(java.lang.String serviceLocationAddress)
          Simple algorithm to generate a unique cache filename suffix to store location information gotten from the specified LocationService
 LogHelper getLogHelper()
          Return the LogHelper object.
 java.lang.String getProperty(java.lang.String propertyName)
          Return value of propertyName passed in set from property file.
 java.util.Map<java.lang.String,java.lang.String> getQuotaFromLastServiceCall(org.apache.axis.client.Stub service)
          Retrieve the quota information and time taken millis from SOAP headers of last call on the input service object
 org.apache.axis.client.Stub getService(java.lang.Class klass)
          Return Axis generated client for the specified service class using default credential.
 org.apache.axis.client.Stub getService(java.lang.Class klass, Credential credential)
          Return Axis generated client for the specified service class with credential.
 org.apache.axis.client.Stub getService(java.lang.String serviceName)
          Return Axis generated client for the specified service name using default credential.
 org.apache.axis.client.Stub getService(java.lang.String serviceName, Credential credential)
          Return Axis generated client for the specified service name with credential.
private  java.lang.String getValue(org.w3c.dom.Element element)
          Extract child node value of element passed as input
private  void init()
          Initiate the service factory through default property file
private  void init(java.util.Properties factoryConfig, boolean readCredential)
          Initiate the service factory through factoryConfig.
private  void init(java.lang.String factoryConfigFileName, boolean readCredential)
          Initiate the service factory through property file factoryConfigFileName.
private  void initLog(java.util.Properties logConfig)
          Initiate the log helper with logConfig.
private  void initLog(java.lang.String logConfigFileName)
          Initiate the log helper with property file logConfigFileName.
private  java.util.Properties loadDefaultConfig()
          Load the default property file.
private  void loadLocationCache()
          Called at initialization time to load previously cached location entries.
 java.util.Properties loadProperties(java.lang.String fileName)
          Load properties from property file filename.
 void logAPTErrors(Error[] errors, java.lang.String errorSource)
          Log errors returned by APT in Response objects
 void logAPTErrors(org.apache.axis.client.Stub service, Error[] errors, java.lang.String errorSource)
          Log errors returned by APT in Response objects as well as the SOAP request and response messages.
private  void persistAccountLocationCache(java.lang.String accountID, java.lang.String serviceLocationAddress)
          Persist location retrieved from LocationService into memory cache and on disk for future use
 void setCredential(Credential credential)
          Set the Credential object.
private  void setHeaders(org.apache.axis.client.Stub _stub, Credential credential, java.lang.String apiNS)
          Set headers on the org.apache.axis.client.Stub object passed in with credential and api namespace passed in and set the timeout on the Stub
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logHelper

private LogHelper logHelper
Loghelper to help logging with SOAP request and response.


credential

private Credential credential
Credential instance which contains the credentials to access the APT Web Service API.


CLASSNAME

private final java.lang.String CLASSNAME
The name of this class.


mProperties

private java.util.Map mProperties
Map instance storing the settings of the servcie factory.


locationCache

private java.util.Properties locationCache
Properties instance for caching locations for accounts


defaultConfig

private java.util.Properties defaultConfig
Properties instance for storing the default settings

Constructor Detail

ServiceFactory

public ServiceFactory()
               throws java.lang.IllegalStateException,
                      java.io.IOException
Default constructor to instantiate ServiceFactory. It will first look for the service factory property filename YAHOO_PROPERTIES_FILE and log property filename YAHOO_LOG_CONFIG_FILE in the system property. If YAHOO_PROPERTIES_FILE variable does not exist, it will read service factory setting from default properties file. If YAHOO_LOG_CONFIG_FILE variable does not exist, it will initiate the log helper from default log properties file.

Throws:
java.io.IOException
java.lang.IllegalStateException

ServiceFactory

public ServiceFactory(Credential credential)
               throws java.lang.IllegalStateException,
                      java.io.IOException
Constructor to instantiate ServiceFactory with credential. It will first look for the service factory property filename YAHOO_PROPERTIES_FILE and log property filename YAHOO_LOG_CONFIG_FILE in the system property. If YAHOO_PROPERTIES_FILE variable does not exist, it will read service factory configures from default property file. If YAHOO_LOG_CONFIG_FILE variable does not exist, it will initiate the log helper from default log property file.

Parameters:
credential - contains the credentials to access the APT Web Service API.
Throws:
java.io.IOException
java.lang.IllegalStateException

ServiceFactory

public ServiceFactory(Credential credential,
                      java.util.Properties factoryConfig,
                      java.util.Properties logConfig)
               throws java.lang.IllegalStateException,
                      java.io.IOException
Constructor to instantiate ServiceFactory with credential. It will read service factory configures from factoryConfig and initiate the log helper with logConfig.

Parameters:
credential - contains the credentials to access the APT! Web Service API.
factoryConfig - contains the service factory configures.
logConfig - contains the log configures.
Throws:
java.io.IOException
java.lang.IllegalStateException

ServiceFactory

public ServiceFactory(Credential credential,
                      java.lang.String factoryConfigFileName,
                      java.lang.String logConfigFileName)
               throws java.lang.IllegalStateException,
                      java.io.IOException
Constructor to instantiate ServiceFactory with credential. It will read service factory configures from property file factoryConfigFileName and initiate the log helper with property file logConfigFileName.

Parameters:
credential - contains the credentials to access the APT Web Service API.
factoryConfigFileName - property file contains the service factory configures.
logConfigFileName - property file contains the log configures.
Throws:
java.io.IOException
java.lang.IllegalStateException

ServiceFactory

public ServiceFactory(java.util.Properties aptConfig,
                      java.util.Properties logConfig)
               throws java.lang.IllegalStateException,
                      java.io.IOException
Constructor to instantiate ServiceFactory. It will read service factory configures from factoryConfig and will construct a Credential object if credential configures are in factoryConfig. It will also initiate the log helper with logConfig.

Parameters:
aptConfig - contains the service factory configures. May also contain the credential configures.
logConfig - contains the log configures
Throws:
java.io.IOException
java.lang.IllegalStateException

ServiceFactory

public ServiceFactory(java.lang.String factoryConfigFileName,
                      java.lang.String logConfigFileName)
               throws java.lang.IllegalStateException,
                      java.io.IOException
Constructor to instantiate ServiceFactory. It will read service factory configures from property file factoryConfigFileName and will construct a Credential object if credential configures are in property file factoryConfigFileName. It will also initiate the log helper with property file logConfigFileName.

Parameters:
factoryConfigFileName - property file contains the service factory configures. May also contain the credential configures.
logConfigFileName - property file contains the log configures.
Throws:
java.io.IOException
java.lang.IllegalStateException
Method Detail

initLog

private void initLog(java.util.Properties logConfig)
Initiate the log helper with logConfig.

Parameters:
logConfig - contains the log configures.

initLog

private void initLog(java.lang.String logConfigFileName)
              throws java.io.IOException
Initiate the log helper with property file logConfigFileName.

Parameters:
logConfigFileName - property file which contains the log configures.
Throws:
java.io.IOException

init

private void init()
           throws java.lang.IllegalStateException,
                  java.io.IOException
Initiate the service factory through default property file

Throws:
java.io.IOException
java.lang.IllegalStateException

init

private void init(java.util.Properties factoryConfig,
                  boolean readCredential)
           throws java.io.IOException,
                  java.lang.IllegalStateException
Initiate the service factory through factoryConfig. Will construct a Credential object if credential configures are in factoryConfig and readCredential is true.

Parameters:
factoryConfig - contains the factory configures. May also contain the credential configures.
readCredential - true if construct Credential object from factoryConfig.
Throws:
java.io.IOException
java.lang.IllegalStateException

init

private void init(java.lang.String factoryConfigFileName,
                  boolean readCredential)
           throws java.lang.IllegalStateException,
                  java.io.IOException
Initiate the service factory through property file factoryConfigFileName. Will construct a Credential object if credential configures are in property file factoryConfigFileName and readCredential is true.

Parameters:
factoryConfigFileName - property file which contains the factory configures. May also contain the credential configures.
readCredential - true if construct Credential object from property file factoryConfigFileName.
Throws:
java.io.IOException
java.lang.IllegalStateException

loadDefaultConfig

private java.util.Properties loadDefaultConfig()
                                        throws java.io.IOException,
                                               java.lang.IllegalStateException
Load the default property file.

Returns:
defaultConfig
Throws:
java.io.IOException
java.lang.IllegalStateException

loadProperties

public java.util.Properties loadProperties(java.lang.String fileName)
                                    throws java.io.IOException
Load properties from property file filename.

Parameters:
fileName - the property file
Returns:
properties
Throws:
java.io.IOException

getService

public org.apache.axis.client.Stub getService(java.lang.Class klass,
                                              Credential credential)
                                       throws java.rmi.RemoteException,
                                              javax.xml.rpc.ServiceException,
                                              java.io.IOException,
                                              java.lang.Exception
Return Axis generated client for the specified service class with credential.

Parameters:
klass - service class.
credential - contains the credentials to access the APT Web Service API.
Returns:
axis client service
Throws:
java.io.IOException
javax.xml.rpc.ServiceException
java.rmi.RemoteException
java.lang.Exception

getService

public org.apache.axis.client.Stub getService(java.lang.Class klass)
                                       throws java.rmi.RemoteException,
                                              javax.xml.rpc.ServiceException,
                                              java.io.IOException,
                                              java.lang.Exception
Return Axis generated client for the specified service class using default credential.

Parameters:
klass - service class.
Returns:
axis client service
Throws:
java.io.IOException
javax.xml.rpc.ServiceException
java.rmi.RemoteException
java.lang.Exception

getService

public org.apache.axis.client.Stub getService(java.lang.String serviceName)
                                       throws java.rmi.RemoteException,
                                              javax.xml.rpc.ServiceException,
                                              java.io.IOException,
                                              java.lang.IllegalArgumentException,
                                              java.lang.Exception
Return Axis generated client for the specified service name using default credential.

Parameters:
serviceName - service name.
Returns:
axis client service
Throws:
java.io.IOException
javax.xml.rpc.ServiceException
java.rmi.RemoteException
java.lang.Exception
java.lang.IllegalArgumentException

getService

public org.apache.axis.client.Stub getService(java.lang.String serviceName,
                                              Credential credential)
                                       throws java.rmi.RemoteException,
                                              javax.xml.rpc.ServiceException,
                                              java.io.IOException,
                                              java.lang.IllegalArgumentException,
                                              java.lang.Exception
Return Axis generated client for the specified service name with credential.

Parameters:
serviceName - service name.
credential - contains the credentials to access the APT Web Service API.
Returns:
axis client service
Throws:
java.io.IOException
javax.xml.rpc.ServiceException
java.rmi.RemoteException
java.lang.Exception
java.lang.IllegalArgumentException

getEndPointLocation

public java.lang.String getEndPointLocation()
                                     throws javax.xml.rpc.ServiceException,
                                            java.rmi.RemoteException,
                                            java.io.IOException,
                                            javax.xml.soap.SOAPException
Retrieves the service endpoint address prefix for the specific accountID in the default credential using the APT LocationService. Retrieved from Cache if present. Else procures a LocationService object with base URL specified in the property file then uses the LocationService object and calls the location service to get the address of the location where the given account resides and stores the location in the cache file and local locationCache before returning it.

Returns:
endPointLocation
Throws:
javax.xml.rpc.ServiceException
java.rmi.RemoteException
java.io.IOException
javax.xml.soap.SOAPException
java.lang.IllegalArgumentException

getEndPointLocation

public java.lang.String getEndPointLocation(Credential credential)
                                     throws javax.xml.rpc.ServiceException,
                                            java.rmi.RemoteException,
                                            java.io.IOException,
                                            java.lang.IllegalArgumentException,
                                            javax.xml.soap.SOAPException
Retrieves the service endpoint address prefix for the specific accountID stored in credential using the APT LocationService. Retrieved from Cache if present. Else procures a LocationService object with base URL specified in the property file then uses the LocationService object and calls the location service to get the address of the location where the given account resides and stores the location in the cache file and local locationCache before returning it.

Parameters:
credential -
Returns:
endPointLocation
Throws:
javax.xml.rpc.ServiceException
java.rmi.RemoteException
java.io.IOException
javax.xml.soap.SOAPException
java.lang.IllegalArgumentException

loadLocationCache

private void loadLocationCache()
                        throws java.io.IOException
Called at initialization time to load previously cached location entries. File contents are in the typical Properties file format: =

Throws:
java.io.IOException

persistAccountLocationCache

private void persistAccountLocationCache(java.lang.String accountID,
                                         java.lang.String serviceLocationAddress)
                                  throws java.io.IOException,
                                         java.lang.IllegalArgumentException
Persist location retrieved from LocationService into memory cache and on disk for future use

Parameters:
accountID -
serviceLocationAddress -
Throws:
java.io.IOException
java.lang.IllegalArgumentException

createDir

private boolean createDir(java.lang.String filepath,
                          java.lang.String filename,
                          java.lang.String instructions)
                   throws java.io.IOException
Creates directory where a required file is to be created.

Parameters:
filepath - The directory which to be created.
filename - The instruction file in the directory.
instructions - The instruction message.
Returns:
created
Throws:
java.io.IOException

getDir

public java.lang.String getDir(java.lang.String filepath)
Utility method to return directory structure from filepath String

Parameters:
filepath -
Returns:
dirStructure

getLocationCacheFileName

private java.lang.String getLocationCacheFileName(java.lang.String serviceLocationAddress)
Simple algorithm to generate a unique cache filename suffix to store location information gotten from the specified LocationService

Parameters:
serviceLocationAddress -
Returns:
filename of the persisted Location Service cache

setHeaders

private void setHeaders(org.apache.axis.client.Stub _stub,
                        Credential credential,
                        java.lang.String apiNS)
                 throws javax.xml.soap.SOAPException
Set headers on the org.apache.axis.client.Stub object passed in with credential and api namespace passed in and set the timeout on the Stub

Parameters:
_stub -
credential -
apiNS -
Throws:
javax.xml.soap.SOAPException

createAxisWSSEHeader

private org.apache.axis.message.SOAPHeaderElement createAxisWSSEHeader(java.lang.String username,
                                                                       java.lang.String pwd)
                                                                throws javax.xml.soap.SOAPException
Creates WS-Security compliant header

Parameters:
username -
pwd -
Returns:
Axis SOAP header element
Throws:
javax.xml.soap.SOAPException

getQuotaFromLastServiceCall

public java.util.Map<java.lang.String,java.lang.String> getQuotaFromLastServiceCall(org.apache.axis.client.Stub service)
                                                                             throws java.lang.IllegalArgumentException
Retrieve the quota information and time taken millis from SOAP headers of last call on the input service object

Parameters:
service -
Returns:
quotaInfo Map which stores the key/value pair of the quotaInfo. The keys are commandGroup, remainingQuota and timeTakenMillis.
Throws:
java.lang.IllegalArgumentException

getValue

private java.lang.String getValue(org.w3c.dom.Element element)
Extract child node value of element passed as input

Parameters:
element -
Returns:
value

logAPTErrors

public void logAPTErrors(Error[] errors,
                         java.lang.String errorSource)
Log errors returned by APT in Response objects

Parameters:
errors -
errorSource -

logAPTErrors

public void logAPTErrors(org.apache.axis.client.Stub service,
                         Error[] errors,
                         java.lang.String errorSource)
Log errors returned by APT in Response objects as well as the SOAP request and response messages.

Parameters:
service -
errors -
errorSource -

getLogHelper

public LogHelper getLogHelper()
Return the LogHelper object.

Returns:
logHelper

getCredential

public Credential getCredential()
Return the Credential object.

Returns:
credential

setCredential

public void setCredential(Credential credential)
Set the Credential object.

Parameters:
credential -

getProperty

public java.lang.String getProperty(java.lang.String propertyName)
Return value of propertyName passed in set from property file.

Parameters:
propertyName -
Returns:
propertyValue