Package: ResolutionStateDumper

ResolutionStateDumper

nameinstructionbranchcomplexitylinemethod
ResolutionStateDumper(SystemStateAccessor, StateWriter)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
dump(File)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
dump(File, State)
M: 33 C: 35
51%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 5 C: 8
62%
M: 0 C: 1
100%
getTmpDir()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
zipStateDirectory(File, File)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.nano.serviceability.dump.internal;
13:
14: import java.io.File;
15: import java.io.IOException;
16:
17: import org.eclipse.osgi.service.resolver.State;
18: import org.slf4j.Logger;
19: import org.slf4j.LoggerFactory;
20:
21: import org.eclipse.virgo.util.io.PathReference;
22: import org.eclipse.virgo.util.io.ZipUtils;
23:
24: /**
25: * Utility class that writes the current Equinox resolver {@link State} to a ZIP file.
26: * <p />
27: *
28: * <strong>Concurrent Semantics</strong><br />
29: *
30: * Threadsafe.
31: *
32: */
33: final class ResolutionStateDumper {
34:
35: private static final String ENTRY_NAME_STATE = "state/";
36:
37: private final Logger logger = LoggerFactory.getLogger(this.getClass());
38:
39: private final SystemStateAccessor systemStateAccessor;
40:
41: private final StateWriter stateWriter;
42:
43: /**
44: * Creates a new <code>ResolutionStateDumper</code>.
45: * @param systemStateAccessor to access live system {@link State}
46: * @param stateWriter to write a {@link State} to permanent storage
47: */
48: public ResolutionStateDumper(SystemStateAccessor systemStateAccessor, StateWriter stateWriter) {
49: this.systemStateAccessor = systemStateAccessor;
50: this.stateWriter = stateWriter;
51: }
52:
53: /**
54: * Dump the global resolver {@link State} into a ZIP file at the supplied location.
55: *
56: * @param outputFile the location to create the ZIP file at.
57: */
58: public void dump(File outputFile) {
59: dump(outputFile, this.systemStateAccessor.getSystemState());
60: }
61:
62: /**
63: * Dump a resolver {@link State} into a ZIP file at the supplied location.
64: *
65: * @param outputFile the location to create the ZIP file at.
66: * @param state the state to dump
67: */
68: public void dump(File outputFile, State state) {
69: File outdir = new File(getTmpDir(), "resolve-" + System.currentTimeMillis());
70:• if (outdir.mkdirs()) {
71: try {
72: this.stateWriter.writeState(state, outdir);
73: } catch (IOException e) {
74: this.logger.error("Unable to write resolver state.", e);
75: }
76: } else {
77: throw new RuntimeException("Unable to create temporary directory '" + outdir.getAbsolutePath() + "'.");
78: }
79:
80: try {
81: zipStateDirectory(outputFile, outdir);
82: } catch (IOException e) {
83: this.logger.error("Unable to create ZIP of state dump", e);
84: } finally {
85:• if (!new PathReference(outdir).delete(true)) {
86: this.logger.warn("Temporary state directory '%s' was not removed after use.", outdir.getAbsolutePath());
87: }
88: }
89: }
90:
91: private void zipStateDirectory(File outputFile, File dumpDir) throws IOException {
92: PathReference output = new PathReference(outputFile);
93: PathReference toZip = new PathReference(dumpDir);
94:
95: ZipUtils.zipTo(toZip, output, ENTRY_NAME_STATE);
96: }
97:
98: private String getTmpDir() {
99: String path = System.getProperty("java.io.tmpdir");
100: return path;
101: }
102: }