1 // Copyright 2008 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.spi
;
5 import java
.io
.BufferedReader
;
6 import java
.io
.BufferedWriter
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.InputStreamReader
;
10 import java
.io
.OutputStream
;
11 import java
.io
.OutputStreamWriter
;
12 import java
.util
.Collection
;
13 import java
.util
.HashSet
;
17 * A helper class for reading and writing Services files.
19 * @see java.util.ServiceLoader
22 public class ServicesFile
{
24 public static final String SERVICES_PATH
= "META-INF/services";
26 private ServicesFile() {}
29 * Returns an absolute path to a service file given the class name of the service.
31 * @param serviceName not {@code null}
32 * @return SERVICES_PATH + serviceName
34 public static String
getPath(String serviceName
) {
35 return SERVICES_PATH
+ "/" + serviceName
;
39 * Reads the set of service classes from a service file.
41 * @param input not {@code null}. Closed after use.
42 * @return a not {@code null Set} of service class names.
45 public static Set
<String
> readServiceFile(InputStream input
) throws IOException
{
46 BufferedReader r
= null;
47 HashSet
<String
> serviceClasses
= new HashSet
<String
>();
49 r
= new BufferedReader(new InputStreamReader(input
, "UTF-8"));
51 while ((line
= r
.readLine()) != null) {
52 int commentStart
= line
.indexOf('#');
53 if (commentStart
>= 0) {
54 line
= line
.substring(0, commentStart
);
57 if (!line
.equals("")) {
58 serviceClasses
.add(line
);
66 } catch (IOException e
) {
69 return serviceClasses
;
73 * Writes the set of service class names to a service file.
75 * @param output not {@code null}. Not closed after use.
76 * @param services a not {@code null Collection} of service class names.
79 public static void writeServiceFile(Collection
<String
> services
, OutputStream output
)
81 BufferedWriter writer
= new BufferedWriter(new OutputStreamWriter(output
, "UTF-8"));
82 for (String service
: services
) {
83 writer
.write(service
);