1 package org.apache.tomcat.maven.plugin.tomcat6;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.catalina.loader.WebappLoader;
23 import org.apache.maven.plugin.logging.Log;
24
25 import java.io.File;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.Map;
31
32
33
34
35
36
37
38
39 public class ExternalRepositoriesReloadableWebappLoader
40 extends WebappLoader
41 {
42
43
44
45
46 private Map<String, Long> modificationTimeMap = new HashMap<String, Long>();
47
48 private Log log;
49
50
51
52
53 public ExternalRepositoriesReloadableWebappLoader()
54 {
55 super();
56 }
57
58
59
60
61
62
63
64 public ExternalRepositoriesReloadableWebappLoader( ClassLoader parent, Log log )
65 {
66 super( parent );
67 this.log = log;
68 }
69
70
71
72
73 @Override
74 public void addRepository( String repository )
75 {
76 super.addRepository( repository );
77 try
78 {
79 File file = new File( new URL( repository ).getPath().replaceAll( "%20", " " ) );
80 if ( file.isDirectory() )
81 {
82 addClassDirectory( file );
83 }
84 else if ( file.isFile() && file.getName().endsWith( ".jar" ) )
85 {
86 addFile( file );
87 }
88 }
89 catch ( MalformedURLException muex )
90 {
91 throw new RuntimeException( muex );
92 }
93 }
94
95
96
97
98
99
100 private void addClassDirectory( File directory )
101 {
102 for ( File file : directory.listFiles() )
103 {
104 if ( file.isDirectory() )
105 {
106
107 addFile( file );
108 addClassDirectory( file );
109 }
110 else if ( file.isFile() )
111 {
112 addFile( file );
113 }
114 }
115 }
116
117
118
119
120
121
122 private void addFile( File file )
123 {
124 modificationTimeMap.put( file.getAbsolutePath(), file.lastModified() );
125 }
126
127
128
129
130 @Override
131 public boolean modified()
132 {
133 boolean modified = super.modified();
134 if ( !modified )
135 {
136 if ( log != null )
137 {
138 log.debug( "classPath scanning started at " + new Date().toString() );
139 }
140 for ( Map.Entry<String, Long> entry : modificationTimeMap.entrySet() )
141 {
142 String key = entry.getKey();
143 File file = new File( key );
144 if ( file.exists() )
145 {
146
147 Long savedLastModified = modificationTimeMap.get( key );
148 if ( file.lastModified() > savedLastModified )
149 {
150 modified = true;
151 modificationTimeMap.put( key, file.lastModified() );
152
153
154
155 if ( file.isDirectory() )
156 {
157 addClassDirectory( file );
158 }
159 }
160 }
161 }
162 }
163 if ( log != null )
164 {
165 log.debug( "context " + modified + " at " + new Date().toString() );
166 }
167 return modified;
168 }
169
170 }