More checkstyle fixes

This commit is contained in:
Christian Schneider 2015-11-24 14:55:35 +01:00
parent b44290df75
commit 8cc6c2f5fc
8 changed files with 389 additions and 315 deletions

View File

@ -1,55 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-pom</artifactId>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-features</artifactId>
<packaging>pom</packaging>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter</id>
<phase>generate-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>target/classes/features.xml</file>
<type>xml</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-pom</artifactId>
<version>1.1.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-features</artifactId>
<packaging>pom</packaging>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter</id>
<phase>generate-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>target/classes/features.xml</file>
<type>xml</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,3 +1,19 @@
<!--
~ 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.
-->
<?xml version="1.0" encoding="UTF-8"?>
<features name="artemis-${pom.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

View File

@ -27,7 +27,7 @@ import org.osgi.service.component.annotations.Component;
import java.util.Collections;
import java.util.List;
@Component(service=ProtocolManagerFactory.class)
@Component(service = ProtocolManagerFactory.class)
public class ProtonProtocolManagerFactory extends AbstractProtocolManagerFactory<Interceptor> {
private static final String AMQP_PROTOCOL_NAME = "AMQP";

View File

@ -24,7 +24,7 @@ import org.apache.activemq.artemis.spi.core.protocol.ProtocolManager;
import org.apache.activemq.artemis.spi.core.protocol.ProtocolManagerFactory;
import org.osgi.service.component.annotations.Component;
@Component(service=ProtocolManagerFactory.class)
@Component(service = ProtocolManagerFactory.class)
public class MQTTProtocolManagerFactory implements ProtocolManagerFactory {
public static final String MQTT_PROTOCOL_NAME = "MQTT";

View File

@ -46,167 +46,167 @@ import org.osgi.service.component.annotations.Deactivate;
import org.osgi.util.tracker.ServiceTracker;
@SuppressWarnings({"unchecked", "rawtypes"})
@Component(configurationPid="org.apache.activemq.artemis")
@Component(configurationPid = "org.apache.activemq.artemis")
public class OsgiBroker {
private String name;
private String configurationUrl;
private Map<String, ActiveMQComponent> components;
private Map<String, ServiceRegistration<?>> registrations;
private ServiceTracker tracker;
private String name;
private String configurationUrl;
private Map<String, ActiveMQComponent> components;
private Map<String, ServiceRegistration<?>> registrations;
private ServiceTracker tracker;
@Activate
public void activate(ComponentContext cctx) throws Exception {
final BundleContext context = cctx.getBundleContext();
final Dictionary<String, Object> properties = cctx.getProperties();
configurationUrl = getMandatory(properties, "config");
name = getMandatory(properties, "name");
String domain = getMandatory(properties, "domain");
ActiveMQJAASSecurityManager security = new ActiveMQJAASSecurityManager(domain);
String brokerInstance = null;
String karafDataDir = System.getProperty("karaf.data");
if (karafDataDir != null) {
brokerInstance = karafDataDir + "/artemis/" + name;
}
@Activate
public void activate(ComponentContext cctx) throws Exception {
final BundleContext context = cctx.getBundleContext();
final Dictionary<String, Object> properties = cctx.getProperties();
configurationUrl = getMandatory(properties, "config");
name = getMandatory(properties, "name");
String domain = getMandatory(properties, "domain");
ActiveMQJAASSecurityManager security = new ActiveMQJAASSecurityManager(domain);
String brokerInstance = null;
String karafDataDir = System.getProperty("karaf.data");
if (karafDataDir != null) {
brokerInstance = karafDataDir + "/artemis/" + name;
}
// todo if we start to pullout more configs from the main config then we
// should pull out the configuration objects from factories if available
FileConfiguration configuration = new FileConfiguration();
if (brokerInstance != null) {
configuration.setBrokerInstance(new File(brokerInstance));
}
FileJMSConfiguration jmsConfiguration = new FileJMSConfiguration();
// todo if we start to pullout more configs from the main config then we
// should pull out the configuration objects from factories if available
FileConfiguration configuration = new FileConfiguration();
if (brokerInstance != null) {
configuration.setBrokerInstance(new File(brokerInstance));
}
FileJMSConfiguration jmsConfiguration = new FileJMSConfiguration();
FileDeploymentManager fileDeploymentManager = new FileDeploymentManager(configurationUrl);
fileDeploymentManager.addDeployable(configuration).addDeployable(jmsConfiguration).readConfiguration();
FileDeploymentManager fileDeploymentManager = new FileDeploymentManager(configurationUrl);
fileDeploymentManager.addDeployable(configuration).addDeployable(jmsConfiguration).readConfiguration();
components = fileDeploymentManager.buildService(security, ManagementFactory.getPlatformMBeanServer());
components = fileDeploymentManager.buildService(security, ManagementFactory.getPlatformMBeanServer());
final ActiveMQServer server = (ActiveMQServer)components.get("core");
final ActiveMQServer server = (ActiveMQServer)components.get("core");
String[] requiredProtocols = getRequiredProtocols(server.getConfiguration().getAcceptorConfigurations());
ProtocolTrackerCallBack callback = new ProtocolTrackerCallBack() {
String[] requiredProtocols = getRequiredProtocols(server.getConfiguration().getAcceptorConfigurations());
ProtocolTrackerCallBack callback = new ProtocolTrackerCallBack() {
@Override
public void addFactory(ProtocolManagerFactory<Interceptor> pmf) {
server.addProtocolManagerFactory(pmf);
@Override
public void addFactory(ProtocolManagerFactory<Interceptor> pmf) {
server.addProtocolManagerFactory(pmf);
}
@Override
public void removeFactory(ProtocolManagerFactory<Interceptor> pmf) {
server.removeProtocolManagerFactory(pmf);
}
@Override
public void stop() throws Exception {
ActiveMQComponent[] mqComponents = new ActiveMQComponent[components.size()];
components.values().toArray(mqComponents);
for (int i = mqComponents.length - 1; i >= 0; i--) {
mqComponents[i].stop();
}
unregister();
}
@Override
public void removeFactory(ProtocolManagerFactory<Interceptor> pmf) {
server.removeProtocolManagerFactory(pmf);
@Override
public void start() throws Exception {
List<ActiveMQComponent> componentsByStartOrder = getComponentsByStartOrder(components);
for (ActiveMQComponent component : componentsByStartOrder) {
component.start();
}
@Override
public void stop() throws Exception {
ActiveMQComponent[] mqComponents = new ActiveMQComponent[components.size()];
components.values().toArray(mqComponents);
for (int i = mqComponents.length - 1; i >= 0; i--) {
mqComponents[i].stop();
}
unregister();
}
@Override
public void start() throws Exception {
List<ActiveMQComponent> componentsByStartOrder = getComponentsByStartOrder(components);
for (ActiveMQComponent component : componentsByStartOrder) {
component.start();
}
register(context, properties);
}
@Override
public boolean isStarted() {
return server.isStarted();
}
};
ProtocolTracker trackerCust = new ProtocolTracker(name, context, requiredProtocols, callback);
tracker = new ServiceTracker(context, ProtocolManagerFactory.class, trackerCust);
tracker.open();
}
private String getMandatory(Dictionary<String, ?> properties, String key) {
String value = (String) properties.get(key);
if (value == null) {
throw new IllegalStateException("Property " + key + " must be set");
}
return value;
}
register(context, properties);
}
@Override
public boolean isStarted() {
return server.isStarted();
}
};
ProtocolTracker trackerCust = new ProtocolTracker(name, context, requiredProtocols, callback);
tracker = new ServiceTracker(context, ProtocolManagerFactory.class, trackerCust);
tracker.open();
}
private String getMandatory(Dictionary<String, ?> properties, String key) {
String value = (String) properties.get(key);
if (value == null) {
throw new IllegalStateException("Property " + key + " must be set");
}
return value;
}
private String[] getRequiredProtocols(Set<TransportConfiguration> acceptors) {
ArrayList<String> protocols = new ArrayList<String>();
for (TransportConfiguration acceptor : acceptors) {
String protoName = acceptor.getName().toUpperCase();
if (!"ARTEMIS".equals(protoName)) {
protocols.add(protoName);
}
}
return protocols.toArray(new String[]{});
}
private String[] getRequiredProtocols(Set<TransportConfiguration> acceptors) {
ArrayList<String> protocols = new ArrayList<String>();
for (TransportConfiguration acceptor : acceptors) {
String protoName = acceptor.getName().toUpperCase();
if (!"ARTEMIS".equals(protoName)) {
protocols.add(protoName);
}
}
return protocols.toArray(new String[]{});
}
@Deactivate
public void stop() throws Exception {
tracker.close();
}
@Deactivate
public void stop() throws Exception {
tracker.close();
}
public Map<String, ActiveMQComponent> getComponents() {
return components;
}
public Map<String, ActiveMQComponent> getComponents() {
return components;
}
/*
* this makes sure the components are started in the correct order. Its
* simple at the mo as e only have core and jms but will need impproving if
* we get more.
*/
public ArrayList<ActiveMQComponent> getComponentsByStartOrder(Map<String, ActiveMQComponent> components) {
ArrayList<ActiveMQComponent> activeMQComponents = new ArrayList<ActiveMQComponent>();
ActiveMQComponent jmsComponent = components.get("jms");
if (jmsComponent != null) {
activeMQComponents.add(jmsComponent);
}
activeMQComponents.add(components.get("core"));
return activeMQComponents;
}
/*
* this makes sure the components are started in the correct order. Its
* simple at the mo as e only have core and jms but will need impproving if
* we get more.
*/
public ArrayList<ActiveMQComponent> getComponentsByStartOrder(Map<String, ActiveMQComponent> components) {
ArrayList<ActiveMQComponent> activeMQComponents = new ArrayList<ActiveMQComponent>();
ActiveMQComponent jmsComponent = components.get("jms");
if (jmsComponent != null) {
activeMQComponents.add(jmsComponent);
}
activeMQComponents.add(components.get("core"));
return activeMQComponents;
}
public void register(BundleContext context, Dictionary<String, ?> properties) {
registrations = new HashMap<>();
for (Map.Entry<String, ActiveMQComponent> component : getComponents().entrySet()) {
String[] classes = getInterfaces(component.getValue());
Hashtable<String, Object> props = new Hashtable<>();
for (Enumeration<String> keyEnum = properties.keys(); keyEnum.hasMoreElements();) {
String key = keyEnum.nextElement();
Object val = properties.get(key);
props.put(key, val);
}
ServiceRegistration<?> registration = context.registerService(classes, component.getValue(), props);
registrations.put(component.getKey(), registration);
}
}
public void register(BundleContext context, Dictionary<String, ?> properties) {
registrations = new HashMap<>();
for (Map.Entry<String, ActiveMQComponent> component : getComponents().entrySet()) {
String[] classes = getInterfaces(component.getValue());
Hashtable<String, Object> props = new Hashtable<>();
for (Enumeration<String> keyEnum = properties.keys(); keyEnum.hasMoreElements();) {
String key = keyEnum.nextElement();
Object val = properties.get(key);
props.put(key, val);
}
ServiceRegistration<?> registration = context.registerService(classes, component.getValue(), props);
registrations.put(component.getKey(), registration);
}
}
private String[] getInterfaces(ActiveMQComponent value) {
Set<String> interfaces = new HashSet<>();
getInterfaces(value.getClass(), interfaces);
return interfaces.toArray(new String[interfaces.size()]);
}
private String[] getInterfaces(ActiveMQComponent value) {
Set<String> interfaces = new HashSet<>();
getInterfaces(value.getClass(), interfaces);
return interfaces.toArray(new String[interfaces.size()]);
}
private void getInterfaces(Class<?> clazz, Set<String> interfaces) {
for (Class<?> itf : clazz.getInterfaces()) {
if (interfaces.add(itf.getName())) {
getInterfaces(itf, interfaces);
}
}
if (clazz.getSuperclass() != null) {
getInterfaces(clazz.getSuperclass(), interfaces);
}
}
private void getInterfaces(Class<?> clazz, Set<String> interfaces) {
for (Class<?> itf : clazz.getInterfaces()) {
if (interfaces.add(itf.getName())) {
getInterfaces(itf, interfaces);
}
}
if (clazz.getSuperclass() != null) {
getInterfaces(clazz.getSuperclass(), interfaces);
}
}
public void unregister() {
if (registrations != null) {
for (ServiceRegistration<?> reg : registrations.values()) {
reg.unregister();
}
}
}
public void unregister() {
if (registrations != null) {
for (ServiceRegistration<?> reg : registrations.values()) {
reg.unregister();
}
}
}
}

View File

@ -34,96 +34,98 @@ import org.osgi.util.tracker.ServiceTrackerCustomizer;
* Tracks the available ProtocolManagerFactory services as well as the required protocols.
* When a new service appears the factory is added to the server.
* When all needed protocols are present the server is started.
* When required a service disappears the server is stopped.
* When required a service disappears the server is stopped.
*/
@SuppressWarnings("rawtypes")
public class ProtocolTracker implements ServiceTrackerCustomizer<ProtocolManagerFactory<Interceptor>, ProtocolManagerFactory<Interceptor>> {
private static Logger LOG = Logger.getLogger(ProtocolTracker.class.getName());
private String name;
private BundleContext context;
private Map<String, Boolean> protocols;
private ProtocolTrackerCallBack callback;
private static Logger LOG = Logger.getLogger(ProtocolTracker.class.getName());
private String name;
private BundleContext context;
private Map<String, Boolean> protocols;
private ProtocolTrackerCallBack callback;
public ProtocolTracker(String name, BundleContext context, String[] requiredProtocols, ProtocolTrackerCallBack callback) {
this.name = name;
this.context = context;
this.callback = callback;
this.protocols = new HashMap<String, Boolean>();
for (String requiredProtocol : requiredProtocols) {
this.protocols.put(requiredProtocol, false);
}
LOG.info("Broker config " + name + " found. Tracking protocols " + Arrays.asList(requiredProtocols));
}
public ProtocolTracker(String name, BundleContext context, String[] requiredProtocols, ProtocolTrackerCallBack callback) {
this.name = name;
this.context = context;
this.callback = callback;
this.protocols = new HashMap<String, Boolean>();
for (String requiredProtocol : requiredProtocols) {
this.protocols.put(requiredProtocol, false);
}
LOG.info("Broker config " + name + " found. Tracking protocols " + Arrays.asList(requiredProtocols));
}
@Override
public ProtocolManagerFactory addingService(ServiceReference<ProtocolManagerFactory<Interceptor>> reference) {
ProtocolManagerFactory<Interceptor> pmf = context.getService(reference);
callback.addFactory(pmf);
for (String protocol : pmf.getProtocols()) {
protocolAdded(protocol);
}
@Override
public ProtocolManagerFactory addingService(ServiceReference<ProtocolManagerFactory<Interceptor>> reference) {
ProtocolManagerFactory<Interceptor> pmf = context.getService(reference);
callback.addFactory(pmf);
for (String protocol : pmf.getProtocols()) {
protocolAdded(protocol);
}
return pmf;
}
return pmf;
}
@Override
public void modifiedService(ServiceReference<ProtocolManagerFactory<Interceptor>> reference, ProtocolManagerFactory<Interceptor> pmf) {
// Not supported
}
@Override
public void modifiedService(ServiceReference<ProtocolManagerFactory<Interceptor>> reference, ProtocolManagerFactory<Interceptor> pmf) {
// Not supported
}
@Override
public void removedService(ServiceReference<ProtocolManagerFactory<Interceptor>> reference, ProtocolManagerFactory<Interceptor> pmf) {
for (String protocol : pmf.getProtocols()) {
protocolRemoved(protocol);
}
callback.removeFactory(pmf);
}
private void protocolAdded(String protocol) {
Boolean present = this.protocols.get(protocol);
if (present != null && !present) {
this.protocols.put(protocol, true);
List<String> missing = getMissing();
LOG.info("Required protocol " + protocol + " was added for broker " + name + ". " +
(missing.isEmpty() ? "Starting broker." : "Still waiting for " + missing));
if (missing.isEmpty()) {
try {
callback.start();
} catch (Exception e) {
LOG.log(Level.WARNING, "Error starting broker " + name, e);
}
@Override
public void removedService(ServiceReference<ProtocolManagerFactory<Interceptor>> reference, ProtocolManagerFactory<Interceptor> pmf) {
for (String protocol : pmf.getProtocols()) {
protocolRemoved(protocol);
}
callback.removeFactory(pmf);
}
private void protocolAdded(String protocol) {
Boolean present = this.protocols.get(protocol);
if (present != null && !present) {
this.protocols.put(protocol, true);
List<String> missing = getMissing();
LOG.info("Required protocol " + protocol + " was added for broker " + name + ". " +
(missing.isEmpty() ? "Starting broker." : "Still waiting for " + missing));
if (missing.isEmpty()) {
try {
callback.start();
}
}
}
private void protocolRemoved(String protocol) {
Boolean present = this.protocols.get(protocol);
if (present != null && present) {
List<String> missing = getMissing();
LOG.info("Required protocol " + protocol + " was removed for broker " + name + ". "
+ (missing.isEmpty() ? "Stopping broker. " : ""));
if (missing.isEmpty()) {
try {
callback.stop();
} catch (Exception e) {
LOG.log(Level.WARNING, "Error stopping broker " + name, e);
}
catch (Exception e) {
LOG.log(Level.WARNING, "Error starting broker " + name, e);
}
this.protocols.put(protocol, false);
}
}
}
}
}
private List<String> getMissing() {
List<String> missing = new ArrayList<String>();
for (String protocol : protocols.keySet()) {
Boolean present = protocols.get(protocol);
if (!present) {
missing.add(protocol);
private void protocolRemoved(String protocol) {
Boolean present = this.protocols.get(protocol);
if (present != null && present) {
List<String> missing = getMissing();
LOG.info("Required protocol " + protocol + " was removed for broker " + name + ". "
+ (missing.isEmpty() ? "Stopping broker. " : ""));
if (missing.isEmpty()) {
try {
callback.stop();
}
}
return missing;
}
catch (Exception e) {
LOG.log(Level.WARNING, "Error stopping broker " + name, e);
}
}
this.protocols.put(protocol, false);
}
}
private List<String> getMissing() {
List<String> missing = new ArrayList<String>();
for (String protocol : protocols.keySet()) {
Boolean present = protocols.get(protocol);
if (!present) {
missing.add(protocol);
}
}
return missing;
}
}

View File

@ -1,3 +1,19 @@
/*
* 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.
*/
package org.apache.activemq.artemis.osgi;
import org.apache.activemq.artemis.api.core.Interceptor;
@ -5,6 +21,7 @@ import org.apache.activemq.artemis.core.server.ActiveMQComponent;
import org.apache.activemq.artemis.spi.core.protocol.ProtocolManagerFactory;
public interface ProtocolTrackerCallBack extends ActiveMQComponent {
void addFactory(ProtocolManagerFactory<Interceptor> pmf);
void removeFactory(ProtocolManagerFactory<Interceptor> pmf);
void addFactory(ProtocolManagerFactory<Interceptor> pmf);
void removeFactory(ProtocolManagerFactory<Interceptor> pmf);
}

View File

@ -1,3 +1,19 @@
/*
* 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.
*/
package org.apache.activemq.artemis.osgi;
import static org.easymock.EasyMock.expect;
@ -13,47 +29,47 @@ import org.osgi.framework.ServiceReference;
@SuppressWarnings({"rawtypes", "unchecked"})
public class ProtocolTrackerTest {
@Test
public void testLifecycle() throws Exception {
IMocksControl c = EasyMock.createControl();
BundleContext context = c.createMock(BundleContext.class);
String[] requiredProtocols = {"a", "b"};
ProtocolTrackerCallBack callback = c.createMock(ProtocolTrackerCallBack.class);
@Test
public void testLifecycle() throws Exception {
IMocksControl c = EasyMock.createControl();
BundleContext context = c.createMock(BundleContext.class);
String[] requiredProtocols = {"a", "b"};
ProtocolTrackerCallBack callback = c.createMock(ProtocolTrackerCallBack.class);
RefFact protA = new RefFact(c, context, new String[]{"a"});
RefFact protB = new RefFact(c, context, new String[]{"b"});
RefFact protA = new RefFact(c, context, new String[]{"a"});
RefFact protB = new RefFact(c, context, new String[]{"b"});
callback.addFactory(protA.factory);
EasyMock.expectLastCall();
callback.addFactory(protA.factory);
EasyMock.expectLastCall();
callback.addFactory(protB.factory);
EasyMock.expectLastCall();
callback.start();
EasyMock.expectLastCall();
callback.addFactory(protB.factory);
EasyMock.expectLastCall();
callback.start();
EasyMock.expectLastCall();
callback.removeFactory(protA.factory);
EasyMock.expectLastCall();
callback.stop();
EasyMock.expectLastCall();
callback.removeFactory(protA.factory);
EasyMock.expectLastCall();
callback.stop();
EasyMock.expectLastCall();
c.replay();
ProtocolTracker tracker = new ProtocolTracker("test", context, requiredProtocols, callback);
tracker.addingService(protA.ref);
tracker.addingService(protB.ref);
tracker.removedService(protA.ref, protA.factory);
c.verify();
}
c.replay();
ProtocolTracker tracker = new ProtocolTracker("test", context, requiredProtocols, callback);
tracker.addingService(protA.ref);
tracker.addingService(protB.ref);
tracker.removedService(protA.ref, protA.factory);
c.verify();
}
class RefFact {
ServiceReference<ProtocolManagerFactory<Interceptor>> ref;
ProtocolManagerFactory factory;
public RefFact(IMocksControl c, BundleContext context, String[] protocols) {
ref = c.createMock(ServiceReference.class);
factory = c.createMock(ProtocolManagerFactory.class);
expect(factory.getProtocols()).andReturn(protocols).atLeastOnce();
expect(context.getService(ref)).andReturn(factory).atLeastOnce();
}
}
class RefFact {
ServiceReference<ProtocolManagerFactory<Interceptor>> ref;
ProtocolManagerFactory factory;
public RefFact(IMocksControl c, BundleContext context, String[] protocols) {
ref = c.createMock(ServiceReference.class);
factory = c.createMock(ProtocolManagerFactory.class);
expect(factory.getProtocols()).andReturn(protocols).atLeastOnce();
expect(context.getService(ref)).andReturn(factory).atLeastOnce();
}
}
}