use of org.eclipse.jst.server.tomcat.core.internal.xml.server32.ServerInstance in project liferay-ide by liferay.
the class LiferayTomcatServerBehavior method moveContextToAutoDeployDir.
public IStatus moveContextToAutoDeployDir(IModule module, IPath deployDir, IPath baseDir, IPath autoDeployDir, boolean noPath, boolean serverStopped) {
// $NON-NLS-1$
IPath confDir = baseDir.append("conf");
// $NON-NLS-1$
IPath serverXml = confDir.append("server.xml");
try (InputStream newInputStream = Files.newInputStream(serverXml.toFile().toPath())) {
Factory factory = new Factory();
// $NON-NLS-1$
factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
Server publishedServer = (Server) factory.loadDocument(newInputStream);
ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
IPath contextPath = null;
if (autoDeployDir.isAbsolute()) {
contextPath = autoDeployDir;
} else {
contextPath = baseDir.append(autoDeployDir);
}
File contextDir = contextPath.toFile();
if (!contextDir.exists()) {
contextDir.mkdirs();
}
Context context = publishedInstance.createContext(-1);
// $NON-NLS-1$
context.setReloadable("true");
final String moduleName = module.getName();
final String requiredSuffix = ProjectUtil.getRequiredSuffix(module.getProject());
String contextName = moduleName;
if (!moduleName.endsWith(requiredSuffix)) {
contextName = moduleName + requiredSuffix;
}
// $NON-NLS-1$
context.setSource("org.eclipse.jst.jee.server:" + contextName);
if (// $NON-NLS-1$
Boolean.valueOf(context.getAttributeValue("antiResourceLocking")).booleanValue()) {
// $NON-NLS-1$ //$NON-NLS-2$
context.setAttributeValue("antiResourceLocking", "false");
}
// $NON-NLS-1$
File contextFile = new File(contextDir, contextName + ".xml");
if (!LiferayTomcatUtil.isExtProjectContext(context)) {
// If requested, remove path attribute
if (noPath) {
// $NON-NLS-1$
context.removeAttribute("path");
}
// need to fix the doc base to contain entire path to help autoDeployer for Liferay
context.setDocBase(deployDir.toOSString());
// context.setAttributeValue("antiJARLocking", "true");
// check to see if we need to move from conf folder
// IPath existingContextPath = confDir.append("Catalina/localhost").append(contextFile.getName());
// if (existingContextPath.toFile().exists()) {
// existingContextPath.toFile().delete();
// }
DocumentBuilder builder = XMLUtil.getDocumentBuilder();
Document contextDoc = builder.newDocument();
contextDoc.appendChild(contextDoc.importNode(context.getElementNode(), true));
XMLUtil.save(contextFile.getAbsolutePath(), contextDoc);
}
} catch (Exception e) {
// 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.server32.ServerInstance in project liferay-ide by liferay.
the class LiferayPublishOperation method publishDir.
private void publishDir(IModule module2, List status, IProgressMonitor monitor) throws CoreException {
final IPath path = server.getModuleDeployDirectory(module2);
// Remove if requested or if previously published and are now serving without publishing
if (kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish()) {
File f = path.toFile();
if (f.exists()) {
try {
IPath baseDir = server.getRuntimeBaseDirectory();
// $NON-NLS-1$ //$NON-NLS-2$
IPath serverXml = baseDir.append("conf").append("server.xml");
ServerInstance oldInstance = TomcatVersionHelper.getCatalinaServerInstance(serverXml, null, null);
// $NON-NLS-1$
IPath contextDir = oldInstance.getContextXmlDirectory(baseDir.append("conf"));
// $NON-NLS-1$
String contextFileName = path.lastSegment() + ".xml";
File contextFile = contextDir.append(contextFileName).toFile();
if (contextFile.exists()) {
contextFile.delete();
}
File autoDeployDir = baseDir.append(server.getLiferayTomcatServer().getAutoDeployDirectory()).toFile();
File autoDeployFile = new File(autoDeployDir, contextFileName);
if (autoDeployFile.exists()) {
autoDeployFile.delete();
}
} catch (Exception e) {
// $NON-NLS-1$
LiferayTomcatPlugin.logError("Could not delete context xml file.", e);
}
IStatus[] stat = PublishHelper.deleteDirectory(f, monitor);
addArrayToList(status, stat);
}
if (deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish())
return;
}
IPath baseDir = server.getTomcatServer().getRuntimeBaseDirectory();
IPath autoDeployDir = new Path(server.getLiferayTomcatServer().getAutoDeployDirectory());
boolean serverStopped = server.getServer().getServerState() == IServer.STATE_STOPPED;
if (kind == IServer.PUBLISH_CLEAN || kind == IServer.PUBLISH_FULL) {
IModuleResource[] mr = server.getResources(module);
IStatus[] stat = helper.publishFull(mr, path, monitor);
addArrayToList(status, stat);
clearWebXmlDescriptors(module2.getProject(), path, monitor);
server.moveContextToAutoDeployDir(module2, path, baseDir, autoDeployDir, true, serverStopped);
return;
}
IModuleResourceDelta[] delta = server.getPublishedResourceDelta(module);
// check if we have a anti*Locking directory temp files and copy the resources out there as well
File[] antiDirs = new File[0];
try {
File tempDir = // $NON-NLS-1$
server.getLiferayTomcatServer().getTomcatRuntime().getRuntime().getLocation().append("temp").toFile();
antiDirs = tempDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(path.lastSegment());
}
});
} catch (Exception e) {
}
int size = delta.length;
for (int i = 0; i < size; i++) {
IStatus[] stat = helper.publishDelta(delta[i], path, monitor);
for (File antiDir : antiDirs) {
if (antiDir.exists()) {
try {
helper.publishDelta(delta[i], new Path(antiDir.getCanonicalPath()), monitor);
} catch (Exception e) {
// best effort
}
}
}
addArrayToList(status, stat);
}
// check to see if we need to re-invoke the liferay plugin deployer
String[] paths = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
WEB_XML_PATH, // $NON-NLS-1$ //$NON-NLS-2$
"WEB-INF/portlet.xml", // $NON-NLS-1$ //$NON-NLS-2$
"WEB-INF/liferay-portlet.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
"WEB-INF/liferay-display.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
"WEB-INF/liferay-look-and-feel.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
"WEB-INF/liferay-hook.xml", // $NON-NLS-1$//$NON-NLS-2$
"WEB-INF/liferay-layout-templates.xml", // $NON-NLS-1$//$NON-NLS-2$
"WEB-INF/liferay-plugin-package.properties", "WEB-INF/liferay-plugin-package.xml", // $NON-NLS-1$ //$NON-NLS-2$
"WEB-INF/server-config.wsdd" };
for (IModuleResourceDelta del : delta) {
if (ComponentUtil.containsMember(del, paths) || isHookProjectDelta(del)) {
clearWebXmlDescriptors(module2.getProject(), path, monitor);
server.moveContextToAutoDeployDir(module2, path, baseDir, autoDeployDir, true, serverStopped);
break;
}
}
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server32.ServerInstance in project webtools.servertools by eclipse.
the class TomcatVersionHelper method cleanupCatalinaServer.
/**
* Cleanup server instance location in preparation for next server publish.
* This currently involves deleting work directories for currently
* existing Contexts which will not be included in the next publish.
* In addition, Context XML files which may have been created for these
* Contexts are also deleted. If requested, Context XML files for
* kept Contexts will be deleted since they will be kept in server.xml.
*
* @param baseDir path to server instance directory, i.e. catalina.base
* @param installDir path to server installation directory (not currently used)
* @param removeKeptContextFiles true if kept contexts should have a separate
* context XML file removed
* @param modules list of currently added modules
* @param monitor a progress monitor or null
* @return MultiStatus containing results of the cleanup operation
*/
public static IStatus cleanupCatalinaServer(IPath baseDir, IPath installDir, boolean removeKeptContextFiles, List modules, IProgressMonitor monitor) {
MultiStatus ms = new MultiStatus(TomcatPlugin.PLUGIN_ID, 0, Messages.cleanupServerTask, null);
try {
monitor = ProgressUtil.getMonitorFor(monitor);
monitor.beginTask(Messages.cleanupServerTask, 200);
monitor.subTask(Messages.detectingRemovedProjects);
IPath serverXml = baseDir.append("conf").append("server.xml");
ServerInstance oldInstance = TomcatVersionHelper.getCatalinaServerInstance(serverXml, null, null);
if (oldInstance != null) {
Map<String, Context> removedContextsMap = new HashMap<String, Context>();
Map<String, Context> keptContextsMap = new HashMap<String, Context>();
TomcatVersionHelper.getRemovedKeptCatalinaContexts(oldInstance, modules, removedContextsMap, keptContextsMap);
monitor.worked(100);
if (removedContextsMap.size() > 0) {
// Delete context files and work directories for managed web modules that have gone away
IProgressMonitor subMonitor = ProgressUtil.getSubMonitorFor(monitor, 100);
subMonitor.beginTask(Messages.deletingContextFilesTask, removedContextsMap.size() * 200);
Iterator iter = removedContextsMap.keySet().iterator();
while (iter.hasNext()) {
String oldPath = (String) iter.next();
Context ctx = removedContextsMap.get(oldPath);
// Delete the corresponding context file, if it exists
IPath ctxFilePath = oldInstance.getContextFilePath(baseDir, ctx);
if (ctxFilePath != null) {
File ctxFile = ctxFilePath.toFile();
if (ctxFile.exists()) {
subMonitor.subTask(NLS.bind(Messages.deletingContextFile, ctxFile.getName()));
if (ctxFile.delete()) {
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Leftover context file " + ctxFile.getName() + " deleted.");
ms.add(new Status(IStatus.OK, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.deletedContextFile, ctxFile.getName()), null));
} else {
Trace.trace(Trace.SEVERE, "Could not delete obsolete context file " + ctxFilePath.toOSString());
ms.add(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotDeleteContextFile, ctxFilePath.toOSString()), null));
}
}
}
subMonitor.worked(100);
// Delete work directory associated with the removed context if it is within confDir.
// If it is outside of confDir, assume user is going to manage it.
IPath ctxWorkPath = oldInstance.getContextWorkDirectory(baseDir, ctx);
if (baseDir.isPrefixOf(ctxWorkPath)) {
File ctxWorkDir = ctxWorkPath.toFile();
if (ctxWorkDir.exists() && ctxWorkDir.isDirectory()) {
IStatus[] results = PublishHelper.deleteDirectory(ctxWorkDir, ProgressUtil.getSubMonitorFor(monitor, 100));
if (results.length > 0) {
Trace.trace(Trace.SEVERE, "Could not delete work directory " + ctxWorkDir.getPath() + " for removed context " + oldPath);
for (int i = 0; i < results.length; i++) {
ms.add(results[i]);
}
}
} else
subMonitor.worked(100);
} else
subMonitor.worked(100);
}
subMonitor.done();
}
monitor.worked(100);
// If requested, remove any separate context XML files for contexts being kept
if (removeKeptContextFiles && keptContextsMap.size() > 0) {
// Delete context files and work directories for managed web modules that have gone away
IProgressMonitor subMonitor = ProgressUtil.getSubMonitorFor(monitor, 100);
// TODO Improve task name
subMonitor.beginTask(Messages.deletingContextFilesTask, keptContextsMap.size() * 100);
Iterator iter = keptContextsMap.keySet().iterator();
while (iter.hasNext()) {
String keptPath = (String) iter.next();
Context ctx = keptContextsMap.get(keptPath);
// Delete the corresponding context file, if it exists
IPath ctxFilePath = oldInstance.getContextFilePath(baseDir, ctx);
if (ctxFilePath != null) {
File ctxFile = ctxFilePath.toFile();
if (ctxFile.exists()) {
subMonitor.subTask(NLS.bind(Messages.deletingContextFile, ctxFile.getName()));
if (ctxFile.delete()) {
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Leftover context file " + ctxFile.getName() + " deleted.");
ms.add(new Status(IStatus.OK, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.deletedContextFile, ctxFile.getName()), null));
} else {
Trace.trace(Trace.SEVERE, "Could not delete obsolete context file " + ctxFilePath.toOSString());
ms.add(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotDeleteContextFile, ctxFilePath.toOSString()), null));
}
}
}
subMonitor.worked(100);
}
subMonitor.done();
}
} else // Else no server.xml. Assume first publish to new temp directory
{
monitor.worked(200);
}
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Server cleaned");
} catch (Exception e) {
Trace.trace(Trace.SEVERE, "Could not cleanup server at " + baseDir.toOSString() + ": " + e.getMessage());
ms.add(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCleanupServer, new String[] { e.getLocalizedMessage() }), e));
} finally {
monitor.done();
}
return ms;
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server32.ServerInstance in project webtools.servertools by eclipse.
the class TomcatVersionHelper method publishCatalinaContextConfig.
/**
* Add context configuration found in META-INF/context.xml files
* present in projects to published server.xml. Used by
* Tomcat 4.1, 5.0, and 5.5 which support use of META-INF/context.xml
* in some form.
*
* @param baseDir absolute path to catalina instance directory
* @param webappsDir absolute path to deployment directory
* @param monitor a progress monitor or null
* @return result of operation
*/
public static IStatus publishCatalinaContextConfig(IPath baseDir, IPath webappsDir, IProgressMonitor monitor) {
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Apply context configurations");
IPath confDir = baseDir.append("conf");
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(confDir.append("server.xml").toFile()));
ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
monitor.worked(100);
boolean modified = false;
MultiStatus ms = new MultiStatus(TomcatPlugin.PLUGIN_ID, 0, Messages.publishContextConfigTask, null);
Context[] contexts = publishedInstance.getContexts();
if (contexts != null) {
for (int i = 0; i < contexts.length; i++) {
Context context = contexts[i];
monitor.subTask(NLS.bind(Messages.checkingContextTask, new String[] { context.getPath() }));
if (addCatalinaContextConfig(webappsDir, context, ms)) {
modified = true;
}
}
}
monitor.worked(100);
if (modified) {
monitor.subTask(Messages.savingContextConfigTask);
factory.save(confDir.append("server.xml").toOSString());
}
// If problem(s) occurred adding context configurations, return error status
if (ms.getChildren().length > 0) {
return ms;
}
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Server.xml updated with context.xml configurations");
return Status.OK_STATUS;
} catch (Exception e) {
Trace.trace(Trace.WARNING, "Could not apply context configurations to published Tomcat configuration from " + 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();
}
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server32.ServerInstance in project webtools.servertools by eclipse.
the class TomcatVersionHelper method getCatalinaServerInstance.
/**
* Gets a ServerInstance for the specified server.xml, Service name,
* and Host name. Returns null if server.xml does not exist
* or an error occurs.
*
* @param serverXml path to previously published server.xml
* @param serviceName name of Service to be used by this instance or null
* @param hostName name of Host to be used by this instance or null
* @return ServerInstance for specified server.xml using specified
* Service and Host names. null if server.xml does not exist.
* @throws FileNotFoundException should not occur since existence is tested
* @throws IOException if there is an error reading server.xml
* @throws SAXException if there is a syntax error in server.xml
*/
public static ServerInstance getCatalinaServerInstance(IPath serverXml, String serviceName, String hostName) throws FileNotFoundException, IOException, SAXException {
ServerInstance serverInstance = null;
Factory factory = new Factory();
factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
File serverFile = serverXml.toFile();
if (serverFile.exists()) {
Server server = (Server) factory.loadDocument(new FileInputStream(serverFile));
serverInstance = new ServerInstance(server, serviceName, hostName);
IPath contextPath = serverInstance.getContextXmlDirectory(serverXml.removeLastSegments(1));
File contextDir = contextPath.toFile();
if (contextDir.exists()) {
Map<File, Context> projectContexts = new HashMap<File, Context>();
loadSeparateContextFiles(contextPath.toFile(), factory, projectContexts);
// add any separately saved contexts
Host host = serverInstance.getHost();
Collection contexts = projectContexts.values();
Iterator iter = contexts.iterator();
while (iter.hasNext()) {
Context context = (Context) iter.next();
host.importNode(context.getElementNode(), true);
}
// TODO Add handling for non-project contexts when there removal can be addressed
}
}
return serverInstance;
}
Aggregations