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
40 public class ExternalRepositoriesReloadableWebappLoader
41 extends WebappLoader
42 {
43
44
45
46
47 private Map<String, Long> modificationTimeMap = new HashMap<String, Long>();
48
49 private Log log;
50
51
52
53
54 public ExternalRepositoriesReloadableWebappLoader()
55 {
56 super();
57 }
58
59
60
61
62
63
64
65 public ExternalRepositoriesReloadableWebappLoader( ClassLoader parent, Log log )
66 {
67 super( parent );
68 this.log = log;
69 }
70
71
72
73
74 @Override
75 public void addRepository( String repository )
76 {
77 super.addRepository( repository );
78 try
79 {
80 File file = new File( new URL( repository ).getPath().replaceAll( "%20", " " ) );
81 if ( file.isDirectory() )
82 {
83 addClassDirectory( file );
84 }
85 else if ( file.isFile() && file.getName().endsWith( ".jar" ) )
86 {
87 addFile( file );
88 }
89 }
90 catch ( MalformedURLException muex )
91 {
92 throw new RuntimeException( muex );
93 }
94 }
95
96
97
98
99
100
101 private void addClassDirectory( File directory )
102 {
103 for ( File file : directory.listFiles() )
104 {
105 if ( file.isDirectory() )
106 {
107
108 addFile( file );
109 addClassDirectory( file );
110 }
111 else if ( file.isFile() )
112 {
113 addFile( file );
114 }
115 }
116 }
117
118
119
120
121
122
123 private void addFile( File file )
124 {
125 modificationTimeMap.put( file.getAbsolutePath(), file.lastModified() );
126 }
127
128
129
130
131 @Override
132 public boolean modified()
133 {
134 boolean modified = super.modified();
135 if ( !modified )
136 {
137 if ( log != null )
138 {
139 log.debug( "classPath scanning started at " + new Date().toString() );
140 }
141 for ( Map.Entry<String, Long> entry : modificationTimeMap.entrySet() )
142 {
143 String key = entry.getKey();
144 File file = new File( key );
145 if ( file.exists() )
146 {
147
148 Long savedLastModified = modificationTimeMap.get( key );
149 if ( file.lastModified() > savedLastModified )
150 {
151 modified = true;
152 modificationTimeMap.put( key, file.lastModified() );
153
154
155
156 if ( file.isDirectory() )
157 {
158 addClassDirectory( file );
159 }
160 }
161 }
162 }
163 }
164 if ( log != null )
165 {
166 log.debug( "context " + modified + " at " + new Date().toString() );
167 }
168 return modified;
169 }
170
171 }