use of org.glassfish.embeddable.web.VirtualServer in project Payara by payara.
the class WebContainerImpl method addVirtualServer.
/**
* Adds the given <tt>VirtualServer</tt> to this
* <tt>WebContainer</tt>.
*
* <p>If this <tt>WebContainer</tt> has already been started,
* the given <tt>virtualServer</tt> will be started as well.
*
* @param virtualServer the <tt>VirtualServer</tt> to add
*
* @throws ConfigException if a <tt>VirtualServer</tt> with the
* same id has already been registered with this
* <tt>WebContainer</tt>
* @throws GlassFishException if the given <tt>virtualServer</tt> fails
* to be started
*/
public void addVirtualServer(VirtualServer virtualServer) throws ConfigException, GlassFishException {
if (!initialized) {
init();
}
if (log.isLoggable(Level.INFO)) {
log.info("Adding virtual server " + virtualServer.getID());
}
com.sun.enterprise.web.VirtualServer vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(virtualServer.getID());
if (vs != null) {
throw new ConfigException("VirtualServer with id " + virtualServer.getID() + " is already registered");
}
Collection<WebListener> webListeners = virtualServer.getWebListeners();
List<String> names = new ArrayList<String>();
if ((webListeners != null) && (!webListeners.isEmpty())) {
for (WebListener listener : webListeners) {
names.add(listener.getId());
}
} else {
for (NetworkListener networkListener : networkConfig.getNetworkListeners().getNetworkListener()) {
names.add(networkListener.getName());
}
webListeners = listeners;
}
StringBuffer networkListeners = new StringBuffer("");
if (names.size() > 0) {
networkListeners.append(names.get(0));
}
for (int i = 1; i < names.size(); i++) {
networkListeners.append(",");
networkListeners.append(names.get(i));
}
String docRoot = null;
if (virtualServer.getDocRoot() != null) {
docRoot = virtualServer.getDocRoot().getAbsolutePath();
}
String hostName = null;
if (virtualServer.getConfig() != null) {
hostName = virtualServer.getConfig().getHostNames();
}
final String root = docRoot;
final String nl = networkListeners.toString();
final String id = virtualServer.getID();
final String hosts = hostName;
try {
ConfigSupport.apply(new SingleConfigCode<HttpService>() {
public Object run(HttpService param) throws PropertyVetoException, TransactionFailure {
com.sun.enterprise.config.serverbeans.VirtualServer newVirtualServer = param.createChild(com.sun.enterprise.config.serverbeans.VirtualServer.class);
newVirtualServer.setId(id);
newVirtualServer.setNetworkListeners(nl);
if (hosts != null) {
newVirtualServer.setHosts(hosts);
}
Property property = newVirtualServer.createChild(Property.class);
property.setName("docroot");
property.setValue(root);
newVirtualServer.getProperty().add(property);
param.getVirtualServer().add(newVirtualServer);
return newVirtualServer;
}
}, httpService);
} catch (Exception ex) {
throw new GlassFishException(ex);
}
if ((webListeners != null) && (!webListeners.isEmpty())) {
for (WebListener listener : webListeners) {
if (getWebListener(listener.getId()) == null) {
addWebListener(listener, virtualServer.getID());
}
}
}
vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(id);
if (vs != null) {
if (log.isLoggable(Level.INFO)) {
log.info("Added virtual server " + id + " docroot " + docRoot + " networklisteners " + nl);
}
if (virtualServer instanceof VirtualServerFacade) {
((VirtualServerFacade) virtualServer).setVirtualServer(vs);
}
vs.setNetworkListenerNames(names.toArray(new String[names.size()]));
} else {
log.severe("Could not add virtual server " + id);
throw new GlassFishException(new Exception("Cannot add virtual server " + id));
}
}
use of org.glassfish.embeddable.web.VirtualServer in project Payara by payara.
the class WebContainerImpl method setConfiguration.
// --------------------------------------------------------- Public Methods
public void setConfiguration(WebContainerConfig config) {
if (!initialized) {
init();
}
this.config = config;
final WebContainerConfig webConfig = config;
try {
VirtualServer vs = getVirtualServer(config.getVirtualServerId());
if (vs != null) {
((StandardHost) vs).setDefaultWebXmlLocation(config.getDefaultWebXml().getPath());
}
com.sun.enterprise.config.serverbeans.VirtualServer vsBean = httpService.getVirtualServerByName(config.getVirtualServerId());
if (vsBean != null) {
ConfigSupport.apply(new SingleConfigCode<com.sun.enterprise.config.serverbeans.VirtualServer>() {
public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs) throws PropertyVetoException, TransactionFailure {
avs.setId(webConfig.getVirtualServerId());
if (webConfig.getDocRootDir() != null) {
avs.setDocroot(webConfig.getDocRootDir().getAbsolutePath());
}
avs.setHosts(webConfig.getHostNames());
avs.setNetworkListeners(webConfig.getListenerName());
Property property = avs.createChild(Property.class);
property.setName("default-web-xml");
property.setValue(webConfig.getDefaultWebXml().getPath());
avs.getProperty().add(property);
return avs;
}
}, vsBean);
} else {
vs = createVirtualServer(config.getVirtualServerId(), config.getDocRootDir());
addVirtualServer(vs);
}
EmbeddedWebArchivist archivist = habitat.<EmbeddedWebArchivist>getService(EmbeddedWebArchivist.class);
archivist.setDefaultWebXml(config.getDefaultWebXml());
embedded.setDirectoryListing(config.getListings());
WebListener listener = getWebListener(config.getListenerName());
if (listener == null) {
listener = getWebListener(config.getPort());
if (listener == null) {
boolean found = false;
for (Map.Entry entry : webContainer.getConnectorMap().entrySet()) {
if (((WebConnector) entry.getValue()).getPort() == config.getPort()) {
found = true;
log.info("Port " + config.getPort() + " is already configured");
}
}
if (!found) {
listener = createWebListener(config.getListenerName(), HttpListener.class);
listener.setPort(config.getPort());
addWebListener(listener, config.getVirtualServerId());
}
}
} else {
if (listener.getPort() != config.getPort()) {
listener.setPort(config.getPort());
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of org.glassfish.embeddable.web.VirtualServer in project Payara by payara.
the class WebContainerImpl method removeListener.
private void removeListener(String name) {
try {
NetworkListeners networkListeners = networkConfig.getNetworkListeners();
final NetworkListener listenerToBeRemoved = networkConfig.getNetworkListener(name);
final Protocols protocols = networkConfig.getProtocols();
final Protocol protocol = networkConfig.findProtocol(name);
if (listenerToBeRemoved == null) {
log.severe("Network Listener " + name + " doesn't exist");
} else {
final com.sun.enterprise.config.serverbeans.VirtualServer virtualServer = httpService.getVirtualServerByName(listenerToBeRemoved.findHttpProtocol().getHttp().getDefaultVirtualServer());
ConfigSupport.apply(new ConfigCode() {
public Object run(ConfigBeanProxy... params) throws PropertyVetoException {
final NetworkListeners listeners = (NetworkListeners) params[0];
final com.sun.enterprise.config.serverbeans.VirtualServer server = (com.sun.enterprise.config.serverbeans.VirtualServer) params[1];
listeners.getNetworkListener().remove(listenerToBeRemoved);
server.removeNetworkListener(listenerToBeRemoved.getName());
return listenerToBeRemoved;
}
}, networkListeners, virtualServer);
ConfigSupport.apply(new ConfigCode() {
public Object run(ConfigBeanProxy... params) throws PropertyVetoException {
final Protocols protocols = (Protocols) params[0];
final Protocol protocol = (Protocol) params[1];
protocols.getProtocol().remove(protocol);
return protocol;
}
}, protocols, protocol);
}
} catch (TransactionFailure e) {
log.severe("Remove listener " + name + " failed " + e.getMessage());
}
}
Aggregations