Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
:enh: Injectable ClientIdGenerator
Browse files Browse the repository at this point in the history
Signed-off-by: dseurotech <davide.salvador@eurotech.com>
dseurotech committed Sep 6, 2023
1 parent d0f8e06 commit 70ab4ed
Showing 8 changed files with 54 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
*
* @since 1.2.0
*/
//TODO: FIXME: promote from static utility to injectable collaborator
public class RandomUtils {

private static final Logger LOG = LoggerFactory.getLogger(RandomUtils.class);
1 change: 1 addition & 0 deletions job-engine/app/web/src/main/resources/locator.xml
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@

<packages>
<package>org.eclipse.kapua.job.engine.app.web</package>
<package>org.eclipse.kapua.plugin</package>
<package>org.eclipse.kapua.commons</package>
<package>org.eclipse.kapua.job.engine.jbatch</package>
<package>org.eclipse.kapua.job.engine.queue.jbatch</package>
1 change: 1 addition & 0 deletions rest-api/web/src/main/resources/locator.xml
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
</provided>
<packages>
<package>org.eclipse.kapua.app.api.web</package>
<package>org.eclipse.kapua.plugin</package>
<package>org.eclipse.kapua.commons</package>
<package>org.eclipse.kapua.job.engine.client</package>
<package>org.eclipse.kapua.message</package>
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@
*
* @since 1.0
*/
//TODO: FIXME: promote from static utility to injectable collaborator
public class AuthenticationUtils {

private static final String CIPHER_ALGORITHM = "AES";
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.transport;

import com.google.inject.Provides;
import org.eclipse.kapua.commons.core.AbstractKapuaModule;
import org.eclipse.kapua.commons.util.RandomUtils;
import org.eclipse.kapua.transport.utils.ClientIdGenerator;

import javax.inject.Singleton;

public class TransportModule extends AbstractKapuaModule {
@Override
protected void configureModule() {
}

@Provides
@Singleton
ClientIdGenerator clientIdGenerator() {
return new ClientIdGenerator(RandomUtils.getInstance());
}
}
Original file line number Diff line number Diff line change
@@ -13,8 +13,7 @@
package org.eclipse.kapua.transport.utils;


import org.eclipse.kapua.commons.util.RandomUtils;

import javax.inject.Inject;
import java.util.Random;

/**
@@ -23,7 +22,6 @@
* @author alberto.codutti
* @since 1.0.0
*/
//TODO: FIXME: singletons should not be handled manually, we have DI for that
public class ClientIdGenerator {

/**
@@ -38,31 +36,16 @@ public class ClientIdGenerator {
*
* @since 1.2.0
*/
private static final Random RANDOM = RandomUtils.getInstance();

/**
* {@code static} instance singleton reference
*
* @since 1.0.0
*/
private static final ClientIdGenerator INSTANCE = new ClientIdGenerator();

/**
* Private default constructor. To obtain an instance of {@link ClientIdGenerator} use {@link ClientIdGenerator#getInstance()}.
*
* @since 1.0.0
*/
private ClientIdGenerator() {
}
private final Random random;

/**
* Returns a {@code static} instance of the {@link ClientIdGenerator}.
* Default constructor.
*
* @return The singleton instance of {@link ClientIdGenerator}
* @since 1.0.0
*/
public static ClientIdGenerator getInstance() {
return INSTANCE;
@Inject
public ClientIdGenerator(Random random) {
this.random = random;
}

/**
@@ -87,7 +70,7 @@ public String next() {
*/
public String next(String prefix) {
long timestamp = System.currentTimeMillis();
long randomNumber = RANDOM.nextLong();
long randomNumber = random.nextLong();

return String.format(GENERATED_ID_STRING_FORMAT,
prefix,
Original file line number Diff line number Diff line change
@@ -20,7 +20,10 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
* {@link ClientIdGenerator} tests.
@@ -32,20 +35,9 @@ public class ClientIdGeneratorTest {

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

@Test
public void getInstanceTest() {
ClientIdGenerator clientIdGenerator1 = ClientIdGenerator.getInstance();
Assert.assertNotNull(clientIdGenerator1);

ClientIdGenerator clientIdGenerator2 = ClientIdGenerator.getInstance();
Assert.assertNotNull(clientIdGenerator2);

Assert.assertEquals(clientIdGenerator1, clientIdGenerator2);
}

@Test
public void nextTest() {
ClientIdGenerator clientIdGenerator = ClientIdGenerator.getInstance();
ClientIdGenerator clientIdGenerator = new ClientIdGenerator(new Random());

String nextId = clientIdGenerator.next();

@@ -56,9 +48,9 @@ public void nextTest() {

@Test
public void nextGenerationTest() {
ClientIdGenerator clientIdGenerator = ClientIdGenerator.getInstance();
ClientIdGenerator clientIdGenerator = new ClientIdGenerator(new Random());

List<String> generatedIds = new ArrayList<>();
Set<String> generatedIds = new HashSet<>();
for (int i = 0; i < 10000; i++) {
String nextId = clientIdGenerator.next();
LOG.trace("Generated Id: {}", nextId);
@@ -72,7 +64,7 @@ public void nextGenerationTest() {

@Test
public void nextWithPrefixTest() {
ClientIdGenerator clientIdGenerator = ClientIdGenerator.getInstance();
ClientIdGenerator clientIdGenerator = new ClientIdGenerator(new Random());

String nextId = clientIdGenerator.next("MyPrefix");

@@ -83,7 +75,7 @@ public void nextWithPrefixTest() {

@Test
public void nextWithPrefixGenerationTest() {
ClientIdGenerator clientIdGenerator = ClientIdGenerator.getInstance();
ClientIdGenerator clientIdGenerator = new ClientIdGenerator(new Random());

List<String> generatedIds = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.transport.mqtt.MqttClient;
import org.eclipse.kapua.transport.mqtt.MqttClientConnectionOptions;
import org.eclipse.kapua.transport.mqtt.exception.MqttClientException;
@@ -37,7 +38,8 @@
public class PooledMqttClientFactory extends BasePooledObjectFactory<MqttClient> {

private static final Logger LOG = LoggerFactory.getLogger(PooledMqttClientFactory.class);
private final ClientIdGenerator clientIdGenerator = ClientIdGenerator.getInstance();
//TODO: Inject if possible
private final ClientIdGenerator clientIdGenerator = KapuaLocator.getInstance().getComponent(ClientIdGenerator.class);

private final String serverURI;

0 comments on commit 70ab4ed

Please sign in to comment.