All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fax4j.spi.AbstractFaxClientSpi Maven / Gradle / Ivy

package org.fax4j.spi;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.fax4j.FaxClientActionEvent;
import org.fax4j.FaxClientActionEvent.FaxClientActionEventID;
import org.fax4j.FaxClientActionEventListener;
import org.fax4j.FaxException;
import org.fax4j.FaxJob;
import org.fax4j.FaxJob.FaxJobPriority;
import org.fax4j.FaxJobStatus;
import org.fax4j.FaxMonitorEvent;
import org.fax4j.FaxMonitorEvent.FaxMonitorEventID;
import org.fax4j.FaxMonitorEventListener;
import org.fax4j.common.Logger;

/**
 * This class provides partial/common functionlity of the fax client service provider interface.
* This is the actual engine behind the fax4j framework.
* It handles the fax client activities such as sending/suspending fax jobs and so on.
* The SPI is an internal class that should be used internally only.
*
* The configuration of the fax4j framework is made up of 3 layers.
* The configuration is based on simple properties.
* Each layer overrides the lower layers by adding/changing the property values.
* The first layer is the internal fax4j.properties file located in the fax4j jar.
* This layer contains the preconfigured values for the fax4j framework and can be changed * by updating these properties in the higher layers.
* The second layer is the external fax4j.properties file that is located on the classpath.
* This file is optional and provides the ability to override the internal configuration for the * entire fax4j framework.
* The top most layer is the optional java.util.Properties object provided by the external classes * when creating a new fax client.
* These properties enable to override the configuration of the lower 2 layers.
*
* For SPI specific configuration, see the relevant SPI class javadoc. * * @author Sagie Gur-Ari * @version 1.14 * @since 0.1 */ public abstract class AbstractFaxClientSpi implements FaxClientSpi { /**The initialized flag*/ private boolean initialized; /**The fax client SPI configuration*/ private Map spiConfiguration; /**The logger*/ private Logger spiLogger; /**The fax job monitor*/ private FaxJobMonitor spiFaxJobMonitor; /**The fax client action event listeners*/ private Set faxClientActionEventListeners; /**The fax monitor event listeners*/ private Set faxMonitorEventListeners; /** * This class holds the fax job extended properties. * * @author Sagie Gur-Ari * @version 1.0 * @since 0.38 */ public static class FaxJobExtendedPropertyConstants { /** * This is the class constructor. */ private FaxJobExtendedPropertyConstants() { super(); } /**The test file encoding name*/ public static final String TEXT_FILE_ENCODING_FAX_JOB_PROPERTY_KEY="text.file.encoding"; } /** * This is the default constructor. */ public AbstractFaxClientSpi() { super(); //set flag this.initialized=false; } /** * This function initializes the fax client SPI.
* This method is called by the FaxClientSpiFactory. * * @param configuration * The fax client configuration * @param logger * The internal logger * @param faxJobMonitor * The fax job monitor */ public final void initialize(Map configuration,Logger logger,FaxJobMonitor faxJobMonitor) { if(this.initialized) { throw new FaxException("Fax client SPI already initialized."); } //set flag this.initialized=true; //get configuration this.spiConfiguration=new HashMap(configuration); this.spiConfiguration=Collections.unmodifiableMap(this.spiConfiguration); //get logger this.spiLogger=logger; //log fax client SPI information this.spiLogger.logDebug(new Object[]{"Initializing fax client SPI of type: ",this.getClass().getName(),"\nProvider Information:\n",this.getProvider(),"\nSPI Configuration:\n",configuration},null); //get fax job monitor this.spiFaxJobMonitor=faxJobMonitor; //init listeners data structures this.faxClientActionEventListeners=new HashSet(); this.faxMonitorEventListeners=new HashSet(); //initialize this.initializeImpl(); } /** * Returns the configuration. * * @return The configuration */ protected final Map getConfiguration() { return this.spiConfiguration; } /** * Returns the internal logger. * * @return The internal logger */ public final Logger getLogger() { return this.spiLogger; } /** * Returns the fax job monitor. * * @return The fax job monitor */ public final FaxJobMonitor getFaxJobMonitor() { return this.spiFaxJobMonitor; } /** * Returns the value from the SPI configuration based on the provided * configuration key. * * @param key * The configuration key * @return The value */ public final String getConfigurationValue(String key) { //get value String value=this.spiConfiguration.get(key); //trim value if(value!=null) { value=value.trim(); //set as null if empty string if(value.length()==0) { value=null; } } this.spiLogger.logDebug(new Object[]{"Extracted configuration for key: ",key," value: ",value},null); return value; } /** * This function fires a new fax monitor event. * * @param id * The fax monitor event ID * @param faxJob * The fax job * @param faxJobStatus * The fax job status */ public final void fireFaxMonitorEvent(FaxMonitorEventID id,FaxJob faxJob,FaxJobStatus faxJobStatus) { this.fireFaxEvent(id,faxJob,faxJobStatus); } /** * This function creates a new fax job instance to be used * by the caller to submit a new fax job and so on. * * @return The fax job instance */ public FaxJob createFaxJob() { //create new fax job FaxJob faxJob=this.createFaxJobImpl(); //fire event this.fireFaxEvent(FaxClientActionEventID.CREATE_FAX_JOB,faxJob); return faxJob; } /** * This function will submit a new fax job.
* The fax job ID may be populated by this method in the provided * fax job object. * * @param faxJob * The fax job object containing the needed information */ public void submitFaxJob(FaxJob faxJob) { //validate fax job this.invokeFaxJobNullValidation(faxJob); //validate fax job target address String targetAddress=faxJob.getTargetAddress(); if((targetAddress==null)||(targetAddress.length()==0)) { throw new FaxException("Fax job target address not provided."); } //validate fax job file File file=faxJob.getFile(); if(file==null) { throw new FaxException("Fax job file not provided."); } else if(!file.exists()) { throw new FaxException("Fax job file "+file.getPath()+" does not exist."); } else if(!file.isFile()) { throw new FaxException("Fax job file "+file.getPath()+" is not a file."); } else if(file.length()==0) { throw new FaxException("Fax job file "+file.getPath()+" is empty."); } //invoke action this.submitFaxJobImpl(faxJob); //fire event this.fireFaxEvent(FaxClientActionEventID.SUBMIT_FAX_JOB,faxJob); //start monitoring fax job if and only if monitoring is supported and there are existing listeners if(this.isFaxMonitorEventsSupported()) { synchronized(this.faxMonitorEventListeners) { if(!this.faxMonitorEventListeners.isEmpty()) { //monitor the submitted fax job this.spiFaxJobMonitor.monitorFaxJob(this,faxJob); } } } } /** * This function will suspend an existing fax job. * * @param faxJob * The fax job object containing the needed information */ public void suspendFaxJob(FaxJob faxJob) { //validate fax job ID this.invokeFaxJobIDValidation(faxJob); //invoke action this.suspendFaxJobImpl(faxJob); //fire event this.fireFaxEvent(FaxClientActionEventID.SUSPEND_FAX_JOB,faxJob); } /** * This function will resume an existing fax job. * * @param faxJob * The fax job object containing the needed information */ public void resumeFaxJob(FaxJob faxJob) { //validate fax job ID this.invokeFaxJobIDValidation(faxJob); //invoke action this.resumeFaxJobImpl(faxJob); //fire event this.fireFaxEvent(FaxClientActionEventID.RESUME_FAX_JOB,faxJob); } /** * This function will cancel an existing fax job. * * @param faxJob * The fax job object containing the needed information */ public void cancelFaxJob(FaxJob faxJob) { //validate fax job ID this.invokeFaxJobIDValidation(faxJob); //invoke action this.cancelFaxJobImpl(faxJob); //fire event this.fireFaxEvent(FaxClientActionEventID.CANCEL_FAX_JOB,faxJob); } /** * This function returns the fax job status.
* Not all SPIs support extraction of the fax job status.
* In case the SPI is unable to extract or does not support extracting * of the fax job status, it will return the UNKNOWN status. * * @param faxJob * The fax job object containing the needed information * @return The fax job status */ public FaxJobStatus getFaxJobStatus(FaxJob faxJob) { //validate fax job ID this.invokeFaxJobIDValidation(faxJob); //invoke action FaxJobStatus faxJobStatus=this.getFaxJobStatusImpl(faxJob); return faxJobStatus; } /** * This function adds the fax client action event listener to the internal fax * event listeners data structure. * * @param listener * The fax client action event listener */ public void addFaxClientActionEventListener(FaxClientActionEventListener listener) { synchronized(this.faxClientActionEventListeners) { this.faxClientActionEventListeners.add(listener); } } /** * This function removes the fax client action event listener from the internal fax * event listeners data structure. * * @param listener * The fax client action event listener */ public void removeFaxClientActionEventListener(FaxClientActionEventListener listener) { synchronized(this.faxClientActionEventListeners) { this.faxClientActionEventListeners.remove(listener); } } /** * This function removes all fax client action event listeners from the internal fax * event listeners data structure. */ public void removeAllFaxClientActionEventListeners() { synchronized(this.faxClientActionEventListeners) { this.faxClientActionEventListeners.clear(); } } /** * This function adds the fax monitor event listener to the internal fax * event listeners data structure.
* Fax jobs will be monitored only if there are active listeners registered.
* If the listeners are added after a fob job was submitted, that fax job would not be monitored.
* Not all SPIs support monitoring events, in which case this method will throw an exception. * * @param listener * The fax monitor event listener */ public void addFaxMonitorEventListener(FaxMonitorEventListener listener) { boolean faxMonitorEventsSupported=this.isFaxMonitorEventsSupported(); if(faxMonitorEventsSupported) { synchronized(this.faxMonitorEventListeners) { this.faxMonitorEventListeners.add(listener); } } else { this.throwUnsupportedException(); } } /** * This function removes the fax monitor event listener from the internal fax * event listeners data structure.
* Not all SPIs support monitoring events. * * @param listener * The fax monitor event listener */ public void removeFaxMonitorEventListener(FaxMonitorEventListener listener) { synchronized(this.faxMonitorEventListeners) { this.faxMonitorEventListeners.remove(listener); //stop fax job monitoring for this SPI this.checkAndStopMonitoringAllFaxJobs(); } } /** * This function removes all fax monitor event listeners from the internal fax * event listeners data structure.
* Not all SPIs support monitoring events. */ public void removeAllFaxMonitorEventListeners() { synchronized(this.faxMonitorEventListeners) { //clear data structure this.faxMonitorEventListeners.clear(); //stop fax job monitoring for this SPI this.checkAndStopMonitoringAllFaxJobs(); } } /** * Checks and if needed stops monitoring fax jobs for this SPI. */ private synchronized void checkAndStopMonitoringAllFaxJobs() { //stop fax job monitoring for this SPI if(this.isFaxMonitorEventsSupported()) { if(this.faxMonitorEventListeners.size()==0) { this.spiFaxJobMonitor.stopMonitoringAllFaxJobs(this); } } } /** * This function fires a new fax event. * * @param id * The fax event ID * @param faxJob * The fax job */ protected void fireFaxEvent(FaxClientActionEventID id,FaxJob faxJob) { //create new fax event FaxClientActionEvent event=new FaxClientActionEvent(id,faxJob); //get listeners FaxClientActionEventListener[] listeners=null; synchronized(this.faxClientActionEventListeners) { listeners=this.faxClientActionEventListeners.toArray(new FaxClientActionEventListener[this.faxClientActionEventListeners.size()]); } int amount=listeners.length; FaxClientActionEventListener listener=null; for(int index=0;index * The fax job ID may be populated by this method in the provided * fax job object. * * @param faxJob * The fax job object containing the needed information */ protected abstract void submitFaxJobImpl(FaxJob faxJob); /** * This function will suspend an existing fax job. * * @param faxJob * The fax job object containing the needed information */ protected abstract void suspendFaxJobImpl(FaxJob faxJob); /** * This function will resume an existing fax job. * * @param faxJob * The fax job object containing the needed information */ protected abstract void resumeFaxJobImpl(FaxJob faxJob); /** * This function will cancel an existing fax job. * * @param faxJob * The fax job object containing the needed information */ protected abstract void cancelFaxJobImpl(FaxJob faxJob); /** * This function returns the fax job status.
* Not all SPIs support extraction of the fax job status.
* In case the SPI is unable to extract or does not support extracting * of the fax job status, it will return the UNKNOWN status. * * @param faxJob * The fax job object containing the needed information * @return The fax job status */ protected abstract FaxJobStatus getFaxJobStatusImpl(FaxJob faxJob); /** * This function returns true if the fax monitor events are supported by this SPI. * * @return True if the fax monitor events are supported by this SPI */ protected abstract boolean isFaxMonitorEventsSupported(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy