1 | |
package org.apache.maven.archiva.configuration; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.collections.CollectionUtils; |
23 | |
import org.apache.commons.collections.MapUtils; |
24 | |
import org.apache.commons.io.FileUtils; |
25 | |
import org.apache.commons.lang.StringUtils; |
26 | |
import org.apache.maven.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator; |
27 | |
import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader; |
28 | |
import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryWriter; |
29 | |
import org.apache.maven.archiva.policies.AbstractUpdatePolicy; |
30 | |
import org.apache.maven.archiva.policies.CachedFailuresPolicy; |
31 | |
import org.apache.maven.archiva.policies.ChecksumPolicy; |
32 | |
import org.apache.maven.archiva.policies.Policy; |
33 | |
import org.apache.maven.archiva.policies.PostDownloadPolicy; |
34 | |
import org.apache.maven.archiva.policies.PreDownloadPolicy; |
35 | |
import org.codehaus.plexus.evaluator.DefaultExpressionEvaluator; |
36 | |
import org.codehaus.plexus.evaluator.EvaluatorException; |
37 | |
import org.codehaus.plexus.evaluator.ExpressionEvaluator; |
38 | |
import org.codehaus.plexus.evaluator.sources.SystemPropertyExpressionSource; |
39 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; |
40 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; |
41 | |
import org.codehaus.plexus.registry.Registry; |
42 | |
import org.codehaus.plexus.registry.RegistryException; |
43 | |
import org.codehaus.plexus.registry.RegistryListener; |
44 | |
import org.slf4j.Logger; |
45 | |
import org.slf4j.LoggerFactory; |
46 | |
|
47 | |
import java.io.File; |
48 | |
import java.io.IOException; |
49 | |
import java.util.ArrayList; |
50 | |
import java.util.Collection; |
51 | |
import java.util.Collections; |
52 | |
import java.util.HashMap; |
53 | |
import java.util.HashSet; |
54 | |
import java.util.Iterator; |
55 | |
import java.util.List; |
56 | |
import java.util.Map; |
57 | |
import java.util.Map.Entry; |
58 | |
import java.util.Set; |
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 | 0 | public class DefaultArchivaConfiguration |
87 | |
implements ArchivaConfiguration, RegistryListener, Initializable |
88 | |
{ |
89 | 0 | private Logger log = LoggerFactory.getLogger( DefaultArchivaConfiguration.class ); |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
private Registry registry; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
private Configuration configuration; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private Map<String, PreDownloadPolicy> prePolicies; |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private Map<String, PostDownloadPolicy> postPolicies; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
private String userConfigFilename; |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
private String altConfigFilename; |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | 0 | private Set<ConfigurationListener> listeners = new HashSet<ConfigurationListener>(); |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | 0 | private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>(); |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | 0 | private boolean isConfigurationDefaulted = false; |
140 | |
|
141 | |
private static final String KEY = "org.apache.maven.archiva"; |
142 | |
|
143 | |
public Configuration getConfiguration() |
144 | |
{ |
145 | 0 | return loadConfiguration(); |
146 | |
} |
147 | |
|
148 | |
private synchronized Configuration loadConfiguration() |
149 | |
{ |
150 | 0 | if ( configuration == null ) |
151 | |
{ |
152 | 0 | configuration = load(); |
153 | 0 | configuration = unescapeExpressions( configuration ); |
154 | 0 | if( isConfigurationDefaulted ) |
155 | |
{ |
156 | 0 | configuration = checkRepositoryLocations( configuration ); |
157 | |
} |
158 | |
} |
159 | |
|
160 | 0 | return configuration; |
161 | |
} |
162 | |
|
163 | |
@SuppressWarnings("unchecked") |
164 | |
private Configuration load() |
165 | |
{ |
166 | |
|
167 | 0 | Registry subset = registry.getSubset( KEY ); |
168 | 0 | if ( subset.getString( "version" ) == null ) |
169 | |
{ |
170 | |
|
171 | 0 | if ( subset.getSubset( "repositoryScanning" ).isEmpty() ) |
172 | |
{ |
173 | |
|
174 | 0 | subset = readDefaultConfiguration(); |
175 | |
} |
176 | |
} |
177 | |
|
178 | 0 | Configuration config = new ConfigurationRegistryReader().read( subset ); |
179 | |
|
180 | 0 | if ( !config.getRepositories().isEmpty() ) |
181 | |
{ |
182 | 0 | for ( Iterator<V1RepositoryConfiguration> i = config.getRepositories().iterator(); i.hasNext(); ) |
183 | |
{ |
184 | 0 | V1RepositoryConfiguration r = i.next(); |
185 | 0 | r.setScanned( r.isIndexed() ); |
186 | |
|
187 | 0 | if ( r.getUrl().startsWith( "file://" ) ) |
188 | |
{ |
189 | 0 | r.setLocation( r.getUrl().substring( 7 ) ); |
190 | 0 | config.addManagedRepository( r ); |
191 | |
} |
192 | 0 | else if ( r.getUrl().startsWith( "file:" ) ) |
193 | |
{ |
194 | 0 | r.setLocation( r.getUrl().substring( 5 ) ); |
195 | 0 | config.addManagedRepository( r ); |
196 | |
} |
197 | |
else |
198 | |
{ |
199 | 0 | RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration(); |
200 | 0 | repo.setId( r.getId() ); |
201 | 0 | repo.setLayout( r.getLayout() ); |
202 | 0 | repo.setName( r.getName() ); |
203 | 0 | repo.setUrl( r.getUrl() ); |
204 | 0 | config.addRemoteRepository( repo ); |
205 | |
} |
206 | 0 | } |
207 | |
|
208 | |
|
209 | 0 | config.getRepositories().clear(); |
210 | |
|
211 | 0 | registry.removeSubset( KEY + ".repositories" ); |
212 | |
} |
213 | |
|
214 | 0 | if ( !CollectionUtils.isEmpty( config.getRemoteRepositories() ) ) |
215 | |
{ |
216 | 0 | List<RemoteRepositoryConfiguration> remoteRepos = config.getRemoteRepositories(); |
217 | 0 | for ( RemoteRepositoryConfiguration repo : remoteRepos ) |
218 | |
{ |
219 | |
|
220 | 0 | if ( StringUtils.isBlank( repo.getUsername() ) ) |
221 | |
{ |
222 | 0 | repo.setUsername( null ); |
223 | |
} |
224 | |
|
225 | 0 | if ( StringUtils.isBlank( repo.getPassword() ) ) |
226 | |
{ |
227 | 0 | repo.setPassword( null ); |
228 | |
} |
229 | |
} |
230 | |
} |
231 | |
|
232 | 0 | if ( !config.getProxyConnectors().isEmpty() ) |
233 | |
{ |
234 | |
|
235 | |
|
236 | 0 | List<ProxyConnectorConfiguration> proxyConnectorList = new ArrayList<ProxyConnectorConfiguration>(); |
237 | |
|
238 | 0 | proxyConnectorList.addAll( config.getProxyConnectors() ); |
239 | |
|
240 | 0 | config.getProxyConnectors().clear(); |
241 | |
|
242 | 0 | for ( ProxyConnectorConfiguration connector : proxyConnectorList ) |
243 | |
{ |
244 | |
|
245 | 0 | boolean connectorValid = true; |
246 | |
|
247 | 0 | Map<String, String> policies = new HashMap<String, String>(); |
248 | |
|
249 | 0 | policies.putAll( connector.getPolicies() ); |
250 | |
|
251 | 0 | connector.getPolicies().clear(); |
252 | |
|
253 | |
|
254 | 0 | for ( Entry<String, String> entry : policies.entrySet() ) |
255 | |
{ |
256 | 0 | String policyId = entry.getKey(); |
257 | 0 | String setting = entry.getValue(); |
258 | |
|
259 | |
|
260 | 0 | if ( "releases".equals( policyId ) || "snapshots".equals( policyId ) ) |
261 | |
{ |
262 | 0 | if ( "ignored".equals( setting ) ) |
263 | |
{ |
264 | 0 | setting = AbstractUpdatePolicy.ALWAYS; |
265 | |
} |
266 | 0 | else if ( "disabled".equals( setting ) ) |
267 | |
{ |
268 | 0 | setting = AbstractUpdatePolicy.NEVER; |
269 | |
} |
270 | |
} |
271 | 0 | else if ( "cache-failures".equals( policyId ) ) |
272 | |
{ |
273 | 0 | if ( "ignored".equals( setting ) ) |
274 | |
{ |
275 | 0 | setting = CachedFailuresPolicy.NO; |
276 | |
} |
277 | 0 | else if ( "cached".equals( setting ) ) |
278 | |
{ |
279 | 0 | setting = CachedFailuresPolicy.YES; |
280 | |
} |
281 | |
} |
282 | 0 | else if ( "checksum".equals( policyId ) ) |
283 | |
{ |
284 | 0 | if ( "ignored".equals( setting ) ) |
285 | |
{ |
286 | 0 | setting = ChecksumPolicy.IGNORE; |
287 | |
} |
288 | |
} |
289 | |
|
290 | |
|
291 | 0 | if ( policyExists( policyId ) ) |
292 | |
{ |
293 | 0 | Policy policy = findPolicy( policyId ); |
294 | |
|
295 | 0 | if ( !policy.getOptions().contains( setting ) ) |
296 | |
{ |
297 | 0 | setting = policy.getDefaultOption(); |
298 | |
} |
299 | 0 | connector.addPolicy( policyId, setting ); |
300 | 0 | } |
301 | |
else |
302 | |
{ |
303 | |
|
304 | 0 | log.warn( "Policy [" + policyId + "] does not exist." ); |
305 | |
} |
306 | 0 | } |
307 | |
|
308 | 0 | if ( connectorValid ) |
309 | |
{ |
310 | 0 | config.addProxyConnector( connector ); |
311 | |
} |
312 | 0 | } |
313 | |
|
314 | |
|
315 | 0 | Map<String, java.util.List<ProxyConnectorConfiguration>> proxyConnectorMap = config |
316 | |
.getProxyConnectorAsMap(); |
317 | |
|
318 | 0 | for ( String key : proxyConnectorMap.keySet() ) |
319 | |
{ |
320 | 0 | List<ProxyConnectorConfiguration> connectors = proxyConnectorMap.get( key ); |
321 | |
|
322 | 0 | Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() ); |
323 | |
|
324 | |
|
325 | 0 | int order = 1; |
326 | 0 | for ( ProxyConnectorConfiguration connector : connectors ) |
327 | |
{ |
328 | 0 | connector.setOrder( order++ ); |
329 | |
} |
330 | 0 | } |
331 | |
} |
332 | |
|
333 | 0 | return config; |
334 | |
} |
335 | |
|
336 | |
private Policy findPolicy( String policyId ) |
337 | |
{ |
338 | 0 | if ( MapUtils.isEmpty( prePolicies ) ) |
339 | |
{ |
340 | 0 | log.error( "No PreDownloadPolicies found!" ); |
341 | 0 | return null; |
342 | |
} |
343 | |
|
344 | 0 | if ( MapUtils.isEmpty( postPolicies ) ) |
345 | |
{ |
346 | 0 | log.error( "No PostDownloadPolicies found!" ); |
347 | 0 | return null; |
348 | |
} |
349 | |
|
350 | |
Policy policy; |
351 | |
|
352 | 0 | policy = prePolicies.get( policyId ); |
353 | 0 | if ( policy != null ) |
354 | |
{ |
355 | 0 | return policy; |
356 | |
} |
357 | |
|
358 | 0 | policy = postPolicies.get( policyId ); |
359 | 0 | if ( policy != null ) |
360 | |
{ |
361 | 0 | return policy; |
362 | |
} |
363 | |
|
364 | 0 | return null; |
365 | |
} |
366 | |
|
367 | |
private boolean policyExists( String policyId ) |
368 | |
{ |
369 | 0 | if ( MapUtils.isEmpty( prePolicies ) ) |
370 | |
{ |
371 | 0 | log.error( "No PreDownloadPolicies found!" ); |
372 | 0 | return false; |
373 | |
} |
374 | |
|
375 | 0 | if ( MapUtils.isEmpty( postPolicies ) ) |
376 | |
{ |
377 | 0 | log.error( "No PostDownloadPolicies found!" ); |
378 | 0 | return false; |
379 | |
} |
380 | |
|
381 | 0 | return ( prePolicies.containsKey( policyId ) || postPolicies.containsKey( policyId ) ); |
382 | |
} |
383 | |
|
384 | |
private Registry readDefaultConfiguration() |
385 | |
{ |
386 | |
|
387 | 0 | registry.removeSubset( KEY ); |
388 | |
|
389 | |
try |
390 | |
{ |
391 | 0 | registry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml", KEY ); |
392 | 0 | this.isConfigurationDefaulted = true; |
393 | |
} |
394 | 0 | catch ( RegistryException e ) |
395 | |
{ |
396 | 0 | throw new ConfigurationRuntimeException( |
397 | |
"Fatal error: Unable to find the built-in default configuration and load it into the registry", |
398 | |
e ); |
399 | 0 | } |
400 | 0 | return registry.getSubset( KEY ); |
401 | |
} |
402 | |
|
403 | |
@SuppressWarnings("unchecked") |
404 | |
public synchronized void save( Configuration configuration ) |
405 | |
throws RegistryException, IndeterminateConfigurationException |
406 | |
{ |
407 | 0 | Registry section = registry.getSection( KEY + ".user" ); |
408 | 0 | Registry baseSection = registry.getSection( KEY + ".base" ); |
409 | 0 | if ( section == null ) |
410 | |
{ |
411 | 0 | section = baseSection; |
412 | 0 | if ( section == null ) |
413 | |
{ |
414 | 0 | section = createDefaultConfigurationFile(); |
415 | |
} |
416 | |
} |
417 | 0 | else if ( baseSection != null ) |
418 | |
{ |
419 | 0 | Collection<String> keys = baseSection.getKeys(); |
420 | 0 | boolean foundList = false; |
421 | 0 | for ( Iterator<String> i = keys.iterator(); i.hasNext() && !foundList; ) |
422 | |
{ |
423 | 0 | String key = i.next(); |
424 | |
|
425 | |
|
426 | |
|
427 | 0 | if ( key.startsWith( "repositories" ) || key.startsWith( "proxyConnectors" ) |
428 | |
|| key.startsWith( "networkProxies" ) || key.startsWith( "repositoryScanning" ) |
429 | |
|| key.startsWith( "databaseScanning" ) || key.startsWith( "remoteRepositories" ) |
430 | |
|| key.startsWith( "managedRepositories" ) || key.startsWith( "repositoryGroups" ) ) |
431 | |
{ |
432 | 0 | foundList = true; |
433 | |
} |
434 | 0 | } |
435 | |
|
436 | 0 | if ( foundList ) |
437 | |
{ |
438 | 0 | this.configuration = null; |
439 | |
|
440 | 0 | throw new IndeterminateConfigurationException( |
441 | |
"Configuration can not be saved when it is loaded from two sources" ); |
442 | |
} |
443 | |
} |
444 | |
|
445 | |
|
446 | 0 | escapeCronExpressions( configuration ); |
447 | |
|
448 | |
|
449 | 0 | if ( configuration.getManagedRepositories().isEmpty() ) |
450 | |
{ |
451 | 0 | section.removeSubset( "managedRepositories" ); |
452 | |
} |
453 | 0 | if ( configuration.getRemoteRepositories().isEmpty() ) |
454 | |
{ |
455 | 0 | section.removeSubset( "remoteRepositories" ); |
456 | |
} |
457 | 0 | if ( configuration.getProxyConnectors().isEmpty() ) |
458 | |
{ |
459 | 0 | section.removeSubset( "proxyConnectors" ); |
460 | |
} |
461 | 0 | if ( configuration.getNetworkProxies().isEmpty() ) |
462 | |
{ |
463 | 0 | section.removeSubset( "networkProxies" ); |
464 | |
} |
465 | 0 | if ( configuration.getLegacyArtifactPaths().isEmpty() ) |
466 | |
{ |
467 | 0 | section.removeSubset( "legacyArtifactPaths" ); |
468 | |
} |
469 | 0 | if ( configuration.getRepositoryGroups().isEmpty() ) |
470 | |
{ |
471 | 0 | section.removeSubset( "repositoryGroups" ); |
472 | |
} |
473 | 0 | if ( configuration.getRepositoryScanning() != null ) |
474 | |
{ |
475 | 0 | if ( configuration.getRepositoryScanning().getKnownContentConsumers().isEmpty() ) |
476 | |
{ |
477 | 0 | section.removeSubset( "repositoryScanning.knownContentConsumers" ); |
478 | |
} |
479 | 0 | if ( configuration.getRepositoryScanning().getInvalidContentConsumers().isEmpty() ) |
480 | |
{ |
481 | 0 | section.removeSubset( "repositoryScanning.invalidContentConsumers" ); |
482 | |
} |
483 | |
} |
484 | 0 | if ( configuration.getDatabaseScanning() != null ) |
485 | |
{ |
486 | 0 | if ( configuration.getDatabaseScanning().getCleanupConsumers().isEmpty() ) |
487 | |
{ |
488 | 0 | section.removeSubset( "databaseScanning.cleanupConsumers" ); |
489 | |
} |
490 | 0 | if ( configuration.getDatabaseScanning().getUnprocessedConsumers().isEmpty() ) |
491 | |
{ |
492 | 0 | section.removeSubset( "databaseScanning.unprocessedConsumers" ); |
493 | |
} |
494 | |
} |
495 | |
|
496 | 0 | new ConfigurationRegistryWriter().write( configuration, section ); |
497 | 0 | section.save(); |
498 | |
|
499 | 0 | this.configuration = unescapeExpressions( configuration ); |
500 | |
|
501 | 0 | triggerEvent( ConfigurationEvent.SAVED ); |
502 | 0 | } |
503 | |
|
504 | |
private void escapeCronExpressions( Configuration configuration ) |
505 | |
{ |
506 | 0 | for ( ManagedRepositoryConfiguration c : (List<ManagedRepositoryConfiguration>) configuration.getManagedRepositories() ) |
507 | |
{ |
508 | 0 | c.setRefreshCronExpression( escapeCronExpression( c.getRefreshCronExpression() ) ); |
509 | |
} |
510 | |
|
511 | 0 | DatabaseScanningConfiguration scanning = configuration.getDatabaseScanning(); |
512 | 0 | if ( scanning != null ) |
513 | |
{ |
514 | 0 | scanning.setCronExpression( escapeCronExpression( scanning.getCronExpression() ) ); |
515 | |
} |
516 | 0 | } |
517 | |
|
518 | |
private Registry createDefaultConfigurationFile() |
519 | |
throws RegistryException |
520 | |
{ |
521 | |
|
522 | |
|
523 | |
|
524 | 0 | String contents = "<configuration />"; |
525 | 0 | if ( !writeFile( "user configuration", userConfigFilename, contents ) ) |
526 | |
{ |
527 | 0 | if ( !writeFile( "alternative configuration", altConfigFilename, contents ) ) |
528 | |
{ |
529 | 0 | throw new RegistryException( "Unable to create configuration file in either user [" |
530 | |
+ userConfigFilename + "] or alternative [" + altConfigFilename |
531 | |
+ "] locations on disk, usually happens when not allowed to write to those locations." ); |
532 | |
} |
533 | |
} |
534 | |
|
535 | |
try |
536 | |
{ |
537 | 0 | ( (Initializable) registry ).initialize(); |
538 | |
|
539 | 0 | for ( RegistryListener regListener : registryListeners ) |
540 | |
{ |
541 | 0 | addRegistryChangeListener( regListener ); |
542 | |
} |
543 | |
} |
544 | 0 | catch ( InitializationException e ) |
545 | |
{ |
546 | 0 | throw new RegistryException( "Unable to reinitialize configuration: " + e.getMessage(), e ); |
547 | 0 | } |
548 | |
|
549 | 0 | triggerEvent( ConfigurationEvent.SAVED ); |
550 | |
|
551 | 0 | return registry.getSection( KEY + ".user" ); |
552 | |
} |
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
private boolean writeFile( String filetype, String path, String contents ) |
565 | |
{ |
566 | 0 | File file = new File( path ); |
567 | |
|
568 | |
try |
569 | |
{ |
570 | |
|
571 | 0 | if ( file.getParentFile() != null ) |
572 | |
{ |
573 | |
|
574 | 0 | if ( ( file.getParentFile().exists() == false ) || ( file.getParentFile().isDirectory() == false ) ) |
575 | |
{ |
576 | |
|
577 | 0 | return false; |
578 | |
} |
579 | |
} |
580 | |
|
581 | 0 | FileUtils.writeStringToFile( file, contents, "UTF-8" ); |
582 | 0 | return true; |
583 | |
} |
584 | 0 | catch ( IOException e ) |
585 | |
{ |
586 | 0 | log.error( "Unable to create " + filetype + " file: " + e.getMessage(), e ); |
587 | 0 | return false; |
588 | |
} |
589 | |
} |
590 | |
|
591 | |
private void triggerEvent( int type ) |
592 | |
{ |
593 | 0 | ConfigurationEvent evt = new ConfigurationEvent( type ); |
594 | 0 | for ( ConfigurationListener listener : listeners ) |
595 | |
{ |
596 | 0 | listener.configurationEvent( evt ); |
597 | |
} |
598 | 0 | } |
599 | |
|
600 | |
public void addListener( ConfigurationListener listener ) |
601 | |
{ |
602 | 0 | if ( listener == null ) |
603 | |
{ |
604 | 0 | return; |
605 | |
} |
606 | |
|
607 | 0 | listeners.add( listener ); |
608 | 0 | } |
609 | |
|
610 | |
public void removeListener( ConfigurationListener listener ) |
611 | |
{ |
612 | 0 | if ( listener == null ) |
613 | |
{ |
614 | 0 | return; |
615 | |
} |
616 | |
|
617 | 0 | listeners.remove( listener ); |
618 | 0 | } |
619 | |
|
620 | |
public void addChangeListener( RegistryListener listener ) |
621 | |
{ |
622 | 0 | addRegistryChangeListener( listener ); |
623 | |
|
624 | |
|
625 | 0 | registryListeners.add( listener ); |
626 | 0 | } |
627 | |
|
628 | |
private void addRegistryChangeListener( RegistryListener listener ) |
629 | |
{ |
630 | 0 | Registry section = registry.getSection( KEY + ".user" ); |
631 | 0 | if ( section != null ) |
632 | |
{ |
633 | 0 | section.addChangeListener( listener ); |
634 | |
} |
635 | 0 | section = registry.getSection( KEY + ".base" ); |
636 | 0 | if ( section != null ) |
637 | |
{ |
638 | 0 | section.addChangeListener( listener ); |
639 | |
} |
640 | 0 | } |
641 | |
|
642 | |
public void initialize() |
643 | |
throws InitializationException |
644 | |
{ |
645 | |
|
646 | |
try |
647 | |
{ |
648 | 0 | ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator(); |
649 | 0 | expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() ); |
650 | 0 | userConfigFilename = expressionEvaluator.expand( userConfigFilename ); |
651 | 0 | altConfigFilename = expressionEvaluator.expand( altConfigFilename ); |
652 | 0 | loadConfiguration(); |
653 | |
} |
654 | 0 | catch ( EvaluatorException e ) |
655 | |
{ |
656 | 0 | throw new InitializationException( "Unable to evaluate expressions found in " |
657 | |
+ "userConfigFilename or altConfigFilename." ); |
658 | 0 | } |
659 | 0 | registry.addChangeListener( this ); |
660 | 0 | } |
661 | |
|
662 | |
public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
663 | |
{ |
664 | |
|
665 | 0 | } |
666 | |
|
667 | |
public synchronized void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
668 | |
{ |
669 | 0 | configuration = null; |
670 | 0 | } |
671 | |
|
672 | |
private String removeExpressions( String directory ) |
673 | |
{ |
674 | 0 | String value = StringUtils.replace( directory, "${appserver.base}", registry.getString( "appserver.base", |
675 | |
"${appserver.base}" ) ); |
676 | 0 | value = StringUtils.replace( value, "${appserver.home}", registry.getString( "appserver.home", |
677 | |
"${appserver.home}" ) ); |
678 | 0 | return value; |
679 | |
} |
680 | |
|
681 | |
private String unescapeCronExpression( String cronExpression ) |
682 | |
{ |
683 | 0 | return StringUtils.replace( cronExpression, "\\,", "," ); |
684 | |
} |
685 | |
|
686 | |
private String escapeCronExpression( String cronExpression ) |
687 | |
{ |
688 | 0 | return StringUtils.replace( cronExpression, ",", "\\," ); |
689 | |
} |
690 | |
|
691 | |
private Configuration unescapeExpressions( Configuration config ) |
692 | |
{ |
693 | |
|
694 | 0 | for ( Iterator<ManagedRepositoryConfiguration> i = config.getManagedRepositories().iterator(); i.hasNext(); ) |
695 | |
{ |
696 | 0 | ManagedRepositoryConfiguration c = i.next(); |
697 | 0 | c.setLocation( removeExpressions( c.getLocation() ) ); |
698 | 0 | c.setRefreshCronExpression( unescapeCronExpression( c.getRefreshCronExpression() ) ); |
699 | 0 | } |
700 | |
|
701 | 0 | DatabaseScanningConfiguration databaseScanning = config.getDatabaseScanning(); |
702 | 0 | if ( databaseScanning != null ) |
703 | |
{ |
704 | 0 | String cron = databaseScanning.getCronExpression(); |
705 | 0 | databaseScanning.setCronExpression( unescapeCronExpression( cron ) ); |
706 | |
} |
707 | |
|
708 | 0 | return config; |
709 | |
} |
710 | |
|
711 | |
private Configuration checkRepositoryLocations( Configuration config ) |
712 | |
{ |
713 | |
|
714 | |
|
715 | 0 | for( ManagedRepositoryConfiguration repo : (List<ManagedRepositoryConfiguration>) config.getManagedRepositories() ) |
716 | |
{ |
717 | 0 | String repoPath = repo.getLocation(); |
718 | 0 | File repoLocation = new File( repoPath ); |
719 | |
|
720 | 0 | if( repoLocation.exists() && repoLocation.isDirectory() && !repoPath.endsWith( "data/repositories/" + repo.getId() ) ) |
721 | |
{ |
722 | 0 | repo.setLocation( repoPath + "/data/repositories/" + repo.getId() ); |
723 | |
} |
724 | 0 | } |
725 | |
|
726 | 0 | return config; |
727 | |
} |
728 | |
|
729 | |
public String getUserConfigFilename() |
730 | |
{ |
731 | 0 | return userConfigFilename; |
732 | |
} |
733 | |
|
734 | |
public String getAltConfigFilename() |
735 | |
{ |
736 | 0 | return altConfigFilename; |
737 | |
} |
738 | |
|
739 | |
public boolean isDefaulted() |
740 | |
{ |
741 | 0 | return this.isConfigurationDefaulted; |
742 | |
} |
743 | |
} |