Moved the SCGI storage root default into Environment.
[jsvc.git] / src / dolda / jsvc / scgi / Environment.java
... / ...
CommitLineData
1package dolda.jsvc.scgi;
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6
7public class Environment {
8 public final File root;
9 public final Properties sysconfig = new Properties();
10 private ClassLoader lib = null;
11
12 public Environment(File root) {
13 this.root = root;
14 if(root != null)
15 loadconfig();
16 }
17
18 private static File defroot() {
19 File root = new File(System.getProperty("user.home"), ".jsvc");
20 if(root.exists() && root.isDirectory())
21 return(root);
22 return(null);
23 }
24
25 public Environment() {
26 this(defroot());
27 }
28
29 private void loadconfig() {
30 File sroot = new File(root, "store");
31 sysconfig.put("jsvc.storage", "file:" + sroot.getPath());
32 File conf = new File(root, "jsvc.properties");
33 if(conf.exists()) {
34 try {
35 InputStream in = new FileInputStream(conf);
36 try {
37 sysconfig.load(in);
38 } finally {
39 in.close();
40 }
41 } catch(IOException e) {
42 throw(new RuntimeException(e));
43 }
44 }
45 File lib = new File(root, "lib");
46 if(lib.exists() && lib.isDirectory()) {
47 List<URL> jars = new ArrayList<URL>();
48 for(File f : lib.listFiles()) {
49 if(f.isDirectory())
50 continue;
51 if(!f.canRead())
52 continue;
53 String nm = f.getName();
54 if((nm.length() < 4) || !nm.substring(nm.length() - 4).equals(".jar"))
55 continue;
56 try {
57 jars.add(f.toURI().toURL());
58 } catch(MalformedURLException e) {
59 throw(new Error(e));
60 }
61 }
62 this.lib = URLClassLoader.newInstance(jars.toArray(new URL[0]), Environment.class.getClassLoader());
63 }
64 }
65
66 public ClassLoader libloader() {
67 if(this.lib == null)
68 return(Environment.class.getClassLoader());
69 return(this.lib);
70 }
71
72 public void initvm() {
73 if(root == null)
74 return;
75 File logging = new File(root, "logging.properties");
76 if(logging.exists() && logging.canRead()) {
77 try {
78 InputStream in = new FileInputStream(logging);
79 try {
80 java.util.logging.LogManager.getLogManager().readConfiguration(in);
81 } finally {
82 in.close();
83 }
84 } catch(IOException e) {
85 throw(new RuntimeException(e));
86 }
87 }
88 }
89}