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

Update osgi annotations in proxy #311

Merged
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
37 changes: 15 additions & 22 deletions osgi-dependencies/proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,23 @@
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    <dependency>
      <groupId>org.apache.felix</groupId>
      <artifactId>org.apache.felix.scr.annotations</artifactId>
    </dependency>

Is probably no longer needed here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also

    <dependency>
      <groupId>org.apache.sling</groupId>
      <artifactId>org.apache.sling.commons.osgi</artifactId>
    </dependency>

can be safely removed now.

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.metatype.annotations</artifactId>
</dependency>

<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.osgi</artifactId>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -64,12 +74,7 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>


<!-- This dependency will be embedded -->
<dependency>
<groupId>com.github.detro</groupId>
Expand All @@ -79,18 +84,6 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>generate-scr-descriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@
import com.cognifide.aet.job.api.exceptions.ProxyException;
import com.google.common.collect.Maps;
import java.util.Map;
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.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;

@Service(ProxyServerProvider.class)
@Component(immediate = true, label = "AET Proxy Server Provider", description = "AET Proxy Server Provider")
@Properties({@Property(name = Constants.SERVICE_VENDOR, value = "Cognifide Ltd")})
@Component(
service = ProxyServerProvider.class,
immediate = true,
property = {"name = " + Constants.SERVICE_VENDOR, "value = Cognifide Ltd"}
)
public class ProxyServerProvider {

@Reference(referenceInterface = ProxyManager.class, policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, bind = "bindProxyManager",
@Reference(service = ProxyManager.class, policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.MULTIPLE, bind = "bindProxyManager",
unbind = "unbindProxyManager")
private final Map<String, ProxyManager> collectorManagers = Maps.newConcurrentMap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,32 @@
package com.cognifide.aet.proxy;

import com.cognifide.aet.job.api.exceptions.ProxyException;
import com.cognifide.aet.proxy.configuration.RestProxyManagerConf;
import com.github.detro.browsermobproxyclient.BMPCProxy;
import com.github.detro.browsermobproxyclient.exceptions.BMPCUnableToConnectException;
import com.github.detro.browsermobproxyclient.manager.BMPCDefaultManager;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Component(immediate = true, metatype = true, description = "AET REST Proxy Manager", label = "AET REST Proxy Manager")
@Component(immediate = true)
@Designate(ocd = RestProxyManagerConf.class)
public class RestProxyManager implements ProxyManager {

public static final String CREATE_PROXY_EXCEPTION_FORMAT = "Unable to create proxy! Reached max attempts: %d";

private static final String NAME = "rest";

private static final String SERVER_PROPERTY_NAME = "server";

private static final String PORT_PROPERTY_NAME = "port";

private static final String MAX_ATTEMPTS_PROPERTY_NAME = "maxAttempts";

private static final String ATTEMPTS_INTERVAL_PROPERTY_NAME = "attemptsInterval";

private static final String DEFAULT_SERVER = "localhost";

private static final int DEFAULT_PORT = 9272;

private static final int DEFAULT_MAX_ATTEMPTS = 3;

private static final int DEFAULT_ATTEMPTS_INTERVAL = 50;

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

private final Set<RestProxyServer> proxies = Sets.newHashSet();

@Property(name = SERVER_PROPERTY_NAME, label = "Server", description = "BrowserMob server address", value = DEFAULT_SERVER)
private String server;

@Property(name = PORT_PROPERTY_NAME, label = "Port", description = "BrowserMob API port", intValue = 8080)
private int port;

@Property(name = MAX_ATTEMPTS_PROPERTY_NAME, label = "Max attempts", description = "Maximum number of attempts that will be performed to create single proxy", intValue = 3)
private int maxAttempts;

@Property(name = ATTEMPTS_INTERVAL_PROPERTY_NAME, label = "Attempts interval", description = "Wait interval for failed Attempts", intValue = 50)
private int attemptsInterval;
private RestProxyManagerConf config;

private BMPCDefaultManager manager;

Expand All @@ -79,13 +51,8 @@ public String getName() {
}

@Activate
public void activate(Map<String, ?> properties) {
port = PropertiesUtil.toInteger(properties.get(PORT_PROPERTY_NAME), DEFAULT_PORT);
server = PropertiesUtil.toString(properties.get(SERVER_PROPERTY_NAME), DEFAULT_SERVER);
maxAttempts = PropertiesUtil
.toInteger(properties.get(MAX_ATTEMPTS_PROPERTY_NAME), DEFAULT_MAX_ATTEMPTS);
attemptsInterval = PropertiesUtil
.toInteger(properties.get(ATTEMPTS_INTERVAL_PROPERTY_NAME), DEFAULT_ATTEMPTS_INTERVAL);
public void activate(RestProxyManagerConf config) {
this.config = config;
}

@Deactivate
Expand All @@ -99,7 +66,7 @@ public void deactivate() {
public RestProxyServer createProxy() throws ProxyException {
RestProxyServer proxy;
if (manager == null) {
manager = new BMPCDefaultManager(server, port);
manager = new BMPCDefaultManager(config.server(), config.port());
}
proxy = new RestProxyServer(getBmpcProxy(), this);
proxies.add(proxy);
Expand All @@ -114,13 +81,15 @@ private BMPCProxy getBmpcProxy() throws ProxyException {
try {
bmpcProxy = manager.createProxy();
} catch (BMPCUnableToConnectException e) {
if (++attempt >= maxAttempts) {
throw new ProxyException(String.format(CREATE_PROXY_EXCEPTION_FORMAT, maxAttempts), e);
if (++attempt >= config.maxAttempts()) {
throw new ProxyException(
String.format(CREATE_PROXY_EXCEPTION_FORMAT, config.maxAttempts()), e);
}

LOG.warn(String.format("Failed to create Proxy attempt %d/%d", attempt, maxAttempts));
LOG.warn(
String.format("Failed to create Proxy attempt %d/%d", attempt, config.maxAttempts()));
try {
Thread.sleep(attemptsInterval);
Thread.sleep(config.attemptsInterval());
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
}
Expand All @@ -135,11 +104,11 @@ public void detach(RestProxyServer proxy) {
}

public String getServer() {
return server;
return config.server();
}

public int getPort() {
return port;
return config.port();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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.proxy.configuration;

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

@ObjectClassDefinition(description = "AET REST Proxy Manager", name = "AET REST Proxy Manager")
public @interface RestProxyManagerConf {
String SERVER_PROPERTY_NAME = "server";

String PORT_PROPERTY_NAME = "port";

String MAX_ATTEMPTS_PROPERTY_NAME = "maxAttempts";

String ATTEMPTS_INTERVAL_PROPERTY_NAME = "attemptsInterval";

String DEFAULT_SERVER = "localhost";

@AttributeDefinition(name = SERVER_PROPERTY_NAME, description = "BrowserMob server address", type = AttributeType.STRING)
String server() default DEFAULT_SERVER;

@AttributeDefinition(name = PORT_PROPERTY_NAME, description = "BrowserMob API port", type = AttributeType.INTEGER)
int port() default 8080;

@AttributeDefinition(name = MAX_ATTEMPTS_PROPERTY_NAME, description = "Maximum number of attempts that will be performed to create single proxy", type = AttributeType.INTEGER)
int maxAttempts() default 3;

@AttributeDefinition(name = ATTEMPTS_INTERVAL_PROPERTY_NAME, description = "Wait interval for failed Attempts", type = AttributeType.INTEGER)
int attemptsInterval() default 50;
}
19 changes: 17 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@
<version>1.8.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.metatype.annotations</artifactId>
<version>1.3.0</version>
</dependency>

<!-- provided by Karaf: org.osgi.service.http -->
<dependency>
Expand Down Expand Up @@ -668,7 +683,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.1.1168</version>
<version>3.4.1.1168</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down Expand Up @@ -1098,4 +1113,4 @@
</build>
</profile>
</profiles>
</project>
</project>