src/solaris/classes/sun/print/UnixPrintServiceLookup.java

Print this page

        

@@ -67,11 +67,11 @@
      * will take a hit of needing to regather the list of services.
      */
     private String defaultPrinter;
     private PrintService defaultPrintService;
     private PrintService[] printServices; /* includes the default printer */
-    private Vector lookupListeners = null;
+    private Vector<BackgroundLookupListener> lookupListeners = null;
     private static String debugPrefix = "UnixPrintServiceLookup>> ";
     private static boolean pollServices = true;
     private static final int DEFAULT_MINREFRESH = 120;  // 2 minutes
     private static int minRefreshTime = DEFAULT_MINREFRESH;
 

@@ -237,11 +237,11 @@
         } else {
             return printServices.clone();
         }
     }
 
-    private int addPrintServiceToList(ArrayList printerList, PrintService ps) {
+    private int addPrintServiceToList(ArrayList<PrintService> printerList, PrintService ps) {
         int index = printerList.indexOf(ps);
         // Check if PrintService with same name is already in the list.
         if (CUPSPrinter.isCupsRunning() && index != -1) {
             // Bug in Linux: Duplicate entry of a remote printer
             // and treats it as local printer but it is returning wrong

@@ -251,11 +251,11 @@
             PrinterURI uri = ps.getAttribute(PrinterURI.class);
             if (uri.getURI().getHost().equals("localhost")) {
                 IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, ignoring the new local printer: "+ps);
                 return index;  // Do not add this.
             }
-            PrintService oldPS = (PrintService)(printerList.get(index));
+            PrintService oldPS = printerList.get(index);
             uri = oldPS.getAttribute(PrinterURI.class);
             if (uri.getURI().getHost().equals("localhost")) {
                 IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, removing existing local printer: "+oldPS);
                 printerList.remove(oldPS);
             } else {

@@ -317,11 +317,11 @@
                 printServices = null;
             }
             return;
         }
 
-        ArrayList printerList = new ArrayList();
+        ArrayList<PrintService> printerList = new ArrayList<>();
         int defaultIndex = -1;
         for (int p=0; p<printers.length; p++) {
             if (printers[p] == null) {
                 continue;
             }

@@ -394,12 +394,11 @@
         //if defaultService is not found in printerList
         if (defaultIndex == -1 && defaultPrintService != null) {
             defaultIndex = addPrintServiceToList(printerList, defaultPrintService);
         }
 
-        printServices = (PrintService[])printerList.toArray(
-                                      new PrintService[] {});
+        printServices = printerList.toArray(new PrintService[] {});
 
         // swap default with the first in the list
         if (defaultIndex > 0) {
             PrintService saveService = printServices[0];
             printServices[0] = printServices[defaultIndex];

@@ -409,13 +408,13 @@
 
     private boolean matchesAttributes(PrintService service,
                                       PrintServiceAttributeSet attributes) {
 
         Attribute [] attrs =  attributes.toArray();
-        Attribute serviceAttr;
         for (int i=0; i<attrs.length; i++) {
-            serviceAttr
+            @SuppressWarnings("unchecked")
+            Attribute serviceAttr
                 = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
             if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
                 return false;
             }
         }

@@ -540,20 +539,20 @@
                     return new PrintService[0];
                 }
             }
         } else {
             /* specified service attributes don't include a name.*/
-            Vector matchedServices = new Vector();
+            Vector<PrintService> matchedServices = new Vector<>();
             services = getPrintServices();
             for (int i = 0; i< services.length; i++) {
                 if (matchesAttributes(services[i], serviceSet)) {
                     matchedServices.add(services[i]);
                 }
             }
             services = new PrintService[matchedServices.size()];
             for (int i = 0; i< services.length; i++) {
-                services[i] = (PrintService)matchedServices.elementAt(i);
+                services[i] = matchedServices.elementAt(i);
             }
             return services;
         }
     }
 

