KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > EncryptContainerOperation


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.EncryptContainerOperation
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.raw.data;
23
24 import org.apache.derby.iapi.services.io.StoredFormatIds;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26 import org.apache.derby.iapi.store.raw.Compensation;
27 import org.apache.derby.iapi.store.raw.Transaction;
28 import org.apache.derby.iapi.store.raw.Undoable;
29 import org.apache.derby.iapi.store.raw.Loggable;
30 import org.apache.derby.iapi.store.raw.data.RawContainerHandle;
31 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
32 import org.apache.derby.iapi.store.raw.log.LogInstant;
33 import org.apache.derby.iapi.store.raw.ContainerKey;
34 import org.apache.derby.iapi.error.StandardException;
35
36 import org.apache.derby.iapi.util.ByteArray;
37
38 import java.io.ObjectOutput JavaDoc;
39 import java.io.ObjectInput JavaDoc;
40 import java.io.IOException JavaDoc;
41 import org.apache.derby.iapi.services.io.LimitObjectInput;
42
43 /**
44  * Log operation to encrypt a container with a new encryption key or to encrypt
45  * an unencrypted container while configuring the database for
46  * encryption. Container is synced to the disk when encryption is
47  * is successfull, there is nothing to do on a redo. If there is crash/error
48  * while configuring a database for encryption; original version of the
49  * container is put backup during undo.
50  *
51  * <PRE>
52  * @format_id LOGOP_ENCRYPT_CONTAINER
53  * the formatId is written by FormatIdOutputStream when this object is
54  * written out by writeObject
55  * @purpose to record enctyption of container with a new encryption key.
56  * @upgrade
57  * @disk_layout
58  * containerId(ContainerKey) the id of the container this operation applies to
59  * @end_format
60  * </PRE>
61  *
62  * @author Suresh Thalamati
63  * @see Undoable
64  */

65 public class EncryptContainerOperation implements Undoable
66 {
67
68     private ContainerKey containerId;
69
70     protected EncryptContainerOperation(RawContainerHandle hdl)
71         throws StandardException
72     {
73         containerId = hdl.getId();
74     }
75
76     /*
77      * Formatable methods
78      */

79
80     // no-arg constructor, required by Formatable
81
public EncryptContainerOperation() { super(); }
82
83     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
84     {
85         containerId.writeExternal(out);
86     }
87
88     public void readExternal(ObjectInput JavaDoc in)
89         throws IOException JavaDoc, ClassNotFoundException JavaDoc
90     {
91         containerId = ContainerKey.read(in);
92     }
93
94
95     /**
96         Loggable methods
97     */

98
99     /**
100        the default for prepared log is always null for all the operations
101        that don't have optionalData. If an operation has optional data,
102        the operation need to prepare the optional data for this method.
103        
104        Encrypt Operation has no optional data to write out
105     */

106     public ByteArray getPreparedLog()
107     {
108         return (ByteArray) null;
109     }
110
111     public void releaseResource(Transaction tran)
112     {
113         // no resources held to release.
114
}
115
116     /**
117        A space operation is a RAWSTORE log record
118     */

119     public int group()
120     {
121         return Loggable.RAWSTORE;
122     }
123
124
125     /**
126      * Check if this operation needs to be redone during recovery redo.
127      * Returns true if this op should be redone during recovery redo,
128      * @param xact the transaction that is doing the rollback
129      * @return true, if this operation needs to be redone during recovery.
130      * @exception StandardException Standard Derby error policy
131     */

132     public boolean needsRedo(Transaction xact)
133          throws StandardException
134     {
135         // this opeation should not be redone during recovery. Encrypted version
136
// of the container are synced to the disk when it is complete. In case
137
// rollback containers are replaced with the origincal version.
138
return false;
139     }
140
141
142     /**
143        Return my format identifier.
144     */

145     public int getTypeFormatId() {
146         return StoredFormatIds.LOGOP_ENCRYPT_CONTAINER;
147     }
148
149
150     /**
151      * Containers are not encryped on a redo. Nothing to do in this method.
152      * @param tran transaction doing the operation.
153      * @param instant log instant for this operation.
154      * @param in unused by this log operation.
155      *
156      * @exception StandardException Standard Cloudscape error policy
157      */

158     public final void doMe(Transaction tran, LogInstant instant,
159                            LimitObjectInput in)
160          throws StandardException
161     {
162
163         // nothing to do here, containers are not encrypted on redo,
164
// if confuring the database for encryption fails. it is
165
// undone during recovery. Encryption of the container is done
166
// after the log record is flushed to the disk.
167

168         releaseResource(tran);
169     }
170
171
172     /**
173        Undo of encrytpion of the container. Original version of the container
174        that existed before the start of the database encryption is put back.
175         
176        @param tran the transaction that is undoing this operation
177        @exception StandardException Standard Cloudscape error policy
178     */

179     public void undoMe(Transaction tran) throws StandardException
180     {
181         // restore the container to the state it was before the encrytpion.
182
BaseDataFileFactory bdff =
183             (BaseDataFileFactory) ((RawTransaction) tran).getDataFactory();
184         EncryptData ed = new EncryptData(bdff);
185         ed.restoreContainer(containerId);
186         releaseResource(tran);
187
188     }
189
190     /**
191      * Generate a Compensation (EncryptContainerUndoOperation) that
192      * will rollback the changes made to the container during container
193      * encryption.
194      * @param tran the transaction doing the compensating
195      * @param in optional input; not used by this operation.
196      * @exception StandardException Standard Cloudscape error policy
197      */

198     public Compensation generateUndo(Transaction tran, LimitObjectInput in)
199         throws StandardException
200     {
201         return new EncryptContainerUndoOperation(this);
202     }
203
204     /** debug */
205     public String JavaDoc toString()
206     {
207         if (SanityManager.DEBUG)
208         {
209             return "Encrypt container " + containerId;
210         }
211         
212         return null;
213     }
214 }
215
Popular Tags