You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/12/25 11:43:25 UTC

svn commit: r1052765 - in /camel/trunk/components: camel-eventadmin/ camel-paxlogging/ camel-printer/src/main/java/org/apache/camel/component/printer/ camel-printer/src/test/java/org/apache/camel/component/printer/

Author: davsclaus
Date: Sat Dec 25 10:43:24 2010
New Revision: 1052765

URL: http://svn.apache.org/viewvc?rev=1052765&view=rev
Log:
Polished camel-printer. Fixed testing camel-printer on hudson when no printers on host. Added svn ignore.

Modified:
    camel/trunk/components/camel-eventadmin/   (props changed)
    camel/trunk/components/camel-paxlogging/   (props changed)
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
    camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
    camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java

Propchange: camel/trunk/components/camel-eventadmin/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sat Dec 25 10:43:24 2010
@@ -0,0 +1,9 @@
+.project
+.checkstyle
+.pmd
+.classpath
+target
+.settings
+eclipse-classes
+*.i??
+classes

Propchange: camel/trunk/components/camel-paxlogging/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sat Dec 25 10:43:24 2010
@@ -0,0 +1,9 @@
+.project
+.checkstyle
+.pmd
+.classpath
+target
+.settings
+eclipse-classes
+*.i??
+classes

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java?rev=1052765&r1=1052764&r2=1052765&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java (original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterEndpoint.java Sat Dec 25 10:43:24 2010
@@ -23,8 +23,11 @@ import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultEndpoint;
 
 public class PrinterEndpoint extends DefaultEndpoint {
-    PrinterConfiguration config;
-    
+    private PrinterConfiguration config;
+
+    public PrinterEndpoint() {
+    }
+
     public PrinterEndpoint(String endpointUri, Component component, PrinterConfiguration config) {
         super(endpointUri, component);
         this.config = config;

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java?rev=1052765&r1=1052764&r2=1052765&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java (original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java Sat Dec 25 10:43:24 2010
@@ -32,6 +32,7 @@ import javax.print.attribute.standard.Co
 import javax.print.attribute.standard.MediaSizeName;
 import javax.print.attribute.standard.Sides;
 
+import org.apache.camel.util.IOHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -46,7 +47,7 @@ public class PrinterOperations implement
     public PrinterOperations() throws PrintException {        
         printService = PrintServiceLookup.lookupDefaultPrintService();
         if (printService == null) {
-            throw new PrintException("Printer Lookup Failure. No Default printer set up for this host");
+            throw new PrintException("Printer lookup failure. No default printer set up for this host");
         }
         job = printService.createPrintJob(); 
         flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
@@ -59,40 +60,21 @@ public class PrinterOperations implement
     public PrinterOperations(PrintService printService, DocPrintJob job, DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
         this();
         this.setPrintService(printService);
-        this.setJob(job);  
-        this.setFlavor(flavor);
-        this.setPrintRequestAttributeSet(printRequestAttributeSet);
-    }    
-    
-    public PrinterOperations(DocPrintJob job, DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
-        this();
         this.setJob(job);
         this.setFlavor(flavor);
         this.setPrintRequestAttributeSet(printRequestAttributeSet);
     }
-    
-    public PrinterOperations(DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
-        this();
-        this.setFlavor(flavor);
-        this.setPrintRequestAttributeSet(printRequestAttributeSet);
-    } 
 
-    public PrinterOperations(PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
-        this();
-        this.setPrintRequestAttributeSet(printRequestAttributeSet);
-    }
-    
     public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType) throws PrintException {
-        byte[] buffer = null;
-        File file;
-        
-        LOG.trace("In printerOperations.print()");
-        LOG.trace("Print Service = " + this.printService.getName());
+        LOG.trace("Print Service: " + this.printService.getName());
         LOG.trace("About to print " + copies + " copy(s)");
         
         for (int i = 0; i < copies; i++) {
             if (!sendToPrinter) {
-                LOG.debug("\tPrint Flag is set to false. This job(s) will not be printed until this setting remains in effect. Please set the flag to true or remove the setting");
+                LOG.debug("Print flag is set to false. This job will not be printed until this setting remains in effect."
+                        + " Please set the flag to true or remove the setting.");
+
+                File file;
                 if (mimeType.equalsIgnoreCase("GIF") || mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
                     file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".gif");
                 } else if (mimeType.equalsIgnoreCase("JPEG")) {
@@ -102,25 +84,20 @@ public class PrinterOperations implement
                 } else {
                     file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".txt");
                 }
-                LOG.debug("\tWriting Print Job to File: " + file.getAbsolutePath());
+
+                LOG.debug("Writing print job to file: " + file.getAbsolutePath());
                 try {
-                    if (buffer == null) {
-                        InputStream stream = doc.getStreamForBytes();
-                        buffer = new byte[stream.available()];
-                        int n = stream.available();
-                        for (int j = 0; j < n; j++) {
-                            buffer[j] = (byte)stream.read();
-                        }
-                    }
-                    FileOutputStream fileOutputStream = new FileOutputStream(file);
-                    fileOutputStream.write(buffer);
-                    fileOutputStream.flush();
-                    fileOutputStream.close();
+                    InputStream in = doc.getStreamForBytes();
+                    FileOutputStream fos = new FileOutputStream(file);
+                    IOHelper.copyAndCloseInput(in, fos);
+                    IOHelper.close(fos);
                 } catch (Exception e) {
                     throw new PrintException("Error writing Document to the target file " + file.getAbsolutePath());
                 }    
             } else {
-                LOG.debug("\tIssuing Job " + i + " to Printer: " + this.printService.getName());
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Issuing Job " + i + " to Printer: " + this.printService.getName());
+                }
                 print(doc);
             }
         }

Modified: camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java?rev=1052765&r1=1052764&r2=1052765&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java (original)
+++ camel/trunk/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java Sat Dec 25 10:43:24 2010
@@ -30,39 +30,23 @@ import javax.print.attribute.standard.Co
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 public class PrinterProducer extends DefaultProducer {
     private static final transient Log LOG = LogFactory.getLog(PrinterProducer.class);
-    Endpoint endpoint;
-    PrinterConfiguration config;
-    PrinterOperations printerOperations;
-    PrintService printService;
-    String printer;
+    private final PrinterConfiguration config;
+    private PrinterOperations printerOperations;
+    private PrintService printService;
+    private String printer;
     
     public PrinterProducer(Endpoint endpoint, PrinterConfiguration config) throws Exception {
         super(endpoint);
-        this.endpoint = endpoint;
         this.config = config;
-        this.printService = assignPrintService();
-        this.printerOperations = new PrinterOperations(printService, assignPrintJob(printService), assignDocFlavor(), assignPrintAttributes());
-    }
-
-    @Override
-    protected void doStart() throws Exception {
-        LOG.info("In PrinterProducer.start()");
-        super.doStart();
-    }
-
-    @Override
-    protected void doStop() throws Exception {
-        LOG.info("In PrinterProducer.stop()");
-        super.doStop();
     }
 
     public void process(Exchange exchange) throws Exception {
-        LOG.info("In printerProducer.print()");
         Object body = exchange.getIn().getBody();
         InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
         print(is);
@@ -107,44 +91,28 @@ public class PrinterProducer extends Def
             setPrinter("\\\\" + config.getHostname() + "\\" + config.getPrintername());
             int position = findPrinter(services, printer);
             if (position < 0) {
-                LOG.error("PrintServiceLookup failed. No printer found with the Printer Name: " + printer);
-                throw new PrintException("Printer Lookup Failure. Please verify that the host and printer are registered and reachable from this machine");
+                throw new PrintException("No printer found with name: " + printer + ". Please verify that the host and printer are registered and reachable from this machine.");
             }         
             printService = services[position];
         }
-        // LOG.info("PrintServiceLookup succeeded. PrintService located at " + printService.getName());
         return printService;
     }
     
     private int findPrinter(PrintService[] services, String printer) {
-        boolean found = false;
         int position = -1;
-        for (int i = 0; (i < services.length) && (!found); i++) {
+        for (int i = 0; i < services.length; i++) {
             if (printer.equalsIgnoreCase(services[i].getName())) {
-                found = true;
                 position = i;
+                break;
             }
         }
-        
         return position;
     }
 
-    public Endpoint getEndpoint() {
-        return endpoint;
-    }
-
-    public void setEndpoint(Endpoint endpoint) {
-        this.endpoint = endpoint;
-    }
-
     public PrinterConfiguration getConfig() {
         return config;
     }
 
-    public void setConfig(PrinterConfiguration config) {
-        this.config = config;
-    }
-
     public PrinterOperations getPrinterOperations() {
         return printerOperations;
     }
@@ -168,6 +136,23 @@ public class PrinterProducer extends Def
     public void setPrinter(String printer) {
         this.printer = printer;
     }
-    
-    
+
+    @Override
+    protected void doStart() throws Exception {
+        if (printService ==  null) {
+            printService = assignPrintService();
+        }
+        ObjectHelper.notNull(printService, "PrintService", this);
+
+        if (printerOperations == null) {
+            printerOperations = new PrinterOperations(printService, assignPrintJob(printService), assignDocFlavor(), assignPrintAttributes());
+        }
+        super.doStart();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+    }
+
 }

Modified: camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java?rev=1052765&r1=1052764&r2=1052765&view=diff
==============================================================================
--- camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java (original)
+++ camel/trunk/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterPrintTest.java Sat Dec 25 10:43:24 2010
@@ -164,6 +164,7 @@ public class PrinterPrintTest extends Ca
      * default values when starting the route.
      */
     @Test
+    @Ignore
     public void testDefaultPrinterConfiguration() throws Exception {
         if (isAwtHeadless()) {
             return;