Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 870b74032a9401d32bcd1e3997d2196cccb54d5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.deploy.providers;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jetty.deploy.App;
import org.eclipse.jetty.deploy.AppProvider;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.util.Scanner;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;

/**
 */
@ManagedObject("Abstract Provider for loading webapps")
public abstract class ScanningAppProvider extends AbstractLifeCycle implements AppProvider
{
    private static final Logger LOG = Log.getLogger(ScanningAppProvider.class);

    private Map<String, App> _appMap = new HashMap<String, App>();

    private DeploymentManager _deploymentManager;
    protected final FilenameFilter _filenameFilter;
    private Resource _monitoredDir;
    private boolean _recursive = false;
    private int _scanInterval = 10;
    private Scanner _scanner;

    /* ------------------------------------------------------------ */
    private final Scanner.DiscreteListener _scannerListener = new Scanner.DiscreteListener()
    {
        public void fileAdded(String filename) throws Exception
        {
            ScanningAppProvider.this.fileAdded(filename);
        }

        public void fileChanged(String filename) throws Exception
        {
            ScanningAppProvider.this.fileChanged(filename);
        }

        public void fileRemoved(String filename) throws Exception
        {
            ScanningAppProvider.this.fileRemoved(filename);
        }
    };

    /* ------------------------------------------------------------ */
    protected ScanningAppProvider(FilenameFilter filter)
    {
        _filenameFilter = filter;
    }

    /* ------------------------------------------------------------ */
    /**
     * @return The index of currently deployed applications.
     */
    protected Map<String, App> getDeployedApps()
    {
        return _appMap;
    }

    /* ------------------------------------------------------------ */
    /**
     * Called by the Scanner.DiscreteListener to create a new App object.
     * Isolated in a method so that it is possible to override the default App
     * object for specialized implementations of the AppProvider.
     * 
     * @param filename
     *            The file that is the context.xml. It is resolved by
     *            {@link Resource#newResource(String)}
     * @return The App object for this particular context definition file.
     */
    protected App createApp(String filename)
    {
        return new App(_deploymentManager,this,filename);
    }

    /* ------------------------------------------------------------ */
    @Override
    protected void doStart() throws Exception
    {
        if (LOG.isDebugEnabled()) 
            LOG.debug(this.getClass().getSimpleName() + ".doStart()");
        if (_monitoredDir == null)
        {
            throw new IllegalStateException("No configuration dir specified");
        }

        File scandir = _monitoredDir.getFile();
        LOG.info("Deployment monitor " + scandir + " at interval " + _scanInterval);
        _scanner = new Scanner();
        _scanner.setScanDirs(Collections.singletonList(scandir));
        _scanner.setScanInterval(_scanInterval);
        _scanner.setRecursive(_recursive);
        _scanner.setFilenameFilter(_filenameFilter);
        _scanner.setReportDirs(true);
        _scanner.addListener(_scannerListener);
        _scanner.start();
    }

    /* ------------------------------------------------------------ */
    @Override
    protected void doStop() throws Exception
    {
        if (_scanner!=null)
        {
            _scanner.stop();
            _scanner.removeListener(_scannerListener);
            _scanner = null;
        }
    }

    /* ------------------------------------------------------------ */
    protected void fileAdded(String filename) throws Exception
    {
        if (LOG.isDebugEnabled()) 
            LOG.debug("added {}",filename);
        App app = ScanningAppProvider.this.createApp(filename);
        if (app != null)
        {
            _appMap.put(filename,app);
            _deploymentManager.addApp(app);
        }
    }

    /* ------------------------------------------------------------ */
    protected void fileChanged(String filename) throws Exception
    {
        if (LOG.isDebugEnabled()) 
            LOG.debug("changed {}",filename);
        App app = _appMap.remove(filename);
        if (app != null)
        {
            _deploymentManager.removeApp(app);
        }
        app = ScanningAppProvider.this.createApp(filename);
        if (app != null)
        {
            _appMap.put(filename,app);
            _deploymentManager.addApp(app);
        }
    }
    
    /* ------------------------------------------------------------ */
    protected void fileRemoved(String filename) throws Exception
    {
        if (LOG.isDebugEnabled()) 
            LOG.debug("removed {}",filename);
        App app = _appMap.remove(filename);
        if (app != null)
            _deploymentManager.removeApp(app);
    }
    
    /* ------------------------------------------------------------ */
    /**
     * Get the deploymentManager.
     * 
     * @return the deploymentManager
     */
    public DeploymentManager getDeploymentManager()
    {
        return _deploymentManager;
    }


    /* ------------------------------------------------------------ */
    public Resource getMonitoredDirResource()
    {
        return _monitoredDir;
    }

    /* ------------------------------------------------------------ */
    public String getMonitoredDirName()
    {
        return _monitoredDir.toString();
    }

    /* ------------------------------------------------------------ */
    @ManagedAttribute("scanning interval to detect changes which need reloaded")
    public int getScanInterval()
    {
        return _scanInterval;
    }

    /* ------------------------------------------------------------ */
    @ManagedAttribute("recursive scanning supported")
    public boolean isRecursive()
    {
        return _recursive;
    }

    /* ------------------------------------------------------------ */
    public void setDeploymentManager(DeploymentManager deploymentManager)
    {
        _deploymentManager = deploymentManager;
    }
    
    /* ------------------------------------------------------------ */
    public void setMonitoredDirResource(Resource contextsDir)
    {
        _monitoredDir = contextsDir;
    }

    /* ------------------------------------------------------------ */
    public void addScannerListener(Scanner.Listener listener)
    {
        _scanner.addListener(listener);
    }

    /* ------------------------------------------------------------ */
    /**
     * @deprecated Use {@link #setMonitoredDirName(String)}
     */
    public void setMonitoredDir(String dir)
    {
        setMonitoredDirName(dir);
    }
    
    /* ------------------------------------------------------------ */
    /**
     * @param dir
     *            Directory to scan for context descriptors or war files
     */
    public void setMonitoredDirName(String dir)
    {
        try
        {
            setMonitoredDirResource(Resource.newResource(dir));
        }
        catch (Exception e)
        {
            throw new IllegalArgumentException(e);
        }
    }

    /* ------------------------------------------------------------ */
    protected void setRecursive(boolean recursive)
    {
        _recursive = recursive;
    }

    /* ------------------------------------------------------------ */
    public void setScanInterval(int scanInterval)
    {
        _scanInterval = scanInterval;
    }
}

Back to the top