use of org.eclipse.jst.server.tomcat.core.internal.xml.Factory 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.Factory in project liferay-ide by liferay.
the class LiferayTomcatUtil method loadContextFile.
public static Context loadContextFile(File contextFile) {
Context context = null;
if (contextFile != null && contextFile.exists()) {
try (InputStream fis = Files.newInputStream(contextFile.toPath())) {
Factory factory = new Factory();
// $NON-NLS-1$
factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
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();
// $NON-NLS-1$
path = fileName.substring(0, fileName.length() - ".xml".length());
if (// $NON-NLS-1$
"ROOT".equals(path))
path = StringPool.EMPTY;
context.setPath(StringPool.FORWARD_SLASH + path);
}
}
} catch (Exception e) {
// may be a spurious xml file in the host dir?
}
}
return context;
}
use of org.eclipse.jst.server.tomcat.core.internal.xml.Factory 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.Factory 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.Factory 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