use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context in project webtools.servertools by eclipse.
the class Tomcat85PublishModuleVisitor method endVisitWebComponent.
/**
* {@inheritDoc}
*/
@Override
public void endVisitWebComponent(IVirtualComponent component) throws CoreException {
// track context changes, don't rewrite if not needed
boolean dirty = false;
IModule module = ServerUtil.getModule(component.getProject());
// we need this for the user-specified context path
Context context = findContext(module);
if (context == null) {
String name = module != null ? module.getName() : component.getName();
Trace.trace(Trace.SEVERE, "Could not find context for module " + name);
throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishContextNotFound, name), null));
}
dirty = includeProjectContextXml(component, context);
dirty = updateDocBaseAndPath(component, context);
// Add WEB-INF/classes elements as PreResources
for (Iterator iterator = virtualClassClasspathElements.iterator(); iterator.hasNext(); ) {
Object virtualClassClasspathElement = iterator.next();
PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
preResources.setBase(virtualClassClasspathElement.toString());
preResources.setWebAppMount("/WEB-INF/classes");
preResources.setInternalPath("/");
preResources.setClassLoaderOnly("false");
}
virtualClassClasspathElements.clear();
// Add Jars as JarResources if a jar, or as PostResources if a utility project
for (Iterator iterator = virtualJarClasspathElements.iterator(); iterator.hasNext(); ) {
Object virtualJarClassClasspathElement = iterator.next();
String jarPath = virtualJarClassClasspathElement.toString();
if (jarPath.endsWith(".jar")) {
JarResources jarResources = (JarResources) context.getResources().createElement("JarResources");
jarResources.setClassName("org.apache.catalina.webresources.JarResourceSet");
jarResources.setBase(jarPath);
jarResources.setWebAppMount("/WEB-INF/classes");
jarResources.setInternalPath("/");
jarResources.setClassLoaderOnly("true");
} else {
PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
postResources.setBase(jarPath);
postResources.setWebAppMount("/WEB-INF/classes");
postResources.setInternalPath("/");
postResources.setClassLoaderOnly("false");
// Map META-INF tld files to WEB-INF
File metaInfDir = new File(jarPath + "/META-INF");
if (metaInfDir.isDirectory() && metaInfDir.exists()) {
// Map META-INF directory directly to /META-INF
postResources = (PostResources) context.getResources().createElement("PostResources");
postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
postResources.setBase(metaInfDir.getPath());
postResources.setWebAppMount("/META-INF");
postResources.setInternalPath("/");
postResources.setClassLoaderOnly("false");
File[] tldFiles = metaInfDir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isFile() && file.getName().endsWith(".tld")) {
return true;
}
return false;
}
});
for (int i = 0; i < tldFiles.length; i++) {
postResources = (PostResources) context.getResources().createElement("PostResources");
postResources.setClassName("org.apache.catalina.webresources.FileResourceSet");
postResources.setBase(tldFiles[0].getPath());
postResources.setWebAppMount("/WEB-INF/" + tldFiles[0].getName());
postResources.setInternalPath("/");
postResources.setClassLoaderOnly("false");
}
}
}
}
virtualJarClasspathElements.clear();
Set<String> rtPathsProcessed = new HashSet<String>();
Set<String> locationsIncluded = new HashSet<String>();
String docBase = context.getDocBase();
locationsIncluded.add(docBase);
Map<String, String> retryLocations = new HashMap<String, String>();
IVirtualResource[] virtualResources = component.getRootFolder().getResources("");
// Loop over the module's resources
for (int i = 0; i < virtualResources.length; i++) {
String rtPath = virtualResources[i].getRuntimePath().toString();
// If this runtime path has not yet been processed
if (!rtPathsProcessed.contains(rtPath)) {
// If not a Java related resource
if (!"/WEB-INF/classes".equals(rtPath)) {
// Get all resources for this runtime path
IResource[] underlyingResources = virtualResources[i].getUnderlyingResources();
// to a mapping in the .components file
if ("/".equals(rtPath)) {
for (int j = 0; j < underlyingResources.length; j++) {
IPath resLoc = underlyingResources[j].getLocation();
String location = resLoc.toOSString();
if (!location.equals(docBase)) {
PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
preResources.setBase(location);
preResources.setWebAppMount("/");
preResources.setInternalPath("/");
preResources.setClassLoaderOnly("false");
// Add to the set of locations included
locationsIncluded.add(location);
}
}
} else // Else this runtime path is something other than "/"
{
int idx = rtPath.lastIndexOf('/');
// If a "normal" runtime path
if (idx >= 0) {
// Get the name of the last segment in the runtime path
String lastSegment = rtPath.substring(idx + 1);
// Check the underlying resources to determine which correspond to mappings
for (int j = 0; j < underlyingResources.length; j++) {
IPath resLoc = underlyingResources[j].getLocation();
String location = resLoc.toOSString();
// from the .contents file.
if (!lastSegment.equals(resLoc.lastSegment())) {
PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
preResources.setBase(location);
preResources.setWebAppMount(rtPath);
preResources.setInternalPath("/");
preResources.setClassLoaderOnly("false");
// Add to the set of locations included
locationsIncluded.add(location);
} else // Else last segment of runtime path did match the last segment
// of the location. We likely have a subfolder of a mapping
// that matches a portion of the runtime path.
{
// Since we can't be sure, save so it can be check again later
retryLocations.put(location, rtPath);
}
}
}
}
}
// Add the runtime path to those already processed
rtPathsProcessed.add(rtPath);
}
}
// If there are locations to retry, add any not yet included in extra paths setting
if (!retryLocations.isEmpty()) {
// Remove retry locations already included in the extra paths
for (Iterator iterator = retryLocations.keySet().iterator(); iterator.hasNext(); ) {
String location = (String) iterator.next();
for (Iterator iterator2 = locationsIncluded.iterator(); iterator2.hasNext(); ) {
String includedLocation = (String) iterator2.next();
if (location.equals(includedLocation) || location.startsWith(includedLocation + File.separator)) {
iterator.remove();
break;
}
}
}
// If any entries are left, include them in the extra paths
if (!retryLocations.isEmpty()) {
for (Iterator iterator = retryLocations.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry entry = (Map.Entry) iterator.next();
String location = (String) entry.getKey();
String rtPath = (String) entry.getValue();
PreResources preResources = (PreResources) context.getResources().createElement("PreResources");
preResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
preResources.setBase(location);
preResources.setWebAppMount(rtPath);
preResources.setInternalPath("/");
preResources.setClassLoaderOnly("false");
}
}
}
if (!virtualDependentResources.isEmpty()) {
for (Map.Entry<String, List<String>> entry : virtualDependentResources.entrySet()) {
String rtPath = entry.getKey();
List<String> locations = entry.getValue();
for (String location : locations) {
PostResources postResources = (PostResources) context.getResources().createElement("PostResources");
postResources.setClassName("org.apache.catalina.webresources.DirResourceSet");
postResources.setBase(location);
postResources.setWebAppMount(rtPath.length() > 0 ? rtPath : "/");
postResources.setInternalPath("/");
postResources.setClassLoaderOnly("false");
}
}
}
virtualDependentResources.clear();
if (dirty) {
// TODO If writing to separate context XML files, save "dirty" status for later use
}
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context in project webtools.servertools by eclipse.
the class TomcatPublishModuleVisitor method getProjectContextXml.
/**
* Load a META-INF/context.xml file from project, if available
*
* @param component web component containing the context.xml
* @return context element containing the context.xml
* @throws CoreException
*/
protected Context getProjectContextXml(IVirtualComponent component) throws CoreException {
// load or create module's context.xml document
IVirtualFile contextFile = (IVirtualFile) component.getRootFolder().findMember("META-INF/context.xml");
Context contextElement = null;
if (contextFile != null && contextFile.exists()) {
Factory factory = new Factory();
factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
InputStream fis = null;
try {
fis = contextFile.getUnderlyingFile().getContents();
contextElement = (Context) factory.loadDocument(fis);
} catch (Exception e) {
Trace.trace(Trace.SEVERE, "Exception reading " + contextFile, e);
} finally {
try {
fis.close();
} catch (IOException e) {
// ignore
}
}
}
return contextElement;
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context 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.server40.Context 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.server40.Context in project webtools.servertools by eclipse.
the class TomcatVersionHelper method loadContextFile.
private static Context loadContextFile(File contextFile) {
FileInputStream fis = null;
Context context = null;
if (contextFile != null && contextFile.exists()) {
try {
Factory factory = new Factory();
factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
fis = new FileInputStream(contextFile);
context = (Context) factory.loadDocument(fis);
if (context != null) {
String path = context.getPath();
// If path attribute is not set, derive from file name
if (path == null) {
String fileName = contextFile.getName();
path = fileName.substring(0, fileName.length() - ".xml".length());
if ("ROOT".equals(path))
path = "";
// Assuming this use for "#" since Tomcat has "reserved" this use of "#" since 5.5.
path = path.replace('#', '/');
context.setPath("/" + path);
}
}
} catch (Exception e) {
// may be a spurious xml file in the host dir?
Trace.trace(Trace.FINER, "Unable to read context " + contextFile.getAbsolutePath());
} finally {
try {
fis.close();
} catch (IOException e) {
// ignore
}
}
}
return context;
}
Aggregations