Source Code Cross Referenced for PrintServiceLookup.java in  » Apache-Harmony-Java-SE » javax-package » javax » print » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.print 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.