View Javadoc

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.util.Date;
21  
22  import org.abstracthorizon.proximity.AccessDeniedException;
23  import org.abstracthorizon.proximity.Item;
24  import org.abstracthorizon.proximity.ProximityRequest;
25  import org.abstracthorizon.proximity.Repository;
26  import org.abstracthorizon.proximity.RepositoryNotAvailableException;
27  
28  // TODO: Auto-generated Javadoc
29  /**
30   * Simple logic with expiration support. If expirationPeriod is not -1, it will apply expiration period onto items, and
31   * will handle removal of them when they expires.
32   * 
33   * @author cstamas
34   */
35  public class DefaultExpiringProxyingRepositoryLogic
36      extends DefaultProxyingRepositoryLogic
37  {
38  
39      /** The Constant NO_EXPIRATION. */
40      public static final long NO_EXPIRATION = -1000;
41  
42      /** The Constant METADATA_EXPIRES. */
43      public static final String METADATA_EXPIRES = "item.expires";
44  
45      /** The item expiration period. */
46      private long itemExpirationPeriod = 86400 * 1000; // 24 hours
47  
48      /**
49       * Gets the item expiration period in seconds.
50       * 
51       * @return the item expiration period in seconds
52       */
53      public long getItemExpirationPeriodInSeconds()
54      {
55          return itemExpirationPeriod / 1000;
56      }
57  
58      /**
59       * Sets the item expiration period in seconds.
60       * 
61       * @param itemExpirationPeriod the new item expiration period in seconds
62       */
63      public void setItemExpirationPeriodInSeconds( long itemExpirationPeriod )
64      {
65          this.itemExpirationPeriod = itemExpirationPeriod * 1000;
66      }
67  
68      /**
69       * If item has defined EXPIRES metadata, will use it and remove item from repository if needed.
70       * 
71       * @param repository the repository
72       * @param request the request
73       * @param item the item
74       * 
75       * @return the item
76       */
77      public Item afterLocalCopyFound( Repository repository, ProximityRequest request, Item item )
78      {
79          if ( item.getProperties().getMetadata( DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES ) != null )
80          {
81              logger.debug( "Item has expiration, checking it." );
82              Date expires = new Date( Long.parseLong( item.getProperties().getMetadata(
83                  DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES ) ) );
84              if ( expires.before( new Date( System.currentTimeMillis() ) ) )
85              {
86                  logger.info( "Item has expired on " + expires + ", DELETING it." );
87                  try
88                  {
89                      repository.deleteItem( request );
90                  }
91                  catch ( RepositoryNotAvailableException ex )
92                  {
93                      logger.warn( "Repository unavailable, cannot delete expired item.", ex );
94                  }
95                  catch ( AccessDeniedException ex )
96                  {
97                      logger.warn( "Access is denied, cannot delete expired item.", ex );
98                  }
99                  return null;
100             }
101         }
102         return item;
103     }
104 
105     /**
106      * If expiration period is not NO_EXPIRATION, it will apply it on all items.
107      * 
108      * @param repository the repository
109      * @param request the request
110      * @param localItem the local item
111      * @param remoteItem the remote item
112      * 
113      * @return the item
114      */
115     public Item afterRemoteCopyFound( Repository repository, ProximityRequest request, Item localItem, Item remoteItem )
116     {
117         if ( itemExpirationPeriod != NO_EXPIRATION )
118         {
119             Date expires = new Date( System.currentTimeMillis() + itemExpirationPeriod );
120             logger.info( "Setting expires on item  to " + expires.toString() );
121             remoteItem.getProperties().setMetadata(
122                 DefaultExpiringProxyingRepositoryLogic.METADATA_EXPIRES,
123                 Long.toString( expires.getTime() ) );
124         }
125         return remoteItem;
126     }
127 
128 }