Skip to content

Commit 9408b66

Browse files
authored
Merge pull request #436 from rpmoore/3_2_11
Adding an aggregate exception class to the interface module to be use…
2 parents 74ef4ec + 58daef5 commit 9408b66

File tree

6 files changed

+177
-7
lines changed

6 files changed

+177
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
allprojects {
1717
group = 'com.spectralogic.ds3'
18-
version = '3.2.10'
18+
version = '3.2.11'
1919
}
2020

2121
subprojects {

ds3-interfaces/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ buildscript {
77

88
apply plugin: 'com.github.johnrengelman.shadow'
99

10+
dependencies {
11+
compile 'commons-io:commons-io:2.4'
12+
}
13+
1014
shadowJar {
15+
relocate 'org.apache', 'ds3fatjar.org.apache'
1116
dependencies {
1217
exclude(dependency('org.hamcrest:hamcrest-library:1.3'))
1318
exclude(dependency('org.mockito:mockito-core:1.10.19'))
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* ****************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.exceptions;
17+
18+
import org.apache.commons.io.output.StringBuilderWriter;
19+
20+
import java.io.PrintWriter;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
public class AggregateException extends RuntimeException {
25+
26+
private final List<Throwable> exceptions;
27+
28+
public AggregateException(final Iterable<Throwable> exceptions) {
29+
super("One or more exceptions were aggregated together");
30+
this.exceptions = exceptionList(exceptions);
31+
}
32+
33+
private static List<Throwable> exceptionList(final Iterable<Throwable> exceptions) {
34+
final List<Throwable> exceptionList = new ArrayList<>();
35+
for (final Throwable t : exceptions) {
36+
exceptionList.add(t);
37+
}
38+
return exceptionList;
39+
}
40+
41+
public Iterable<Throwable> getExceptions() {
42+
return exceptions;
43+
}
44+
45+
public String toString() {
46+
final StringBuilder builder = new StringBuilder();
47+
builder.append("One or more exceptions were aggregated:");
48+
49+
try (final StringBuilderWriter writer = new StringBuilderWriter(builder);
50+
final PrintWriter printWriter = new PrintWriter(writer)) {
51+
52+
for (final Throwable t : exceptions) {
53+
printWriter.append("Exception: ").append(t.getMessage());
54+
t.printStackTrace(printWriter);
55+
}
56+
}
57+
58+
return builder.toString();
59+
}
60+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* ****************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.exceptions;
17+
18+
import org.junit.Test;
19+
20+
import java.util.ArrayList;
21+
import java.util.Iterator;
22+
import java.util.List;
23+
24+
import static org.hamcrest.CoreMatchers.is;
25+
import static org.hamcrest.CoreMatchers.notNullValue;
26+
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertThat;
28+
import static org.junit.Assert.assertTrue;
29+
30+
public class AggregateException_Test {
31+
32+
@Test
33+
public void basicAggregate() {
34+
final Exception e1 = new Exception("first exception");
35+
final Exception e2 = new Exception("second exception");
36+
37+
final List<Throwable> exceptionList = new ArrayList<>();
38+
exceptionList.add(e1);
39+
exceptionList.add(e2);
40+
41+
final AggregateException aggregateException = new AggregateException(exceptionList);
42+
43+
final Iterator<Throwable> exceptionIter = aggregateException.getExceptions().iterator();
44+
45+
assertTrue(exceptionIter.hasNext());
46+
assertThat(exceptionIter.next(), is(notNullValue()));
47+
assertTrue(exceptionIter.hasNext());
48+
assertThat(exceptionIter.next(), is(notNullValue()));
49+
assertFalse(exceptionIter.hasNext());
50+
}
51+
52+
@Test
53+
public void repeatIteration() {
54+
final Exception e1 = new Exception("first exception");
55+
56+
final List<Throwable> exceptionList = new ArrayList<>();
57+
exceptionList.add(e1);
58+
59+
final AggregateException aggregateException = new AggregateException(exceptionList);
60+
61+
final Iterator<Throwable> exceptionIter = aggregateException.getExceptions().iterator();
62+
63+
assertTrue(exceptionIter.hasNext());
64+
assertThat(exceptionIter.next(), is(notNullValue()));
65+
assertFalse(exceptionIter.hasNext());
66+
67+
final Iterator<Throwable> repeatIter = aggregateException.getExceptions().iterator();
68+
assertTrue(repeatIter.hasNext());
69+
assertThat(repeatIter.next(), is(notNullValue()));
70+
assertFalse(repeatIter.hasNext());
71+
72+
}
73+
}

ds3-metadata/src/main/java/com/spectralogic/ds3client/metadata/MetadataReceivedListenerImpl.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@
1515

1616
package com.spectralogic.ds3client.metadata;
1717

18+
import com.google.common.collect.ImmutableList;
19+
import com.spectralogic.ds3client.exceptions.AggregateException;
1820
import com.spectralogic.ds3client.helpers.MetadataReceivedListener;
1921
import com.spectralogic.ds3client.metadata.interfaces.MetadataRestore;
2022
import com.spectralogic.ds3client.networking.Metadata;
23+
import com.spectralogic.ds3client.utils.Guard;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2126

2227
import java.io.IOException;
2328

2429
public class MetadataReceivedListenerImpl implements MetadataReceivedListener {
30+
31+
private final static Logger LOG = LoggerFactory.getLogger(MetadataReceivedListenerImpl.class);
32+
2533
private final String localFilePath;
2634

35+
2736
public MetadataReceivedListenerImpl(final String localFilePath) {
2837
this.localFilePath = localFilePath;
2938
}
@@ -45,17 +54,41 @@ public void metadataReceived(final String filename, final Metadata metadata) {
4554
* @param metadata metadata which needs to be set on local file
4655
*/
4756
private void restoreMetaData(final String objectName, final Metadata metadata) throws IOException, InterruptedException {
57+
58+
final ImmutableList.Builder<Throwable> exceptionBuilder = ImmutableList.builder();
59+
4860
//get metadatarestore on the basis of os
4961
final MetadataRestore metadataRestore = new MetadataRestoreFactory().getOSSpecificMetadataRestore(metadata, objectName);
5062
//restore os name
5163
metadataRestore.restoreOSName();
64+
5265
//restore user and owner based on OS
53-
metadataRestore.restoreUserAndOwner();
66+
try {
67+
metadataRestore.restoreUserAndOwner();
68+
} catch (final Throwable t) {
69+
LOG.error("Could not restore owner and owner information", t);
70+
exceptionBuilder.add(t);
71+
}
72+
5473
//restore creation and modified time based on OS
55-
metadataRestore.restoreFileTimes();
56-
//restore permissions based on OS
57-
metadataRestore.restorePermissions();
58-
}
74+
try {
75+
metadataRestore.restoreFileTimes();
76+
} catch (final Throwable t) {
77+
LOG.error("Could not restore the file times", t);
78+
exceptionBuilder.add(t);
79+
}
5980

81+
//restore permissions based on OS
82+
try {
83+
metadataRestore.restorePermissions();
84+
} catch (final Throwable t) {
85+
LOG.error("Could not restore the file permissions", t);
86+
exceptionBuilder.add(t);
87+
}
6088

89+
final ImmutableList<Throwable> exceptions = exceptionBuilder.build();
90+
if (!Guard.isNullOrEmpty(exceptions)) {
91+
throw new AggregateException(exceptions);
92+
}
93+
}
6194
}

ds3-sdk/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ jar.dependsOn genConfigProperties
7777

7878
dependencies {
7979
compile 'org.apache.httpcomponents:httpclient:4.5.1'
80-
compile 'commons-io:commons-io:2.4'
8180
compile 'org.codehaus.woodstox:woodstox-core-asl:4.4.1'
8281
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.7.1'
8382
compile 'com.google.guava:guava:20.0'

0 commit comments

Comments
 (0)