3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.container;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.Member;
24 import java.util.List;
25 import org.picocontainer.ComponentAdapter;
26 import org.picocontainer.ComponentMonitor;
27 import org.picocontainer.DefaultPicoContainer;
28 import org.picocontainer.MutablePicoContainer;
29 import org.picocontainer.PicoContainer;
30 import org.picocontainer.behaviors.OptInCaching;
31 import org.picocontainer.lifecycle.ReflectionLifecycleStrategy;
32 import org.picocontainer.monitors.ComponentMonitorHelper;
33 import org.picocontainer.monitors.NullComponentMonitor;
34 import org.sonar.api.utils.log.Logger;
35 import org.sonar.api.utils.log.Loggers;
36 import org.sonar.core.platform.ComponentContainer;
37 import org.sonar.core.platform.ContainerPopulator;
38 import org.sonar.core.platform.Module;
40 import static java.util.Objects.requireNonNull;
41 import static org.picocontainer.monitors.ComponentMonitorHelper.ctorToString;
42 import static org.picocontainer.monitors.ComponentMonitorHelper.format;
43 import static org.picocontainer.monitors.ComponentMonitorHelper.methodToString;
44 import static org.picocontainer.monitors.ComponentMonitorHelper.parmsToString;
46 public class ComputeEngineContainerImpl extends ComponentContainer implements ComputeEngineContainer {
47 private static final Logger LOG = Loggers.get(ComputeEngineContainerImpl.class);
49 public ComputeEngineContainerImpl(ComponentContainer parent, ContainerPopulator<ComputeEngineContainer> populator) {
50 super(createContainer(requireNonNull(parent)));
52 populateContainer(requireNonNull(populator));
56 private void populateContainer(ContainerPopulator<ComputeEngineContainer> populator) {
57 populator.populateContainer(this);
58 populateFromModules();
61 private void populateFromModules() {
62 List<Module> modules = getComponentsByType(Module.class);
63 for (Module module : modules) {
64 module.configure(this);
69 * Creates a PicContainer which extends the specified ComponentContainer <strong>but is not referenced in return</strong>
70 * and lazily starts its components.
72 private static MutablePicoContainer createContainer(ComponentContainer parent) {
73 ComponentMonitor componentMonitor = instanceComponentMonitor();
74 ReflectionLifecycleStrategy lifecycleStrategy = new ReflectionLifecycleStrategy(componentMonitor, "start", "stop", "close") {
76 public boolean isLazy(ComponentAdapter<?> adapter) {
81 return new DefaultPicoContainer(new OptInCaching(), lifecycleStrategy, parent.getPicoContainer(), componentMonitor);
84 private static ComponentMonitor instanceComponentMonitor() {
85 if (!LOG.isTraceEnabled()) {
86 return new NullComponentMonitor();
88 return new ComputeEngineComponentMonitor();
91 private static class ComputeEngineComponentMonitor extends NullComponentMonitor {
94 public <T> void instantiated(PicoContainer container, ComponentAdapter<T> componentAdapter,
95 Constructor<T> constructor, Object instantiated, Object[] parameters, long duration) {
96 LOG.trace(format(ComponentMonitorHelper.INSTANTIATED, ctorToString(constructor), duration, instantiated.getClass().getName(), parmsToString(parameters)));
100 public void invoked(PicoContainer container, ComponentAdapter<?> componentAdapter, Member member, Object instance, long duration, Object[] args, Object retVal) {
101 LOG.trace(format(ComponentMonitorHelper.INVOKED, methodToString(member), instance, duration));
107 public void cleanup() {
110 } catch (Throwable t) {
111 Loggers.get(ComputeEngineContainerImpl.class).error("Cleanup of container failed", t);
116 public String toString() {
117 return "ComputeEngineContainerImpl";