Merge branch 'master' of git.dolda2000.com:/srv/git/r/jsvc
[jsvc.git] / src / dolda / jsvc / scgi / Environment.java
CommitLineData
22779185
FT
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 conf = new File(root, "jsvc.properties");
31 if(conf.exists()) {
32 try {
33 InputStream in = new FileInputStream(conf);
34 try {
35 sysconfig.load(in);
36 } finally {
37 in.close();
38 }
39 } catch(IOException e) {
40 throw(new RuntimeException(e));
41 }
42 }
43 File lib = new File(root, "lib");
44 if(lib.exists() && lib.isDirectory()) {
45 List<URL> jars = new ArrayList<URL>();
46 for(File f : lib.listFiles()) {
47 if(f.isDirectory())
48 continue;
49 if(!f.canRead())
50 continue;
51 String nm = f.getName();
52 if((nm.length() < 4) || !nm.substring(nm.length() - 4).equals(".jar"))
53 continue;
54 try {
55 jars.add(f.toURI().toURL());
56 } catch(MalformedURLException e) {
57 throw(new Error(e));
58 }
59 }
60 this.lib = URLClassLoader.newInstance(jars.toArray(new URL[0]), Environment.class.getClassLoader());
61 }
62 }
63
64 public ClassLoader libloader() {
65 if(this.lib == null)
66 return(Environment.class.getClassLoader());
67 return(this.lib);
68 }
69
70 public void initvm() {
71 if(root == null)
72 return;
73 File logging = new File(root, "logging.properties");
74 if(logging.exists() && logging.canRead()) {
75 try {
76 InputStream in = new FileInputStream(logging);
77 try {
78 java.util.logging.LogManager.getLogManager().readConfiguration(in);
79 } finally {
80 in.close();
81 }
82 } catch(IOException e) {
83 throw(new RuntimeException(e));
84 }
85 }
86 }
87}