# Class FarmWarDeployer

java.lang.Object
org.apache.catalina.ha.ClusterListener
org.apache.catalina.ha.deploy.FarmWarDeployer

All Implemented Interfaces:
  `ClusterDeployer, FileChangeListener, ChannelListener`

public class FarmWarDeployer extends ClusterListener implements ClusterDeployer, FileChangeListener

A farm war deployer is a class that is able to deploy/undeploy web applications in WAR from within the cluster.

Any host can act as the admin, and will have three directories

- watchDir - the directory where we watch for changes
- deployDir - the directory where we install applications
- tempDir - a temporaryDirectory to store binary data when downloading a war from the cluster

Currently we only support deployment of WAR files since they are easier to send across the wire.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected File`
`configBase`
Path where context descriptors should be deployed.
`protected String`
`deployDir`
Deployment directory.
`protected final Map<String, FileMessageFactory>`
`fileFactories`
Map of file name to factory for in-progress file transfers.
`protected Host`
`host`
The associated host.
`protected int`
`maxValidTime`
The maximum valid time(in seconds) for FileMessageFactory.
`protected MBeanServer`
`mBeanServer`
MBean server.
`protected ObjectName`
`oname`
The associated deployer ObjectName.
`protected int`
`processDeployFrequency`
Frequency of the Farm watchDir check.
`protected boolean`
`started`
Whether the deployer has been started.
`protected String`
`tempDir`
Temporary directory.
`protected String`
`watchDir`
Watch directory.
`protected boolean`
`watchEnabled`
Whether the watch directory feature is enabled.
`protected WarWatcher`
`watcher`
The watcher for monitoring the watch directory.

### Fields inherited from class ClusterListener

`cluster`

## Constructor Summary

Constructors
Constructor
Description
`FarmWarDeployer()`
Creates a new FarmWarDeployer instance.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`accept(ClusterMessage msg)`
Accept only a certain type of messages.
`void`
`backgroundProcess()`
Call watcher to check for deploy changes
`protected void`
`check(String name)`
Check a context for deployment operations.
`protected boolean`
`copy(File from, File to)`
Copy a file to the specified temp directory.
`void`
`fileModified(File newWar)`
Modification from watchDir war detected.
`void`
`fileRemoved(File removeWar)`
War remove from watchDir.
`String`
`getDeployDir()`
Returns the deployment directory path.
`File`
`getDeployDirFile()`
Returns the deployment directory as a File object.
`FileMessageFactory`
`getFactory(FileMessage msg)`
Create factory for all transported war files
`int`
`getMaxValidTime()`
Returns the maximum valid time for FileMessageFactory.
`int`
`getProcessDeployFrequency()`
Returns the frequency of watcher checks.
`String`
`getTempDir()`
Returns the temporary directory path.
`File`
`getTempDirFile()`
Returns the temporary directory as a File object.
`String`
`getWatchDir()`
Returns the watch directory path.
`File`
`getWatchDirFile()`
Returns the watch directory as a File object.
`boolean`
`getWatchEnabled()`
Returns whether the watch directory feature is enabled.
`void`
`install(String contextName, File webapp)`
Install a new web application, whose web application archive is at the specified URL, into this container and all the other members of the cluster with the specified context name.
`boolean`
`isWatchEnabled()`
Returns whether the watch directory feature is enabled.
`void`
`messageReceived(ClusterMessage msg)`
Callback from the cluster, when a message is received, The cluster will broadcast it invoking the messageReceived on the receiver.
`protected void`
`remove(String contextName)`
Invoke the remove method on the deployer.
`void`
`remove(String contextName, boolean undeploy)`
Remove an existing web application, attached to the specified context name.
`void`
`removeFactory(FileMessage msg)`
Remove file (war) from messages
`protected void`
`removeInvalidFileFactories()`
Removes invalid file factories from the factory map.
`protected void`
`removeServiced(String name)`
Mark a context as no longer being serviced.
`void`
`setDeployDir(String deployDir)`
Sets the deployment directory path.
`void`
`setMaxValidTime(int maxValidTime)`
Sets the maximum valid time for FileMessageFactory.
`void`
`setProcessDeployFrequency(int processExpiresFrequency)`
Set the watcher checks frequency.
`void`
`setTempDir(String tempDir)`
Sets the temporary directory path.
`void`
`setWatchDir(String watchDir)`
Sets the watch directory path.
`void`
`setWatchEnabled(boolean watchEnabled)`
Sets whether the watch directory feature is enabled.
`void`
`start()`
Start the cluster deployer, the owning container will invoke this
`void`
`stop()`
Stops the cluster deployer, the owning container will invoke this
`protected boolean`
`tryAddServiced(String name)`
Attempt to mark a context as being serviced
`protected void`
`undeployDir(File dir)`
Delete the specified directory, including all of its contents and subdirectories recursively.

### Methods inherited from class ClusterListener

`accept, getCluster, messageReceived, setCluster`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface ChannelListener

`accept, messageReceived`

### Methods inherited from interface ClusterDeployer

`getCluster, setCluster`

## Field Details

### started

protected boolean started
Whether the deployer has been started.

### fileFactories

protected final Map<String, FileMessageFactory> fileFactories
Map of file name to factory for in-progress file transfers.

### deployDir

protected String deployDir
Deployment directory.

### tempDir

protected String tempDir
Temporary directory.

### watchDir

protected String watchDir
Watch directory.

### watchEnabled

protected boolean watchEnabled
Whether the watch directory feature is enabled.

### watcher

protected WarWatcher watcher
The watcher for monitoring the watch directory.

### processDeployFrequency

protected int processDeployFrequency
Frequency of the Farm watchDir check. Cluster wide deployment will be done once for the specified amount of backgroundProcess calls (ie, the lower the amount, the most often the checks will occur).

### configBase

protected File configBase
Path where context descriptors should be deployed.

### host

protected Host host
The associated host.

### mBeanServer

protected MBeanServer mBeanServer
MBean server.

### oname

protected ObjectName oname
The associated deployer ObjectName.

### maxValidTime

protected int maxValidTime
The maximum valid time(in seconds) for FileMessageFactory.

## Constructor Details

### FarmWarDeployer

public FarmWarDeployer()
Creates a new FarmWarDeployer instance.

## Method Details

### start

public void start() throws Exception
Description copied from interface: `ClusterDeployer`
Start the cluster deployer, the owning container will invoke this

Specified by:
  `start` in interface `ClusterDeployer`
Throws:
  `Exception` - - if failure to start cluster

### stop

public void stop() throws LifecycleException
Description copied from interface: `ClusterDeployer`
Stops the cluster deployer, the owning container will invoke this

Specified by:
  `stop` in interface `ClusterDeployer`
Throws:
  `LifecycleException` - Error stopping cluster deployer

### messageReceived

public void messageReceived(ClusterMessage msg)
Callback from the cluster, when a message is received, The cluster will broadcast it invoking the messageReceived on the receiver.

Specified by:
  `messageReceived` in class `ClusterListener`
Parameters:
  `msg` - ClusterMessage - the message received from the cluster

### getFactory

public FileMessageFactory getFactory(FileMessage msg) throws FileNotFoundException, IOException
Create factory for all transported war files

Parameters:
  `msg` - The file
Returns:
  Factory for all app message (war files)
Throws:
  `FileNotFoundException` - Missing file error
  `IOException` - Other IO error

### removeFactory

public void removeFactory(FileMessage msg)
Remove file (war) from messages

Parameters:
  `msg` - The file

### accept

public boolean accept(ClusterMessage msg)
Accept only a certain type of messages.

This listener accepts only FileMessage or UndeployMessage.

Specified by:
  `accept` in class `ClusterListener`
Parameters:
  `msg` - the message
Returns:
  `true` to indicate that messageReceived should be invoked. If `false` is returned, the messageReceived method will not be invoked.

### install

public void install(String contextName, File webapp) throws IOException
Install a new web application, whose web application archive is at the specified URL, into this container and all the other members of the cluster with the specified context name.

If this application is successfully installed locally, a ContainerEvent of type `INSTALL_EVENT` will be sent to all registered listeners, with the newly created `Context` as an argument.

Specified by:
  `install` in interface `ClusterDeployer`
Parameters:
  `contextName` - The context name to which this application should be installed (must be unique)
  `webapp` - A WAR file or unpacked directory structure containing the web application to be installed
Throws:
  `IllegalArgumentException` - if the specified context name is malformed
  `IllegalStateException` - if the specified context name is already deployed
  `IOException` - if an input/output error was encountered during installation

### remove

public void remove(String contextName, boolean undeploy) throws IOException
Remove an existing web application, attached to the specified context name. If this application is successfully removed, a ContainerEvent of type `REMOVE_EVENT` will be sent to all registered listeners, with the removed `Context` as an argument. Deletes the web application war file and/or directory if they exist in the Host's appBase.

Specified by:
  `remove` in interface `ClusterDeployer`
Parameters:
  `contextName` - The context name of the application to be removed
  `undeploy` - boolean flag to remove web application from server
Throws:
  `IllegalArgumentException` - if the specified context name is malformed
  `IllegalArgumentException` - if the specified context name does not identify a currently installed web application
  `IOException` - if an input/output error occurs during removal

### fileModified

public void fileModified(File newWar)
Description copied from interface: `FileChangeListener`
Modification from watchDir war detected.

Specified by:
  `fileModified` in interface `FileChangeListener`
Parameters:
  `newWar` - the modified file

### fileRemoved

public void fileRemoved(File removeWar)
Description copied from interface: `FileChangeListener`
War remove from watchDir.

Specified by:
  `fileRemoved` in interface `FileChangeListener`
Parameters:
  `removeWar` - the file removed

### remove

protected void remove(String contextName) throws Exception
Invoke the remove method on the deployer.

Parameters:
  `contextName` - The context to remove
Throws:
  `Exception` - If an error occurs removing the context

### undeployDir

protected void undeployDir(File dir)
Delete the specified directory, including all of its contents and subdirectories recursively.

Parameters:
  `dir` - File object representing the directory to be deleted

### backgroundProcess

public void backgroundProcess()
Call watcher to check for deploy changes

Specified by:
  `backgroundProcess` in interface `ClusterDeployer`
See Also:
  - `ClusterDeployer.backgroundProcess()`

### check

protected void check(String name) throws Exception
Check a context for deployment operations.

Parameters:
  `name` - The context name
Throws:
  `Exception` - Error invoking the deployer

### tryAddServiced

protected boolean tryAddServiced(String name) throws Exception
Attempt to mark a context as being serviced

Parameters:
  `name` - The context name
Returns:
  `true` if the application was marked as being serviced and `false` if the application was already marked as being serviced
Throws:
  `Exception` - Error invoking the deployer

### removeServiced

protected void removeServiced(String name) throws Exception
Mark a context as no longer being serviced.

Parameters:
  `name` - The context name
Throws:
  `Exception` - Error invoking the deployer

### getDeployDir

public String getDeployDir()
Returns the deployment directory path.

Returns:
  the deployment directory path

### getDeployDirFile

public File getDeployDirFile()
Returns the deployment directory as a File object.

Returns:
  the deployment directory file

### setDeployDir

public void setDeployDir(String deployDir)
Sets the deployment directory path.

Parameters:
  `deployDir` - the deployment directory path

### getTempDir

public String getTempDir()
Returns the temporary directory path.

Returns:
  the temporary directory path

### getTempDirFile

public File getTempDirFile()
Returns the temporary directory as a File object.

Returns:
  the temporary directory file

### setTempDir

public void setTempDir(String tempDir)
Sets the temporary directory path.

Parameters:
  `tempDir` - the temporary directory path

### getWatchDir

public String getWatchDir()
Returns the watch directory path.

Returns:
  the watch directory path

### getWatchDirFile

public File getWatchDirFile()
Returns the watch directory as a File object.

Returns:
  the watch directory file

### setWatchDir

public void setWatchDir(String watchDir)
Sets the watch directory path.

Parameters:
  `watchDir` - the watch directory path

### isWatchEnabled

public boolean isWatchEnabled()
Returns whether the watch directory feature is enabled.

Returns:
  `true` if watch is enabled

### getWatchEnabled

public boolean getWatchEnabled()
Returns whether the watch directory feature is enabled.

Returns:
  `true` if watch is enabled

### setWatchEnabled

public void setWatchEnabled(boolean watchEnabled)
Sets whether the watch directory feature is enabled.

Parameters:
  `watchEnabled` - `true` to enable watch

### getProcessDeployFrequency

public int getProcessDeployFrequency()
Returns the frequency of watcher checks.

Returns:
  the frequency of watcher checks

### setProcessDeployFrequency

public void setProcessDeployFrequency(int processExpiresFrequency)
Set the watcher checks frequency.

Parameters:
  `processExpiresFrequency` - the new manager checks frequency

### getMaxValidTime

public int getMaxValidTime()
Returns the maximum valid time for FileMessageFactory.

Returns:
  the maximum valid time in seconds

### setMaxValidTime

public void setMaxValidTime(int maxValidTime)
Sets the maximum valid time for FileMessageFactory.

Parameters:
  `maxValidTime` - the maximum valid time in seconds

### copy

protected boolean copy(File from, File to)
Copy a file to the specified temp directory.

Parameters:
  `from` - copy from temp
  `to` - to host appBase directory
Returns:
  true, copy successful

### removeInvalidFileFactories

protected void removeInvalidFileFactories()
Removes invalid file factories from the factory map.