/* * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.clouds.vmware; import com.intellij.openapi.diagnostic.Logger; import java.util.Collection; import jetbrains.buildServer.clouds.CloudException; import jetbrains.buildServer.clouds.CloudInstanceUserData; import jetbrains.buildServer.clouds.vmware.settings.VMImageInfo; import jetbrains.buildServer.clouds.vmware.vmrun.VMRun; import jetbrains.buildServer.clouds.vmware.vmrun.VMRunParameters; import org.jetbrains.annotations.NotNull; /** * @author Eugene Petrenko * Created: 20.03.2010 18:08:18 */ public class CachedVMRun implements VMRun { private static final Logger LOG = Logger.getInstance(CachedVMRun.class.getName()); private final VMRun myVMRun; private volatile String myClientId; private volatile String myHostName; private volatile Integer myAllowedMachines; private volatile Integer myVersion; public CachedVMRun(@NotNull final VMRun VMRun) { myVMRun = VMRun; } @NotNull public String getHostName() { return myHostName != null ? myHostName : (myHostName = myVMRun.getHostName()); } @NotNull public Collection getLocalImages() { return myVMRun.getLocalImages(); } public synchronized int getAllowedMachinesToRun() { if (myAllowedMachines != null) { return myAllowedMachines; } else { try { myAllowedMachines = myVMRun.getAllowedMachinesToRun(); } catch (Throwable t) { warnObsoleteClient(t); myAllowedMachines = 1; } return myAllowedMachines; } } @NotNull public String getLocationId() { return myClientId != null ? myClientId : (myClientId = myVMRun.getLocationId()); } @NotNull public Collection getRunningImages() { return myVMRun.getRunningImages(); } public void startMachine(@NotNull final VMRunParameters instance, @NotNull final CloudInstanceUserData data) throws CloudException { myVMRun.startMachine(instance, data); } public void terminateMachine(@NotNull final VMImageInfo image) { myVMRun.terminateMachine(image); } public synchronized int getVersion(final int serverVersion) { if (myVersion != null) { return myVersion; } try { myVersion = myVMRun.getVersion(serverVersion); } catch (Throwable t) { warnObsoleteClient(t); myVersion = 0; } return myVersion; } private void warnObsoleteClient(final Throwable t) { LOG.info("Client: " + getHostName() + " is obsolete"); LOG.debug(t.getMessage(), t); } }