use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.
the class TomcatVersionHelper method moveContextsToSeparateFiles.
/**
* Moves contexts out of current published server.xml and into individual
* context XML files.
*
* @param baseDir directory where the Catalina instance is found
* @param noPath true if path attribute should be removed from the context
* @param serverStopped true if the server is stopped
* @param monitor a progress monitor
* @return result of operation
*/
public static IStatus moveContextsToSeparateFiles(IPath baseDir, boolean noPath, boolean serverStopped, IProgressMonitor monitor) {
IPath confDir = baseDir.append("conf");
IPath serverXml = confDir.append("server.xml");
try {
monitor = ProgressUtil.getMonitorFor(monitor);
monitor.beginTask(Messages.publishConfigurationTask, 300);
monitor.subTask(Messages.publishContextConfigTask);
Factory factory = new Factory();
factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
Server publishedServer = (Server) factory.loadDocument(new FileInputStream(serverXml.toFile()));
ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
monitor.worked(100);
boolean modified = false;
Host host = publishedInstance.getHost();
Context[] wtpContexts = publishedInstance.getContexts();
if (wtpContexts != null && wtpContexts.length > 0) {
IPath contextPath = publishedInstance.getContextXmlDirectory(serverXml.removeLastSegments(1));
File contextDir = contextPath.toFile();
if (!contextDir.exists()) {
contextDir.mkdirs();
}
// Process in reverse order, since contexts may be removed
for (int i = wtpContexts.length - 1; i >= 0; i--) {
Context context = wtpContexts[i];
// TODO Handle non-project contexts when their removal can be addressed
if (context.getSource() == null)
continue;
String name = context.getPath();
if (name.startsWith("/")) {
name = name.substring(1);
}
// If the default context, adjust the file name
if (name.length() == 0) {
name = "ROOT";
}
// Update name if multi-level path. For 5.5 and later the "#" has been
// "reserved" as a legal file name placeholder for "/". For Tomcat 5.0,
// we just need a legal unique file name since "/" will fail. Prior to
// 5.0, this feature is not supported.
name = name.replace('/', '#');
// TODO Determine circumstances, if any, where setting antiResourceLocking true can cause the original docBase content to be deleted.
if (Boolean.valueOf(context.getAttributeValue("antiResourceLocking")).booleanValue())
context.setAttributeValue("antiResourceLocking", "false");
File contextFile = new File(contextDir, name + ".xml");
Context existingContext = loadContextFile(contextFile);
// If server is stopped or if contexts are not the equivalent, write the context file
if (serverStopped || !context.isEquivalent(existingContext)) {
// If requested, remove path attribute
if (noPath)
context.removeAttribute("path");
DocumentBuilder builder = XMLUtil.getDocumentBuilder();
Document contextDoc = builder.newDocument();
contextDoc.appendChild(contextDoc.importNode(context.getElementNode(), true));
XMLUtil.save(contextFile.getAbsolutePath(), contextDoc);
}
host.removeElement("Context", i);
modified = true;
}
}
monitor.worked(100);
if (modified) {
monitor.subTask(Messages.savingContextConfigTask);
factory.save(serverXml.toOSString());
}
monitor.worked(100);
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Context docBase settings updated in server.xml.");
} catch (Exception e) {
Trace.trace(Trace.SEVERE, "Could not modify context configurations to serve directly for Tomcat configuration " + confDir.toOSString() + ": " + e.getMessage());
return new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishConfiguration, new String[] { e.getLocalizedMessage() }), e);
} finally {
monitor.done();
}
return Status.OK_STATUS;
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.
the class Tomcat40Configuration method getWebModules.
/**
* Return a list of the web modules in this server.
* @return java.util.List
*/
public List getWebModules() {
List<WebModule> list = new ArrayList<WebModule>();
try {
Context[] contexts = serverInstance.getContexts();
if (contexts != null) {
for (int i = 0; i < contexts.length; i++) {
Context context = contexts[i];
String reload = context.getReloadable();
if (reload == null)
reload = "false";
WebModule module = new WebModule(context.getPath(), context.getDocBase(), context.getSource(), reload.equalsIgnoreCase("true") ? true : false);
list.add(module);
}
}
} catch (Exception e) {
Trace.trace(Trace.SEVERE, "Error getting modules", e);
}
return list;
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.
the class Tomcat40Configuration method load.
/**
* @see TomcatConfiguration#load(IPath, IProgressMonitor)
*/
public void load(IPath path, IProgressMonitor monitor) throws CoreException {
try {
monitor = ProgressUtil.getMonitorFor(monitor);
monitor.beginTask(Messages.loadingTask, 5);
// check for catalina.policy to verify that this is a v4.0 config
InputStream in = new FileInputStream(path.append("catalina.policy").toFile());
in.read();
in.close();
monitor.worked(1);
serverFactory = new Factory();
serverFactory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
server = (Server) serverFactory.loadDocument(new FileInputStream(path.append("server.xml").toFile()));
serverInstance = new ServerInstance(server, null, null);
monitor.worked(1);
webAppDocument = new WebAppDocument(path.append("web.xml"));
monitor.worked(1);
tomcatUsersDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(new FileInputStream(path.append("tomcat-users.xml").toFile())));
monitor.worked(1);
// load policy file
policyFile = TomcatVersionHelper.getFileContents(new FileInputStream(path.append("catalina.policy").toFile()));
monitor.worked(1);
if (monitor.isCanceled())
return;
monitor.done();
} catch (Exception e) {
Trace.trace(Trace.WARNING, "Could not load Tomcat v4.0 configuration from " + path.toOSString() + ": " + e.getMessage());
throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, path.toOSString()), e));
}
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.
the class Tomcat40Configuration method modifyServerPort.
/**
* Modify the port with the given id.
*
* @param id java.lang.String
* @param port int
*/
public void modifyServerPort(String id, int port) {
try {
if ("server".equals(id)) {
server.setPort(port + "");
isServerDirty = true;
firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
return;
}
int i = id.indexOf("/");
// If a connector in the instance Service
if (i < 0) {
int connNum = Integer.parseInt(id);
Connector connector = serverInstance.getConnector(connNum);
if (connector != null) {
connector.setPort(port + "");
isServerDirty = true;
firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
}
} else // Else a connector in another Service
{
int servNum = Integer.parseInt(id.substring(0, i));
int connNum = Integer.parseInt(id.substring(i + 1));
Service service = server.getService(servNum);
Connector connector = service.getConnector(connNum);
connector.setPort(port + "");
isServerDirty = true;
firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
}
} catch (Exception e) {
Trace.trace(Trace.SEVERE, "Error modifying server port " + id, e);
}
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.
the class Tomcat41Configuration method getServerPorts.
/**
* Returns a list of ServerPorts that this configuration uses.
*
* @return java.util.List
*/
public List getServerPorts() {
List<ServerPort> ports = new ArrayList<ServerPort>();
// first add server port
try {
int port = Integer.parseInt(server.getPort());
ports.add(new ServerPort("server", Messages.portServer, port, "TCPIP"));
} catch (Exception e) {
// ignore
}
// add connectors
try {
String instanceServiceName = serverInstance.getService().getName();
int size = server.getServiceCount();
for (int i = 0; i < size; i++) {
Service service = server.getService(i);
int size2 = service.getConnectorCount();
for (int j = 0; j < size2; j++) {
Connector connector = service.getConnector(j);
String className = connector.getClassName();
String name = className;
String protocol = "TCPIP";
boolean advanced = true;
String[] contentTypes = null;
int port = -1;
try {
port = Integer.parseInt(connector.getPort());
} catch (Exception e) {
// ignore
}
if (HTTP_CONNECTOR.equals(className)) {
name = "HTTP Connector";
protocol = "HTTP";
contentTypes = new String[] { "web", "webservices" };
// check for AJP/1.3 Coyote connector
String protocolHandler = connector.getProtocolHandlerClassName();
if (JK_PROTOCOL_HANDLER.equals(protocolHandler)) {
name = "AJP/1.3 Connector";
protocol = "AJP/1.3";
} else {
// assume HTTP, check for HTTP SSL connector
try {
Element element = connector.getSubElement("Factory");
if (SSL_SOCKET_FACTORY.equals(element.getAttribute("className"))) {
name = "SSL Connector";
protocol = "SSL";
}
} catch (Exception e) {
// ignore
}
}
if ("HTTP".equals(protocol))
advanced = false;
} else if (APACHE_CONNECTOR.equals(className))
name = "Apache Connector";
String portId;
if (instanceServiceName != null && instanceServiceName.equals(service.getName()))
portId = Integer.toString(j);
else
portId = i + "/" + j;
if (className != null && className.length() > 0)
ports.add(new ServerPort(portId, name, port, protocol, contentTypes, advanced));
}
}
} catch (Exception e) {
Trace.trace(Trace.SEVERE, "Error getting server ports", e);
}
return ports;
}
Aggregations