1 /*
2 * Copyright (c) 2003 Peter Antman, Teknik i Media <peter.antman@tim.se>
3 *
4 * $Id: GlobalUtil.java 15205 2006-06-29 17:25:12Z ruff $
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 package org.xmlBlaster.j2ee.util;
21 import java.util.Properties;
22 import java.io.InputStream;
23 import java.io.IOException;
24 import java.net.URL;
25 import java.rmi.RMISecurityManager;
26
27 import org.xmlBlaster.util.Global;
28 import org.xmlBlaster.util.XmlBlasterException;
29 import org.xmlBlaster.util.def.Constants;
30 import org.xmlBlaster.util.property.Property;
31 import org.xmlBlaster.util.property.Property.FileInfo;
32
33 /**
34 * A Global helper class to make it easier to work with Global in an embedded
35 * J2EE environment.
36 * <p>The helper may be used in two major ways: created standalone, or looked up through JNDI. When looked up trough JNDI it might be possible to have access to
37 * the engine.Global if accessed in the same VM as the engine.</p>
38 *
39 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
40 * @version $Revision: 1.2 $
41 */
42
43 public class GlobalUtil implements java.io.Serializable {
44 /**
45 *
46 */
47 private static final long serialVersionUID = 1L;
48 private transient org.xmlBlaster.engine.ServerScope engineGlobal;
49
50 /**
51 * Create a utility class without any engine global.
52 */
53 public GlobalUtil (){
54
55 }
56
57 /**
58 * Create a utility class with access to engine global, use this to
59 * bind into jndi.
60 */
61 public GlobalUtil(org.xmlBlaster.engine.ServerScope engineGlobal) {
62 this.engineGlobal = engineGlobal;
63 }
64
65 /**
66 * Create a new Global without any automatic background loading of
67 * xmlBlaster.properties.
68 * <p>if engineGlobal is available, its set as a ServerNode.</p>
69 * <p>This Global will be setup to be used in a server environment.</p>
70 * <p>if an engine global exists, the properties from that one will be
71 * set first in the created Global.</p>
72 */
73 public Global newGlobal(String propertyFileName, Properties args) throws IllegalStateException {
74 Global glob = new Global(new String[]{},false,false);
75 Global clone = getClone(glob);
76 addEngineProperties(clone);
77 addServerProperties(clone);
78 loadPropertyFile(clone,propertyFileName);
79 addArguments(clone,args);
80 return clone;
81 }
82
83 /**
84 * Set the properties from the serverside engine global in the given global if it exists.
85 */
86 public void addEngineProperties(Global glob) throws IllegalStateException{
87 if ( engineGlobal != null) {
88 Property p = glob.getProperty();
89 String[] args = Property.propsToArgs(engineGlobal.getProperty().getProperties() );
90 try {
91 p.addArgs2Props( args != null ? args : new String[0] );
92 } catch (XmlBlasterException e) {
93 IllegalStateException x = new IllegalStateException("Could not engine properties into global: " + e);
94 throw x;
95 } // end of try-catch
96
97 } // end of if ()
98
99 }
100
101 /**
102 * Clone the given Global, check if it contains a ServerNode, or set
103 * this engineGlobal if not null as ServerNode.
104 */
105 public Global getClone(Global global) {
106 Global g = global.getClone(null);
107
108 Object engine = global.getObjectEntry(Constants.OBJECT_ENTRY_ServerScope);
109 Object eg = engine != null ? engine : engineGlobal;
110 if ( eg != null) {
111 g.addObjectEntry(Constants.OBJECT_ENTRY_ServerScope, eg);
112 } // end of if ()
113
114 // Should we perhaps also clone POA???
115
116 return g;
117 }
118
119 /**
120 * Load properties from propertyFile into global.
121 * <p>The property file is first looked up the the context classpath, next the xmlBlaster lookup algorithm is used.</p>
122 * <p>The global is returned as is, that means that any properties found
123 * in propertyFile, such as logging, is ignored until a cloned copy of
124 * the global is retained.</p>
125 */
126 public void loadPropertyFile(Global glob, String propFile) throws IllegalStateException{
127 if (propFile== null )
128 return;
129
130 try {
131 Property p = glob.getProperty();
132 URL url = Thread.currentThread().getContextClassLoader().getResource(propFile);
133 InputStream is = null;
134 if ( url != null) {
135 try {
136 is= url.openStream();
137
138 }catch(java.io.IOException ex) {
139 is = null;
140 }
141 } // end of if ()
142
143 if ( is == null) {
144 // Use xmlBlaster way of searching
145 FileInfo i = p.findPath(propFile);
146 if ( i != null) {
147 is = i.getInputStream();
148 } // end of if ()
149
150 } // end of if ()
151
152 if ( is != null) {
153 Properties prop = new Properties();
154 prop.load(is);
155 addArguments(glob, prop);
156 } // end of if ()
157
158 } catch (IOException e) {
159 IllegalStateException x = new IllegalStateException("Could not load properties from file " + propFile + " :"+e);
160 throw x;
161
162 } catch (XmlBlasterException e) {
163 IllegalStateException x = new IllegalStateException("Could not load properties into Property: " + e);
164 throw x;
165 } // end of try-catch
166 }
167
168
169 /**
170 * Ad arguments found in props to the global.
171 * <p>The global is returned as is, that means that any properties found
172 * in props, such as logging, is ignored until a cloned copy of
173 * the global is retained.</p>
174 */
175 public void addArguments(Global glob, Properties props) throws IllegalStateException{
176 if ( props == null) {
177 return;
178 } // end of if ()
179 try {
180 Property p = glob.getProperty();
181 String[] args = Property.propsToArgs(props);
182 p.addArgs2Props( args != null ? args : new String[0] );
183 } catch (XmlBlasterException e) {
184 IllegalStateException x = new IllegalStateException("Could not load properties into Property: " + e);
185 throw x;
186 } // end of try-catch
187 }
188
189 /**
190 * Ad typical properties needed to run embedded in a J2EE/JBoss server.
191 */
192 public void addServerProperties(Global glob) throws IllegalStateException {
193 try {
194 glob.getProperty().set("classLoaderFactory","org.xmlBlaster.util.classloader.ContextClassLoaderFactory");
195 glob.getProperty().set("xmlBlaster.isEmbedded", "true");
196 glob.getProperty().set("useSignalCatcher","false");
197 } catch (XmlBlasterException e) {
198 IllegalStateException x = new IllegalStateException("Could not set serverside properties: " + e);
199 throw x;
200 } // end of try-catch
201
202
203 }
204
205 /**
206 * Check and possibly setup a security manager if an RMI driver is loaded.
207 */
208 public void setupSecurityManager(Global glob) throws SecurityException{
209 // This is really only interesting if we are loading an RMIDriver
210 if (glob.getProperty().get("ProtocolPlugin[RMI][1.0]",(String)null) == null) {
211 return;// We only care about this if the RMI driver should be loaded
212
213 } // end of if ()
214
215
216 if (System.getSecurityManager() == null) {
217 String exist = System.getProperty("java.security.policy");
218 if (exist == null) {
219 throw new SecurityException("You must specify a -Djava.security.policy when starting the server to be able to use the RMI driver");
220 }else {
221 System.setSecurityManager(new RMISecurityManager());
222 } // end of else
223 }
224 }
225 }// GlobalUtil
syntax highlighted by Code2HTML, v. 0.9.1