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: package javax.print;
019:
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.Reader;
025: import java.io.UnsupportedEncodingException;
026: import java.net.URL;
027: import java.security.AccessController;
028: import java.security.PrivilegedAction;
029: import java.util.ArrayList;
030: import java.util.Enumeration;
031: import java.util.List;
032: import javax.print.attribute.AttributeSet;
033:
034: public abstract class PrintServiceLookup {
035: private static List<PrintService> services;
036:
037: private static List<PrintServiceLookup> providers;
038: static {
039: services = new ArrayList<PrintService>();
040: providers = new ArrayList<PrintServiceLookup>();
041: ClassLoader classLoader = AccessController
042: .doPrivileged(new PrivilegedAction<ClassLoader>() {
043: public ClassLoader run() {
044: ClassLoader cl = Thread.currentThread()
045: .getContextClassLoader();
046: if (cl == null) {
047: cl = ClassLoader.getSystemClassLoader();
048: }
049: return cl;
050: }
051: });
052: if (classLoader != null) {
053: Enumeration<URL> providersEnum;
054: try {
055: providersEnum = classLoader
056: .getResources("META-INF/services/javax.print.PrintServiceLookup");
057: } catch (IOException e) {
058: providersEnum = null;
059: }
060: if (providersEnum != null) {
061: while (providersEnum.hasMoreElements()) {
062: registerProvidersFromURL(providersEnum
063: .nextElement());
064: }
065: }
066: }
067: }
068:
069: private static void registerProvidersFromURL(URL url) {
070: InputStream is;
071: try {
072: is = url.openStream();
073: } catch (IOException e) {
074: e.printStackTrace();
075: return;
076: }
077: BufferedReader providerNameReader;
078: String name = null;
079: try {
080: Reader temp = new InputStreamReader(is, "UTF-8");
081: providerNameReader = new BufferedReader(temp);
082: } catch (UnsupportedEncodingException uee) {
083: // UTF-8 must be supported by the JRE
084: throw new AssertionError(uee);
085: }
086: if (providerNameReader != null) {
087: try {
088: name = providerNameReader.readLine();
089: while (name != null) {
090: if (name.length() > 0 && name.charAt(0) != '#') {
091: final Class<?> providerClass = Class
092: .forName(name);
093: class Action implements
094: PrivilegedAction<Object> {
095: public Object run() {
096: try {
097: Object inst = providerClass
098: .newInstance();
099: return inst;
100: } catch (InstantiationException ie) {
101: System.err
102: .println("Can't instantiate class "
103: + providerClass
104: .getName()
105: + ": " + ie);
106: } catch (IllegalAccessException iae) {
107: System.out
108: .println("Illegal access for class "
109: + providerClass
110: .getName()
111: + ": " + iae);
112: }
113: return null;
114: }
115: }
116: Object provider = AccessController
117: .doPrivileged(new Action());
118: if (provider != null) {
119: registerServiceProvider((PrintServiceLookup) provider);
120: }
121: }
122: name = providerNameReader.readLine();
123: }
124: } catch (IOException e) {
125: System.out.println("IOException during reading file:"
126: + e);
127: } catch (ClassNotFoundException e) {
128: System.out.println("Class" + name + " is not found:"
129: + e);
130: }
131: }
132: }
133:
134: public PrintServiceLookup() {
135: super ();
136: }
137:
138: public abstract PrintService getDefaultPrintService();
139:
140: public abstract PrintService[] getPrintServices();
141:
142: public abstract PrintService[] getPrintServices(DocFlavor flavor,
143: AttributeSet attributes);
144:
145: public abstract MultiDocPrintService[] getMultiDocPrintServices(
146: DocFlavor[] flavors, AttributeSet attributes);
147:
148: public static final PrintService lookupDefaultPrintService() {
149: synchronized (providers) {
150: if (providers.isEmpty()) {
151: return null;
152: }
153: PrintServiceLookup provider = providers.get(0);
154: return provider.getDefaultPrintService();
155: }
156: }
157:
158: public static final PrintService[] lookupPrintServices(
159: DocFlavor flavor, AttributeSet attributes) {
160: synchronized (providers) {
161: List<PrintService> printServices = new ArrayList<PrintService>();
162: int providersSize = providers.size();
163: for (int i = 0; i < providersSize; i++) {
164: PrintServiceLookup provider = providers.get(i);
165: PrintService[] providerServices = provider
166: .getPrintServices(flavor, attributes);
167: for (int j = 0; j < providerServices.length; j++) {
168: printServices.add(providerServices[j]);
169: }
170: }
171: return printServices.toArray(new PrintService[printServices
172: .size()]);
173: }
174: }
175:
176: public static final MultiDocPrintService[] lookupMultiDocPrintServices(
177: DocFlavor[] flavors, AttributeSet attributes) {
178: synchronized (providers) {
179: List<MultiDocPrintService> printServices = new ArrayList<MultiDocPrintService>();
180: int providersSize = providers.size();
181: for (int i = 0; i < providersSize; i++) {
182: PrintServiceLookup provider = providers.get(i);
183: MultiDocPrintService[] providerServices = provider
184: .getMultiDocPrintServices(flavors, attributes);
185: if (providerServices != null) {
186: for (int j = 0; j < providerServices.length; j++) {
187: printServices.add(providerServices[j]);
188: }
189: }
190: }
191: return printServices
192: .toArray(new MultiDocPrintService[printServices
193: .size()]);
194: }
195: }
196:
197: public static boolean registerService(PrintService service) {
198: synchronized (services) {
199: return services.add(service);
200: }
201: }
202:
203: public static boolean registerServiceProvider(
204: PrintServiceLookup provider) {
205: synchronized (providers) {
206: return providers.add(provider);
207: }
208: }
209: }
|