use of org.kie.workbench.common.dmn.api.definition.v1_1.Context in project webtools.servertools by eclipse.
the class TomcatPublishModuleVisitor method includeProjectContextXml.
boolean includeProjectContextXml(IVirtualComponent component, Context context) throws CoreException {
boolean dirty = false;
// handle project context.xml
Context projectContext = getProjectContextXml(component);
if (projectContext != null) {
// copy configuration to server context
projectContext.copyChildrenTo(context);
Map attrs = projectContext.getAttributes();
Iterator iter = attrs.keySet().iterator();
while (iter.hasNext()) {
String name = (String) iter.next();
if (!name.equalsIgnoreCase("path") && !name.equalsIgnoreCase("docBase") && !name.equalsIgnoreCase("source")) {
String value = (String) attrs.get(name);
String actualValue = context.getAttributeValue(name);
if (!value.equals(actualValue)) {
context.setAttributeValue(name, value);
dirty = true;
}
}
}
}
return dirty;
}
use of org.kie.workbench.common.dmn.api.definition.v1_1.Context in project webtools.servertools by eclipse.
the class TomcatPublishModuleVisitor method endVisitWebComponent.
/**
* {@inheritDoc}
*/
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);
context.getResources().setClassName("org.eclipse.jst.server.tomcat.loader.WtpDirContext");
Loader loader = context.getLoader();
loader.setClassName("org.eclipse.jst.server.tomcat.loader.WtpWebappLoader");
// required for tomcat 5.5.20 due to the change in
// http://issues.apache.org/bugzilla/show_bug.cgi?id=39704
loader.setUseSystemClassLoaderAsParent(Boolean.FALSE.toString());
// Build the virtual classPath setting
// Filled with classes entries first, then jar entries added
StringBuffer vcBuffer = new StringBuffer();
// Filled with jar enteries only
StringBuffer vcJarBuffer = new StringBuffer();
// Build list of additional resource paths and check for additional jars
StringBuffer rpBuffer = new StringBuffer();
boolean isTomcat7 = tomcatVersion.startsWith("7.");
// Add WEB-INF/classes elements to both settings
for (Iterator iterator = virtualClassClasspathElements.iterator(); iterator.hasNext(); ) {
Object element = iterator.next();
if (vcBuffer.length() > 0) {
vcBuffer.append(";");
}
vcBuffer.append(element);
if (isTomcat7) {
if (rpBuffer.length() > 0) {
rpBuffer.append(";");
}
// Add to resource paths too, so resource artifacts can be found
rpBuffer.append("/WEB-INF/classes").append("|").append(element);
}
}
for (Iterator iterator = virtualJarClasspathElements.iterator(); iterator.hasNext(); ) {
vcJarBuffer.append(iterator.next());
if (iterator.hasNext()) {
vcJarBuffer.append(";");
}
}
virtualClassClasspathElements.clear();
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)) {
if (rpBuffer.length() != 0) {
rpBuffer.append(";");
}
// Add this location to extra paths setting
rpBuffer.append(location);
// Add to the set of locations included
locationsIncluded.add(location);
// If a "WEB-INF/classes" exists and is a directory, add to virtual classpath
File webInfClasses = resLoc.append("WEB-INF/classes").toFile();
if (webInfClasses.exists() && webInfClasses.isDirectory()) {
if (vcBuffer.length() != 0) {
vcBuffer.append(";");
}
vcBuffer.append(webInfClasses.getPath());
}
// Check if this extra content location contains jars
File webInfLib = resLoc.append("WEB-INF/lib").toFile();
// its jars to the virtual classpath
if (webInfLib.exists() && webInfLib.isDirectory()) {
String[] jars = webInfLib.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return f.isFile() && name.endsWith(".jar");
}
});
for (int k = 0; k < jars.length; k++) {
if (vcJarBuffer.length() != 0) {
vcJarBuffer.append(";");
}
vcJarBuffer.append(webInfLib.getPath() + File.separator + jars[k]);
}
}
}
}
} 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())) {
if (rpBuffer.length() != 0) {
rpBuffer.append(";");
}
// Add this location to extra paths setting
rpBuffer.append(rtPath).append("|").append(location);
// Add to the set of locations included
locationsIncluded.add(location);
// Check if this extra content location contains jars
File webInfLib = null;
File webInfClasses = null;
if ("/WEB-INF".equals(rtPath)) {
webInfLib = resLoc.append("lib").toFile();
webInfClasses = resLoc.append("classes").toFile();
} else if ("/WEB-INF/lib".equals(rtPath)) {
webInfLib = resLoc.toFile();
} else if ("/WEB-INF/classes".equals(rtPath)) {
webInfClasses = resLoc.toFile();
}
// If a "WEB-INF/classes" exists and is a directory, add to virtual classpath
if (webInfClasses != null && webInfClasses.exists() && webInfClasses.isDirectory()) {
if (vcBuffer.length() != 0) {
vcBuffer.append(";");
}
vcBuffer.append(webInfClasses.getPath());
}
// its jars to the virtual classpath
if (webInfLib != null && webInfLib.exists() && webInfLib.isDirectory()) {
String[] jars = webInfLib.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return f.isFile() && name.endsWith(".jar");
}
});
for (int k = 0; k < jars.length; k++) {
if (vcJarBuffer.length() != 0) {
vcJarBuffer.append(";");
}
vcJarBuffer.append(webInfLib.getPath() + File.separator + jars[k]);
}
}
} 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();
if (rpBuffer.length() != 0) {
rpBuffer.append(";");
}
rpBuffer.append(rtPath).append("|").append(location);
// Check if this extra content location contains jars
File webInfLib = null;
File webInfClasses = null;
if ("/WEB-INF".equals(rtPath)) {
webInfLib = new File(location, "lib");
webInfClasses = new File(location, "classes");
} else if ("/WEB-INF/lib".equals(rtPath)) {
webInfLib = new File(location);
} else if ("/WEB-INF/classes".equals(rtPath)) {
webInfClasses = new File(location);
}
// If a "WEB-INF/classes" exists and is a directory, add to virtual classpath
if (webInfClasses != null && webInfClasses.exists() && webInfClasses.isDirectory()) {
if (vcBuffer.length() != 0) {
vcBuffer.append(";");
}
vcBuffer.append(webInfClasses.getPath());
}
// its jars to the virtual classpath
if (webInfLib != null && webInfLib.exists() && webInfLib.isDirectory()) {
String[] jars = webInfLib.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return f.isFile() && name.endsWith(".jar");
}
});
for (int k = 0; k < jars.length; k++) {
if (vcJarBuffer.length() != 0) {
vcJarBuffer.append(";");
}
vcJarBuffer.append(webInfLib.getPath() + File.separator + jars[k]);
}
}
}
}
}
if (!virtualDependentResources.isEmpty()) {
for (Iterator iterator = virtualDependentResources.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry entry = (Map.Entry) iterator.next();
String rtPath = (String) entry.getKey();
List locations = (List) entry.getValue();
for (Iterator iterator2 = locations.iterator(); iterator2.hasNext(); ) {
String location = (String) iterator2.next();
if (rpBuffer.length() != 0) {
rpBuffer.append(";");
}
if (rtPath.length() > 0) {
rpBuffer.append(entry.getKey()).append("|").append(location);
} else {
rpBuffer.append(location);
}
}
}
}
virtualDependentResources.clear();
// Combine the classes and jar virtual classpaths
if (vcJarBuffer.length() > 0) {
if (vcBuffer.length() > 0) {
vcBuffer.append(';');
}
vcBuffer.append(vcJarBuffer);
}
String vcp = vcBuffer.toString();
String oldVcp = loader.getVirtualClasspath();
if (!vcp.equals(oldVcp)) {
// save only if needed
dirty = true;
loader.setVirtualClasspath(vcp);
context.getResources().setVirtualClasspath(vcp);
}
String resPaths = rpBuffer.toString();
String oldResPaths = context.getResources().getExtraResourcePaths();
if (!resPaths.equals(oldResPaths)) {
dirty = true;
context.getResources().setExtraResourcePaths(resPaths);
}
if (enableMetaInfResources) {
context.findElement("JarScanner").setAttributeValue("scanAllDirectories", "true");
}
if (dirty) {
// TODO If writing to separate context XML files, save "dirty" status for later use
}
}
use of org.kie.workbench.common.dmn.api.definition.v1_1.Context in project webtools.servertools by eclipse.
the class TomcatVersionHelper method updateContextsToServeDirectly.
/**
* Update Contexts to serve web projects directly.
*
* @param baseDir directory where the Catalina instance is found
* @param loader name of the catalina.properties loader to use for global
* classpath entries
* @param monitor a progress monitor
* @return result of update operation
*/
public static IStatus updateContextsToServeDirectly(IPath baseDir, String tomcatVersion, String loader, boolean enableMetaInfResources, 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;
boolean isTomcat80 = tomcatVersion.startsWith("8.0");
boolean isTomcat85 = tomcatVersion.startsWith("8.5");
boolean isTomcat9 = tomcatVersion.startsWith("9.");
// care about top-level modules only
TomcatPublishModuleVisitor visitor;
if (isTomcat80) {
visitor = new Tomcat80PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
} else if (isTomcat85) {
visitor = new Tomcat85PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
} else if (isTomcat9) {
visitor = new Tomcat90PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
} else {
visitor = new TomcatPublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
}
Context[] contexts = publishedInstance.getContexts();
for (int i = 0; i < contexts.length; i++) {
String moduleId = contexts[i].getSource();
if (moduleId != null && moduleId.length() > 0) {
IModule module = ServerUtil.getModule(moduleId);
ModuleTraverser.traverse(module, visitor, monitor);
modified = true;
}
}
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.kie.workbench.common.dmn.api.definition.v1_1.Context in project webtools.servertools by eclipse.
the class TomcatVersionHelper method getRemovedKeptCatalinaContexts.
/**
* Gets the paths for Contexts that are being removed in the
* next server publish. Reads the old server.xml to determine
* what Contexts were previously servered and returns those
* that are not included in the specified list of modules.
*
* @param oldServerInstance for server.xml from previous server publish
* @param modules list of currently added modules
* @param removedContextsMap Map to receive removed contexts mapped by path
* @param keptContextsMap Map to receive kept contexts mapped by path
*/
public static void getRemovedKeptCatalinaContexts(ServerInstance oldServerInstance, List modules, Map<String, Context> removedContextsMap, Map<String, Context> keptContextsMap) {
// Collect paths of old web modules managed by WTP
Context[] contexts = oldServerInstance.getContexts();
if (contexts != null) {
for (int i = 0; i < contexts.length; i++) {
String source = contexts[i].getSource();
if (source != null && source.length() > 0) {
removedContextsMap.put(contexts[i].getPath(), contexts[i]);
}
}
}
// Remove paths for web modules that are staying around
int size = modules.size();
for (int i = 0; i < size; i++) {
WebModule module = (WebModule) modules.get(i);
String modulePath = module.getPath();
// normalize "/" to ""
if (modulePath.equals("/"))
modulePath = "";
Context context = removedContextsMap.remove(modulePath);
if (context != null)
keptContextsMap.put(context.getPath(), context);
}
}
use of org.kie.workbench.common.dmn.api.definition.v1_1.Context in project webtools.servertools by eclipse.
the class TomcatVersionHelper method localizeConfiguration.
/**
* If modules are not being deployed to the "webapps" directory, the
* context for the published modules is updated to contain the
* corrected docBase.
*
* @param baseDir runtime base directory for the server
* @param deployDir deployment directory for the server
* @param server server being localized
* @param monitor a progress monitor
* @return result of operation
*/
public static IStatus localizeConfiguration(IPath baseDir, IPath deployDir, TomcatServer server, IProgressMonitor monitor) {
try {
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Localizing configuration at " + baseDir);
monitor = ProgressUtil.getMonitorFor(monitor);
monitor.beginTask(Messages.publishConfigurationTask, 300);
IPath serverXml = baseDir.append("conf/server.xml");
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);
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
boolean modified = false;
// Only add root module if running in a test env (i.e. not on the installation)
boolean addRootWebapp = server.isTestEnvironment();
// If not deploying to "webapps", context docBase attributes need updating
// TODO Improve to compare with appBase value instead of hardcoded "webapps"
boolean deployingToAppBase = "webapps".equals(server.getDeployDirectory());
Map<String, String> pathMap = new HashMap<String, String>();
MultiStatus ms = new MultiStatus(TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishServer, server.getServer().getName()), null);
Context[] contexts = publishedInstance.getContexts();
if (contexts != null) {
for (int i = 0; i < contexts.length; i++) {
Context context = contexts[i];
// Normalize path and check for duplicates
String path = context.getPath();
if (path != null) {
// Save a copy of original in case it's "/"
String origPath = path;
// Normalize "/" to ""
if ("/".equals(path)) {
if (Trace.isTraceEnabled())
Trace.trace(Trace.FINER, "Context path is being changed from \"/\" to \"\".");
path = "";
context.setPath(path);
modified = true;
}
// Context paths that are the same or differ only in case are not allowed
String lcPath = path.toLowerCase();
if (!pathMap.containsKey(lcPath)) {
pathMap.put(lcPath, origPath);
} else {
String otherPath = pathMap.get(lcPath);
IStatus s = new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, origPath.equals(otherPath) ? NLS.bind(Messages.errorPublishPathDup, origPath) : NLS.bind(Messages.errorPublishPathConflict, origPath, otherPath));
ms.add(s);
}
} else {
IStatus s = new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, Messages.errorPublishPathMissing);
ms.add(s);
}
// TODO Need to add a root context if deploying to webapps but with auto-deploy off
if (addRootWebapp && "".equals(context.getPath())) {
// A default webapp is being deployed, don't add one
addRootWebapp = false;
}
// If not deploying to appBase, convert to absolute path under deploy dir
if (!deployingToAppBase) {
String source = context.getSource();
if (source != null && source.length() > 0) {
context.setDocBase(deployDir.append(context.getDocBase()).toOSString());
modified = true;
}
}
}
}
// If errors are present, return status
if (!ms.isOK())
return ms;
if (addRootWebapp) {
// Add a context for the default webapp
Context rootContext = publishedInstance.createContext(0);
rootContext.setPath("");
rootContext.setDocBase(deployDir.append("ROOT").toOSString());
rootContext.setReloadable("false");
modified = true;
}
monitor.worked(100);
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
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.WARNING, "Could not localize server configuration published to " + baseDir.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;
}
Aggregations