1 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 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 65 public EmbedResinServer() 66 throws Exception 67 { 68 _server = new ServerController(); 69 } 70 71 74 public EmbedResinServer(String id, Path rootDirectory) 75 throws Exception 76 { 77 _server = new ServerController(id, rootDirectory); 78 } 79 80 83 public EmbedResinServer(ServerConfig config) 84 throws Exception 85 { 86 _server = new ServerController(config); 87 } 88 89 92 public void setServerId(String id) 93 { 94 _server.setServerId(id); 95 } 96 97 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 113 public void setServerHeader(String serverString) 114 { 115 setProperty("server-header", serverString); 116 } 117 118 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 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 146 protected String getSchema() 147 { 148 return "com/caucho/server/resin/server.rnc"; 149 } 150 151 154 public boolean isActive() 155 { 156 return _server != null && _server.isActive(); 157 } 158 159 162 public boolean isDestroyed() 163 { 164 return _server == null || _server.isDestroyed(); 165 } 166 167 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 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 201 public void destroy() 202 { 203 ServerController server = _server; 204 _server = null; 205 206 if (server != null) 207 server.destroy(); 208 } 209 210 213 public ServletServer getDeployInstance() 214 { 215 if (_server != null) 216 return _server.getDeployInstance(); 217 else 218 return null; 219 } 220 221 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 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 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 |