Clover coverage report - Ledge LGPL - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:23:51 CET
file stats: LOC: 351   Methods: 10
NCLOC: 236   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
HylafaxManager.java 0% 0% 0% 0%
coverage
 1   
 2    package org.objectledge.fax;
 3   
 4   
 5    import gnu.hylafax.HylaFAXClient;
 6    import gnu.hylafax.HylaFAXClientProtocol;
 7    import gnu.hylafax.Job;
 8    import gnu.inet.ftp.FtpClient;
 9    import gnu.inet.ftp.ServerResponseException;
 10   
 11    import java.awt.Dimension;
 12    import java.io.ByteArrayInputStream;
 13    import java.io.IOException;
 14    import java.io.InputStream;
 15   
 16    import org.jcontainer.dna.Configuration;
 17    import org.jcontainer.dna.Logger;
 18    import org.objectledge.context.Context;
 19    import org.objectledge.parameters.DefaultParameters;
 20    import org.objectledge.parameters.Parameters;
 21    import org.objectledge.threads.Task;
 22    import org.objectledge.threads.ThreadPool;
 23   
 24    /**
 25    * Fax manager implementation based on gnu.hylafax.* library.
 26    *
 27    * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski </a>
 28    * @version $Id: HylafaxManager.java,v 1.4 2005/04/19 11:26:22 pablo Exp $
 29    */
 30    public class HylafaxManager implements FaxManager
 31    {
 32    /** logging facility */
 33    private Logger logger;
 34   
 35    /** fax server ip */
 36    private String host;
 37   
 38    /** fax server port */
 39    private int port;
 40   
 41    /** fax user login */
 42    private String login;
 43   
 44    /** fax user password */
 45    private String password;
 46   
 47    /** default page size */
 48    private String pageSize;
 49   
 50    /** default vertical resolution */
 51    private int resolution;
 52   
 53    /** debug on switch */
 54    private boolean verbose;
 55   
 56    /** default kill time */
 57    private String killtime;
 58   
 59    /** default max dials */
 60    private int maxdials;
 61   
 62    /** default max tries */
 63    private int maxtries;
 64   
 65    /** default priority */
 66    private int priority;
 67   
 68    /** default pagechop */
 69    private String pagechop;
 70   
 71    /** default chop treshold */
 72    private int chopthreshold;
 73   
 74    /** default notification address */
 75    private String notificationAddress;
 76   
 77    /** fax client */
 78    private HylaFAXClient client;
 79   
 80    /** ftp client */
 81    private FtpClient ftpClient;
 82   
 83    /** The thread pool component */
 84    private ThreadPool threadPool;
 85   
 86    /** synchronization object */
 87    private Object pingObject;
 88   
 89    /** ping delay */
 90    private long pingDelay;
 91   
 92    /** connected flag */
 93    private boolean connected;
 94   
 95    /**
 96    * The component constructor.
 97    *
 98    * @param config
 99    * the configuration.
 100    * @param logger
 101    * the logger.
 102    * @param threadPool
 103    * the thread pool.
 104    */
 105  0 public HylafaxManager(Configuration config, Logger logger, ThreadPool threadPool)
 106    {
 107  0 this.logger = logger;
 108  0 this.threadPool = threadPool;
 109  0 host = config.getChild("host").getValue("localhost");
 110  0 port = config.getChild("port").getValueAsInteger(12345);
 111  0 login = config.getChild("login").getValue("top");
 112  0 password = config.getChild("password").getValue("secret");
 113  0 pageSize = config.getChild("pagesize").getValue("");
 114  0 verbose = config.getChild("verbose").getValueAsBoolean(true);
 115  0 resolution = config.getChild("resolution").getValue("high").equals("high") ? 196 : 98;
 116  0 killtime = config.getChild("killtime").getValue("000259");
 117  0 maxdials = config.getChild("maxdials").getValueAsInteger(12);
 118  0 maxtries = config.getChild("maxtries").getValueAsInteger(3);
 119  0 priority = config.getChild("priority").getValueAsInteger(127);
 120  0 pagechop = config.getChild("pagechop").getValue("default");
 121  0 chopthreshold = config.getChild("chopthreshold").getValueAsInteger(3);
 122  0 notificationAddress = config.getChild("notification_address").getValue("root@localhost");
 123  0 pingDelay = config.getChild("ping_delay").getValueAsLong(600000);
 124  0 connected = false;
 125    }
 126   
 127    /**
 128    * {@inheritDoc}
 129    */
 130  0 public void sendFax(String destinationAddress, String content, String encoding, boolean notify,
 131    String notificationAddress, boolean highResolution, String from) throws FaxManagerException
 132    {
 133  0 try
 134    {
 135  0 Parameters parameters = new DefaultParameters();
 136  0 parameters.add("notify", notify);
 137  0 parameters.add("notificationAddress", notificationAddress);
 138  0 if(highResolution)
 139    {
 140  0 parameters.add("resolution", 196);
 141    }
 142    else
 143    {
 144  0 parameters.add("resolution", 98);
 145    }
 146  0 ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(encoding));
 147  0 sendFax(destinationAddress, from, bais, parameters);
 148    }
 149    catch(Exception e)
 150    {
 151  0 throw new FaxManagerException("Failed to queue fax", e);
 152    }
 153    }
 154   
 155    /**
 156    * {@inheritDoc}
 157    */
 158  0 public void sendFax(String destinationAddress, String from, InputStream content,
 159    Parameters parameters) throws FaxManagerException
 160    {
 161  0 destroy();
 162  0 prepare();
 163  0 sendFaxSave(destinationAddress, from, content, parameters);
 164    }
 165   
 166  0 private void sendFaxSave(String destinationAddress, String from, InputStream content,
 167    Parameters parameters) throws FaxManagerException
 168    {
 169  0 try
 170    {
 171  0 String remoteFilename = client.putTemporary(content);
 172  0 Job job = client.createJob();
 173  0 job.addDocument(remoteFilename);
 174  0 job.setDialstring(destinationAddress);
 175  0 job.setFromUser(from);
 176  0 job.setVerticalResolution(parameters.getInt("resolution", resolution));
 177   
 178  0 if(parameters.getBoolean("notify", false))
 179    {
 180  0 job.setNotifyType(Job.NOTIFY_DONE);
 181    }
 182    else
 183    {
 184  0 job.setNotifyType(Job.NOTIFY_NONE);
 185    }
 186  0 job.setNotifyAddress(parameters.get("notificationAddress", notificationAddress));
 187  0 job.setKilltime(parameters.get("killtime", killtime));
 188  0 job.setMaximumDials(parameters.getInt("maxdials", maxdials));
 189  0 job.setMaximumTries(parameters.getInt("maxtries", maxtries));
 190  0 job.setPriority(parameters.getInt("priority", priority));
 191  0 Dimension dimension = (Dimension)Job.pagesizes
 192    .get(parameters.get("pagesize", pageSize));
 193  0 job.setPageDimension(dimension);
 194  0 job.setChopThreshold(parameters.getInt("chopthreshold", chopthreshold));
 195  0 client.submit(job);
 196    }
 197    /**
 198    catch(SocketException e)
 199    {
 200    throw e;
 201    }
 202    */
 203    catch(Exception e)
 204    {
 205  0 throw new FaxManagerException("Failed to queue fax", e);
 206    }
 207    }
 208   
 209    /**
 210    * Initializes the service.
 211    */
 212  0 public void start()
 213    {
 214  0 threadPool.runDaemon(new PingTask());
 215    }
 216   
 217    /**
 218    * Initializes the service.
 219    */
 220  0 public void stop()
 221    {
 222    //TODO stop the task.
 223    }
 224   
 225   
 226    /**
 227    * Prepare the fax manager.
 228    *
 229    * @throws FaxManagerException
 230    */
 231  0 synchronized void prepare() throws FaxManagerException
 232    {
 233  0 if(!connected)
 234    {
 235  0 try
 236    {
 237  0 client = new HylaFAXClient();
 238  0 client.setDebug(verbose);
 239  0 client.open(host, port);
 240   
 241  0 if(client.user(login))
 242    {
 243  0 client.pass(password);
 244    }
 245  0 client.noop();
 246  0 client.tzone(HylaFAXClientProtocol.TZONE_LOCAL);
 247   
 248  0 ftpClient = new FtpClient();
 249  0 ftpClient.setDebug(verbose);
 250  0 ftpClient.open(host, port);
 251  0 if(ftpClient.user(login))
 252    {
 253  0 ftpClient.pass(password);
 254    }
 255  0 ftpClient.noop();
 256    }
 257    catch(ServerResponseException e)
 258    {
 259  0 throw new FaxManagerException("Couldn't start fax service", e);
 260    }
 261    catch(IOException e)
 262    {
 263  0 throw new FaxManagerException("Couldn't start fax service", e);
 264    }
 265  0 connected = true;
 266    }
 267    }
 268   
 269    /**
 270    * Disconnect from server.
 271    *
 272    * @throws FaxManagerException
 273    */
 274  0 synchronized void destroy() throws FaxManagerException
 275    {
 276  0 if(connected)
 277    {
 278  0 connected = false;
 279  0 try
 280    {
 281  0 client.quit();
 282    }
 283    catch(Exception e)
 284    {
 285    //
 286    }
 287  0 try
 288    {
 289  0 ftpClient.quit();
 290    }
 291    catch(Exception e)
 292    {
 293    //
 294    }
 295    }
 296    }
 297   
 298    /**
 299    * A task for pinging the HylaFax process.
 300    */
 301    private class PingTask extends Task
 302    {
 303    /**
 304    * Main processing loop.
 305    */
 306  0 public void process(Context context)
 307    {
 308  0 loop: while(!Thread.interrupted())
 309    {
 310  0 try
 311    {
 312  0 try
 313    {
 314  0 prepare();
 315  0 client.noop();
 316    }
 317    catch(ServerResponseException e)
 318    {
 319  0 logger.error("Couldn't ping to server", e);
 320  0 destroy();
 321    }
 322    catch(IOException e)
 323    {
 324  0 logger.error("Couldn't ping to server", e);
 325  0 destroy();
 326    }
 327    }
 328    catch(FaxManagerException e)
 329    {
 330  0 logger.error("Couldn't manage the hylafax connection");
 331    }
 332  0 synchronized(pingObject)
 333    {
 334  0 try
 335    {
 336  0 pingObject.wait(pingDelay);
 337    }
 338    catch(InterruptedException e)
 339    {
 340  0 break loop;
 341    }
 342    }
 343    }
 344    }
 345   
 346  0 public String getName()
 347    {
 348  0 return "Hylafax Task";
 349    }
 350    }
 351    }