Skip to content

Commit

Permalink
:enh: Injectable ClientIdGenerator
Browse files Browse the repository at this point in the history
Signed-off-by: dseurotech <[email protected]>
  • Loading branch information
dseurotech committed Sep 6, 2023
1 parent d0f8e06 commit 8b45732
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
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
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions rest-api/web/src/main/resources/locator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions service/security/shiro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-security-certificate-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-security-certificate-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-user-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2021, 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.service.authentication;

import com.google.inject.Provides;
import org.eclipse.kapua.commons.core.AbstractKapuaModule;

import javax.inject.Named;

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

}

@Provides
@Named(value = "metricModuleName")
String metricModuleName() {
return "test";
}
}
22 changes: 22 additions & 0 deletions service/security/shiro/src/test/resources/locator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
-->
<!DOCTYPE xml>
<locator-config>
<provided>
</provided>

<packages>
<package>org.eclipse.kapua</package>
</packages>
</locator-config>
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
Expand Up @@ -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;

/**
Expand All @@ -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 {

/**
Expand All @@ -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;
}

/**
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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");

Expand All @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 8b45732

Please sign in to comment.