001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Aleksei V. Ivaschenko
019: * @version $Revision: 1.2 $
020: */package org.apache.harmony.x.print;
021:
022: import java.security.AccessController;
023: import java.security.PrivilegedAction;
024: import java.util.ArrayList;
025: import javax.print.DocFlavor;
026: import javax.print.MultiDocPrintService;
027: import javax.print.PrintService;
028: import javax.print.PrintServiceLookup;
029: import javax.print.attribute.AttributeSet;
030:
031: import org.apache.harmony.x.print.DefaultPrintService;
032:
033: /*
034: * Print service provider for windows. Loads native library,
035: * searches printers and creates GDI clients for them.
036: */
037: public class Win32PrintServiceProvider extends PrintServiceLookup {
038:
039: private static boolean libraryLoaded = false;
040:
041: static {
042: Object result = AccessController
043: .doPrivileged(new PrivilegedAction() {
044: public Object run() {
045: try {
046: System.loadLibrary("print");
047: return new Boolean(true);
048: } catch (SecurityException se) {
049: // SecurityManager doesn't permit library loading.
050: } catch (UnsatisfiedLinkError ule) {
051: // Can't find library.
052: }
053: return new Boolean(false);
054: }
055: });
056: libraryLoaded = ((Boolean) result).booleanValue();
057: }
058:
059: private static ArrayList services = new ArrayList();
060:
061: /*
062: * Default public constructor.
063: */
064: public Win32PrintServiceProvider() {
065: super ();
066: }
067:
068: /*
069: * Searches default printer connected to current host.
070: * @see javax.print.PrintServiceLookup#getDefaultPrintService()
071: */
072: public PrintService getDefaultPrintService() {
073: if (libraryLoaded) {
074: String defaultService = findDefaultPrintService();
075: if (defaultService != null) {
076: PrintService service = getServiceStored(defaultService);
077: if (service != null) {
078: return service;
079: }
080: GDIClient client = new GDIClient(defaultService);
081: service = new DefaultPrintService(defaultService,
082: client);
083: services.add(service);
084: return service;
085: }
086: }
087: return null;
088: }
089:
090: /*
091: * Searches all printers connected to current host.
092: * @see javax.print.PrintServiceLookup#getPrintServices()
093: */
094: public PrintService[] getPrintServices() {
095: if (!libraryLoaded) {
096: return new PrintService[0];
097: }
098: String[] serviceNames = findPrintServices();
099: if (serviceNames == null || serviceNames.length == 0) {
100: services.clear();
101: return new PrintService[0];
102: }
103: ArrayList newServices = new ArrayList();
104: for (int i = 0; i < serviceNames.length; i++) {
105: PrintService service = getServiceStored(serviceNames[i]);
106: if (service != null) {
107: newServices.add(service);
108: } else {
109: GDIClient client = new GDIClient(serviceNames[i]);
110: service = new DefaultPrintService(serviceNames[i],
111: client);
112: newServices.add(service);
113: }
114: }
115: services.clear();
116: services = newServices;
117: return (services.size() == 0) ? new PrintService[0]
118: : (PrintService[]) services
119: .toArray(new PrintService[0]);
120: }
121:
122: private PrintService getServiceStored(String serviceName) {
123: for (int i = 0; i < services.size(); i++) {
124: PrintService service = (PrintService) services.get(i);
125: if (service.getName().equals(serviceName)) {
126: return service;
127: }
128: }
129: return null;
130: }
131:
132: /*
133: * Searches printers connected to current host, which match
134: * requested doc's flavor and attributes.
135: * @see javax.print.PrintServiceLookup#getPrintServices(
136: * javax.print.DocFlavor, javax.print.attribute.AttributeSet)
137: */
138: public PrintService[] getPrintServices(DocFlavor flavor,
139: AttributeSet attributes) {
140: PrintService[] services = getPrintServices();
141: if (flavor == null && attributes == null) {
142: return services;
143: }
144: ArrayList requestedServices = new ArrayList();
145: for (int i = 0; i < services.length; i++) {
146:
147: try {
148: AttributeSet unsupportedSet = services[i]
149: .getUnsupportedAttributes(flavor, attributes);
150: if (unsupportedSet == null
151: && (flavor == null || services[i]
152: .isDocFlavorSupported(flavor))) {
153: requestedServices.add(services[i]);
154: }
155: } catch (IllegalArgumentException iae) {
156: // DocFlavor not supported by service, skiping.
157: }
158: }
159: return (requestedServices.size() == 0) ? new PrintService[0]
160: : (PrintService[]) requestedServices
161: .toArray(new PrintService[0]);
162: }
163:
164: /*
165: * Searches printers connected to current host, which are able
166: * to print multidocs and match requested doc's flavor and attributes.
167: * @see javax.print.PrintServiceLookup#getMultiDocPrintServices(
168: * javax.print.DocFlavor[], javax.print.attribute.AttributeSet)
169: */
170: public MultiDocPrintService[] getMultiDocPrintServices(
171: DocFlavor[] flavors, AttributeSet attributes) {
172: // No multidoc print services available.
173: return new MultiDocPrintService[0];
174: }
175:
176: private static native String[] findPrintServices();
177:
178: private static native String findDefaultPrintService();
179: }
|