1 /*
2
3 Copyright 2005-2007 Tamas Cservenak (t.cservenak@gmail.com)
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 */
18 package org.abstracthorizon.proximity.logic;
19
20 import java.io.IOException;
21
22 import org.abstracthorizon.proximity.Item;
23 import org.abstracthorizon.proximity.ProximityRequest;
24 import org.abstracthorizon.proximity.Repository;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 // TODO: Auto-generated Javadoc
29 /**
30 * Default implementation of RepositoryLogic. It always checks for local copy, does nothing after local copy is found,
31 * forces remote retrieval if local copy is not found, does nothing after remote copy is found and always stores freshly
32 * retrieved item. Index item that are not directorties.
33 *
34 * @author cstamas
35 */
36 public class DefaultProxyingRepositoryLogic
37 implements RepositoryLogic
38 {
39
40 /** The logger. */
41 protected Logger logger = LoggerFactory.getLogger( this.getClass() );
42
43 /**
44 * Always returns true.
45 *
46 * @param repository the repository
47 * @param request the request
48 *
49 * @return true, if should check for local copy
50 */
51 public boolean shouldCheckForLocalCopy( Repository repository, ProximityRequest request )
52 {
53 return true;
54 }
55
56 /**
57 * Does nothing and returns item unmodified.
58 *
59 * @param repository the repository
60 * @param request the request
61 * @param item the item
62 *
63 * @return the item
64 */
65 public Item afterLocalCopyFound( Repository repository, ProximityRequest request, Item item )
66 {
67 return item;
68 }
69
70 /**
71 * Always returns !locallyExists.
72 *
73 * @param repository the repository
74 * @param request the request
75 * @param localItem the local item
76 *
77 * @return true, if should check for remote copy
78 */
79 public boolean shouldCheckForRemoteCopy( Repository repository, ProximityRequest request, Item localItem )
80 {
81 return localItem == null;
82 }
83
84 /**
85 * Does nothing and returns item unmodified.
86 *
87 * @param repository the repository
88 * @param request the request
89 * @param localItem the local item
90 * @param remoteItem the remote item
91 *
92 * @return the item
93 */
94 public Item afterRemoteCopyFound( Repository repository, ProximityRequest request, Item localItem, Item remoteItem )
95 {
96 return remoteItem;
97 }
98
99 /**
100 * Always returns true.
101 *
102 * @param repository the repository
103 * @param request the request
104 * @param localItem the local item
105 * @param remoteItem the remote item
106 *
107 * @return true, if should store locally after remote retrieval
108 */
109 public boolean shouldStoreLocallyAfterRemoteRetrieval( Repository repository, ProximityRequest request,
110 Item localItem, Item remoteItem )
111 {
112 return true;
113 }
114
115 /**
116 * Always give the best what we have.
117 *
118 * @param repository the repository
119 * @param request the request
120 * @param localItem the local item
121 * @param remoteItem the remote item
122 *
123 * @return the item
124 */
125 public Item afterRetrieval( Repository repository, ProximityRequest request, Item localItem, Item remoteItem )
126 {
127 if ( remoteItem != null )
128 {
129 if ( localItem != null && localItem.getStream() != null )
130 {
131 try
132 {
133 localItem.getStream().close();
134 }
135 catch ( IOException ex )
136 {
137 logger.warn( "Had a problem trying to close a file: {}", localItem.getProperties(), ex );
138 }
139 }
140 return remoteItem;
141 }
142 if ( localItem != null )
143 {
144 return localItem;
145 }
146 return null;
147 }
148
149 }