Skip to content

Commit

Permalink
JENKINS-12741 Use Plexus to ensure a single Sauce Connect Manager ins…
Browse files Browse the repository at this point in the history
…tance is used
  • Loading branch information
rossrowe committed Feb 13, 2012
1 parent 482ac45 commit 3162ee8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 15 deletions.
33 changes: 32 additions & 1 deletion pom.xml
Expand Up @@ -25,13 +25,38 @@
</distributionManagement>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<goals>deploy</goals>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-maven-plugin</artifactId>
<executions>
<execution>
<id>merge</id>
<configuration>
<descriptors>
<descriptor>src/main/resources/META-INF/plexus/components.xml</descriptor>
</descriptors>
</configuration>
<phase>generate-resources</phase>
<goals>
<goal>merge-descriptors</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>
Expand Down Expand Up @@ -80,11 +105,17 @@
<artifactId>sauce-rest-api</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-plexus</artifactId>
<version>2.2.0</version>
</dependency>

<!-- ci-sauce is contained in the https://repository-saucelabs.forge.cloudbees.com/release Maven repository -->
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>ci-sauce</artifactId>
<version>1.5</version>
<version>1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/saucelabs/hudson/HudsonSauceManagerFactory.java
@@ -0,0 +1,48 @@
package com.saucelabs.hudson;

import com.saucelabs.ci.sauceconnect.SauceConnectTwoManager;
import com.saucelabs.ci.sauceconnect.SauceTunnelManager;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

/**
* @author Ross Rowe
*/
public class HudsonSauceManagerFactory {

private static final HudsonSauceManagerFactory INSTANCE = new HudsonSauceManagerFactory();

private PlexusContainer plexus = null;

public static HudsonSauceManagerFactory getInstance() {
return INSTANCE;
}


private HudsonSauceManagerFactory() {

}

public SauceConnectTwoManager createSauceConnectManager() throws ComponentLookupException {
return (SauceConnectTwoManager) this.plexus.lookup(SauceTunnelManager.class.getName());
}

public void start() {
if (plexus == null) {
try {
this.plexus = new DefaultPlexusContainer();
} catch (PlexusContainerException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
// These will only be useful for Hudson v1.395 and under
// ... Since the use of sisu-plexus-inject will initialize
// everything in the constructor
PlexusContainer.class.getDeclaredMethod("initialize").invoke(this.plexus);
PlexusContainer.class.getDeclaredMethod("start").invoke(this.plexus);
} catch (Throwable e) { /* Don't do anything here ... initialize/start methods should be called prior to v1.395 ! */ }
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/sauce_ondemand/PluginImpl.java
Expand Up @@ -25,6 +25,7 @@

import com.saucelabs.ci.SauceLibraryManager;
import com.saucelabs.hudson.HudsonSauceLibraryManager;
import com.saucelabs.hudson.HudsonSauceManagerFactory;
import com.saucelabs.rest.Credential;
import com.saucelabs.rest.SauceTunnelFactory;
import hudson.Extension;
Expand Down Expand Up @@ -83,6 +84,7 @@ public void start() throws Exception {
Items.XSTREAM.alias("hudson.plugins.sauce_ondemand.SauceOnDemandBuildWrapper", SauceOnDemandBuildWrapper.class);

load();
HudsonSauceManagerFactory.getInstance().start();
}

public void setCredential(String username, String apiKey) throws IOException {
Expand Down
Expand Up @@ -24,9 +24,9 @@
package hudson.plugins.sauce_ondemand;

import com.michelin.cio.hudson.plugins.copytoslave.MyFilePath;
import com.saucelabs.ci.sauceconnect.SauceConnectTwoManager;
import com.saucelabs.ci.sauceconnect.SauceConnectUtils;
import com.saucelabs.ci.sauceconnect.SauceTunnelManager;
import com.saucelabs.hudson.HudsonSauceManagerFactory;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
Expand All @@ -37,6 +37,7 @@
import hudson.tasks.BuildWrapper;
import hudson.util.Secret;
import org.apache.commons.lang.StringUtils;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;
Expand Down Expand Up @@ -209,11 +210,9 @@ private interface ITunnelHolder {
private static final class TunnelHolder implements ITunnelHolder, Serializable {

private List<SauceTunnelManager> tunnelManagers = new ArrayList<SauceTunnelManager>();
private String buildName;
private String username;

public TunnelHolder(String buildName, String username) {
this.buildName = buildName;
public TunnelHolder(String username) {
this.username = username;
}

Expand All @@ -227,9 +226,8 @@ public Object writeReplace() {

public void close(TaskListener listener) {
for (SauceTunnelManager tunnelManager : tunnelManagers) {
tunnelManager.closeTunnelsForPlan(username, buildName);
tunnelManager.closeTunnelsForPlan(username);
}

}
}

Expand Down Expand Up @@ -268,14 +266,19 @@ public SauceConnectStarter(String buildName, BuildListener listener, int port, F
}

public ITunnelHolder call() throws IOException {
TunnelHolder tunnelHolder = new TunnelHolder(buildName, username);
SauceTunnelManager tunnelManager = new SauceConnectTwoManager();
tunnelManager.setSauceConnectJar(sauceConnectJar);
tunnelManager.setPrintStream(listener.getLogger());
Object process = tunnelManager.openConnection(username, key, port);
tunnelManager.addTunnelToMap(buildName, process);
tunnelHolder.tunnelManagers.add(tunnelManager);
return tunnelHolder;
TunnelHolder tunnelHolder = new TunnelHolder(username);
//SauceTunnelManager sauceManager = new SauceConnectTwoManager();
SauceTunnelManager sauceManager = null;
try {
sauceManager = HudsonSauceManagerFactory.getInstance().createSauceConnectManager();
Object process = sauceManager.openConnection(username, key, port, sauceConnectJar, listener.getLogger());
sauceManager.addTunnelToMap(buildName, process);
tunnelHolder.tunnelManagers.add(sauceManager);
return tunnelHolder;
} catch (ComponentLookupException e) {
throw new IOException(e);
}

}
}

Expand Down
26 changes: 26 additions & 0 deletions src/main/resources/META-INF/plexus/components.xml
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
--><component-set>
<components>
<component>
<role>com.saucelabs.ci.sauceconnect.SauceTunnelManager</role>
<implementation>com.saucelabs.ci.sauceconnect.SauceConnectTwoManager</implementation>
</component>
</components>
</component-set>

0 comments on commit 3162ee8

Please sign in to comment.