This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
AbstractRepository.java
1234 lines (1015 loc) · 39.7 KB
/
AbstractRepository.java
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Sonatype Nexus (TM) Open Source Version.
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
* Includes the third-party code listed at http://nexus.sonatype.org/dev/attributions.html
* This program is licensed to you under Version 3 only of the GNU General Public License as published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License Version 3 for more details.
* You should have received a copy of the GNU General Public License Version 3 along with this program.
* If not, see http://www.gnu.org/licenses/.
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc.
* "Sonatype" and "Sonatype Nexus" are trademarks of Sonatype, Inc.
*/
package org.sonatype.nexus.proxy.repository;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.sonatype.configuration.ConfigurationException;
import org.sonatype.nexus.configuration.Configurator;
import org.sonatype.nexus.configuration.application.ApplicationConfiguration;
import org.sonatype.nexus.configuration.model.CRepositoryExternalConfigurationHolderFactory;
import org.sonatype.nexus.feeds.FeedRecorder;
import org.sonatype.nexus.mime.MimeUtil;
import org.sonatype.nexus.proxy.AccessDeniedException;
import org.sonatype.nexus.proxy.IllegalOperationException;
import org.sonatype.nexus.proxy.IllegalRequestException;
import org.sonatype.nexus.proxy.ItemNotFoundException;
import org.sonatype.nexus.proxy.RepositoryNotAvailableException;
import org.sonatype.nexus.proxy.ResourceStoreRequest;
import org.sonatype.nexus.proxy.StorageException;
import org.sonatype.nexus.proxy.access.AccessManager;
import org.sonatype.nexus.proxy.access.Action;
import org.sonatype.nexus.proxy.cache.CacheManager;
import org.sonatype.nexus.proxy.cache.PathCache;
import org.sonatype.nexus.proxy.events.RepositoryConfigurationUpdatedEvent;
import org.sonatype.nexus.proxy.events.RepositoryEventExpireCaches;
import org.sonatype.nexus.proxy.events.RepositoryEventLocalStatusChanged;
import org.sonatype.nexus.proxy.events.RepositoryEventRecreateAttributes;
import org.sonatype.nexus.proxy.events.RepositoryItemEventDelete;
import org.sonatype.nexus.proxy.events.RepositoryItemEventRetrieve;
import org.sonatype.nexus.proxy.events.RepositoryItemEventStore;
import org.sonatype.nexus.proxy.item.AbstractStorageItem;
import org.sonatype.nexus.proxy.item.ByteArrayContentLocator;
import org.sonatype.nexus.proxy.item.ContentGenerator;
import org.sonatype.nexus.proxy.item.ContentLocator;
import org.sonatype.nexus.proxy.item.DefaultStorageCollectionItem;
import org.sonatype.nexus.proxy.item.DefaultStorageFileItem;
import org.sonatype.nexus.proxy.item.PreparedContentLocator;
import org.sonatype.nexus.proxy.item.ReadLockingContentLocator;
import org.sonatype.nexus.proxy.item.RepositoryItemUid;
import org.sonatype.nexus.proxy.item.RepositoryItemUidFactory;
import org.sonatype.nexus.proxy.item.StorageCollectionItem;
import org.sonatype.nexus.proxy.item.StorageFileItem;
import org.sonatype.nexus.proxy.item.StorageItem;
import org.sonatype.nexus.proxy.storage.UnsupportedStorageOperationException;
import org.sonatype.nexus.proxy.storage.local.LocalRepositoryStorage;
import org.sonatype.nexus.proxy.target.TargetRegistry;
import org.sonatype.nexus.proxy.target.TargetSet;
import org.sonatype.nexus.proxy.walker.DefaultWalkerContext;
import org.sonatype.nexus.proxy.walker.Walker;
import org.sonatype.nexus.proxy.walker.WalkerException;
import org.sonatype.nexus.scheduling.DefaultRepositoryTaskActivityDescriptor;
import org.sonatype.nexus.scheduling.DefaultRepositoryTaskFilter;
import org.sonatype.nexus.scheduling.RepositoryTaskFilter;
/**
* <p>
* A common base for Proximity repository. It defines all the needed properties and main methods as in
* ProximityRepository interface.
* <p>
* This abstract class handles the following functionalities:
* <ul>
* <li>Holds base properties like repo ID, group ID, rank</li>
* <li>Manages AccessManager</li>
* <li>Manages notFoundCache to speed up responses</li>
* <li>Manages event listeners</li>
* </ul>
* <p>
* The subclasses only needs to implement the abstract method focusing on item retrieaval and other "basic" functions.
*
* @author cstamas
*/
public abstract class AbstractRepository
extends ConfigurableRepository
implements Repository
{
@Requirement
private Logger logger;
@Requirement
private ApplicationConfiguration applicationConfiguration;
@Requirement
private CacheManager cacheManager;
@Requirement
private TargetRegistry targetRegistry;
@Requirement
private RepositoryItemUidFactory repositoryItemUidFactory;
@Requirement
private AccessManager accessManager;
@Requirement
private Walker walker;
@Requirement
private MimeUtil mimeUtil;
@Requirement
private FeedRecorder feedRecorder;
@Requirement( role = ContentGenerator.class )
private Map<String, ContentGenerator> contentGenerators;
/** The local storage. */
private LocalRepositoryStorage localStorage;
/** The not found cache. */
private PathCache notFoundCache;
/** Request processors list */
private Map<String, RequestProcessor> requestProcessors;
/** if local url changed, need special handling after save */
private boolean localUrlChanged = false;
/** if non-indexable -> indexable change occured, need special handling after save */
private boolean madeSearchable = false;
// --
protected Logger getLogger()
{
return logger;
}
protected MimeUtil getMimeUtil()
{
return mimeUtil;
}
protected FeedRecorder getFeedRecorder()
{
return feedRecorder;
}
// ==
@Override
protected abstract Configurator getConfigurator();
@Override
protected abstract CRepositoryExternalConfigurationHolderFactory<?> getExternalConfigurationHolderFactory();
@Override
public boolean commitChanges()
throws ConfigurationException
{
boolean wasDirty = super.commitChanges();
if ( wasDirty )
{
getApplicationEventMulticaster().notifyEventListeners( getRepositoryConfigurationUpdatedEvent() );
}
this.localUrlChanged = false;
this.madeSearchable = false;
return wasDirty;
}
@Override
public boolean rollbackChanges()
{
this.localUrlChanged = false;
this.madeSearchable = false;
return super.rollbackChanges();
}
@Override
protected ApplicationConfiguration getApplicationConfiguration()
{
return applicationConfiguration;
}
protected RepositoryConfigurationUpdatedEvent getRepositoryConfigurationUpdatedEvent()
{
RepositoryConfigurationUpdatedEvent event = new RepositoryConfigurationUpdatedEvent( this );
event.setLocalUrlChanged( this.localUrlChanged );
event.setMadeSearchable( this.madeSearchable );
return event;
}
protected AbstractRepositoryConfiguration getExternalConfiguration( boolean forModification )
{
return (AbstractRepositoryConfiguration) getCurrentCoreConfiguration().getExternalConfiguration()
.getConfiguration( forModification );
}
// ==
public RepositoryTaskFilter getRepositoryTaskFilter()
{
// we are allowing all, and subclasses will filter as they want
return new DefaultRepositoryTaskFilter().setAllowsRepositoryScanning( true ).setAllowsScheduledTasks( true )
.setAllowsUserInitiatedTasks( true ).setContentOperators(
DefaultRepositoryTaskActivityDescriptor.ALL_CONTENT_OPERATIONS ).setAttributeOperators(
DefaultRepositoryTaskActivityDescriptor.ALL_ATTRIBUTES_OPERATIONS );
}
public Map<String, RequestProcessor> getRequestProcessors()
{
if ( requestProcessors == null )
{
requestProcessors = new HashMap<String, RequestProcessor>();
}
return requestProcessors;
}
/**
* Gets the cache manager.
*
* @return the cache manager
*/
protected CacheManager getCacheManager()
{
return cacheManager;
}
/**
* Sets the cache manager.
*
* @param cacheManager the new cache manager
*/
protected void setCacheManager( CacheManager cacheManager )
{
this.cacheManager = cacheManager;
}
/**
* Returns the repository Item Uid Factory.
*
* @return
*/
protected RepositoryItemUidFactory getRepositoryItemUidFactory()
{
return repositoryItemUidFactory;
}
/**
* Gets the not found cache.
*
* @return the not found cache
*/
public PathCache getNotFoundCache()
{
if ( notFoundCache == null )
{
// getting it lazily
notFoundCache = getCacheManager().getPathCache( getId() );
}
return notFoundCache;
}
/**
* Sets the not found cache.
*
* @param notFoundcache the new not found cache
*/
public void setNotFoundCache( PathCache notFoundcache )
{
this.notFoundCache = notFoundcache;
}
@Override
public void setIndexable( boolean indexable )
{
if ( !isIndexable() && indexable )
{
// we have a non-indexable -> indexable transition
madeSearchable = true;
}
super.setIndexable( indexable );
}
@Override
public void setLocalUrl( String localUrl )
throws StorageException
{
String newLocalUrl = null;
if ( localUrl != null )
{
newLocalUrl = localUrl.trim();
}
if ( newLocalUrl != null )
{
if ( newLocalUrl.endsWith( RepositoryItemUid.PATH_SEPARATOR ) )
{
newLocalUrl = newLocalUrl.substring( 0, newLocalUrl.length() - 1 );
}
getLocalStorage().validateStorageUrl( newLocalUrl );
}
// Dont use getLocalUrl since that applies default
if ( getCurrentConfiguration( false ).getLocalStorage() != null
&& !StringUtils.equals( newLocalUrl, getCurrentConfiguration( false ).getLocalStorage().getUrl() ) )
{
this.localUrlChanged = true;
}
super.setLocalUrl( localUrl );
}
@Override
public void setLocalStatus( LocalStatus localStatus )
{
if ( !localStatus.equals( getLocalStatus() ) )
{
LocalStatus oldLocalStatus = getLocalStatus();
super.setLocalStatus( localStatus );
getApplicationEventMulticaster().notifyEventListeners(
new RepositoryEventLocalStatusChanged( this, oldLocalStatus, localStatus ) );
}
}
@SuppressWarnings( "unchecked" )
public <F> F adaptToFacet( Class<F> t )
{
if ( getRepositoryKind().isFacetAvailable( t ) )
{
return (F) this;
}
else
{
return null;
}
}
protected Walker getWalker()
{
return walker;
}
protected Map<String, ContentGenerator> getContentGenerators()
{
return contentGenerators;
}
// ===================================================================================
// Repository iface
public AccessManager getAccessManager()
{
return accessManager;
}
public void setAccessManager( AccessManager accessManager )
{
this.accessManager = accessManager;
}
public void expireCaches( ResourceStoreRequest request )
{
if ( StringUtils.isEmpty( request.getRequestPath() ) )
{
request.setRequestPath( RepositoryItemUid.PATH_ROOT );
}
request.setRequestLocalOnly( true );
getLogger().info(
"Expiring local cache in repository ID='" + getId() + "' from path='" + request.getRequestPath() + "'" );
// 1st, expire all the files below path
DefaultWalkerContext ctx = new DefaultWalkerContext( this, request );
ctx.getProcessors().add( new ExpireCacheWalker( this ) );
try
{
getWalker().walk( ctx );
}
catch ( WalkerException e )
{
if ( !( e.getWalkerContext().getStopCause() instanceof ItemNotFoundException ) )
{
// everything that is not ItemNotFound should be reported,
// otherwise just neglect it
throw e;
}
}
// 2nd, remove the items from NFC
expireNotFoundCaches( request );
}
public void expireNotFoundCaches( ResourceStoreRequest request )
{
if ( StringUtils.isBlank( request.getRequestPath() ) )
{
request.setRequestPath( RepositoryItemUid.PATH_ROOT );
}
getLogger().info(
"Clearing NFC cache in repository ID='" + getId() + "' from path='" + request.getRequestPath() + "'" );
// remove the items from NFC
if ( RepositoryItemUid.PATH_ROOT.equals( request.getRequestPath() ) )
{
// purge all
if ( getNotFoundCache() != null )
{
getNotFoundCache().purge();
}
}
else
{
// purge below and above path only
if ( getNotFoundCache() != null )
{
getNotFoundCache().removeWithParents( request.getRequestPath() );
getNotFoundCache().removeWithChildren( request.getRequestPath() );
}
}
getApplicationEventMulticaster().notifyEventListeners(
new RepositoryEventExpireCaches( this, request.getRequestPath() ) );
}
public Collection<String> evictUnusedItems( ResourceStoreRequest request, final long timestamp )
{
// this is noop at this "level"
return Collections.emptyList();
}
public boolean recreateAttributes( ResourceStoreRequest request, final Map<String, String> initialData )
{
if ( StringUtils.isEmpty( request.getRequestPath() ) )
{
request.setRequestPath( RepositoryItemUid.PATH_ROOT );
}
getLogger().info(
"Rebuilding attributes in repository ID='" + getId() + "' from path='" + request.getRequestPath() + "'" );
RecreateAttributesWalker walkerProcessor = new RecreateAttributesWalker( this, initialData );
DefaultWalkerContext ctx = new DefaultWalkerContext( this, request );
ctx.getProcessors().add( walkerProcessor );
// let it loose
try
{
getWalker().walk( ctx );
}
catch ( WalkerException e )
{
if ( !( e.getWalkerContext().getStopCause() instanceof ItemNotFoundException ) )
{
// everything that is not ItemNotFound should be reported,
// otherwise just neglect it
throw e;
}
}
getApplicationEventMulticaster().notifyEventListeners( new RepositoryEventRecreateAttributes( this ) );
return true;
}
public LocalRepositoryStorage getLocalStorage()
{
return localStorage;
}
public void setLocalStorage( LocalRepositoryStorage localStorage )
{
getCurrentConfiguration( true ).getLocalStorage().setProvider( localStorage.getProviderId() );
this.localStorage = localStorage;
}
// ===================================================================================
// Store iface
public StorageItem retrieveItem( ResourceStoreRequest request )
throws IllegalOperationException, ItemNotFoundException, StorageException, AccessDeniedException
{
if ( !checkConditions( request, Action.read ) )
{
throw new ItemNotFoundException( request, this );
}
StorageItem item = retrieveItem( false, request );
if ( StorageCollectionItem.class.isAssignableFrom( item.getClass() ) && !isBrowseable() )
{
getLogger().debug(
getId() + " retrieveItem() :: FOUND a collection on " + request.toString()
+ " but repository is not Browseable." );
throw new ItemNotFoundException( request, this );
}
return item;
}
public void copyItem( ResourceStoreRequest from, ResourceStoreRequest to )
throws UnsupportedStorageOperationException, IllegalOperationException, ItemNotFoundException,
StorageException, AccessDeniedException
{
if ( !checkConditions( from, Action.read ) )
{
throw new IllegalRequestException( from, "copyItem: Operation does not fills needed requirements!" );
}
if ( !checkConditions( to, getResultingActionOnWrite( to ) ) )
{
throw new IllegalRequestException( to, "copyItem: Operation does not fills needed requirements!" );
}
copyItem( false, from, to );
}
public void moveItem( ResourceStoreRequest from, ResourceStoreRequest to )
throws UnsupportedStorageOperationException, IllegalOperationException, ItemNotFoundException,
StorageException, AccessDeniedException
{
if ( !checkConditions( from, Action.read ) )
{
throw new AccessDeniedException( from, "Operation does not fills needed requirements!" );
}
if ( !checkConditions( from, Action.delete ) )
{
throw new AccessDeniedException( from, "Operation does not fills needed requirements!" );
}
if ( !checkConditions( to, getResultingActionOnWrite( to ) ) )
{
throw new AccessDeniedException( to, "Operation does not fills needed requirements!" );
}
moveItem( false, from, to );
}
public void deleteItem( ResourceStoreRequest request )
throws UnsupportedStorageOperationException, IllegalOperationException, ItemNotFoundException,
StorageException, AccessDeniedException
{
if ( !checkConditions( request, Action.delete ) )
{
throw new AccessDeniedException( request, "Operation does not fills needed requirements!" );
}
deleteItem( false, request );
}
public void storeItem( ResourceStoreRequest request, InputStream is, Map<String, String> userAttributes )
throws UnsupportedStorageOperationException, IllegalOperationException, StorageException, AccessDeniedException
{
if ( !checkConditions( request, getResultingActionOnWrite( request ) ) )
{
throw new AccessDeniedException( request, "Operation does not fills needed requirements!" );
}
DefaultStorageFileItem fItem =
new DefaultStorageFileItem( this, request, true, true, new PreparedContentLocator( is, getMimeUtil()
.getMimeType( request.getRequestPath() ) ) );
if ( userAttributes != null )
{
fItem.getAttributes().putAll( userAttributes );
}
storeItem( false, fItem );
}
public void createCollection( ResourceStoreRequest request, Map<String, String> userAttributes )
throws UnsupportedStorageOperationException, IllegalOperationException, StorageException, AccessDeniedException
{
if ( !checkConditions( request, getResultingActionOnWrite( request ) ) )
{
throw new AccessDeniedException( request, "Operation does not fills needed requirements!" );
}
DefaultStorageCollectionItem coll = new DefaultStorageCollectionItem( this, request, true, true );
if ( userAttributes != null )
{
coll.getAttributes().putAll( userAttributes );
}
storeItem( false, coll );
}
public Collection<StorageItem> list( ResourceStoreRequest request )
throws IllegalOperationException, ItemNotFoundException, StorageException, AccessDeniedException
{
if ( !checkConditions( request, Action.read ) )
{
throw new ItemNotFoundException( request, this );
}
Collection<StorageItem> items = null;
if ( isBrowseable() )
{
items = list( false, request );
}
else
{
throw new ItemNotFoundException( request, this );
}
return items;
}
public TargetSet getTargetsForRequest( ResourceStoreRequest request )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "getTargetsForRequest() :: " + this.getId() + ":" + request.getRequestPath() );
}
return targetRegistry.getTargetsForRepositoryPath( this, request.getRequestPath() );
}
public boolean hasAnyTargetsForRequest( ResourceStoreRequest request )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "hasAnyTargetsForRequest() :: " + this.getId() );
}
return targetRegistry.hasAnyApplicableTarget( this );
}
public Action getResultingActionOnWrite( ResourceStoreRequest rsr )
{
try
{
boolean isInLocalStorage = getLocalStorage().containsItem( this, rsr );
if ( isInLocalStorage )
{
return Action.update;
}
else
{
return Action.create;
}
}
catch ( StorageException e )
{
getLogger().error(
"Could not resolve the local presence of \"" + rsr.getRequestPath() + "\" path in repository ID=\""
+ getId() + "\"!", e );
return null;
}
}
// ===================================================================================
// Repositry store-like
public StorageItem retrieveItem( boolean fromTask, ResourceStoreRequest request )
throws IllegalOperationException, ItemNotFoundException, StorageException
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + ".retrieveItem() :: " + request.toString() );
}
if ( !getLocalStatus().shouldServiceRequest() )
{
throw new RepositoryNotAvailableException( this );
}
request.addProcessedRepository( this );
maintainNotFoundCache( request );
RepositoryItemUid uid = createUid( request.getRequestPath() );
uid.lock( Action.read );
try
{
StorageItem item = doRetrieveItem( request );
// file with generated content?
if ( item instanceof StorageFileItem
&& item.getAttributes().containsKey( ContentGenerator.CONTENT_GENERATOR_ID ) )
{
StorageFileItem file = (StorageFileItem) item;
String key = file.getAttributes().get( ContentGenerator.CONTENT_GENERATOR_ID );
if ( getContentGenerators().containsKey( key ) )
{
ContentGenerator generator = getContentGenerators().get( key );
try
{
file.setContentLocator( generator.generateContent( this, uid.getPath(), file ) );
}
catch ( Exception e )
{
throw new StorageException( "Could not generate content:", e );
}
}
else
{
getLogger().info(
"The file in repository ID='" + this.getId() + "' on path='" + uid.getPath()
+ "' should be generated by ContentGeneratorId='" + key + "', but it does not exists!" );
throw new ItemNotFoundException( request, this );
}
}
// plain file? wrap it
else if ( item instanceof StorageFileItem )
{
StorageFileItem file = (StorageFileItem) item;
// wrap the content locator if needed
if ( !( file.getContentLocator() instanceof ReadLockingContentLocator ) )
{
file.setContentLocator( new ReadLockingContentLocator( uid, file.getContentLocator() ) );
}
}
getApplicationEventMulticaster().notifyEventListeners( new RepositoryItemEventRetrieve( this, item ) );
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + " retrieveItem() :: FOUND " + uid.toString() );
}
return item;
}
catch ( ItemNotFoundException ex )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + " retrieveItem() :: NOT FOUND " + uid.toString() );
}
// if not local/remote only, add it to NFC
if ( !request.isRequestLocalOnly() && !request.isRequestRemoteOnly() )
{
addToNotFoundCache( request );
}
throw ex;
}
finally
{
uid.unlock();
}
}
public void copyItem( boolean fromTask, ResourceStoreRequest from, ResourceStoreRequest to )
throws UnsupportedStorageOperationException, IllegalOperationException, ItemNotFoundException, StorageException
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + ".copyItem() :: " + from.toString() + " --> " + to.toString() );
}
if ( !getLocalStatus().shouldServiceRequest() )
{
throw new RepositoryNotAvailableException( this );
}
maintainNotFoundCache( from );
RepositoryItemUid fromUid = createUid( from.getRequestPath() );
RepositoryItemUid toUid = createUid( to.getRequestPath() );
fromUid.lock( Action.read );
toUid.lock( getResultingActionOnWrite( to ) );
try
{
StorageItem item = retrieveItem( fromTask, from );
if ( StorageFileItem.class.isAssignableFrom( item.getClass() ) )
{
try
{
DefaultStorageFileItem target =
new DefaultStorageFileItem( this, to, true, true, new PreparedContentLocator(
( (StorageFileItem) item ).getInputStream(), ( (StorageFileItem) item ).getMimeType() ) );
target.getItemContext().putAll( item.getItemContext() );
storeItem( fromTask, target );
// remove the "to" item from n-cache if there
removeFromNotFoundCache( to );
}
catch ( IOException e )
{
throw new StorageException( "Could not get the content of source file (is it file?)!", e );
}
}
}
finally
{
toUid.unlock();
fromUid.unlock();
}
}
public void moveItem( boolean fromTask, ResourceStoreRequest from, ResourceStoreRequest to )
throws UnsupportedStorageOperationException, IllegalOperationException, ItemNotFoundException, StorageException
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + ".moveItem() :: " + from.toString() + " --> " + to.toString() );
}
if ( !getLocalStatus().shouldServiceRequest() )
{
throw new RepositoryNotAvailableException( this );
}
copyItem( fromTask, from, to );
deleteItem( fromTask, from );
}
public void deleteItem( boolean fromTask, ResourceStoreRequest request )
throws UnsupportedStorageOperationException, IllegalOperationException, ItemNotFoundException, StorageException
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + ".deleteItem() :: " + request.toString() );
}
if ( !getLocalStatus().shouldServiceRequest() )
{
throw new RepositoryNotAvailableException( this );
}
maintainNotFoundCache( request );
RepositoryItemUid uid = createUid( request.getRequestPath() );
uid.lock( Action.delete );
try
{
// determine is the thing to be deleted a collection or not
StorageItem item = getLocalStorage().retrieveItem( this, request );
// fire the event for file being deleted
getApplicationEventMulticaster().notifyEventListeners( new RepositoryItemEventDelete( this, item ) );
if ( StorageCollectionItem.class.isAssignableFrom( item.getClass() ) )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug(
"We are deleting a collection, starting a walker to send delete notifications per-file." );
}
// it is collection, walk it and below and fire events for all files
DeletionNotifierWalker dnw = new DeletionNotifierWalker( getApplicationEventMulticaster(), request );
DefaultWalkerContext ctx = new DefaultWalkerContext( this, request );
ctx.getProcessors().add( dnw );
try
{
getWalker().walk( ctx );
}
catch ( WalkerException e )
{
if ( !( e.getWalkerContext().getStopCause() instanceof ItemNotFoundException ) )
{
// everything that is not ItemNotFound should be reported,
// otherwise just neglect it
throw e;
}
}
}
doDeleteItem( request );
}
finally
{
uid.unlock();
}
}
public void storeItem( boolean fromTask, StorageItem item )
throws UnsupportedStorageOperationException, IllegalOperationException, StorageException
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + ".storeItem() :: " + item.getRepositoryItemUid().toString() );
}
if ( !getLocalStatus().shouldServiceRequest() )
{
throw new RepositoryNotAvailableException( this );
}
RepositoryItemUid uid = createUid( item.getPath() );
// replace UID to own one
item.setRepositoryItemUid( uid );
uid.lock( getResultingActionOnWrite( new ResourceStoreRequest( item ) ) );
try
{
// store it
getLocalStorage().storeItem( this, item );
}
finally
{
uid.unlock();
}
// remove the "request" item from n-cache if there
removeFromNotFoundCache( item.getResourceStoreRequest() );
getApplicationEventMulticaster().notifyEventListeners( new RepositoryItemEventStore( this, item ) );
}
public Collection<StorageItem> list( boolean fromTask, ResourceStoreRequest request )
throws IllegalOperationException, ItemNotFoundException, StorageException
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + ".list() :: " + request.toString() );
}
if ( !getLocalStatus().shouldServiceRequest() )
{
throw new RepositoryNotAvailableException( this );
}
request.addProcessedRepository( this );
StorageItem item = retrieveItem( fromTask, request );
if ( item instanceof StorageCollectionItem )
{
return list( fromTask, (StorageCollectionItem) item );
}
else
{
throw new ItemNotFoundException( request, this );
}
}
public Collection<StorageItem> list( boolean fromTask, StorageCollectionItem coll )
throws IllegalOperationException, ItemNotFoundException, StorageException
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( getId() + ".list() :: " + coll.getRepositoryItemUid().toString() );
}
if ( !getLocalStatus().shouldServiceRequest() )
{
throw new RepositoryNotAvailableException( this );
}