1 7 8 package javax.print; 9 10 import java.io.OutputStream ; 11 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 15 import javax.print.DocFlavor ; 16 17 import sun.awt.AppContext; 18 import sun.misc.Service; 19 20 39 40 public abstract class StreamPrintServiceFactory { 41 42 static class Services { 43 private ArrayList listOfFactories = null; 44 } 45 46 private static Services getServices() { 47 Services services = 48 (Services)AppContext.getAppContext().get(Services.class); 49 if (services == null) { 50 services = new Services(); 51 AppContext.getAppContext().put(Services.class, services); 52 } 53 return services; 54 } 55 56 private static ArrayList getListOfFactories() { 57 return getServices().listOfFactories; 58 } 59 60 private static ArrayList initListOfFactories() { 61 ArrayList listOfFactories = new ArrayList (); 62 getServices().listOfFactories = listOfFactories; 63 return listOfFactories; 64 } 65 66 86 public static StreamPrintServiceFactory [] 87 lookupStreamPrintServiceFactories(DocFlavor flavor, 88 String outputMimeType) { 89 90 ArrayList list = getFactories(flavor, outputMimeType); 91 return (StreamPrintServiceFactory []) 92 (list.toArray(new StreamPrintServiceFactory [list.size()])); 93 } 94 95 100 public abstract String getOutputFormat(); 101 102 107 public abstract DocFlavor [] getSupportedDocFlavors(); 108 109 141 public abstract StreamPrintService getPrintService(OutputStream out); 142 143 144 private static ArrayList getAllFactories() { 145 synchronized (StreamPrintServiceFactory .class) { 146 147 ArrayList listOfFactories = getListOfFactories(); 148 if (listOfFactories != null) { 149 return listOfFactories; 150 } else { 151 listOfFactories = initListOfFactories(); 152 } 153 154 try { 155 java.security.AccessController.doPrivileged( 156 new java.security.PrivilegedExceptionAction () { 157 public Object run() { 158 Iterator iterator = 159 Service.providers( 160 StreamPrintServiceFactory .class); 161 ArrayList lof = getListOfFactories(); 162 while (iterator.hasNext()) { 163 try { 164 StreamPrintServiceFactory factory = 165 (StreamPrintServiceFactory ) 166 iterator.next(); 167 lof.add(factory); 168 } catch (Exception e) { 169 } 170 } 171 return null; 172 } 173 }); 174 } catch (java.security.PrivilegedActionException e) { 175 } 176 return listOfFactories; 177 } 178 } 179 180 private static boolean isMember(DocFlavor flavor, DocFlavor [] flavors) { 181 for (int f=0; f<flavors.length; f++ ) { 182 if (flavor.equals(flavors[f])) { 183 return true; 184 } 185 } 186 return false; 187 } 188 189 private static ArrayList getFactories(DocFlavor flavor, String outType) { 190 191 if (flavor == null && outType == null) { 192 return getAllFactories(); 193 } 194 195 ArrayList list = new ArrayList (); 196 Iterator iterator = getAllFactories().iterator(); 197 while (iterator.hasNext()) { 198 StreamPrintServiceFactory factory = 199 (StreamPrintServiceFactory )iterator.next(); 200 if ((outType == null || 201 outType.equalsIgnoreCase(factory.getOutputFormat())) && 202 (flavor == null || 203 isMember(flavor, factory.getSupportedDocFlavors()))) { 204 list.add(factory); 205 } 206 } 207 208 return list; 209 } 210 211 } 212 | Popular Tags |