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.maven;
19
20 import java.util.Date;
21
22 import org.abstracthorizon.proximity.Item;
23 import org.abstracthorizon.proximity.ItemProperties;
24 import org.abstracthorizon.proximity.ProximityRequest;
25 import org.abstracthorizon.proximity.Repository;
26 import org.abstracthorizon.proximity.logic.DefaultExpiringProxyingRepositoryLogic;
27
28 // TODO: Auto-generated Javadoc
29 /**
30 * Maven 1 and 2 aware proxy logic. It is configurable about expiring time for SNAPSHOTs, POMs and METADATAs.
31 *
32 * @author cstamas
33 */
34 public class MavenProxyRepositoryLogic
35 extends DefaultExpiringProxyingRepositoryLogic
36 {
37
38 /** Expiration period of SNAPSHOT artifacts in mseconds or 0 to refetch. Put NO_EXPIRATION to disable this feature. always. */
39 private long snapshotExpirationPeriod = 86400 * 1000; // 24 hours
40
41 /** Expiration period of METADATA artifacts in mseconds or 0 to refetch. Put NO_EXPIRATION to disable this feature. always. */
42 private long metadataExpirationPeriod = 86400 * 1000; // 24 hours
43
44 /** Expiration period of POM artifacts in mseconds or 0 to refetch always. Put NO_EXPIRATION to disable this feature. */
45 private long pomExpirationPeriod = 86400 * 1000; // 24 hours
46
47 /** Should repository driven by this logic serve snapshots?. */
48 private boolean shouldServeSnapshots = false;
49
50 /** Should repository driven by this logic serve releases?. */
51 private boolean shouldServeReleases = true;
52
53 /**
54 * Checks if is should serve releases.
55 *
56 * @return true, if is should serve releases
57 */
58 public boolean isShouldServeReleases()
59 {
60 return shouldServeReleases;
61 }
62
63 /**
64 * Sets the should serve releases.
65 *
66 * @param shouldServeReleases the new should serve releases
67 */
68 public void setShouldServeReleases( boolean shouldServeReleases )
69 {
70 this.shouldServeReleases = shouldServeReleases;
71 }
72
73 /**
74 * Checks if is should serve snapshots.
75 *
76 * @return true, if is should serve snapshots
77 */
78 public boolean isShouldServeSnapshots()
79 {
80 return shouldServeSnapshots;
81 }
82
83 /**
84 * Sets the should serve snapshots.
85 *
86 * @param shouldServeSnapshots the new should serve snapshots
87 */
88 public void setShouldServeSnapshots( boolean shouldServeSnapshots )
89 {
90 this.shouldServeSnapshots = shouldServeSnapshots;
91 }
92
93 /**
94 * Gets the metadata expiration period in seconds.
95 *
96 * @return the metadata expiration period in seconds
97 */
98 public long getMetadataExpirationPeriodInSeconds()
99 {
100 return metadataExpirationPeriod / 1000;
101 }
102
103 /**
104 * Sets the metadata expiration period in seconds.
105 *
106 * @param metadataExpirationPeriod the new metadata expiration period in seconds
107 */
108 public void setMetadataExpirationPeriodInSeconds( long metadataExpirationPeriod )
109 {
110 this.metadataExpirationPeriod = metadataExpirationPeriod * 1000;
111 }
112
113 /**
114 * Gets the pom expiration period in seconds.
115 *
116 * @return the pom expiration period in seconds
117 */
118 public long getPomExpirationPeriodInSeconds()
119 {
120 return pomExpirationPeriod / 1000;
121 }
122
123 /**
124 * Sets the pom expiration period in seconds.
125 *
126 * @param pomExpirationPeriod the new pom expiration period in seconds
127 */
128 public void setPomExpirationPeriodInSeconds( long pomExpirationPeriod )
129 {
130 this.pomExpirationPeriod = pomExpirationPeriod * 1000;
131 }
132
133 /**
134 * Gets the snapshot expiration period in seconds.
135 *
136 * @return the snapshot expiration period in seconds
137 */
138 public long getSnapshotExpirationPeriodInSeconds()
139 {
140 return snapshotExpirationPeriod / 1000;
141 }
142
143 /**
144 * Sets the snapshot expiration period in seconds.
145 *
146 * @param snapshotExpirationPeriod the new snapshot expiration period in seconds
147 */
148 public void setSnapshotExpirationPeriodInSeconds( long snapshotExpirationPeriod )
149 {
150 this.snapshotExpirationPeriod = snapshotExpirationPeriod * 1000;
151 }
152
153 // =========================================================================
154 // Logic iface
155
156 /* (non-Javadoc)
157 * @see org.abstracthorizon.proximity.logic.DefaultExpiringProxyingRepositoryLogic#afterLocalCopyFound(org.abstracthorizon.proximity.Repository, org.abstracthorizon.proximity.ProximityRequest, org.abstracthorizon.proximity.Item)
158 */
159 public Item afterLocalCopyFound( Repository repository, ProximityRequest request, Item item )
160 {
161 // override super, should not delete even if expired!
162 if ( shouldServeByPolicies( item.getProperties() ) )
163 {
164 return item;
165 }
166 else
167 {
168 logger.info( "Logic vetoed the [{}] item local retrieval due to repo policies!", request.getPath() );
169 return null;
170 }
171 }
172
173 /* (non-Javadoc)
174 * @see org.abstracthorizon.proximity.logic.DefaultProxyingRepositoryLogic#shouldCheckForRemoteCopy(org.abstracthorizon.proximity.Repository, org.abstracthorizon.proximity.ProximityRequest, org.abstracthorizon.proximity.Item)
175 */
176 public boolean shouldCheckForRemoteCopy( Repository repository, ProximityRequest request, Item localItem )
177 {
178 if ( localItem != null )
179 {
180 if ( localItem.getProperties().getMetadata( DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES ) != null )
181 {
182 logger.debug( "Item has expiration, checking it." );
183 Date expires = new Date( Long.parseLong( localItem.getProperties().getMetadata(
184 DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES ) ) );
185 if ( expires.before( new Date( System.currentTimeMillis() ) ) )
186 {
187 // expired
188 // forcing remote retrieval, which will replace the item
189 // locally, but do not delete
190 logger.info( "Item {} has expired on {}, trying to refetch it.", request.getPath(), expires );
191 // Fix
192 // fix for issue #104: Problem with item expiration in class MavenProxyRepositoryLogic
193 return true;
194 // was:
195 // return super.shouldCheckForRemoteCopy(repository, request, localItem);
196 }
197 else
198 {
199 // has expiration but not expired
200 return false;
201 }
202 }
203 else
204 {
205 // we have it locally and have no expiration
206 return false;
207 }
208 }
209 else
210 {
211 // we have no local item
212 return super.shouldCheckForRemoteCopy( repository, request, localItem );
213 }
214 }
215
216 /* (non-Javadoc)
217 * @see org.abstracthorizon.proximity.logic.DefaultExpiringProxyingRepositoryLogic#afterRemoteCopyFound(org.abstracthorizon.proximity.Repository, org.abstracthorizon.proximity.ProximityRequest, org.abstracthorizon.proximity.Item, org.abstracthorizon.proximity.Item)
218 */
219 public Item afterRemoteCopyFound( Repository repository, ProximityRequest request, Item localItem, Item remoteItem )
220 {
221
222 if ( MavenArtifactRecognizer.isSnapshot( remoteItem.getProperties().getDirectoryPath(), remoteItem
223 .getProperties().getName() ) )
224 {
225 if ( snapshotExpirationPeriod != NO_EXPIRATION )
226 {
227 logger.debug( "Item is Maven 1/2 Snapshot, setting expires on it to " + snapshotExpirationPeriod / 1000
228 + " seconds." );
229 remoteItem.getProperties().setMetadata(
230 DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES,
231 Long.toString( System.currentTimeMillis() + snapshotExpirationPeriod ) );
232 }
233
234 }
235 else if ( MavenArtifactRecognizer.isPom( remoteItem.getProperties().getName() ) )
236 {
237
238 if ( pomExpirationPeriod != NO_EXPIRATION )
239 {
240 logger.debug( "Item is Maven 2 POM, setting expires on it to " + ( pomExpirationPeriod / 1000 )
241 + " seconds." );
242 remoteItem.getProperties().setMetadata(
243 DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES,
244 Long.toString( System.currentTimeMillis() + pomExpirationPeriod ) );
245 }
246
247 }
248 else if ( MavenArtifactRecognizer.isMetadata( remoteItem.getProperties().getName() ) )
249 {
250
251 if ( metadataExpirationPeriod != NO_EXPIRATION )
252 {
253 logger.debug( "Item is Maven 2 Metadata, setting expires on it to " + metadataExpirationPeriod / 1000
254 + " seconds." );
255 remoteItem.getProperties().setMetadata(
256 DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES,
257 Long.toString( System.currentTimeMillis() + metadataExpirationPeriod ) );
258 }
259
260 }
261 else
262 {
263
264 remoteItem = super.afterRemoteCopyFound( repository, request, localItem, remoteItem );
265 }
266
267 if ( shouldServeByPolicies( remoteItem.getProperties() ) )
268 {
269 return remoteItem;
270 }
271 else
272 {
273 logger.info( "Logic vetoed the [{}] item remote retrieval due to repo policies!", request.getPath() );
274 return null;
275 }
276 }
277
278 // =========================================================================
279 // Logic iface
280
281 /**
282 * Simply apply the policies.
283 *
284 * @param item the item
285 *
286 * @return true, if should serve by policies
287 */
288 protected boolean shouldServeByPolicies( ItemProperties item )
289 {
290 if ( MavenArtifactRecognizer.isMetadata( item.getName() ) )
291 {
292 // metadatas goes always
293 return true;
294 }
295 if ( MavenArtifactRecognizer.isSnapshot( item.getDirectoryPath(), item.getName() ) )
296 {
297 // snapshots goes if enabled
298 return isShouldServeSnapshots();
299 }
300
301 // in any other case take it as release
302 // TODO: review this, WHAT is a "release"?
303 return item.isDirectory() || isShouldServeReleases();
304 }
305
306 }