001: /*
002:
003: Derby - Class org.apache.derby.impl.store.raw.data.EncryptContainerOperation
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.store.raw.data;
023:
024: import org.apache.derby.iapi.services.io.StoredFormatIds;
025: import org.apache.derby.iapi.services.sanity.SanityManager;
026: import org.apache.derby.iapi.store.raw.Compensation;
027: import org.apache.derby.iapi.store.raw.Transaction;
028: import org.apache.derby.iapi.store.raw.Undoable;
029: import org.apache.derby.iapi.store.raw.Loggable;
030: import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
031: import org.apache.derby.iapi.store.raw.xact.RawTransaction;
032: import org.apache.derby.iapi.store.raw.log.LogInstant;
033: import org.apache.derby.iapi.store.raw.ContainerKey;
034: import org.apache.derby.iapi.error.StandardException;
035:
036: import org.apache.derby.iapi.util.ByteArray;
037:
038: import java.io.ObjectOutput;
039: import java.io.ObjectInput;
040: import java.io.IOException;
041: import org.apache.derby.iapi.services.io.LimitObjectInput;
042:
043: /**
044: * Log operation to encrypt a container with a new encryption key or to encrypt
045: * an unencrypted container while configuring the database for
046: * encryption. Container is synced to the disk when encryption is
047: * is successfull, there is nothing to do on a redo. If there is crash/error
048: * while configuring a database for encryption; original version of the
049: * container is put backup during undo.
050: *
051: * <PRE>
052: * @format_id LOGOP_ENCRYPT_CONTAINER
053: * the formatId is written by FormatIdOutputStream when this object is
054: * written out by writeObject
055: * @purpose to record enctyption of container with a new encryption key.
056: * @upgrade
057: * @disk_layout
058: * containerId(ContainerKey) the id of the container this operation applies to
059: * @end_format
060: * </PRE>
061: *
062: * @author Suresh Thalamati
063: * @see Undoable
064: */
065: public class EncryptContainerOperation implements Undoable {
066:
067: private ContainerKey containerId;
068:
069: protected EncryptContainerOperation(RawContainerHandle hdl)
070: throws StandardException {
071: containerId = hdl.getId();
072: }
073:
074: /*
075: * Formatable methods
076: */
077:
078: // no-arg constructor, required by Formatable
079: public EncryptContainerOperation() {
080: super ();
081: }
082:
083: public void writeExternal(ObjectOutput out) throws IOException {
084: containerId.writeExternal(out);
085: }
086:
087: public void readExternal(ObjectInput in) throws IOException,
088: ClassNotFoundException {
089: containerId = ContainerKey.read(in);
090: }
091:
092: /**
093: Loggable methods
094: */
095:
096: /**
097: the default for prepared log is always null for all the operations
098: that don't have optionalData. If an operation has optional data,
099: the operation need to prepare the optional data for this method.
100:
101: Encrypt Operation has no optional data to write out
102: */
103: public ByteArray getPreparedLog() {
104: return (ByteArray) null;
105: }
106:
107: public void releaseResource(Transaction tran) {
108: // no resources held to release.
109: }
110:
111: /**
112: A space operation is a RAWSTORE log record
113: */
114: public int group() {
115: return Loggable.RAWSTORE;
116: }
117:
118: /**
119: * Check if this operation needs to be redone during recovery redo.
120: * Returns true if this op should be redone during recovery redo,
121: * @param xact the transaction that is doing the rollback
122: * @return true, if this operation needs to be redone during recovery.
123: * @exception StandardException Standard Derby error policy
124: */
125: public boolean needsRedo(Transaction xact) throws StandardException {
126: // this opeation should not be redone during recovery. Encrypted version
127: // of the container are synced to the disk when it is complete. In case
128: // rollback containers are replaced with the origincal version.
129: return false;
130: }
131:
132: /**
133: Return my format identifier.
134: */
135: public int getTypeFormatId() {
136: return StoredFormatIds.LOGOP_ENCRYPT_CONTAINER;
137: }
138:
139: /**
140: * Containers are not encryped on a redo. Nothing to do in this method.
141: * @param tran transaction doing the operation.
142: * @param instant log instant for this operation.
143: * @param in unused by this log operation.
144: *
145: * @exception StandardException Standard Cloudscape error policy
146: */
147: public final void doMe(Transaction tran, LogInstant instant,
148: LimitObjectInput in) throws StandardException {
149:
150: // nothing to do here, containers are not encrypted on redo,
151: // if confuring the database for encryption fails. it is
152: // undone during recovery. Encryption of the container is done
153: // after the log record is flushed to the disk.
154:
155: releaseResource(tran);
156: }
157:
158: /**
159: Undo of encrytpion of the container. Original version of the container
160: that existed before the start of the database encryption is put back.
161:
162: @param tran the transaction that is undoing this operation
163: @exception StandardException Standard Cloudscape error policy
164: */
165: public void undoMe(Transaction tran) throws StandardException {
166: // restore the container to the state it was before the encrytpion.
167: BaseDataFileFactory bdff = (BaseDataFileFactory) ((RawTransaction) tran)
168: .getDataFactory();
169: EncryptData ed = new EncryptData(bdff);
170: ed.restoreContainer(containerId);
171: releaseResource(tran);
172:
173: }
174:
175: /**
176: * Generate a Compensation (EncryptContainerUndoOperation) that
177: * will rollback the changes made to the container during container
178: * encryption.
179: * @param tran the transaction doing the compensating
180: * @param in optional input; not used by this operation.
181: * @exception StandardException Standard Cloudscape error policy
182: */
183: public Compensation generateUndo(Transaction tran,
184: LimitObjectInput in) throws StandardException {
185: return new EncryptContainerUndoOperation(this );
186: }
187:
188: /** debug */
189: public String toString() {
190: if (SanityManager.DEBUG) {
191: return "Encrypt container " + containerId;
192: }
193:
194: return null;
195: }
196: }
|