001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.kfs.batch;
017:
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.HashSet;
021: import java.util.Properties;
022: import java.util.Set;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.core.bo.PersistableBusinessObject;
026: import org.kuali.core.service.BusinessObjectService;
027: import org.kuali.core.service.PostDataLoadEncryptionService;
028: import org.kuali.kfs.context.SpringContext;
029: import org.springframework.core.io.FileSystemResource;
030:
031: public class PostDataLoadEncryptionStep extends AbstractStep {
032: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
033: .getLogger(PostDataLoadEncryptionStep.class);
034: private PostDataLoadEncryptionService postDataLoadEncryptionService;
035: private String attributesToEncryptProperties;
036:
037: public boolean execute(String jobName) {
038: Properties attributesToEncryptProperties = new Properties();
039: try {
040: attributesToEncryptProperties.load(new FileSystemResource(
041: this .attributesToEncryptProperties)
042: .getInputStream());
043: } catch (Exception e) {
044: throw new IllegalArgumentException(
045: "PostDataLoadEncrypter requires the full, absolute path to a properties file where the keys are the names of the BusinessObject classes that should be processed and the values are the list of attributes on each that require encryption",
046: e);
047: }
048: for (Object businessObjectClassName : attributesToEncryptProperties
049: .keySet()) {
050: Class businessObjectClass;
051: try {
052: businessObjectClass = Class
053: .forName((String) businessObjectClassName);
054: } catch (Exception e) {
055: throw new IllegalArgumentException(
056: new StringBuffer("Unable to load Class ")
057: .append(businessObjectClassName)
058: .append(
059: " specified by name in attributesToEncryptProperties file ")
060: .append(attributesToEncryptProperties)
061: .toString(), e);
062: }
063: Set<String> attributeNames = null;
064: try {
065: attributeNames = new HashSet(Arrays.asList(StringUtils
066: .split((String) attributesToEncryptProperties
067: .get(businessObjectClassName), ",")));
068: } catch (Exception e) {
069: throw new IllegalArgumentException(
070: new StringBuffer(
071: "Unable to load attributeNames Set from comma-delimited list of attribute names specified as value for property with Class name ")
072: .append(businessObjectClassName)
073: .append(
074: " key in attributesToEncryptProperties file ")
075: .append(attributesToEncryptProperties)
076: .toString(), e);
077: }
078: postDataLoadEncryptionService.checkArguments(
079: businessObjectClass, attributeNames);
080: postDataLoadEncryptionService
081: .createBackupTable(businessObjectClass);
082: try {
083: postDataLoadEncryptionService.prepClassDescriptor(
084: businessObjectClass, attributeNames);
085: Collection objectsToEncrypt = SpringContext.getBean(
086: BusinessObjectService.class).findAll(
087: businessObjectClass);
088: for (Object businessObject : objectsToEncrypt) {
089: postDataLoadEncryptionService.encrypt(
090: (PersistableBusinessObject) businessObject,
091: attributeNames);
092: }
093: postDataLoadEncryptionService.restoreClassDescriptor(
094: businessObjectClass, attributeNames);
095: LOG.info(new StringBuffer("Encrypted ").append(
096: attributesToEncryptProperties
097: .get(businessObjectClassName)).append(
098: " attributes of Class ").append(
099: businessObjectClassName));
100: } catch (Exception e) {
101: postDataLoadEncryptionService
102: .restoreTableFromBackup(businessObjectClass);
103: LOG.error(new StringBuffer(
104: "Caught exception, while encrypting ").append(
105: attributesToEncryptProperties
106: .get(businessObjectClassName)).append(
107: " attributes of Class ").append(
108: businessObjectClassName).append(
109: " and restored table from backup"), e);
110: }
111: postDataLoadEncryptionService
112: .dropBackupTable(businessObjectClass);
113: }
114: return true;
115: }
116:
117: public void setPostDataLoadEncryptionService(
118: PostDataLoadEncryptionService postDataLoadEncryptionService) {
119: this .postDataLoadEncryptionService = postDataLoadEncryptionService;
120: }
121:
122: /**
123: * Sets the attributesToEncryptProperties attribute value.
124: *
125: * @param attributesToEncryptProperties The attributesToEncryptProperties to set.
126: */
127: public void setAttributesToEncryptProperties(
128: String attributesToEncryptProperties) {
129: this.attributesToEncryptProperties = attributesToEncryptProperties;
130: }
131: }
|