Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / spi / ServicesFile.java
blob017ddf2d27ac4112f4fd6139c7f3b7e2ed37d4b3
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;
14 import java.util.Set;
16 /**
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() {}
28 /**
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;
38 /**
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.
43 * @throws IOException
45 public static Set<String> readServiceFile(InputStream input) throws IOException {
46 BufferedReader r = null;
47 HashSet<String> serviceClasses = new HashSet<String>();
48 try {
49 r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
50 String line;
51 while ((line = r.readLine()) != null) {
52 int commentStart = line.indexOf('#');
53 if (commentStart >= 0) {
54 line = line.substring(0, commentStart);
56 line = line.trim();
57 if (!line.equals("")) {
58 serviceClasses.add(line);
61 } finally {
62 try {
63 if (r != null) {
64 r.close();
66 } catch (IOException e) {
69 return serviceClasses;
72 /**
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.
77 * @throws IOException
79 public static void writeServiceFile(Collection<String> services, OutputStream output)
80 throws IOException {
81 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
82 for (String service : services) {
83 writer.write(service);
84 writer.newLine();
86 writer.flush();