@@ -589,22 +588,22 @@
         if (services.length == 0) {
             return services;
         }
 
         if (CUPSPrinter.isCupsRunning()) {
-            ArrayList matchingServices = new ArrayList();
+            ArrayList<PrintService> matchingServices = new ArrayList<>();
             for (int i=0; i<services.length; i++) {
                 try {
                     if (services[i].
                         getUnsupportedAttributes(flavor, requestSet) == null) {
                         matchingServices.add(services[i]);
                     }
                 } catch (IllegalArgumentException e) {
                 }
             }
             services = new PrintService[matchingServices.size()];
-            return (PrintService[])matchingServices.toArray(services);
+            return matchingServices.toArray(services);
 
         } else {
             // We only need to compare 1 PrintService because all
             // UnixPrintServices are the same anyway.  We will not use
             // default PrintService because it might be null.

@@ -702,11 +701,11 @@
         getServicesInbackground(BackgroundLookupListener listener) {
         if (printServices != null) {
             listener.notifyServices(printServices);
         } else {
             if (lookupListeners == null) {
-                lookupListeners = new Vector();
+                lookupListeners = new Vector<>();
                 lookupListeners.add(listener);
                 Thread lookupThread = new Thread(this);
                 lookupThread.start();
             } else {
                 lookupListeners.add(listener);

@@ -731,12 +730,11 @@
     public void run() {
         PrintService[] services = getPrintServices();
         synchronized (this) {
             BackgroundLookupListener listener;
             for (int i=0; i<lookupListeners.size(); i++) {
-                listener =
-                    (BackgroundLookupListener)lookupListeners.elementAt(i);
+                listener = lookupListeners.elementAt(i);
                 listener.notifyServices(copyOf(services));
             }
             lookupListeners = null;
         }
     }

@@ -818,19 +816,19 @@
     private String[] getAllPrinterNamesSysV() {
         String defaultPrinter = "lp";
         String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':' | /usr/bin/sort";
 
         String [] names = execCmd(command);
-        ArrayList printerNames = new ArrayList();
+        ArrayList<String> printerNames = new ArrayList<>();
         for (int i=0; i < names.length; i++) {
             if (!names[i].equals("_default") &&
                 !names[i].equals(defaultPrinter) &&
                 !names[i].equals("")) {
                 printerNames.add(names[i]);
             }
         }
-        return (String[])printerNames.toArray(new String[printerNames.size()]);
+        return printerNames.toArray(new String[printerNames.size()]);
     }
 
     private String getDefaultPrinterNameAIX() {
         String[] names = execCmd(lpNameComAix[aix_lpstat_d]);
         // Remove headers and bogus entries added by remote printers.

@@ -868,11 +866,11 @@
         }
         return printerNames.toArray(new String[printerNames.size()]);
     }
 
     static String[] execCmd(final String command) {
-        ArrayList results = null;
+        ArrayList<String> results = null;
         try {
             final String[] cmd = new String[3];
             if (isSysV() || isAIX()) {
                 cmd[0] = "/usr/bin/sh";
                 cmd[1] = "-c";

@@ -881,13 +879,13 @@
                 cmd[0] = "/bin/sh";
                 cmd[1] = "-c";
                 cmd[2] = "LC_ALL=C " + command;
             }
 
-            results = (ArrayList)AccessController.doPrivileged(
-                new PrivilegedExceptionAction() {
-                    public Object run() throws IOException {
+            results = AccessController.doPrivileged(
+                new PrivilegedExceptionAction<ArrayList<String>>() {
+                    public ArrayList<String> run() throws IOException {
 
                         Process proc;
                         BufferedReader bufferedReader = null;
                         File f = Files.createTempFile("prn","xc").toFile();
                         cmd[2] = cmd[2]+">"+f.getAbsolutePath();

@@ -905,11 +903,11 @@
 
                             if (proc.exitValue() == 0) {
                                 FileReader reader = new FileReader(f);
                                 bufferedReader = new BufferedReader(reader);
                                 String line;
-                                ArrayList results = new ArrayList();
+                                ArrayList<String> results = new ArrayList<>();
                                 while ((line = bufferedReader.readLine())
                                        != null) {
                                     results.add(line);
                                 }
                                 return results;

@@ -930,11 +928,11 @@
         } catch (PrivilegedActionException e) {
         }
         if (results == null) {
             return new String[0];
         } else {
-            return (String[])results.toArray(new String[results.size()]);
+            return results.toArray(new String[results.size()]);
         }
     }
 
     private class PrinterChangeListener extends Thread {