Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/new osgi annotations #312

Merged
merged 20 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions core/worker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.osgi</artifactId>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.metatype.annotations</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -146,6 +150,18 @@
</execution>
</executions>
</plugin>
<!-- overwrites configuration from parent pom to disable SCR plugin -->
<!-- ToDo: remove me when all felix dependencies are removed -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>generate-scr-descriptor</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<!-- could be replaced with truezip plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import com.cognifide.aet.job.api.collector.HttpRequestExecutor;
import com.cognifide.aet.job.api.collector.HttpRequestExecutorFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.annotations.Component;

@Service
@Component
public class HttpRequestExecutorFactoryImpl implements HttpRequestExecutorFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,21 @@
import com.cognifide.aet.job.api.collector.ProxyServerWrapper;
import com.cognifide.aet.worker.exceptions.WorkerException;
import java.net.UnknownHostException;
import java.util.Map;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.openqa.selenium.Proxy;

/**
* Some constants and utils used by web driver factory implementations.
*/
public final class WebDriverHelper {

public static final String NAME = "name";

public static final String NAME_LABEL = "Name";

public static final String NAME_DESC = "Driver name that will be used by other services like Screenshot Collectors";

public static final String PATH = "path";

public static final String PATH_LABEL = "Path";

public static final String PATH_DESC = "Custom path to driver binary";

public static final String SELENIUM_GRID_URL = "seleniumGridUrl";

public static final String SELENIUM_GRID_URL_LABEL = "Selenium grid URL";

public static final String DEFAULT_SELENIUM_GRID_URL = "http://localhost:4444/wd/hub";

private WebDriverHelper() {
// restrict instantiation
}

public static String getProp(Map<String, String> properties, String name, String defaultValue) {
return PropertiesUtil.toString(properties.get(name), defaultValue);
}

public static Proxy setupProxy(ProxyServerWrapper proxyServer) throws WorkerException {
proxyServer.setCaptureContent(true);
proxyServer.setCaptureHeaders(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,46 @@
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;

import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author lukasz.wieczorek
*/
@Service(WebDriverProvider.class)
@Component(label = "AET WebDriver Provider", description = "AET WebDriver Provider", immediate = true, metatype = true)
@Properties({@Property(name = Constants.SERVICE_VENDOR, value = "Cognifide Ltd")})
@Component(
service = WebDriverProvider.class,
property = {"name = " + Constants.SERVICE_VENDOR, "value = Cognifide Ltd"},
immediate = true
)
@Designate(ocd = WebDriverProviderConfig.class)
public class WebDriverProvider {

private static final Logger LOG = LoggerFactory.getLogger(WebDriverProvider.class);

private static final String DEFAULT_WEB_DRIVER_NAME = "defaultWebDriverName";

@Property(name = DEFAULT_WEB_DRIVER_NAME, label = "Default Web Driver name", value = "ff")
private String defaultWebDriverName;
private WebDriverProviderConfig config;

@Reference(referenceInterface = WebDriverFactory.class, policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, bind = "bindWebDriverFactory", unbind = "unbindWebDriverFactory")
@Reference(
service = WebDriverFactory.class,
policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.MULTIPLE,
bind = "bindWebDriverFactory",
unbind = "unbindWebDriverFactory")
private final Map<String, WebDriverFactory> collectorFactories = Maps.newConcurrentMap();

@Reference
private ProxyServerProvider proxyServerProvider;

@Activate
void activate(Map<String, String> properties) {
defaultWebDriverName = properties.get(DEFAULT_WEB_DRIVER_NAME);
void activate(WebDriverProviderConfig config) {
this.config = config;
}

public WebCommunicationWrapper createWebDriverWithProxy(String preferredWebDriver, String proxyName)
Expand Down Expand Up @@ -94,7 +98,8 @@ protected void unbindWebDriverFactory(WebDriverFactory webDriverFactory) {
*/
private WebDriverFactory findWebDriverFactory(String preferredWebDriver) throws WorkerException {
final WebDriverFactory webDriverFactory;
String id = preferredWebDriver == null ? defaultWebDriverName : preferredWebDriver;
String id = preferredWebDriver == null ?
config.defaultWebDriverName() : preferredWebDriver;
webDriverFactory = collectorFactories.get(id);
if (webDriverFactory == null) {
String webDrivers = Joiner.on(", ").join(collectorFactories.keySet());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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 com.cognifide.aet.worker.drivers;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name = "AET WebDriver Provider", description = "AET WebDriver Provider")
public @interface WebDriverProviderConfig {

String DEFAULT_WEB_DRIVER_NAME_LABEL = "Default Web Driver name";

@AttributeDefinition(name = DEFAULT_WEB_DRIVER_NAME_LABEL, defaultValue = "ff")
String defaultWebDriverName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
*/
package com.cognifide.aet.worker.drivers.chrome;

import static com.cognifide.aet.worker.drivers.WebDriverHelper.DEFAULT_SELENIUM_GRID_URL;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.NAME;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.NAME_DESC;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.NAME_LABEL;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.SELENIUM_GRID_URL;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.SELENIUM_GRID_URL_LABEL;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.getProp;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.setupProxy;

import com.cognifide.aet.job.api.collector.HttpRequestExecutorFactory;
Expand All @@ -31,17 +24,9 @@
import com.cognifide.aet.worker.exceptions.WorkerException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
Expand All @@ -52,37 +37,28 @@
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;


@Service
@Component(
policy = ConfigurationPolicy.REQUIRE,
description = "AET Chrome WebDriver Factory",
label = "AET Chrome WebDriver Factory",
metatype = true)
@Properties({@Property(name = Constants.SERVICE_VENDOR, value = "Cognifide Ltd")})
configurationPolicy = ConfigurationPolicy.REQUIRE,
property = {"name = " + Constants.SERVICE_VENDOR, "value = Cognifide Ltd"}
)
@Designate(ocd = ChromeWebDriverFactoryConfig.class)
public class ChromeWebDriverFactory implements WebDriverFactory {

private static final String DEFAULT_BROWSER_NAME = "chrome";

@Reference
private HttpRequestExecutorFactory requestExecutorFactory;

@Property(name = NAME,
label = NAME_LABEL,
description = NAME_DESC,
value = DEFAULT_BROWSER_NAME)
private String name;

@Property(name = SELENIUM_GRID_URL,
label = SELENIUM_GRID_URL_LABEL,
description = "Url to selenium grid hub. When null local Chrome driver will be used. Local Chrome driver does not work on Linux",
value = DEFAULT_SELENIUM_GRID_URL)
private String seleniumGridUrl;
private ChromeWebDriverFactoryConfig config;

@Activate
public void activate(Map<String, String> properties) {
this.name = getProp(properties, NAME, DEFAULT_BROWSER_NAME);
this.seleniumGridUrl = getProp(properties, SELENIUM_GRID_URL, DEFAULT_SELENIUM_GRID_URL);
public void activate(ChromeWebDriverFactoryConfig config) {
this.config = config;
}

@Override
Expand All @@ -104,7 +80,7 @@ public WebCommunicationWrapper createWebDriver(ProxyServerWrapper proxyServer)

@Override
public String getName() {
return name;
return config.name();
}

private WebCommunicationWrapper createWebDriver(DesiredCapabilities capabilities,
Expand All @@ -120,8 +96,10 @@ private WebCommunicationWrapper createWebDriver(DesiredCapabilities capabilities

private WebDriver getChromeDriver(DesiredCapabilities capabilities)
throws MalformedURLException {
WebDriver driver = StringUtils.isNotBlank(seleniumGridUrl) ? new RemoteWebDriver(
new URL(seleniumGridUrl), capabilities) : new ChromeDriver(capabilities);
WebDriver driver =
StringUtils.isNotBlank(config.seleniumGridUrl()) ?
new RemoteWebDriver(new URL(config.seleniumGridUrl()), capabilities)
: new ChromeDriver(capabilities);
driver.manage().timeouts().pageLoadTimeout(5L, TimeUnit.MINUTES);
return driver;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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 com.cognifide.aet.worker.drivers.chrome;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

import static com.cognifide.aet.worker.drivers.WebDriverHelper.NAME_LABEL;
import static com.cognifide.aet.worker.drivers.WebDriverHelper.NAME_DESC;

@ObjectClassDefinition(name = "AET Chrome WebDriver Factory", description = "AET Chrome WebDriver Factory")
public @interface ChromeWebDriverFactoryConfig {

String DEFAULT_BROWSER_NAME = "chrome";

String SELENIUM_GRID_URL_LABEL = "Selenium grid URL";
String SELENIUM_GRID_URL_DESC = "Url to selenium grid hub. When null local Chrome driver will be used. Local Chrome driver does not work on Linux";
String DEFAULT_SELENIUM_GRID_URL = "http://localhost:4444/wd/hub";

@AttributeDefinition(
name = NAME_LABEL,
description = NAME_DESC,
defaultValue = DEFAULT_BROWSER_NAME)
String name();

@AttributeDefinition(
name = ChromeWebDriverFactoryConfig.SELENIUM_GRID_URL_LABEL,
description = ChromeWebDriverFactoryConfig.SELENIUM_GRID_URL_DESC,
defaultValue = ChromeWebDriverFactoryConfig.DEFAULT_SELENIUM_GRID_URL)
String seleniumGridUrl();
}
Loading