KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > embed > EmbedResinServer


1 /*
2  * Copyright (c) 1998-2004 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 Scott Ferguson
28  */

29
30 package com.caucho.server.embed;
31
32 import java.util.logging.Logger;
33
34 import com.caucho.loader.EnvironmentClassLoader;
35
36 import com.caucho.server.port.Port;
37
38 import com.caucho.server.resin.ServerController;
39 import com.caucho.server.resin.ServerConfig;
40 import com.caucho.server.resin.ServletServer;
41
42 import com.caucho.config.Config;
43
44 import com.caucho.config.types.InitProgram;
45
46 import com.caucho.util.Log;
47 import com.caucho.util.L10N;
48
49 import com.caucho.vfs.Path;
50 import com.caucho.vfs.Vfs;
51
52 /**
53  * Main facade for embedding Resin in an application.
54  */

55 public class EmbedResinServer {
56   private static final Logger log = Log.open(EmbedResinServer.class);
57   private static final L10N L = new L10N(EmbedResinServer.class);
58
59   private ServerController _server;
60   private ServerConfig _config;
61
62   /**
63    * Creates a new resin server.
64    */

65   public EmbedResinServer()
66     throws Exception
67   {
68     _server = new ServerController();
69   }
70
71   /**
72    * Creates a new resin server.
73    */

74   public EmbedResinServer(String id, Path rootDirectory)
75     throws Exception
76   {
77     _server = new ServerController(id, rootDirectory);
78   }
79
80   /**
81    * Creates a new resin server.
82    */

83   public EmbedResinServer(ServerConfig config)
84     throws Exception
85   {
86     _server = new ServerController(config);
87   }
88
89   /**
90    * Sets the cluster server id.
91    */

92   public void setServerId(String id)
93   {
94     _server.setServerId(id);
95   }
96
97   /**
98    * Adds a HTTP protocol.
99    */

100   public void addHttp(String host, int port)
101     throws Exception
102   {
103     Port httpPort = new Port();
104     httpPort.setHost(host);
105     httpPort.setPort(port);
106
107     setProperty("http", httpPort);
108   }
109
110   /**
111    * Sets the exported server header.
112    */

113   public void setServerHeader(String serverString)
114   {
115     setProperty("server-header", serverString);
116   }
117
118   /**
119    * Adds a web-app.
120    */

121   public EmbedWebApp addWebApp(String contextPath, String rootDirectory)
122   {
123     Path pwd = Vfs.getPwd();
124     
125     EmbedWebApp webApp = new EmbedWebApp();
126
127     return webApp;
128   }
129
130   /**
131    * Add config file.
132    */

133   public void addConfigFile(Path path)
134     throws Throwable
135   {
136     ServerConfig config = new ServerConfig();
137
138     new Config().configure(config, path, getSchema());
139
140     addConfig(config);
141   }
142
143   /**
144    * Returns the server schema.
145    */

146   protected String getSchema()
147   {
148     return "com/caucho/server/resin/server.rnc";
149   }
150
151   /**
152    * Returns true for the active state.
153    */

154   public boolean isActive()
155   {
156     return _server != null && _server.isActive();
157   }
158
159   /**
160    * Returns true for the destroyed state.
161    */

162   public boolean isDestroyed()
163   {
164     return _server == null || _server.isDestroyed();
165   }
166
167   /**
168    * Starts the server.
169    */

170   public void start()
171     throws Throwable
172   {
173     if (_server == null)
174       throw new IllegalStateException(L.l("tried to start destroyed server"));
175
176     _server.init();
177
178     if (_config != null) {
179       _server.setConfig(_config);
180       _config = null;
181     }
182     
183     _server.start();
184   }
185
186   /**
187    * Stops the server.
188    */

189   public void stop()
190     throws Throwable
191   {
192     if (_server == null)
193       throw new IllegalStateException(L.l("tried to stop destroyed server"));
194     
195     _server.stop();
196   }
197
198   /**
199    * Destroys the server.
200    */

201   public void destroy()
202   {
203     ServerController server = _server;
204     _server = null;
205
206     if (server != null)
207       server.destroy();
208   }
209
210   /**
211    * Returns the deployed instance.
212    */

213   public ServletServer getDeployInstance()
214   {
215     if (_server != null)
216       return _server.getDeployInstance();
217     else
218       return null;
219   }
220
221   /**
222    * Adds a configuration
223    */

224   public void addConfig(ServerConfig config)
225   {
226     if (_config != null) {
227       _server.setConfig(_config);
228       _config = null;
229     }
230
231     _server.setConfig(config);
232   }
233
234   /**
235    * Adds a program
236    */

237   public void setConfigProgram(InitProgram program)
238     throws Throwable
239   {
240     if (_config == null)
241       _config = new ServerConfig();
242
243     program.configure(_config);
244   }
245
246   /**
247    * Adds a property.
248    */

249   protected void setProperty(String name, Object value)
250   {
251     if (_config == null)
252       _config = new ServerConfig();
253
254     _config.addPropertyProgram(name, value);
255   }
256 }
257
Popular Tags