001    package org.cumulus4j.store.datastoreversion;
002    
003    import java.util.Date;
004    import java.util.Properties;
005    
006    import javax.jdo.PersistenceManager;
007    
008    import org.cumulus4j.store.Cumulus4jStoreManager;
009    import org.cumulus4j.store.WorkInProgressException;
010    import org.cumulus4j.store.crypto.CryptoContext;
011    import org.cumulus4j.store.model.DatastoreVersion;
012    import org.slf4j.Logger;
013    import org.slf4j.LoggerFactory;
014    
015    public class CommandApplyParam
016    {
017            private static final Logger logger = LoggerFactory.getLogger(CommandApplyParam.class);
018            public static final String PROPERTY_KEY_TIMEOUT = "cumulus4j.DatastoreVersionCommand.applyWorkInProgressTimeout";
019    
020            private Date applyStartTimestamp = new Date();
021            private Cumulus4jStoreManager storeManager;
022            private CryptoContext cryptoContext;
023            private PersistenceManager persistenceManager;
024            private DatastoreVersion datastoreVersion;
025    //      private Map<String, DatastoreVersion> datastoreVersionID2DatastoreVersionMap;
026            private Properties workInProgressStateProperties;
027    
028            /**
029             *
030             * @param storeManager TODO
031             * @param cryptoContext the context; must not be <code>null</code>.
032             * @param persistenceManager the persistence-manager; must not be <code>null</code>.
033             * @param datastoreVersion the current datastore-version (representing the last execution of the same command
034             * as currently being applied). Always <code>null</code>, if the command is final. Only not <code>null</code>, if
035             * the command is not final and was already applied in an earlier version.
036             * @param workInProgressStateProperties TODO
037             */
038            public CommandApplyParam(
039                            Cumulus4jStoreManager storeManager, CryptoContext cryptoContext, PersistenceManager persistenceManager,
040                            DatastoreVersion datastoreVersion,
041                            //                      Map<String, DatastoreVersion> datastoreVersionID2DatastoreVersionMap,
042                            Properties workInProgressStateProperties
043            )
044            {
045                    if (storeManager == null)
046                            throw new IllegalArgumentException("storeManager == null");
047    
048                    if (cryptoContext == null)
049                            throw new IllegalArgumentException("cryptoContext == null");
050    
051                    if (persistenceManager == null)
052                            throw new IllegalArgumentException("persistenceManager == null");
053    
054    //              if (datastoreVersionID2DatastoreVersionMap == null)
055    //                      throw new IllegalArgumentException("datastoreVersionID2DatastoreVersionMap == null");
056    
057                    if (workInProgressStateProperties == null)
058                            throw new IllegalArgumentException("workInProgressStateProperties == null");
059    
060                    this.storeManager = storeManager;
061                    this.cryptoContext = cryptoContext;
062                    this.persistenceManager = persistenceManager;
063                    this.datastoreVersion = datastoreVersion;
064    //              this.datastoreVersionID2DatastoreVersionMap = datastoreVersionID2DatastoreVersionMap;
065                    this.workInProgressStateProperties = workInProgressStateProperties;
066            }
067    
068            public Cumulus4jStoreManager getStoreManager() {
069                    return storeManager;
070            }
071    
072            public CryptoContext getCryptoContext() {
073                    return cryptoContext;
074            }
075    
076            public PersistenceManager getPersistenceManager() {
077                    return persistenceManager;
078            }
079    
080            public DatastoreVersion getDatastoreVersion() {
081                    return datastoreVersion;
082            }
083    
084    //      public Map<String, DatastoreVersion> getDatastoreVersionID2DatastoreVersionMap() {
085    //              return datastoreVersionID2DatastoreVersionMap;
086    //      }
087    
088            public Properties getWorkInProgressStateProperties() {
089                    return workInProgressStateProperties;
090            }
091    
092            /**
093             * Get the time in milliseconds after which a {@link WorkInProgressException} should be thrown by the
094             * {@link DatastoreVersionCommand}.
095             * @return the timeout in milliseconds.
096             */
097            public long getDatastoreVersionCommandApplyWorkInProgressTimeout() {
098                    long result = 10L * 1000L;
099                    Object propertyValue = storeManager.getProperty(PROPERTY_KEY_TIMEOUT);
100                    if (propertyValue == null)
101                            return result;
102    
103                    long val;
104                    try {
105                            val = Long.parseLong(propertyValue.toString());
106                    } catch (NumberFormatException x) {
107                            logger.warn(String.format("Value for property '%s' is not a valid number!", PROPERTY_KEY_TIMEOUT), x);
108                            return result;
109                    }
110    
111                    if (val <= 0) {
112                            logger.warn("Value for property '{}' is zero or negative, but must be positive!", PROPERTY_KEY_TIMEOUT);
113                            return result;
114                    }
115    
116                    result = val;
117                    return result;
118            }
119    
120            public Date getApplyStartTimestamp() {
121                    return applyStartTimestamp;
122            }
123    
124            public boolean isDatastoreVersionCommandApplyWorkInProgressTimeoutExceeded() {
125                    return System.currentTimeMillis() - applyStartTimestamp.getTime() > getDatastoreVersionCommandApplyWorkInProgressTimeout();
126            }
127    }