KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > resin > ResinELContext


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Sam
28  */

29
30 package com.caucho.server.resin;
31
32 import com.caucho.config.ConfigELContext;
33 import com.caucho.config.ConfigException;
34 import com.caucho.naming.Jndi;
35 import com.caucho.server.util.CauchoSystem;
36 import com.caucho.util.Alarm;
37 import com.caucho.vfs.Path;
38
39 import java.net.InetAddress JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 abstract public class ResinELContext
44   extends ConfigELContext
45 {
46   private final JavaVar _javaVar = new JavaVar();
47   private final ResinVar _resinVar = new ResinVar();
48   private final ServerVar _serverVar = new ServerVar();
49
50   public ResinELContext()
51   {
52     setValue("java", new JavaVar());
53     setValue("resin", new ResinVar());
54     setValue("server", new ServerVar());
55
56     setValue("fmt", new com.caucho.config.functions.FmtFunctions());
57
58     try {
59       setValue("jndi", Jndi.class.getMethod("lookup", new Class JavaDoc[] { String JavaDoc.class }));
60       setValue("jndi:lookup", Jndi.class.getMethod("lookup", new Class JavaDoc[] { String JavaDoc.class }));
61     } catch (Exception JavaDoc e) {
62       throw new ConfigException(e);
63     }
64   }
65
66   public JavaVar getJavaVar()
67   {
68     return _javaVar;
69   }
70
71   public ResinVar getResinVar()
72   {
73     return _resinVar;
74   }
75
76   public ServerVar getServerVar()
77   {
78     return _serverVar;
79   }
80
81   public Object JavaDoc getValue(String JavaDoc var)
82   {
83     // backwards compatibility
84

85     if ("resinHome".equals(var) || "resin-home".equals(var))
86       return getResinHome();
87
88     if ("serverRoot".equals(var) || "server-root".equals(var))
89       return getRootDirectory();
90
91     return super.getValue(var);
92   }
93
94   public String JavaDoc toString()
95   {
96     return getClass().getSimpleName() + "[]";
97   }
98
99   abstract public Path getResinHome();
100
101   abstract public Path getRootDirectory();
102
103   abstract public Path getResinConf();
104
105   abstract public String JavaDoc getServerId();
106
107   abstract public boolean isResinProfessional();
108
109   /**
110    * Java variables
111    */

112   public class JavaVar {
113     /**
114      * Returns true for JDK 5
115      */

116     public boolean isJava5()
117     {
118       return CauchoSystem.isJdk15();
119     }
120     /**
121      * Returns the JDK version
122      */

123     public String JavaDoc getVersion()
124     {
125       return System.getProperty("java.version");
126     }
127   }
128
129   public class ResinVar {
130     /**
131      * Returns the -server id
132      */

133     public String JavaDoc getServerId()
134     {
135       return ResinELContext.this.getServerId();
136     }
137
138     /**
139      * @deprecated use {@link #getServerId()}
140      */

141     public String JavaDoc getId()
142     {
143       return getServerId();
144     }
145
146
147     /**
148      * Returns the local address
149      *
150      * @return IP address
151      */

152     public String JavaDoc getAddress()
153     {
154       try {
155         if (Alarm.isTest())
156           return "127.0.0.1";
157         else
158           return InetAddress.getLocalHost().getHostAddress();
159       } catch (Exception JavaDoc e) {
160         Logger.getLogger(ResinELContext.class.getName()).log(Level.FINE, e.toString(), e);
161
162         return "localhost";
163       }
164     }
165
166     /**
167      * Returns the resin config.
168      */

169     public Path getConf()
170     {
171       return ResinELContext.this.getResinConf();
172     }
173
174     /**
175      * Returns the resin home.
176      */

177     public Path getHome()
178     {
179       return ResinELContext.this.getResinHome();
180     }
181
182     /**
183      * Returns the root directory.
184      *
185      * @return the root directory
186      */

187     public Path getRoot()
188     {
189       return ResinELContext.this.getRootDirectory();
190     }
191
192     /**
193      * @deprecated use {@link #getRoot()}
194      */

195     public Path getRootDir()
196     {
197       return getRoot();
198     }
199
200     /**
201      * @deprecated use {@link #getRoot()}
202      */

203     public Path getRootDirectory()
204     {
205       return getRoot();
206     }
207
208     /**
209      * Returns the version
210      *
211      * @return version
212      */

213     public String JavaDoc getVersion()
214     {
215       if (Alarm.isTest())
216         return "3.1.test";
217       else
218         return com.caucho.Version.VERSION;
219     }
220
221     /**
222      * Returns the local hostname
223      *
224      * @return version
225      */

226     public String JavaDoc getHostName()
227     {
228       try {
229         if (Alarm.isTest())
230           return "localhost";
231         else
232           return InetAddress.getLocalHost().getHostName();
233       } catch (Exception JavaDoc e) {
234         Logger.getLogger(ResinELContext.class.getName()).log(Level.FINE, e.toString(), e);
235
236         return "localhost";
237       }
238     }
239
240     /**
241      * Returns true for Resin professional.
242      */

243     public boolean isProfessional()
244     {
245       return ResinELContext.this.isResinProfessional();
246     }
247   }
248
249   public class ServerVar {
250     public String JavaDoc getId()
251     {
252       return ResinELContext.this.getServerId();
253     }
254
255     /**
256      * Returns the root directory.
257      *
258      * @return the root directory
259      */

260     public Path getRoot()
261     {
262       return ResinELContext.this.getRootDirectory();
263     }
264
265     /**
266      * @deprecated use {@link #getRoot()}
267      */

268     public Path getRootDir()
269     {
270       return getRoot();
271     }
272
273     /**
274      * @deprecated use {@link #getRoot()}
275      */

276     public Path getRootDirectory()
277     {
278       return getRoot();
279     }
280   }
281 }
282
Popular Tags