01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.responders;
04:
05: import fitnesse.util.PropertiesUtil;
06: import fitnesse.http.*;
07: import fitnesse.*;
08: import java.io.*;
09:
10: public class ProjectExtensionsResponder extends BasicResponder {
11: private static String PROJECT_EXTENSIONS_FILE_DIRECTORY = "STIQProjectExtensionsFileDirectory";
12:
13: public Response makeResponse(FitNesseContext context,
14: Request request) throws Exception {
15: String projectExtensionsFileDirectory = PropertiesUtil
16: .getProperty(PROJECT_EXTENSIONS_FILE_DIRECTORY);
17: if (projectExtensionsFileDirectory != null
18: && projectExtensionsFileDirectory != "") {
19: return responseWith(readProjectExtensionsFileFromDirectory(projectExtensionsFileDirectory));
20: } else {
21: String defaultProjectExtensionsFileDirectory = (String) System
22: .getProperties().get("user.dir");
23: if (!projectExtensionsFileExistsInDirectory(defaultProjectExtensionsFileDirectory)) {
24: System.out
25: .println("No value was provided for "
26: + PROJECT_EXTENSIONS_FILE_DIRECTORY
27: + ", and no project-extensions.js was found at default location "
28: + defaultProjectExtensionsFileDirectory
29: + ", so project-extensions.js will not be loaded.");
30: return responseWith("");
31: } else {
32: return responseWith(readProjectExtensionsFileFromDirectory(defaultProjectExtensionsFileDirectory));
33: }
34: }
35: }
36:
37: private String fullProjectExtensionsFilePath(String directoryPath) {
38: return directoryPath
39: + System.getProperties().get("file.separator")
40: + "project-extensions.js";
41: }
42:
43: private boolean projectExtensionsFileExistsInDirectory(
44: String directoryPath) {
45: try {
46: FileInputStream fstream = new FileInputStream(
47: fullProjectExtensionsFilePath(directoryPath));
48: fstream.markSupported();
49: return true;
50: } catch (Exception e) {
51: return false;
52: }
53: }
54:
55: private String readProjectExtensionsFileFromDirectory(
56: String directoryPath) {
57: return readFileAsString(fullProjectExtensionsFilePath(directoryPath));
58: }
59:
60: private String readFileAsString(String filePath) {
61: String this Line;
62: try {
63: StringBuffer contents = new StringBuffer();
64: BufferedReader br = new BufferedReader(new FileReader(
65: filePath));
66: while ((this Line = br.readLine()) != null) {
67: contents.append(this Line + "\n");
68: }
69: br.close();
70: return contents.toString();
71: } catch (IOException e) {
72: return "alert('Unable to read project extensions file ["
73: + filePath + "].');";
74: }
75: }
76:
77: }
|