|
Added
Link Here
|
| 1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - Initial API and implementation |
| 10 |
**********************************************************************/ |
| 11 |
package org.eclipse.jst.server.tomcat.core.internal; |
| 12 |
|
| 13 |
import java.io.BufferedWriter; |
| 14 |
import java.io.ByteArrayInputStream; |
| 15 |
import java.io.File; |
| 16 |
import java.io.FileInputStream; |
| 17 |
import java.io.FileWriter; |
| 18 |
import java.io.InputStream; |
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.Iterator; |
| 22 |
import java.util.List; |
| 23 |
import java.util.Map; |
| 24 |
|
| 25 |
import org.eclipse.core.resources.IFile; |
| 26 |
import org.eclipse.core.resources.IFolder; |
| 27 |
import org.eclipse.core.runtime.CoreException; |
| 28 |
import org.eclipse.core.runtime.IPath; |
| 29 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 30 |
import org.eclipse.core.runtime.IStatus; |
| 31 |
import org.eclipse.core.runtime.Status; |
| 32 |
import org.eclipse.jst.server.tomcat.core.internal.xml.Factory; |
| 33 |
import org.eclipse.jst.server.tomcat.core.internal.xml.XMLUtil; |
| 34 |
import org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector; |
| 35 |
import org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context; |
| 36 |
import org.eclipse.jst.server.tomcat.core.internal.xml.server40.Listener; |
| 37 |
import org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server; |
| 38 |
import org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance; |
| 39 |
import org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service; |
| 40 |
import org.eclipse.osgi.util.NLS; |
| 41 |
import org.eclipse.wst.server.core.ServerPort; |
| 42 |
import org.w3c.dom.Document; |
| 43 |
import org.xml.sax.InputSource; |
| 44 |
/** |
| 45 |
* Tomcat v7.0 server configuration. |
| 46 |
*/ |
| 47 |
public class Tomcat70Configuration extends TomcatConfiguration { |
| 48 |
protected static final String DEFAULT_SERVICE = "Catalina"; |
| 49 |
protected static final String EOL = System.getProperty("line.separator"); |
| 50 |
protected Server server; |
| 51 |
protected ServerInstance serverInstance; |
| 52 |
protected Factory serverFactory; |
| 53 |
protected boolean isServerDirty; |
| 54 |
|
| 55 |
protected WebAppDocument webAppDocument; |
| 56 |
|
| 57 |
protected Document contextDocument; |
| 58 |
|
| 59 |
protected Document tomcatUsersDocument; |
| 60 |
|
| 61 |
protected String policyFile; |
| 62 |
|
| 63 |
protected String propertiesFile; |
| 64 |
|
| 65 |
protected static final Map protocolHandlerMap = new HashMap(); |
| 66 |
static { |
| 67 |
protocolHandlerMap.put("org.apache.coyote.http11.Http11Protocol", "HTTP/1.1"); |
| 68 |
protocolHandlerMap.put("org.apache.coyote.http11.Http11NioProtocol", "HTTP/1.1"); |
| 69 |
protocolHandlerMap.put("org.apache.coyote.http11.Http11AprProtocol", "HTTP/1.1"); |
| 70 |
protocolHandlerMap.put("org.apache.coyote.ajp.AjpAprProtocol", "AJP/1.3"); |
| 71 |
protocolHandlerMap.put("org.apache.jk.server.JkCoyoteHandler", "AJP/1.3"); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Tomcat60Configuration constructor. |
| 76 |
* |
| 77 |
* @param path a path |
| 78 |
*/ |
| 79 |
public Tomcat70Configuration(IFolder path) { |
| 80 |
super(path); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Return the port number. |
| 85 |
* @return int |
| 86 |
*/ |
| 87 |
public ServerPort getMainPort() { |
| 88 |
Iterator iterator = getServerPorts().iterator(); |
| 89 |
while (iterator.hasNext()) { |
| 90 |
ServerPort port = (ServerPort) iterator.next(); |
| 91 |
// Return only an HTTP port from the selected Service |
| 92 |
if (port.getProtocol().toLowerCase().equals("http") && port.getId().indexOf('/') < 0) |
| 93 |
return port; |
| 94 |
} |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the mime mappings. |
| 100 |
* @return java.util.List |
| 101 |
*/ |
| 102 |
public List getMimeMappings() { |
| 103 |
return webAppDocument.getMimeMappings(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns a list of ServerPorts that this configuration uses. |
| 108 |
* |
| 109 |
* @return java.util.List |
| 110 |
*/ |
| 111 |
public List getServerPorts() { |
| 112 |
List ports = new ArrayList(); |
| 113 |
|
| 114 |
// first add server port |
| 115 |
try { |
| 116 |
int port = Integer.parseInt(server.getPort()); |
| 117 |
ports.add(new ServerPort("server", Messages.portServer, port, "TCPIP")); |
| 118 |
} catch (Exception e) { |
| 119 |
// ignore |
| 120 |
} |
| 121 |
|
| 122 |
// add connectors |
| 123 |
try { |
| 124 |
String instanceServiceName = serverInstance.getService().getName(); |
| 125 |
int size = server.getServiceCount(); |
| 126 |
for (int i = 0; i < size; i++) { |
| 127 |
Service service = server.getService(i); |
| 128 |
int size2 = service.getConnectorCount(); |
| 129 |
for (int j = 0; j < size2; j++) { |
| 130 |
Connector connector = service.getConnector(j); |
| 131 |
String name = "HTTP/1.1"; |
| 132 |
String protocol2 = "HTTP"; |
| 133 |
boolean advanced = true; |
| 134 |
String[] contentTypes = null; |
| 135 |
int port = -1; |
| 136 |
try { |
| 137 |
port = Integer.parseInt(connector.getPort()); |
| 138 |
} catch (Exception e) { |
| 139 |
// ignore |
| 140 |
} |
| 141 |
String protocol = connector.getProtocol(); |
| 142 |
if (protocol != null && protocol.length() > 0) { |
| 143 |
if (protocol.startsWith("HTTP")) { |
| 144 |
name = protocol; |
| 145 |
} |
| 146 |
else if (protocol.startsWith("AJP")) { |
| 147 |
name = protocol; |
| 148 |
protocol2 = "AJP"; |
| 149 |
} |
| 150 |
else { |
| 151 |
// Get Tomcat equivalent name if protocol handler class specified |
| 152 |
name = (String)protocolHandlerMap.get(protocol); |
| 153 |
if (name != null) { |
| 154 |
// Prepare simple protocol string for ServerPort protocol |
| 155 |
int index = name.indexOf('/'); |
| 156 |
if (index > 0) |
| 157 |
protocol2 = name.substring(0, index); |
| 158 |
else |
| 159 |
protocol2 = name; |
| 160 |
} |
| 161 |
// Specified protocol is unknown, just use as is |
| 162 |
else { |
| 163 |
name = protocol; |
| 164 |
protocol2 = protocol; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
if (protocol2.toLowerCase().equals("http")) |
| 169 |
contentTypes = new String[] { "web", "webservices" }; |
| 170 |
String secure = connector.getSecure(); |
| 171 |
if (secure != null && secure.length() > 0) { |
| 172 |
name = "SSL"; |
| 173 |
protocol2 = "SSL"; |
| 174 |
} else |
| 175 |
advanced = false; |
| 176 |
String portId; |
| 177 |
if (instanceServiceName != null && instanceServiceName.equals(service.getName())) |
| 178 |
portId = Integer.toString(j); |
| 179 |
else |
| 180 |
portId = i +"/" + j; |
| 181 |
ports.add(new ServerPort(portId, name, port, protocol2, contentTypes, advanced)); |
| 182 |
} |
| 183 |
} |
| 184 |
} catch (Exception e) { |
| 185 |
Trace.trace(Trace.SEVERE, "Error getting server ports", e); |
| 186 |
} |
| 187 |
return ports; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Return a list of the web modules in this server. |
| 192 |
* @return java.util.List |
| 193 |
*/ |
| 194 |
public List getWebModules() { |
| 195 |
List list = new ArrayList(); |
| 196 |
|
| 197 |
try { |
| 198 |
Context [] contexts = serverInstance.getContexts(); |
| 199 |
if (contexts != null) { |
| 200 |
for (int i = 0; i < contexts.length; i++) { |
| 201 |
Context context = contexts[i]; |
| 202 |
String reload = context.getReloadable(); |
| 203 |
if (reload == null) |
| 204 |
reload = "false"; |
| 205 |
WebModule module = new WebModule(context.getPath(), |
| 206 |
context.getDocBase(), context.getSource(), |
| 207 |
reload.equalsIgnoreCase("true") ? true : false); |
| 208 |
list.add(module); |
| 209 |
} |
| 210 |
} |
| 211 |
} catch (Exception e) { |
| 212 |
Trace.trace(Trace.SEVERE, "Error getting project refs", e); |
| 213 |
} |
| 214 |
return list; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @see TomcatConfiguration#getServerWorkDirectory(IPath) |
| 219 |
*/ |
| 220 |
public IPath getServerWorkDirectory(IPath basePath) { |
| 221 |
return serverInstance.getHostWorkDirectory(basePath); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* @see TomcatConfiguration#getContextWorkDirectory(IPath, ITomcatWebModule) |
| 226 |
*/ |
| 227 |
public IPath getContextWorkDirectory(IPath basePath, ITomcatWebModule module) { |
| 228 |
Context context = serverInstance.getContext(module.getPath()); |
| 229 |
if (context != null) |
| 230 |
return serverInstance.getContextWorkDirectory(basePath, context); |
| 231 |
|
| 232 |
return null; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* @see TomcatConfiguration#load(IPath, IProgressMonitor) |
| 237 |
*/ |
| 238 |
public void load(IPath path, IProgressMonitor monitor) throws CoreException { |
| 239 |
try { |
| 240 |
monitor = ProgressUtil.getMonitorFor(monitor); |
| 241 |
monitor.beginTask(Messages.loadingTask, 7); |
| 242 |
|
| 243 |
// check for catalina.policy to verify that this is a v5.5 config |
| 244 |
InputStream in = new FileInputStream(path.append("catalina.policy").toFile()); |
| 245 |
in.read(); |
| 246 |
in.close(); |
| 247 |
monitor.worked(1); |
| 248 |
|
| 249 |
serverFactory = new Factory(); |
| 250 |
serverFactory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40"); |
| 251 |
server = (Server) serverFactory.loadDocument(new FileInputStream(path.append("server.xml").toFile())); |
| 252 |
serverInstance = new ServerInstance(server, null, null); |
| 253 |
monitor.worked(1); |
| 254 |
|
| 255 |
webAppDocument = new WebAppDocument(path.append("web.xml")); |
| 256 |
monitor.worked(1); |
| 257 |
|
| 258 |
File file = path.append("context.xml").toFile(); |
| 259 |
if (file.exists()) |
| 260 |
contextDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(new FileInputStream(file))); |
| 261 |
monitor.worked(1); |
| 262 |
|
| 263 |
tomcatUsersDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(new FileInputStream(path.append("tomcat-users.xml").toFile()))); |
| 264 |
monitor.worked(1); |
| 265 |
|
| 266 |
// load policy file |
| 267 |
policyFile = TomcatVersionHelper.getFileContents(new FileInputStream(path.append("catalina.policy").toFile())); |
| 268 |
monitor.worked(1); |
| 269 |
|
| 270 |
// load properties file |
| 271 |
file = path.append("catalina.properties").toFile(); |
| 272 |
if (file.exists()) |
| 273 |
propertiesFile = TomcatVersionHelper.getFileContents(new FileInputStream(file)); |
| 274 |
else |
| 275 |
propertiesFile = null; |
| 276 |
monitor.worked(1); |
| 277 |
|
| 278 |
if (monitor.isCanceled()) |
| 279 |
return; |
| 280 |
monitor.done(); |
| 281 |
} catch (Exception e) { |
| 282 |
Trace.trace(Trace.WARNING, "Could not load Tomcat v5.5 configuration from " + path.toOSString() + ": " + e.getMessage()); |
| 283 |
throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, path.toOSString()), e)); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* @see TomcatConfiguration#importFromPath(IPath, boolean, IProgressMonitor) |
| 289 |
*/ |
| 290 |
public void importFromPath(IPath path, boolean isTestEnv, IProgressMonitor monitor) throws CoreException { |
| 291 |
load(path, monitor); |
| 292 |
|
| 293 |
// for test environment, remove existing contexts since a separate |
| 294 |
// catalina.base will be used |
| 295 |
if (isTestEnv) { |
| 296 |
while (serverInstance.removeContext(0)) { |
| 297 |
// no-op |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* @see TomcatConfiguration#load(IFolder, IProgressMonitor) |
| 304 |
*/ |
| 305 |
public void load(IFolder folder, IProgressMonitor monitor) throws CoreException { |
| 306 |
try { |
| 307 |
monitor = ProgressUtil.getMonitorFor(monitor); |
| 308 |
monitor.beginTask(Messages.loadingTask, 1200); |
| 309 |
|
| 310 |
// check for catalina.policy to verify that this is a v4.0 config |
| 311 |
IFile file = folder.getFile("catalina.policy"); |
| 312 |
if (!file.exists()) |
| 313 |
throw new CoreException(new Status(IStatus.WARNING, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, folder.getFullPath().toOSString()), null)); |
| 314 |
|
| 315 |
// load server.xml |
| 316 |
file = folder.getFile("server.xml"); |
| 317 |
InputStream in = file.getContents(); |
| 318 |
serverFactory = new Factory(); |
| 319 |
serverFactory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40"); |
| 320 |
server = (Server) serverFactory.loadDocument(in); |
| 321 |
serverInstance = new ServerInstance(server, null, null); |
| 322 |
monitor.worked(200); |
| 323 |
|
| 324 |
// load web.xml |
| 325 |
file = folder.getFile("web.xml"); |
| 326 |
webAppDocument = new WebAppDocument(file); |
| 327 |
monitor.worked(200); |
| 328 |
|
| 329 |
// load context.xml |
| 330 |
file = folder.getFile("context.xml"); |
| 331 |
if (file.exists()) { |
| 332 |
in = file.getContents(); |
| 333 |
contextDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(in)); |
| 334 |
} |
| 335 |
else |
| 336 |
contextDocument = null; |
| 337 |
monitor.worked(200); |
| 338 |
|
| 339 |
// load tomcat-users.xml |
| 340 |
file = folder.getFile("tomcat-users.xml"); |
| 341 |
in = file.getContents(); |
| 342 |
|
| 343 |
tomcatUsersDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(in)); |
| 344 |
monitor.worked(200); |
| 345 |
|
| 346 |
// load catalina.policy |
| 347 |
file = folder.getFile("catalina.policy"); |
| 348 |
in = file.getContents(); |
| 349 |
policyFile = TomcatVersionHelper.getFileContents(in); |
| 350 |
monitor.worked(200); |
| 351 |
|
| 352 |
// load catalina.properties |
| 353 |
file = folder.getFile("catalina.properties"); |
| 354 |
if (file.exists()) { |
| 355 |
in = file.getContents(); |
| 356 |
propertiesFile = TomcatVersionHelper.getFileContents(in); |
| 357 |
} |
| 358 |
else |
| 359 |
propertiesFile = null; |
| 360 |
monitor.worked(200); |
| 361 |
|
| 362 |
if (monitor.isCanceled()) |
| 363 |
throw new Exception("Cancelled"); |
| 364 |
monitor.done(); |
| 365 |
} catch (Exception e) { |
| 366 |
Trace.trace(Trace.WARNING, "Could not reload Tomcat v5.5 configuration from: " + folder.getFullPath() + ": " + e.getMessage()); |
| 367 |
throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, folder.getFullPath().toOSString()), e)); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Save to the given directory. |
| 373 |
* @param path a path |
| 374 |
* @param forceDirty boolean |
| 375 |
* @param monitor a progress monitor |
| 376 |
* @exception CoreException |
| 377 |
*/ |
| 378 |
protected void save(IPath path, boolean forceDirty, IProgressMonitor monitor) throws CoreException { |
| 379 |
try { |
| 380 |
monitor = ProgressUtil.getMonitorFor(monitor); |
| 381 |
monitor.beginTask(Messages.savingTask, 5); |
| 382 |
|
| 383 |
// make sure directory exists |
| 384 |
if (!path.toFile().exists()) { |
| 385 |
forceDirty = true; |
| 386 |
path.toFile().mkdir(); |
| 387 |
} |
| 388 |
monitor.worked(1); |
| 389 |
|
| 390 |
// save files |
| 391 |
if (forceDirty || isServerDirty) { |
| 392 |
serverFactory.save(path.append("server.xml").toOSString()); |
| 393 |
isServerDirty = false; |
| 394 |
} |
| 395 |
monitor.worked(1); |
| 396 |
|
| 397 |
webAppDocument.save(path.append("web.xml").toOSString(), forceDirty); |
| 398 |
monitor.worked(1); |
| 399 |
|
| 400 |
if (forceDirty && contextDocument != null) |
| 401 |
XMLUtil.save(path.append("context.xml").toOSString(), contextDocument); |
| 402 |
monitor.worked(1); |
| 403 |
|
| 404 |
if (forceDirty) |
| 405 |
XMLUtil.save(path.append("tomcat-users.xml").toOSString(), tomcatUsersDocument); |
| 406 |
monitor.worked(1); |
| 407 |
|
| 408 |
if (forceDirty) { |
| 409 |
BufferedWriter bw = new BufferedWriter(new FileWriter(path.append("catalina.policy").toFile())); |
| 410 |
bw.write(policyFile); |
| 411 |
bw.close(); |
| 412 |
} |
| 413 |
monitor.worked(1); |
| 414 |
if (propertiesFile != null && forceDirty) { |
| 415 |
BufferedWriter bw = new BufferedWriter(new FileWriter(path.append("catalina.properties").toFile())); |
| 416 |
bw.write(propertiesFile); |
| 417 |
bw.close(); |
| 418 |
} |
| 419 |
monitor.worked(1); |
| 420 |
|
| 421 |
if (monitor.isCanceled()) |
| 422 |
return; |
| 423 |
monitor.done(); |
| 424 |
} catch (Exception e) { |
| 425 |
Trace.trace(Trace.SEVERE, "Could not save Tomcat v5.5 configuration to " + path, e); |
| 426 |
throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotSaveConfiguration, new String[] {e.getLocalizedMessage()}), e)); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Save to the given directory. All configuration files |
| 432 |
* are forced to be saved. |
| 433 |
* |
| 434 |
* @param path Desination path for the configuration files. |
| 435 |
* @param monitor A progress monitor |
| 436 |
* @exception CoreException |
| 437 |
*/ |
| 438 |
public void save(IPath path, IProgressMonitor monitor) throws CoreException { |
| 439 |
save(path, true, monitor); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Save the information held by this object to the given directory. |
| 444 |
* |
| 445 |
* @param folder a folder |
| 446 |
* @param monitor a progress monitor |
| 447 |
* @throws CoreException |
| 448 |
*/ |
| 449 |
public void save(IFolder folder, IProgressMonitor monitor) throws CoreException { |
| 450 |
try { |
| 451 |
monitor = ProgressUtil.getMonitorFor(monitor); |
| 452 |
monitor.beginTask(Messages.savingTask, 1200); |
| 453 |
|
| 454 |
// save server.xml |
| 455 |
byte[] data = serverFactory.getContents(); |
| 456 |
InputStream in = new ByteArrayInputStream(data); |
| 457 |
IFile file = folder.getFile("server.xml"); |
| 458 |
if (file.exists()) { |
| 459 |
if (isServerDirty) |
| 460 |
file.setContents(in, true, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 461 |
else |
| 462 |
monitor.worked(200); |
| 463 |
} else |
| 464 |
file.create(in, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 465 |
isServerDirty = false; |
| 466 |
|
| 467 |
// save web.xml |
| 468 |
webAppDocument.save(folder.getFile("web.xml"), ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 469 |
|
| 470 |
// save context.xml |
| 471 |
if (contextDocument != null) { |
| 472 |
data = XMLUtil.getContents(contextDocument); |
| 473 |
in = new ByteArrayInputStream(data); |
| 474 |
file = folder.getFile("context.xml"); |
| 475 |
if (file.exists()) |
| 476 |
monitor.worked(200); |
| 477 |
//file.setContents(in, true, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 478 |
else |
| 479 |
file.create(in, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 480 |
} |
| 481 |
|
| 482 |
// save tomcat-users.xml |
| 483 |
data = XMLUtil.getContents(tomcatUsersDocument); |
| 484 |
in = new ByteArrayInputStream(data); |
| 485 |
file = folder.getFile("tomcat-users.xml"); |
| 486 |
if (file.exists()) |
| 487 |
monitor.worked(200); |
| 488 |
//file.setContents(in, true, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 489 |
else |
| 490 |
file.create(in, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 491 |
|
| 492 |
// save catalina.policy |
| 493 |
in = new ByteArrayInputStream(policyFile.getBytes()); |
| 494 |
file = folder.getFile("catalina.policy"); |
| 495 |
if (file.exists()) |
| 496 |
monitor.worked(200); |
| 497 |
//file.setContents(in, true, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 498 |
else |
| 499 |
file.create(in, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 500 |
|
| 501 |
// save catalina.properties |
| 502 |
if (propertiesFile != null) { |
| 503 |
in = new ByteArrayInputStream(propertiesFile.getBytes()); |
| 504 |
file = folder.getFile("catalina.properties"); |
| 505 |
if (file.exists()) |
| 506 |
monitor.worked(200); |
| 507 |
//file.setContents(in, true, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 508 |
else |
| 509 |
file.create(in, true, ProgressUtil.getSubMonitorFor(monitor, 200)); |
| 510 |
} else |
| 511 |
monitor.worked(200); |
| 512 |
|
| 513 |
if (monitor.isCanceled()) |
| 514 |
return; |
| 515 |
monitor.done(); |
| 516 |
} catch (Exception e) { |
| 517 |
Trace.trace(Trace.SEVERE, "Could not save Tomcat v5.5 configuration to " + folder.toString(), e); |
| 518 |
throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotSaveConfiguration, new String[] {e.getLocalizedMessage()}), e)); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
protected static boolean hasMDBListener(Server server) { |
| 523 |
if (server == null) |
| 524 |
return false; |
| 525 |
|
| 526 |
int count = server.getListenerCount(); |
| 527 |
if (count == 0) |
| 528 |
return false; |
| 529 |
|
| 530 |
for (int i = 0; i < count; i++) { |
| 531 |
Listener listener = server.getListener(i); |
| 532 |
if (listener != null && listener.getClassName() != null && listener.getClassName().indexOf("mbean") >= 0) |
| 533 |
return true; |
| 534 |
} |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* @see ITomcatConfigurationWorkingCopy#addMimeMapping(int, IMimeMapping) |
| 540 |
*/ |
| 541 |
public void addMimeMapping(int index, IMimeMapping map) { |
| 542 |
webAppDocument.addMimeMapping(index, map); |
| 543 |
firePropertyChangeEvent(ADD_MAPPING_PROPERTY, new Integer(index), map); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* @see ITomcatConfigurationWorkingCopy#addWebModule(int, ITomcatWebModule) |
| 548 |
*/ |
| 549 |
public void addWebModule(int index, ITomcatWebModule module) { |
| 550 |
try { |
| 551 |
Context context = serverInstance.createContext(index); |
| 552 |
if (context != null) { |
| 553 |
context.setDocBase(module.getDocumentBase()); |
| 554 |
context.setPath(module.getPath()); |
| 555 |
context.setReloadable(module.isReloadable() ? "true" : "false"); |
| 556 |
if (module.getMemento() != null && module.getMemento().length() > 0) |
| 557 |
context.setSource(module.getMemento()); |
| 558 |
isServerDirty = true; |
| 559 |
firePropertyChangeEvent(ADD_WEB_MODULE_PROPERTY, null, module); |
| 560 |
} |
| 561 |
} catch (Exception e) { |
| 562 |
Trace.trace(Trace.SEVERE, "Error adding web module " + module.getPath(), e); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Change the extension of a mime mapping. |
| 568 |
* |
| 569 |
* @param index |
| 570 |
* @param map |
| 571 |
*/ |
| 572 |
public void modifyMimeMapping(int index, IMimeMapping map) { |
| 573 |
webAppDocument.modifyMimeMapping(index, map); |
| 574 |
firePropertyChangeEvent(MODIFY_MAPPING_PROPERTY, new Integer(index), map); |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Modify the port with the given id. |
| 579 |
* |
| 580 |
* @param id java.lang.String |
| 581 |
* @param port int |
| 582 |
*/ |
| 583 |
public void modifyServerPort(String id, int port) { |
| 584 |
try { |
| 585 |
if ("server".equals(id)) { |
| 586 |
server.setPort(port + ""); |
| 587 |
isServerDirty = true; |
| 588 |
firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port)); |
| 589 |
return; |
| 590 |
} |
| 591 |
|
| 592 |
int i = id.indexOf("/"); |
| 593 |
// If a connector in the instance Service |
| 594 |
if (i < 0) { |
| 595 |
int connNum = Integer.parseInt(id); |
| 596 |
Connector connector = serverInstance.getConnector(connNum); |
| 597 |
if (connector != null) { |
| 598 |
connector.setPort(port + ""); |
| 599 |
isServerDirty = true; |
| 600 |
firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port)); |
| 601 |
} |
| 602 |
} |
| 603 |
// Else a connector in another Service |
| 604 |
else { |
| 605 |
int servNum = Integer.parseInt(id.substring(0, i)); |
| 606 |
int connNum = Integer.parseInt(id.substring(i + 1)); |
| 607 |
|
| 608 |
Service service = server.getService(servNum); |
| 609 |
Connector connector = service.getConnector(connNum); |
| 610 |
connector.setPort(port + ""); |
| 611 |
isServerDirty = true; |
| 612 |
firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port)); |
| 613 |
} |
| 614 |
} catch (Exception e) { |
| 615 |
Trace.trace(Trace.SEVERE, "Error modifying server port " + id, e); |
| 616 |
} |
| 617 |
} |
| 618 |
/** |
| 619 |
* Change a web module. |
| 620 |
* @param index int |
| 621 |
* @param docBase java.lang.String |
| 622 |
* @param path java.lang.String |
| 623 |
* @param reloadable boolean |
| 624 |
*/ |
| 625 |
public void modifyWebModule(int index, String docBase, String path, boolean reloadable) { |
| 626 |
try { |
| 627 |
Context context = serverInstance.getContext(index); |
| 628 |
if (context != null) { |
| 629 |
context.setPath(path); |
| 630 |
context.setDocBase(docBase); |
| 631 |
context.setReloadable(reloadable ? "true" : "false"); |
| 632 |
isServerDirty = true; |
| 633 |
WebModule module = new WebModule(path, docBase, null, reloadable); |
| 634 |
firePropertyChangeEvent(MODIFY_WEB_MODULE_PROPERTY, new Integer(index), module); |
| 635 |
} |
| 636 |
} catch (Exception e) { |
| 637 |
Trace.trace(Trace.SEVERE, "Error modifying web module " + index, e); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Removes a mime mapping. |
| 643 |
* @param index int |
| 644 |
*/ |
| 645 |
public void removeMimeMapping(int index) { |
| 646 |
webAppDocument.removeMimeMapping(index); |
| 647 |
firePropertyChangeEvent(REMOVE_MAPPING_PROPERTY, null, new Integer(index)); |
| 648 |
} |
| 649 |
|
| 650 |
/** |
| 651 |
* Removes a web module. |
| 652 |
* @param index int |
| 653 |
*/ |
| 654 |
public void removeWebModule(int index) { |
| 655 |
try { |
| 656 |
serverInstance.removeContext(index); |
| 657 |
isServerDirty = true; |
| 658 |
firePropertyChangeEvent(REMOVE_WEB_MODULE_PROPERTY, null, new Integer(index)); |
| 659 |
} catch (Exception e) { |
| 660 |
Trace.trace(Trace.SEVERE, "Error removing module ref " + index, e); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Add context configuration found in META-INF/context.xml files |
| 666 |
* present in projects to published server.xml. |
| 667 |
* |
| 668 |
* @param baseDir path to catalina instance directory |
| 669 |
* @param deployDir path to deployment directory |
| 670 |
* @param monitor a progress monitor or null |
| 671 |
* @return result of operation |
| 672 |
*/ |
| 673 |
protected IStatus publishContextConfig(IPath baseDir, IPath deployDir, IProgressMonitor monitor) { |
| 674 |
return TomcatVersionHelper.publishCatalinaContextConfig(baseDir, deployDir, monitor); |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* Update contexts in server.xml to serve projects directly without |
| 679 |
* publishing. |
| 680 |
* |
| 681 |
* @param baseDir path to catalina instance directory |
| 682 |
* @param monitor a progress monitor or null |
| 683 |
* @return result of operation |
| 684 |
*/ |
| 685 |
protected IStatus updateContextsToServeDirectly(IPath baseDir, String loader, IProgressMonitor monitor) { |
| 686 |
return TomcatVersionHelper.updateContextsToServeDirectly(baseDir, loader, monitor); |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* Cleanup the server instance. This consists of deleting the work |
| 691 |
* directory associated with Contexts that are going away in the |
| 692 |
* up coming publish. |
| 693 |
* |
| 694 |
* @param baseDir path to server instance directory, i.e. catalina.base |
| 695 |
* @param installDir path to server installation directory (not currently used) |
| 696 |
* @param monitor a progress monitor or null |
| 697 |
* @return MultiStatus containing results of the cleanup operation |
| 698 |
*/ |
| 699 |
protected IStatus cleanupServer(IPath baseDir, IPath installDir, boolean removeKeptContextFiles, IProgressMonitor monitor) { |
| 700 |
List modules = getWebModules(); |
| 701 |
return TomcatVersionHelper.cleanupCatalinaServer(baseDir, installDir, removeKeptContextFiles, modules, monitor); |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* @see TomcatConfiguration#localizeConfiguration(IPath, IPath, TomcatServer, IProgressMonitor) |
| 706 |
*/ |
| 707 |
public IStatus localizeConfiguration(IPath baseDir, IPath deployDir, TomcatServer tomcatServer, IProgressMonitor monitor) { |
| 708 |
return TomcatVersionHelper.localizeConfiguration(baseDir, deployDir, tomcatServer, monitor); |
| 709 |
} |
| 710 |
} |