001    package org.cumulus4j.store.crypto;
002    
003    import javax.jdo.PersistenceManager;
004    
005    import org.cumulus4j.store.EncryptionCoordinateSetManager;
006    import org.cumulus4j.store.PersistenceManagerConnection;
007    import org.datanucleus.store.ExecutionContext;
008    
009    /**
010     * Context for encryption and decryption.
011     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
012     */
013    public class CryptoContext
014    {
015            /**
016             * Create a new context.
017             * @param encryptionCoordinateSetManager the <code>EncryptionCoordinateSetManager</code> to be used in this context; must not be <code>null</code>.
018             * @param executionContext the <code>ExecutionContext</code> to be used in this context; must not be <code>null</code>.
019             * @param persistenceManagerConnection the <code>PersistenceManagerConnection</code> to be used in this context; must not be <code>null</code>.
020             */
021            public CryptoContext(EncryptionCoordinateSetManager encryptionCoordinateSetManager, ExecutionContext executionContext, PersistenceManagerConnection persistenceManagerConnection)
022            {
023                    if (encryptionCoordinateSetManager == null)
024                            throw new IllegalArgumentException("encryptionCoordinateSetManager == null");
025    
026                    if (executionContext == null)
027                            throw new IllegalArgumentException("executionContext == null");
028    
029                    if (persistenceManagerConnection == null)
030                            throw new IllegalArgumentException("persistenceManagerConnection == null");
031    
032                    this.encryptionCoordinateSetManager = encryptionCoordinateSetManager;
033                    this.executionContext = executionContext;
034                    this.persistenceManagerConnection = persistenceManagerConnection;
035                    this.persistenceManagerForData = persistenceManagerConnection.getDataPM();
036                    this.persistenceManagerForIndex = persistenceManagerConnection.getIndexPM();
037            }
038    
039            private EncryptionCoordinateSetManager encryptionCoordinateSetManager;
040    
041            /**
042             * Get the <code>EncryptionCoordinateSetManager</code> to be used in this context; never <code>null</code>.
043             * @return the <code>EncryptionCoordinateSetManager</code> to be used in this context; never <code>null</code>.
044             */
045            public EncryptionCoordinateSetManager getEncryptionCoordinateSetManager() {
046                    return encryptionCoordinateSetManager;
047            }
048    
049            private ExecutionContext executionContext;
050    
051            /**
052             * Get the <code>ExecutionContext</code> to be used in this context; never <code>null</code>.
053             * @return the <code>ExecutionContext</code> to be used in this context; never <code>null</code>.
054             */
055            public ExecutionContext getExecutionContext() {
056                    return executionContext;
057            }
058    
059            private PersistenceManagerConnection persistenceManagerConnection;
060    
061            /**
062             * Get the <code>PersistenceManagerConnection</code> to be used in this context; never <code>null</code>.
063             * @return the <code>PersistenceManagerConnection</code> to be used in this context; never <code>null</code>.
064             */
065            public PersistenceManagerConnection getPersistenceManagerConnection() {
066                    return persistenceManagerConnection;
067            }
068    
069            private PersistenceManager persistenceManagerForData;
070    
071            /**
072             * Convenience method synonymous to {@link PersistenceManagerConnection#getDataPM()}.
073             * @return the PM used for the actual data.
074             */
075            public PersistenceManager getPersistenceManagerForData() {
076                    return persistenceManagerForData;
077            }
078    
079            private PersistenceManager persistenceManagerForIndex;
080    
081            /**
082             * Convenience method synonymous to {@link PersistenceManagerConnection#getIndexPM()}.
083             * @return the PM used for index data. If there is no separate index-datastore, this
084             * is the same as {@link #getPersistenceManagerForData()}.
085             */
086            public PersistenceManager getPersistenceManagerForIndex() {
087                    return persistenceManagerForIndex;
088            }
089    }