KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > loader > SystemInstance


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: SystemInstance.java 2172 2005-09-20 23:58:27Z dblevins $
44  */

45
46 package org.openejb.loader;
47
48 import java.util.Properties JavaDoc;
49 import java.util.HashMap JavaDoc;
50
51 import org.openejb.util.FileUtils;
52
53 /**
54  * This class aims to be the one and only static in the entire system
55  * A static, singleton, instance of this class can be created with the init(props) method
56  *
57  * It is assumed that only one singleton per classloader is possible in any given VM
58  * Thus loading this instance in a classloader will mean there can only be one OpenEJB
59  * instance for that classloader and all children classloaders.
60  *
61  * @version $Revision: 2172 $ $Date: 2005-09-20 16:58:27 -0700 (Tue, 20 Sep 2005) $
62  */

63 public class SystemInstance {
64
65     private final long startTime = System.currentTimeMillis();
66     private final Properties JavaDoc properties;
67     private final FileUtils home;
68     private final FileUtils base;
69     private final ClassLoader JavaDoc classLoader;
70     private final HashMap JavaDoc components;
71     private final ClassPath classPath;
72
73     private SystemInstance(Properties JavaDoc properties) throws Exception JavaDoc {
74         this.components = new HashMap JavaDoc();
75         this.properties = new Properties JavaDoc();
76         this.properties.putAll(System.getProperties());
77         this.properties.putAll(properties);
78         
79         this.home = new FileUtils("openejb.home", "user.dir", this.properties);
80         this.base = new FileUtils("openejb.base", "openejb.home", this.properties);
81         this.classPath = ClassPathFactory.createClassPath(this.properties.getProperty("openejb.loader", "context"));
82         this.classLoader = classPath.getClassLoader();
83
84         // TODO Setup the jar url handler too
85

86         this.properties.setProperty("openejb.home", home.getDirectory().getCanonicalPath());
87         this.properties.setProperty("openejb.base", base.getDirectory().getCanonicalPath());
88     }
89
90
91     public long getStartTime() {
92         return startTime;
93     }
94
95     public Properties JavaDoc getProperties() {
96         return properties;
97     }
98
99     public String JavaDoc getProperty(String JavaDoc key) {
100         return properties.getProperty(key);
101     }
102
103     public String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue) {
104         return properties.getProperty(key, defaultValue);
105     }
106
107     public Object JavaDoc setProperty(String JavaDoc key, String JavaDoc value) {
108         return properties.setProperty(key, value);
109     }
110
111     public FileUtils getHome() {
112         return home;
113     }
114
115     public FileUtils getBase() {
116         return base;
117     }
118
119     public ClassPath getClassPath() {
120         return classPath;
121     }
122
123     public ClassLoader JavaDoc getClassLoader() {
124         return classLoader;
125     }
126
127     public Object JavaDoc getObject(String JavaDoc name) {
128         return components.get(name);
129     }
130
131     public Object JavaDoc setObject(String JavaDoc name, Object JavaDoc value) {
132         return components.put(name, value);
133     }
134
135     //----------------------------------------------------//
136
// Static uglyness
137
//----------------------------------------------------//
138

139     private static SystemInstance system;
140     static {
141         try {
142             system = new SystemInstance(System.getProperties());
143         } catch (Exception JavaDoc e) {
144             throw new RuntimeException JavaDoc("Failed to create default instance of SystemInstance",e);
145         }
146     }
147     private static boolean initialized;
148     public static void init(Properties JavaDoc properties) throws Exception JavaDoc{
149         if (initialized) return;
150         system = new SystemInstance(properties);
151         initialized = true;
152     }
153
154     public static SystemInstance get(){
155         return system;
156     }
157
158 }
159
Popular Tags