You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/10/30 01:48:14 UTC
svn commit: r469036 -
/incubator/harmony/enhanced/classlib/trunk/modules/print/src/main/java/common/javax/print/PrintServiceLookup.java
Author: ndbeyer
Date: Sun Oct 29 16:48:13 2006
New Revision: 469036
URL: http://svn.apache.org/viewvc?view=rev&rev=469036
Log:
PrintServiceLookup
* add synchronization to protect shared class fields
* format source
* add missing type variables
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/print/src/main/java/common/javax/print/PrintServiceLookup.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/print/src/main/java/common/javax/print/PrintServiceLookup.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/print/src/main/java/common/javax/print/PrintServiceLookup.java?view=diff&rev=469036&r1=469035&r2=469036
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/print/src/main/java/common/javax/print/PrintServiceLookup.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/print/src/main/java/common/javax/print/PrintServiceLookup.java Sun Oct 29 16:48:13 2006
@@ -14,10 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-/**
- * @author Aleksei V. Ivaschenko
- * @version $Revision: 1.3 $
- */
package javax.print;
@@ -25,177 +21,169 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Enumeration;
-
+import java.util.List;
import javax.print.attribute.AttributeSet;
public abstract class PrintServiceLookup {
+ private static List<PrintService> services;
- private static ArrayList services = null;
- private static ArrayList providers = null;
-
+ private static List<PrintServiceLookup> providers;
static {
- services = new ArrayList();
- providers = new ArrayList();
-
- Enumeration providersEnum = null;
- ClassLoader classLoader = null;
- InputStream is = null;
-
- classLoader = (ClassLoader) AccessController
- .doPrivileged(new PrivilegedAction() {
- public Object run() {
- ClassLoader classLoader = Thread.currentThread()
- .getContextClassLoader();
- return (classLoader == null) ? ClassLoader
- .getSystemClassLoader() : classLoader;
+ services = new ArrayList<PrintService>();
+ providers = new ArrayList<PrintServiceLookup>();
+ ClassLoader classLoader = AccessController
+ .doPrivileged(new PrivilegedAction<ClassLoader>() {
+ public ClassLoader run() {
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ if (cl == null) {
+ cl = ClassLoader.getSystemClassLoader();
+ }
+ return cl;
}
});
-
if (classLoader != null) {
+ Enumeration<URL> providersEnum;
try {
providersEnum = classLoader
.getResources("META-INF/services/javax.print.PrintServiceLookup");
- } catch (IOException ioe1) {
- ioe1.printStackTrace();
+ } catch (IOException e) {
+ providersEnum = null;
}
+ if (providersEnum != null) {
+ while (providersEnum.hasMoreElements()) {
+ registerProvidersFromURL(providersEnum.nextElement());
+ }
+ }
+ }
+ }
- while (providersEnum.hasMoreElements()) {
- URL url = (URL) providersEnum.nextElement();
- try {
- is = url.openStream();
-
- BufferedReader providerNameReader;
- String name = null;
-
- try {
- providerNameReader = new BufferedReader(
- new InputStreamReader(is, "UTF-8"));
- } catch (UnsupportedEncodingException uee) {
- providerNameReader = new BufferedReader(
- new InputStreamReader(is));
- }
-
- if (providerNameReader != null) {
- try {
- name = providerNameReader.readLine();
- while (name != null) {
- if (name.length() > 0 && name.charAt(0) != '#') {
- final Class providerClass = Class
- .forName(name);
-
- Object provider = AccessController
- .doPrivileged(new PrivilegedAction() {
- public Object run() {
- try {
- Object inst = providerClass
- .newInstance();
- return inst;
- } catch (InstantiationException ie) {
- System.err
- .println("Can't instantiate class "
- + providerClass
- .getName()
- + ": "
- + ie);
- } catch (IllegalAccessException iae) {
- System.out
- .println("Illegal access for class "
- + providerClass
- .getName()
- + ": "
- + iae);
- }
- return null;
- }
- });
-
- if (provider != null) {
- registerServiceProvider((PrintServiceLookup) provider);
- }
+ private static void registerProvidersFromURL(URL url) {
+ InputStream is;
+ try {
+ is = url.openStream();
+ } catch (IOException e) {
+ e.printStackTrace();
+ return;
+ }
+ BufferedReader providerNameReader;
+ String name = null;
+ try {
+ Reader temp = new InputStreamReader(is, "UTF-8");
+ providerNameReader = new BufferedReader(temp);
+ } catch (UnsupportedEncodingException uee) {
+ // UTF-8 must be supported by the JRE
+ throw new AssertionError(uee);
+ }
+ if (providerNameReader != null) {
+ try {
+ name = providerNameReader.readLine();
+ while (name != null) {
+ if (name.length() > 0 && name.charAt(0) != '#') {
+ final Class<?> providerClass = Class.forName(name);
+ class Action implements PrivilegedAction<Object> {
+ public Object run() {
+ try {
+ Object inst = providerClass.newInstance();
+ return inst;
+ } catch (InstantiationException ie) {
+ System.err.println("Can't instantiate class "
+ + providerClass.getName() + ": " + ie);
+ } catch (IllegalAccessException iae) {
+ System.out.println("Illegal access for class "
+ + providerClass.getName() + ": " + iae);
}
- name = providerNameReader.readLine();
+ return null;
}
- } catch (IOException ioe) {
- System.out
- .println("IOException during reading file:"
- + ioe);
- } catch (ClassNotFoundException cnfe) {
- System.out.println("Class" + name
- + " is not found:" + cnfe);
+ }
+ Object provider = AccessController.doPrivileged(new Action());
+ if (provider != null) {
+ registerServiceProvider((PrintServiceLookup) provider);
}
}
- } catch (IOException ioe2) {
- ioe2.printStackTrace();
+ name = providerNameReader.readLine();
}
+ } catch (IOException e) {
+ System.out.println("IOException during reading file:" + e);
+ } catch (ClassNotFoundException e) {
+ System.out.println("Class" + name + " is not found:" + e);
}
}
}
public PrintServiceLookup() {
+ super();
}
public abstract PrintService getDefaultPrintService();
public abstract PrintService[] getPrintServices();
- public abstract PrintService[] getPrintServices(DocFlavor flavor,
- AttributeSet attributes);
+ public abstract PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes);
- public abstract MultiDocPrintService[] getMultiDocPrintServices(
- DocFlavor[] flavors, AttributeSet attributes);
+ public abstract MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavors,
+ AttributeSet attributes);
public static final PrintService lookupDefaultPrintService() {
- if (providers.size() > 0) {
- PrintServiceLookup provider = (PrintServiceLookup) providers.get(0);
+ synchronized (providers) {
+ if (providers.isEmpty()) {
+ return null;
+ }
+ PrintServiceLookup provider = providers.get(0);
return provider.getDefaultPrintService();
}
- return null;
}
public static final PrintService[] lookupPrintServices(DocFlavor flavor,
AttributeSet attributes) {
- ArrayList printServices = new ArrayList();
- for (int i = 0; i < providers.size(); i++) {
- PrintServiceLookup provider = (PrintServiceLookup) providers.get(i);
- PrintService[] providerServices = provider.getPrintServices(flavor,
- attributes);
- for (int j = 0; j < providerServices.length; j++) {
- printServices.add(providerServices[j]);
- }
- }
- return (printServices.size() == 0 ? new PrintService[0]
- : (PrintService[]) printServices.toArray(new PrintService[0]));
- }
-
- public static final MultiDocPrintService[] lookupMultiDocPrintServices(
- DocFlavor[] flavors, AttributeSet attributes) {
- ArrayList printServices = new ArrayList();
- for (int i = 0; i < providers.size(); i++) {
- PrintServiceLookup provider = (PrintServiceLookup) providers.get(i);
- MultiDocPrintService[] providerServices = provider
- .getMultiDocPrintServices(flavors, attributes);
- if (providerServices != null) {
+ synchronized (providers) {
+ List<PrintService> printServices = new ArrayList<PrintService>();
+ int providersSize = providers.size();
+ for (int i = 0; i < providersSize; i++) {
+ PrintServiceLookup provider = providers.get(i);
+ PrintService[] providerServices = provider.getPrintServices(flavor, attributes);
for (int j = 0; j < providerServices.length; j++) {
printServices.add(providerServices[j]);
}
}
+ return printServices.toArray(new PrintService[printServices.size()]);
+ }
+ }
+
+ public static final MultiDocPrintService[] lookupMultiDocPrintServices(DocFlavor[] flavors,
+ AttributeSet attributes) {
+ synchronized (providers) {
+ List<MultiDocPrintService> printServices = new ArrayList<MultiDocPrintService>();
+ int providersSize = providers.size();
+ for (int i = 0; i < providersSize; i++) {
+ PrintServiceLookup provider = providers.get(i);
+ MultiDocPrintService[] providerServices = provider.getMultiDocPrintServices(
+ flavors, attributes);
+ if (providerServices != null) {
+ for (int j = 0; j < providerServices.length; j++) {
+ printServices.add(providerServices[j]);
+ }
+ }
+ }
+ return printServices.toArray(new MultiDocPrintService[printServices.size()]);
}
- return (printServices.size() == 0 ? new MultiDocPrintService[0]
- : (MultiDocPrintService[]) printServices
- .toArray(new MultiDocPrintService[0]));
}
public static boolean registerService(PrintService service) {
- return services.add(service);
+ synchronized (services) {
+ return services.add(service);
+ }
}
public static boolean registerServiceProvider(PrintServiceLookup provider) {
- return providers.add(provider);
+ synchronized (providers) {
+ return providers.add(provider);
+ }
}
-}
\ No newline at end of file
+}