blob: 3b56d538e637800f7f5a4d51a3b27f53d756b22d [file] [log] [blame]
Paul Duffine2363012015-11-30 16:20:41 +00001/*
2 * Copyright (C) 2013 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.caliper.runner;
18
19import com.google.caliper.bridge.AbstractLogMessageVisitor;
20import com.google.caliper.bridge.FailureLogMessage;
21import com.google.caliper.bridge.VmOptionLogMessage;
22import com.google.caliper.bridge.VmPropertiesLogMessage;
23import com.google.caliper.model.VmSpec;
24import com.google.caliper.platform.Platform;
25import com.google.common.base.Optional;
26import com.google.common.collect.ImmutableMap;
27import com.google.common.collect.Maps;
28
29import javax.inject.Inject;
30
31/** An {@link AbstractLogMessageVisitor} that collects data about JVM properties and options. */
32@TrialScoped
33final class VmDataCollectingVisitor extends AbstractLogMessageVisitor {
34 private final ImmutableMap.Builder<String, String> vmOptionsBuilder = ImmutableMap.builder();
35 private final Platform platform;
36 private Optional<ImmutableMap<String, String>> vmProperties = Optional.absent();
37
38 @Inject VmDataCollectingVisitor(Platform platform) {
39 this.platform = platform;
40 }
41
42 /**
43 * Returns a {@link VmSpec} based on the data gathered by this visitor.
44 *
45 * @throws IllegalStateException if not all the data has been gathered.
46 */
47 VmSpec vmSpec() {
48 ImmutableMap<String, String> options = vmOptionsBuilder.build();
49 platform.checkVmProperties(options);
50 return new VmSpec.Builder()
51 .addAllProperties(vmProperties.get())
52 .addAllOptions(options)
53 .build();
54 }
55
56 @Override
57 public void visit(FailureLogMessage logMessage) {
58 throw new ProxyWorkerException(logMessage.stackTrace());
59 }
60
61 @Override
62 public void visit(VmOptionLogMessage logMessage) {
63 vmOptionsBuilder.put(logMessage.name(), logMessage.value());
64 }
65
66 @Override
67 public void visit(VmPropertiesLogMessage logMessage) {
68 vmProperties = Optional.of(ImmutableMap.copyOf(
69 Maps.filterKeys(logMessage.properties(), platform.vmPropertiesToRetain())));
70 }
71}